[gdal-dev] Extract Extent or Bounding Box for a subset of shapes for a shapefile

Marius Jigmond mariusjigmond at hotmail.com
Fri Apr 22 17:53:29 EDT 2011


Luca,

You can also loop through the features with relatively simple Python
code to get the extents. See sample below (adapt at will):

def findPoints(geometry, results):
  for i in range(geometry.GetPointCount()):
    x,y,z = geometry.GetPoint(i)
    if results['north'] == None or results['north'][1] < y:
      results['north'] = (x,y)
    if results['south'] == None or results['south'][1] > y:
      results['south'] = (x,y)
    if results['east'] == None or results['east'][0] < x:
      results['east'] = (x,y)
    if results['west'] == None or results['west'][0] > x:
      results['west'] = (x,y)
  for i in range(geometry.GetGeometryCount()):
    findPoints(geometry.GetGeometryRef(i), results)

inSHP = 'test.shp'
shapefile = osgeo.ogr.Open(inSHP)
layer = shapefile.GetLayer(0)
for feat in range(layer.GetFeatureCount()):
	feature = layer.GetFeature(feat)
	geometry = feature.GetGeometryRef()
	results = {'north' : None, 'south' : None, 'east' : None, 'west' :
None}
	findPoints(geometry, results)

-marius

On Fri, 2011-04-22 at 17:39 +0200, Luca Sigfrido Percich wrote:
> Thank you Chaitanya.
> 
> I think that this makes great sense when dealing with a summary of the
> whole layer, especially because a lot of formats store the extent in the
> layer metadata/header.
> 
> But if I need to dump all the features, or to get a dump or summary of a
> filtered set of features (in which case I presume ogr will scan all the
> features to apply the filter), checking for boundary limits shouldn't
> add much cost to the already expensive operation.
> 
> Probably this goes beyond the intended scope of ogrinfo. And Matthew's
> workaround works fine.
> 
> Thanks again
> 
> Sig
> 
> 
> 
> Il giorno ven, 22/04/2011 alle 20.20 +0530, Chaitanya kumar CH ha
> scritto:
> > Sig,
> > 
> > ogrinfo doesn't force the recomputation of extent. It is considered as
> > a costly operation. ogrinfo just reports the stored extent.
> > Whereas in ogr2ogr, creating a new file automatically computes the
> > extents.
> 
> _______________________________________________
> gdal-dev mailing list
> gdal-dev at lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/gdal-dev
> 



More information about the gdal-dev mailing list