5

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?

3
  • Welcome. Please take the tour here: gis.stackexchange.com/tour. I'm a little unclear on the question--Are you saying that you only want to return what is in the second box? Not everything that is included in the first?
    – MaryBeth
    Commented Dec 21, 2015 at 18:58
  • Thanks. Sorry if it wasn't clear enougth. Yes, I need only the second box removing all information about Geometry, Features, Layers, etc. Commented Dec 21, 2015 at 19:03
  • I guess that with some python you could get the schema with OGRLayer::GetLayerDefn () gdal.org/classOGRLayer.html#a80473bcfd11341e70dd35bebe94026cf
    – user30184
    Commented Dec 21, 2015 at 19:04

2 Answers 2

3

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])
1
  • 1
    Hi, with when the file is a KML or a GeoJSON (exported from KML) I aways receive this metadata attributes: Name, description, timestamp, begin, end, altitudeMode, tessellate, extrude, visibility, drawnOrder,icon, but on schema there is not metadata with this name. Did you know whats is happening? How to don't include this attributes? Commented Jan 20, 2016 at 10:50
3

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 info returns a JSON of the ogrinfo output. This is easier and more reliable to parse than pure text output. With jq you just select the information you want to see - the properties in your case.

0

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.