More

How to access Shapefile metadata and metatypes information using OGR?

How to access Shapefile metadata and metatypes information using OGR?


I'm trying to use ogrinfo to get some details on a shapefile. Now, the only way that I know is using orginfo:

ogrinfo -ro -so -al BRA_001.shp

But its display all information about the shape:

INFO: Open of '.BRA_001.shp' using driver 'ESRI Shapefile' successful. Layer name: BRA_001 Geometry: 3D Polygon Feature Count: 273 Extent: (-42.645514, -22.360532) - (-42.321728, -22.198300) Layer SRS WKT: GEOGCS["WGS 84", DATUM["WGS_1984", SPHEROID["WGS 84",6378137,298.257223563, AUTHORITY["EPSG","7030"]], TOWGS84[0,0,0,0,0,0,0], AUTHORITY["EPSG","6326"]], PRIMEM["Greenwich",0, AUTHORITY["EPSG","8901"]], UNIT["degree",0.0174532925199433, AUTHORITY["EPSG","9108"]], AUTHORITY["EPSG","4326"]] Name: String (0.0) description: String (0.0) timestamp: String (0.0) begin: String (0.0) end: String (0.0) altitudeMode: String (0.0) tessellate: Integer (0.0) extrude: Integer (0.0) visibility: Integer (0.0) drawOrder: String (0.0) icon: String (0.0)

It's very useful by the way, but what I need is get only metadata names with types, e.g.:

Name: String (0.0) description: String (0.0) timestamp: String (0.0) begin: String (0.0) end: String (0.0) altitudeMode: String (0.0) tessellate: Integer (0.0) extrude: Integer (0.0) visibility: Integer (0.0) drawOrder: String (0.0) icon: String (0.0)

Can someone help me out?


A very quick way would be to use the linux command line utility tail as follows:

ogrinfo -ro -so -al BRA_001.shp | tail -n + 20

However, this won't always be reprocible if the SRS is different. As user30184 mentioned, you would need to use the OGR API. The following example is in Python:

from osgeo import ogr shpFile = r"./BRA_001.shp" dataSource = ogr.Open(shpFile) layer = dataSource.GetLayer(0) layerDefn = layer.GetLayerDefn() for i in range(layerDefn.GetFieldCount()): fieldName = layerDefn.GetFieldDefn(i).GetName() fieldTypeCode = layerDefn.GetFieldDefn(i).GetType() fieldType = layerDefn.GetFieldDefn(i).GetFieldTypeName(fieldTypeCode) fieldWidth = layerDefn.GetFieldDefn(i).GetPrecision() print str(fieldName) + ": " + str(fieldType) + " (" + str(fieldWidth) + ")"

Here is an example that prints the output as a JSON Array:

import sys try: from osgeo import ogr, gdal except: sys.exit('[ ERROR ]: cannot find GDAL/OGR modules') def printMetadata(daShapefile): dataSource = ogr.Open(daShapefile) daLayer = dataSource.GetLayer(0) layerDefinition = daLayer.GetLayerDefn() c = layerDefinition.GetFieldCount() print "[" for i in range(c): fieldName = layerDefinition.GetFieldDefn(i).GetName() fieldTypeCode = layerDefinition.GetFieldDefn(i).GetType() fieldType = layerDefinition.GetFieldDefn(i).GetFieldTypeName(fieldTypeCode) fieldWidth = layerDefinition.GetFieldDefn(i).GetWidth() GetPrecision = layerDefinition.GetFieldDefn(i).GetPrecision() if((c - 1) != i): scape = ',' else: scape = "" print("{"name":"%s","type":"%s","length":%s,"precision":%s}%s" % (fieldName,fieldType,str(fieldWidth),str(GetPrecision),scape)) print "]" if len( sys.argv ) < 2: sys.exit('[ ERROR ]: You must define a file ') else: file_path_name = sys.argv[1] printMetadata(sys.argv[1])

If you want to stay in the command line here is an example using the OGR wrapper fiona and the JSON parser jq.

>fio info BRA_001.shp | jq '.["schema"]["properties"]' > { "Name": "str:0" "description": "str:0" "timestamp": "str:0" "begin": "str:0" "end": "str:0" "altitudeMode": "str:0" "tessellate": "int:0" "extrude": "int:0" "visibility": "int:0" "drawOrder": "str:0" "icon": "str:0" }

fio inforeturns a JSON of theogrinfooutput. This is easier and more reliable to parse than pure text output. Withjqyou just select the information you want to see - the properties in your case.


Watch the video: CSV File to Point Shapefile - Python GDALOGR