<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN">
<HTML>
<HEAD>
  <META HTTP-EQUIV="Content-Type" CONTENT="text/html; CHARSET=UTF-8">
  <META NAME="GENERATOR" CONTENT="GtkHTML/3.32.2">
</HEAD>
<BODY>
Frank, thanks for the clarification on iterating over the selection. I was able to run my script and in the simple case of returning one polygon feature it works great. However, I ran into some problems when I tried to assume that the filter would return multiple features.<BR>
<BR>
Is it possible to iterate over a selection and add every geometry object to a list, in other words can geometries be handled like other Python objects? I would later like to iterate over the list of geometries and verify whether they contain the centroids I'm interested in. My procedure can be summarized as follows (sample scripts at the end):<BR>
1. Select polygon(s) based on a SQL filter<BR>
2. Iterate over polygons from 1. and add geometries to a list<BR>
3. Iterate over a mesh of polygons, grab their centroids and verify whether they are within the geometries from 2.<BR>
<BR>
Script that fails (assumes I'm getting more than one geometry back and adds it to a list):<BR>
#!/usr/bin/python<BR>
<BR>
from osgeo import ogr<BR>
import sys, math, time, os<BR>
<BR>
grid = 'model1.shp'<BR>
gridDS = ogr.Open(grid, 1)<BR>
gridLayer = gridDS.GetLayer(0)<BR>
<BR>
aquifer = '/data/romania/judeteEPSG3844.shp'<BR>
aqDS = ogr.Open(aquifer)<BR>
aqLayer = aqDS.GetLayerByIndex(0)<BR>
aqLayer.SetAttributeFilter('DENJUD = &quot;CLUJ&quot;')<BR>
aqfeat = aqLayer.GetNextFeature()<BR>
aqgeom = aqfeat.GetGeometryRef()<BR>
geomList = [aqgeom]<BR>
<BR>
while aqfeat is not None: #loop thru features in case of islands/multiple polygons<BR>
&nbsp; aqfeat = aqLayer.GetNextFeature()<BR>
&nbsp; if aqfeat is not None:<BR>
&nbsp;&nbsp;&nbsp; geomList.append(aqfeat.GetGeometryRef())<BR>
<BR>
for i in range(gridLayer.GetFeatureCount()):<BR>
&nbsp; feat = gridLayer.GetFeature(i)<BR>
&nbsp; print feat.GetField('CELL_ID') #see how many features processed before segfault, usually first two features<BR>
&nbsp; geom = feat.GetGeometryRef()<BR>
&nbsp; point = geom.Centroid()<BR>
&nbsp; for polygeom in geomList:<BR>
&nbsp;&nbsp;&nbsp; if polygeom.Contains(point):<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; feat.SetField('IBOUND', 1)<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; gridLayer.SetFeature(feat)<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break #break out of &quot;for&quot; if any poly contains the centroid<BR>
<BR>
Script that works (assumes I'm only getting one geometry back, no need for list):<BR>
#!/usr/bin/python<BR>
<BR>
from osgeo import ogr<BR>
import sys, math, time, os<BR>
<BR>
grid = 'model1.shp'<BR>
gridDS = ogr.Open(grid, 1)<BR>
gridLayer = gridDS.GetLayer(0)<BR>
<BR>
aquifer = '/data/romania/judeteEPSG3844.shp'<BR>
aqDS = ogr.Open(aquifer)<BR>
aqLayer = aqDS.GetLayerByIndex(0)<BR>
aqLayer.SetAttributeFilter('DENJUD = &quot;CLUJ&quot;')<BR>
aqfeat = aqLayer.GetNextFeature()<BR>
aqgeom = aqfeat.GetGeometryRef()<BR>
<BR>
for i in range(gridLayer.GetFeatureCount()):<BR>
&nbsp; feat = gridLayer.GetFeature(i)<BR>
&nbsp; geom = feat.GetGeometryRef()<BR>
&nbsp; point = geom.Centroid()<BR>
&nbsp; if aqgeom.Contains(point):<BR>
&nbsp;&nbsp;&nbsp; feat.SetField('IBOUND', 1)<BR>
&nbsp;&nbsp;&nbsp; gridLayer.SetFeature(feat)<BR>
<BR>
<BR>
-marius<BR>
<BR>
On Mon, 2011-07-11 at 22:01 -0700, Frank Warmerdam wrote:
<BLOCKQUOTE TYPE=CITE>
<PRE>
Marius,

I think a bug is called for.

On Mon, Jul 11, 2011 at 8:00 PM, Marius Jigmond
&lt;<A HREF="mailto:mariusjigmond@hotmail.com">mariusjigmond@hotmail.com</A>&gt; wrote:
&gt; After some more investigation that is likely NOT the issue. I have an
&gt; ExecuteSQL statement which selects a certain polygon based on an attribute
&gt; value. Unfortunately it seems to return the wrong feature. The feature I
&gt; query for is unique so a duplicate is out of the question. Here's the code:
&gt;
&gt; #!/usr/bin/python
&gt;
&gt; from osgeo import ogr
&gt; import sys, math, time, os
&gt;
&gt; aquifer = '/data/romania/judeteEPSG3844.shp'
&gt; aqDS = ogr.Open(aquifer)
&gt; sql = 'select * from judeteEPSG3844 where DENJUD = &quot;CLUJ&quot;'
&gt; aqLayer = aqDS.ExecuteSQL(sql)
&gt; feat = aqLayer.GetFeature(0)
&gt; print aqLayer.GetFeatureCount()
&gt; print feat.GetField('DENJUD')
&gt; aqDS.ReleaseResultSet(aqLayer)
&gt;
&gt; The result:
&gt; marius@mobi:~/temp$ run_sql.py
&gt; 1
&gt; TELEORMAN
&gt; marius@mobi:~/temp$
&gt;
&gt; This explains why none of my centroids were remotely close to the polygon.
&gt;
&gt; Is there something wrong with my query or should I file a bug?
&gt;
&gt; -marius
&gt;
&gt; On Mon, 2011-07-11 at 20:46 -0500, Marius Jigmond wrote:
&gt;
&gt; I suppose a piece of code speaks louder :):
&gt;
&gt; xsect = False
&gt; for i in range(gridLayer.GetFeatureCount()):
&gt; &nbsp; feat = gridLayer.GetFeature(i)
&gt; &nbsp; geom = feat.GetGeometryRef()
&gt; &nbsp; point = geom.Centroid()
&gt; &nbsp; for j in range(aqLayer.GetFeatureCount()):
&gt; &nbsp;&nbsp;&nbsp; aqfeat = aqLayer.GetFeature(j)
&gt; &nbsp;&nbsp;&nbsp; aqgeom = aqfeat.GetGeometryRef()
&gt; &nbsp;&nbsp;&nbsp; if point.Intersect(aqgeom):
&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; xsect = True
&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print 'ibound = 1'
&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break
&gt; &nbsp; if xsect:
&gt; &nbsp;&nbsp;&nbsp; feat.SetField('IBOUND', 1)
&gt; &nbsp;&nbsp;&nbsp; gridLayer.SetFeature(feat)
&gt; &nbsp;&nbsp;&nbsp; xsect = False
&gt;
&gt; -marius
&gt;
&gt; On Mon, 2011-07-11 at 20:34 -0500, Marius Jigmond wrote:
&gt;
&gt; Hi everyone,
&gt;
&gt; I am trying to test whether centroids of polygons lie/intersect within
&gt; another polygon. I have tried Intersect, Within, and Contains but they
&gt; always return false. Should these methods work for my intended purpose or do
&gt; I need to implement a point in polygon function? Thanks.
&gt;
&gt; -marius
&gt;
&gt; _______________________________________________
&gt; gdal-dev mailing list
&gt; <A HREF="mailto:gdal-dev@lists.osgeo.org">gdal-dev@lists.osgeo.org</A>
&gt; <A HREF="http://lists.osgeo.org/mailman/listinfo/gdal-dev">http://lists.osgeo.org/mailman/listinfo/gdal-dev</A>
&gt;
&gt; _______________________________________________
&gt; gdal-dev mailing list
&gt; <A HREF="mailto:gdal-dev@lists.osgeo.org">gdal-dev@lists.osgeo.org</A>
&gt; <A HREF="http://lists.osgeo.org/mailman/listinfo/gdal-dev">http://lists.osgeo.org/mailman/listinfo/gdal-dev</A>
&gt;
&gt; _______________________________________________
&gt; gdal-dev mailing list
&gt; <A HREF="mailto:gdal-dev@lists.osgeo.org">gdal-dev@lists.osgeo.org</A>
&gt; <A HREF="http://lists.osgeo.org/mailman/listinfo/gdal-dev">http://lists.osgeo.org/mailman/listinfo/gdal-dev</A>
&gt;



</PRE>
</BLOCKQUOTE>
</BODY>
</HTML>