4

I've been using OGR/GDAL in its command window to obtain information regarding a vector file with this command

ogrinfo -so -al d:/vector_file.shp

I'm now trying to do the equivalent of this feature in a Python script but I'm unsure how to do it.

I tried looking in this documentation https://pcjericks.github.io/py-gdalogr-cookbook/vector_layers.html#get-shapefile-feature-count

but couldn't find any examples with ogrinfo or how to explicitly state the arguments

I've also seen the answer https://gis.stackexchange.com/a/280900/2581, but would rather not use the subprocess module and instead the osgeo.

10

2 Answers 2

4

Ogrinfo is available nowadays also as a Python library function gdal.VectorInfo and it is very easy to use.

from osgeo import gdal
gdal.VectorInfo("point.shp")

"INFO: Open of `point.shp'\n      using driver `ESRI Shapefile' successful.\n\nLayer name: point\nMetadata:\n  DBF_DATE_LAST_UPDATE=2023-03-01\nGeometry: Point\nFeature Count: 3\nExtent: (7156402.165550, 207.123904) - (7156409.373471, 210.034795)\nLayer SRS WKT:\n(unknown)\nattr: String (1.0)\nnewcol: String (1.0)\nmy_fid: Integer (3.0)\n"
4
  • 1
    In the C api, yes. For Python it is gdal.VectorInfo gdal.org/api/python/utilities.html#osgeo.gdal.VectorInfo.
    – user30184
    Commented Jun 12 at 6:11
  • I do no think that you need ogr library in the imports.
    – Taras
    Commented Jun 12 at 16:35
  • 1
    You are right, verified with GDAL 3.8.5 and answer edited.
    – user30184
    Commented Jun 12 at 16:40
  • Unfortunatelly I do not know you in person, but I am so excited and driven by your answers. Highly cherish and admire your efforts in sharing knowledge and wisdom. Thank you so much!
    – Taras
    Commented Jun 12 at 16:45
1

Another option is to employ the Python subprocess package.

Also, one needs to know where the ogrinfo.exe file is located.

After executing one of the following commands:

import subprocess

command = "C:/OSGeo4W/bin/ogrinfo.exe -so -al D:/qgis_test/points_4326.shp"
output, error = subprocess.Popen(command,
                                 universal_newlines=True,
                                 stdout=subprocess.PIPE,
                                 stderr=subprocess.PIPE
                                 ).communicate()
print(output)

or

import subprocess

command = "C:/OSGeo4W/bin/ogrinfo.exe -so -al D:/qgis_test/points_4326.shp"
output = subprocess.check_call(command)
print(output)

It is possible to get such an output:

INFO: Open of `D:/qgis_test/points_4326.shp'
      using driver `ESRI Shapefile' successful.

Layer name: points_4326
Metadata:
  DBF_DATE_LAST_UPDATE=2023-11-20
Geometry: Point
Feature Count: 3
Extent: (-58.497423, -34.726643) - (-58.495246, -34.725891)
Layer SRS WKT:
GEOGCRS["WGS 84",
    DATUM["World Geodetic System 1984",
        ELLIPSOID["WGS 84",6378137,298.257223563,
            LENGTHUNIT["metre",1]]],
    PRIMEM["Greenwich",0,
        ANGLEUNIT["degree",0.0174532925199433]],
    CS[ellipsoidal,2],
        AXIS["latitude",north,
            ORDER[1],
            ANGLEUNIT["degree",0.0174532925199433]],
        AXIS["longitude",east,
            ORDER[2],
            ANGLEUNIT["degree",0.0174532925199433]],
    ID["EPSG",4326]]
Data axis to CRS axis mapping: 2,1
id: Integer64 (10.0)
city: String (15.0)

References:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.