[Gdal-dev] ogr find neighbours

Didrik Pinte dpinte at itae.be
Wed May 10 09:29:37 EDT 2006


Hi,

In the process of implementing a model, I need to get a list of
neighbours for a shapefile containing polygons.

The file contains 11 polygons having lots of points.

The very simple python code i've developped now is just to test if for
each polygon it touches or overlaps the other polygons from the file.

BUT :

with 11 polygons, I makes 122 (11 * 11) tests of geometry.Touches and
122 tests of geometry.Overlaps. This is done in 60 seconds and thus is
too slow.

By hand, I can do it in 15 seconds maximum taking into account the time
to open the file with GRASS ;-)

Does anyone here have a great idea on how to do this more quickly ? 

I've pasted the code below.

Thanks for your help

Didrik

--

def get_neighbours(self,queryLayer):
 '''
 Returns a list of references to adjacent cells
 '''
 self.neighbours = []
 cobj = self.geom
 queryLayer.ResetReading()
 queryFeat = queryLayer.GetNextFeature()
 while queryFeat is not None:
  geometry = queryFeat.GetGeometryRef()
  found = buffer.Touches(geometry)
  if found > 0:   
    self.neighbours.append(queryFeat.Clone())
  else:
   found = buffer.Overlaps(geometry)
   if found > 0:
    self.neighbours.append(queryFeat.Clone())
   queryFeat.Destroy()
   queryFeat = queryLayer.GetNextFeature()
  queryLayer = None
  return self.neighbours

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Ceci est une partie de message
	=?ISO-8859-1?Q?num=E9riquement?= =?ISO-8859-1?Q?_sign=E9e?=
Url : http://lists.osgeo.org/pipermail/gdal-dev/attachments/20060510/b97f9794/attachment.bin


More information about the Gdal-dev mailing list