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
.
2 Answers 2
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"
-
1In the C api, yes. For Python it is
gdal.VectorInfo
gdal.org/api/python/utilities.html#osgeo.gdal.VectorInfo.user30184– user301842024年06月12日 06:11:39 +00:00Commented Jun 12, 2024 at 6:11 -
I do no think that you need
ogr
library in the imports.2024年06月12日 16:35:28 +00:00Commented Jun 12, 2024 at 16:35 -
1You are right, verified with GDAL 3.8.5 and answer edited.user30184– user301842024年06月12日 16:40:15 +00:00Commented Jun 12, 2024 at 16:40
-
1Unfortunatelly 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!2024年06月12日 16:45:16 +00:00Commented Jun 12, 2024 at 16:45
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:
Explore related questions
See similar questions with these tags.
from osgeo
rather thansubprocess
. Is this not the case?subprocess
is the route. If you want to do the equivalent in Python, then you need to rewrite the Question to ask that.