Sunday, 18 August 2019

gdal - 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?



Answer



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])

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...