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.

Taras
35.8k5 gold badges77 silver badges151 bronze badges
asked Apr 28, 2022 at 10:03
10
  • Duplicate of GDAL/OGR ogrinfo for shapefiles in python ? Commented Apr 28, 2022 at 10:48
  • @gene I had seen this answer, but thought there might be a way to use from osgeo rather than subprocess. Is this not the case? Commented Apr 28, 2022 at 10:58
  • If you want to use the external binary, then subprocess is the route. If you want to do the equivalent in Python, then you need to rewrite the Question to ask that. Commented Apr 28, 2022 at 11:13
  • @Vince sorry, I've modified the question now Commented Apr 28, 2022 at 11:20
  • See also gis.stackexchange.com/questions/429788/…. The whole ogrinfo is also written as a python script in the code samples github.com/OSGeo/gdal/blob/master/swig/python/gdal-utils/…. Commented Apr 28, 2022 at 12:25

2 Answers 2

5

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"
answered Jun 6, 2024 at 20:35
4
  • 1
    In the C api, yes. For Python it is gdal.VectorInfo gdal.org/api/python/utilities.html#osgeo.gdal.VectorInfo. Commented Jun 12, 2024 at 6:11
  • I do no think that you need ogr library in the imports. Commented Jun 12, 2024 at 16:35
  • 1
    You are right, verified with GDAL 3.8.5 and answer edited. Commented Jun 12, 2024 at 16:40
  • 1
    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! Commented Jun 12, 2024 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:

answered Jun 12, 2024 at 16:53

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.