Search This Blog

Friday, 17 March 2017

Python program to extract 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)
    for numLayers in range(layers):
        layer = shapefile.GetLayer(numLayers)
        features = layer.GetFeatureCount()
        print 'Layer {} has {} features'.format(numLayers,features)
        for featureNum in range(features):
            feature = layer.GetFeature(featureNum)
            featureName = feature.GetField('NAME')
            print 'Feature {} had name :{}'.format(featureNum, featureName)
else:
    print 'Failed to load date'
   

No comments:

Post a Comment