Search This Blog

Friday, 17 March 2017

Python program to extract attributes of features of shape file

from osgeo import ogr

shapefile = ogr.Open('D:/vector/TM_WORLD_BORDERS-0.3/TM_WORLD_BORDERS-0.3.shp')


if shapefile is not None:
    print 'Data loaded successfully..'
    layers = shapefile.GetLayerCount()
    print 'Shapefile has {} layers'.format(layers)
    layer = shapefile.GetLayer(0)
    features = layer.GetFeatureCount()
    print 'Layer {} has {} features'.format(layers,features)
    feature = layer.GetFeature(82)
    attributes = feature.items()
    print 'Feature 82 has following attributes'
    for key, value in attributes.items():
        print '{} = {} '.format(key,value)

    geometry = feature.GetGeometryRef()
    geometryName = geometry.GetGeometryName()

    print 'Feature\'s geometry data consist of a {}'.format(geometryName)
else:
    print 'Failed to load date'
   

No comments:

Post a Comment