如何将某些Ogrinfo结果保存到变量中?

发布于 2025-01-24 00:24:37 字数 2426 浏览 2 评论 0原文

我在Python中使用GDAL的Ogrinfo命令来获取有关ShapeFile的信息。

我的代码如下:

from subprocess import Popen, PIPE


args = ['ogrinfo', '-ro', '-so', '-al', 'C:/test/test_shapefile.shp']
process = Popen(args, stdout=PIPE, stderr=PIPE)

stdout = process.communicate()[0].decode('utf-8').strip()
print(stdout)

反过来,这给了我大量信息 例如,

Layer name: test_shapefile
Metadata:
  DBF_DATE_LAST_UPDATE=2022-04-13
Geometry: 3D Point
Feature Count: 28915413
Extent: (317044.250000, 681703.750000) - (322287.250000, 685053.250000)
Layer SRS WKT:
PROJCRS["OSGB 1936 / British National Grid",
    BASEGEOGCRS["OSGB 1936",
        DATUM["OSGB 1936",
            ELLIPSOID["Airy 1830",6377563.396,299.3249646,
                LENGTHUNIT["metre",1]]],
        PRIMEM["Greenwich",0,
            ANGLEUNIT["degree",0.0174532925199433]],
        ID["EPSG",4277]],
    CONVERSION["British National Grid",
        METHOD["Transverse Mercator",
            ID["EPSG",9807]],
        PARAMETER["Latitude of natural origin",49,
            ANGLEUNIT["degree",0.0174532925199433],
            ID["EPSG",8801]],
        PARAMETER["Longitude of natural origin",-2,
            ANGLEUNIT["degree",0.0174532925199433],
            ID["EPSG",8802]],
        PARAMETER["Scale factor at natural origin",0.9996012717,
            SCALEUNIT["unity",1],
            ID["EPSG",8805]],
        PARAMETER["False easting",400000,
            LENGTHUNIT["metre",1],
            ID["EPSG",8806]],
        PARAMETER["False northing",-100000,
            LENGTHUNIT["metre",1],
            ID["EPSG",8807]]],
    CS[Cartesian,2],
        AXIS["(E)",east,
            ORDER[1],
            LENGTHUNIT["metre",1]],
        AXIS["(N)",north,
            ORDER[2],
            LENGTHUNIT["metre",1]],
    USAGE[
        SCOPE["Engineering survey, topographic mapping."],
        AREA["United Kingdom (UK) - offshore to boundary of UKCS within 49°45'N to 61°N and 9°W to 2°E; onshore Great Britain (England, Wales and Scotland). Isle of Man onshore."],
        BBOX[49.75,-9,61.01,2.01]],
    ID["EPSG",27700]]
Data axis to CRS axis mapping: 1,2
X: Real (24.15)
Y: Real (24.15)
Z: Real (24.15)

我想知道,是否有任何方法可以将某些信息作为变量存储?

例如:

one_extent = 317044.250000, 322287.250000
second_extent = 681703.750000, 685053.250000
epsg = 4277

I am using gdal's ogrinfo command in Python to obtain information about a shapefile.

My code is as follows:

from subprocess import Popen, PIPE


args = ['ogrinfo', '-ro', '-so', '-al', 'C:/test/test_shapefile.shp']
process = Popen(args, stdout=PIPE, stderr=PIPE)

stdout = process.communicate()[0].decode('utf-8').strip()
print(stdout)

In turn, this gives me a large amount of information
e.g.

Layer name: test_shapefile
Metadata:
  DBF_DATE_LAST_UPDATE=2022-04-13
Geometry: 3D Point
Feature Count: 28915413
Extent: (317044.250000, 681703.750000) - (322287.250000, 685053.250000)
Layer SRS WKT:
PROJCRS["OSGB 1936 / British National Grid",
    BASEGEOGCRS["OSGB 1936",
        DATUM["OSGB 1936",
            ELLIPSOID["Airy 1830",6377563.396,299.3249646,
                LENGTHUNIT["metre",1]]],
        PRIMEM["Greenwich",0,
            ANGLEUNIT["degree",0.0174532925199433]],
        ID["EPSG",4277]],
    CONVERSION["British National Grid",
        METHOD["Transverse Mercator",
            ID["EPSG",9807]],
        PARAMETER["Latitude of natural origin",49,
            ANGLEUNIT["degree",0.0174532925199433],
            ID["EPSG",8801]],
        PARAMETER["Longitude of natural origin",-2,
            ANGLEUNIT["degree",0.0174532925199433],
            ID["EPSG",8802]],
        PARAMETER["Scale factor at natural origin",0.9996012717,
            SCALEUNIT["unity",1],
            ID["EPSG",8805]],
        PARAMETER["False easting",400000,
            LENGTHUNIT["metre",1],
            ID["EPSG",8806]],
        PARAMETER["False northing",-100000,
            LENGTHUNIT["metre",1],
            ID["EPSG",8807]]],
    CS[Cartesian,2],
        AXIS["(E)",east,
            ORDER[1],
            LENGTHUNIT["metre",1]],
        AXIS["(N)",north,
            ORDER[2],
            LENGTHUNIT["metre",1]],
    USAGE[
        SCOPE["Engineering survey, topographic mapping."],
        AREA["United Kingdom (UK) - offshore to boundary of UKCS within 49°45'N to 61°N and 9°W to 2°E; onshore Great Britain (England, Wales and Scotland). Isle of Man onshore."],
        BBOX[49.75,-9,61.01,2.01]],
    ID["EPSG",27700]]
Data axis to CRS axis mapping: 1,2
X: Real (24.15)
Y: Real (24.15)
Z: Real (24.15)

I'm wondering, is there any way to store certain information as a variable?

For example:

one_extent = 317044.250000, 322287.250000
second_extent = 681703.750000, 685053.250000
epsg = 4277

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笑脸一如从前 2025-01-31 00:24:37

如果您只需要范围和EPSG,则不需要Ogrinfo,只需使用Python OGR和OSR绑定:

from osgeo import ogr, osr
import os

inShapefile = "C:/test/test_shapefile.shp"
inDriver = ogr.GetDriverByName("ESRI Shapefile")
inDataSource = inDriver.Open(inShapefile, 0)
inLayer = inDataSource.GetLayer()
extent = inLayer.GetExtent()

proj = osr.SpatialReference(wkt=inDataSource.GetProjection())
proj.AutoIdentifyEPSG()
epsg = proj.GetAttrValue('AUTHORITY',1)

print(f"extent: {extent} EPSG: {epsg}")

If you need only the extent and the EPSG, you do not need OGRInfo, you can just use Python OGR and OSR bindings:

from osgeo import ogr, osr
import os

inShapefile = "C:/test/test_shapefile.shp"
inDriver = ogr.GetDriverByName("ESRI Shapefile")
inDataSource = inDriver.Open(inShapefile, 0)
inLayer = inDataSource.GetLayer()
extent = inLayer.GetExtent()

proj = osr.SpatialReference(wkt=inDataSource.GetProjection())
proj.AutoIdentifyEPSG()
epsg = proj.GetAttrValue('AUTHORITY',1)

print(f"extent: {extent} EPSG: {epsg}")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文