<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Finding a polygon that contains a point in ArcGIS API for Python Questions</title>
    <link>https://community.esri.com/t5/arcgis-api-for-python-questions/finding-a-polygon-that-contains-a-point/m-p/1042107#M5849</link>
    <description>&lt;P&gt;I'm hoping to automate a process, and doing so involves a python script that can look at points on one feature layer, and determine which polygon, from another feature layer, that point is contained within.&lt;/P&gt;&lt;P&gt;Below, I have included some code that demonstrates my approach. In the end, I would hope for a result of 'True,' if the point was within the polygon, which in this case, it was. However, I get an empty result. This example only tests one point and one polygon, and both were selected because the point is within the polygon.&lt;/P&gt;&lt;P&gt;I've seen many other threads on this subject, but can't seem to find an example that shows how to prepare the point and polygon objects for the use of the 'within()' method.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;from arcgis.gis import GIS
from arcgis.geometry import Geometry
from arcgis.geometry.filters import within

gis = GIS("https://www.arcgis.com", "UserName", "UserPass")

fl1 = gis.content.get('ece8ed133a2149f98f46fac2042db2f8')
query1 = fl1.layers[0].query()
resp1 = query1.features
resp1[0].geometry

# Output
# {'rings': [[[-10397275.0847498, 5628849.44085896],
# [-10385962.4045636, 5628773.00383067],
# [-10386038.8415919, 5618606.87906876],
# [-10397427.9588064, 5618606.87906876],
# [-10397275.0847498, 5628849.44085896]]]}

poly_geometry = Geometry(resp1[0].geometry)

fl2 = gis.content.get('d14af86121b640f68e050d20f3f0e876')
query2 = fl2.layers[0].query()
resp2 = query2.features
resp2[0].geometry

# Output:
# {'x': -10392706.32194956, 'y': 5622351.110157302}

point_geometry = Geometry(resp2[0].geometry)

print(point_geometry.within(poly_geometry))

# Output:
# None&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 30 Mar 2021 20:02:07 GMT</pubDate>
    <dc:creator>NancyLewis</dc:creator>
    <dc:date>2021-03-30T20:02:07Z</dc:date>
    <item>
      <title>Finding a polygon that contains a point</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/finding-a-polygon-that-contains-a-point/m-p/1042107#M5849</link>
      <description>&lt;P&gt;I'm hoping to automate a process, and doing so involves a python script that can look at points on one feature layer, and determine which polygon, from another feature layer, that point is contained within.&lt;/P&gt;&lt;P&gt;Below, I have included some code that demonstrates my approach. In the end, I would hope for a result of 'True,' if the point was within the polygon, which in this case, it was. However, I get an empty result. This example only tests one point and one polygon, and both were selected because the point is within the polygon.&lt;/P&gt;&lt;P&gt;I've seen many other threads on this subject, but can't seem to find an example that shows how to prepare the point and polygon objects for the use of the 'within()' method.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;from arcgis.gis import GIS
from arcgis.geometry import Geometry
from arcgis.geometry.filters import within

gis = GIS("https://www.arcgis.com", "UserName", "UserPass")

fl1 = gis.content.get('ece8ed133a2149f98f46fac2042db2f8')
query1 = fl1.layers[0].query()
resp1 = query1.features
resp1[0].geometry

# Output
# {'rings': [[[-10397275.0847498, 5628849.44085896],
# [-10385962.4045636, 5628773.00383067],
# [-10386038.8415919, 5618606.87906876],
# [-10397427.9588064, 5618606.87906876],
# [-10397275.0847498, 5628849.44085896]]]}

poly_geometry = Geometry(resp1[0].geometry)

fl2 = gis.content.get('d14af86121b640f68e050d20f3f0e876')
query2 = fl2.layers[0].query()
resp2 = query2.features
resp2[0].geometry

# Output:
# {'x': -10392706.32194956, 'y': 5622351.110157302}

point_geometry = Geometry(resp2[0].geometry)

print(point_geometry.within(poly_geometry))

# Output:
# None&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 30 Mar 2021 20:02:07 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/finding-a-polygon-that-contains-a-point/m-p/1042107#M5849</guid>
      <dc:creator>NancyLewis</dc:creator>
      <dc:date>2021-03-30T20:02:07Z</dc:date>
    </item>
    <item>
      <title>Re: Finding a polygon that contains a point</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/finding-a-polygon-that-contains-a-point/m-p/1042113#M5850</link>
      <description>&lt;P&gt;I think you want 'contains' rather than 'within'.&lt;/P&gt;</description>
      <pubDate>Tue, 30 Mar 2021 20:15:11 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/finding-a-polygon-that-contains-a-point/m-p/1042113#M5850</guid>
      <dc:creator>DavidPike</dc:creator>
      <dc:date>2021-03-30T20:15:11Z</dc:date>
    </item>
    <item>
      <title>Re: Finding a polygon that contains a point</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/finding-a-polygon-that-contains-a-point/m-p/1042135#M5851</link>
      <description>&lt;P&gt;If you're looking to find &lt;EM&gt;which &lt;/EM&gt;polygon contains each point, you're better off using the geometry filter in the query function.&lt;/P&gt;&lt;P&gt;The script you've written is correct, but it's comparing the first geometry from each layer. The fact that the output is &lt;STRONG&gt;None&lt;/STRONG&gt; suggests that the two geometries simply don't intersect.&lt;/P&gt;&lt;P&gt;Using that method, the only way to find the polygon would be to iterate over each shape in &lt;STRONG&gt;resp2 &lt;/STRONG&gt;and check if it's within, then stop once the result is valid. But this could create quite a large loop of up to &lt;STRONG&gt;len(resp1) * len(resp2)&lt;/STRONG&gt;.&lt;/P&gt;&lt;P&gt;Here's how you might use that within filter on the query (using the same beginning lines):&lt;/P&gt;&lt;LI-CODE lang="python"&gt;for f in query1.features:
    filt = within(f.geometry, f.spatial_reference)
    
    query2 = fl2.layers[0].query(geometry_filter = filt)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The featureset returned by &lt;STRONG&gt;query2&lt;/STRONG&gt; ought to be only those features which contain the feature &lt;STRONG&gt;f&lt;/STRONG&gt;. From there, you can access that polygon's attributes, etc., for whatever else you need in your script.&lt;/P&gt;&lt;P&gt;What is the intended result of this automated process? There might be other methods than this that could be a little more efficient.&lt;/P&gt;</description>
      <pubDate>Tue, 30 Mar 2021 20:29:44 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/finding-a-polygon-that-contains-a-point/m-p/1042135#M5851</guid>
      <dc:creator>jcarlson</dc:creator>
      <dc:date>2021-03-30T20:29:44Z</dc:date>
    </item>
    <item>
      <title>Re: Finding a polygon that contains a point</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/finding-a-polygon-that-contains-a-point/m-p/1078252#M6346</link>
      <description>&lt;P&gt;Hi Nancy,&lt;/P&gt;&lt;P&gt;I am sure you might have found solution for this. I have achieved a similar functionality recently and sharing as it might help someone else.&lt;/P&gt;&lt;P&gt;At line 31, try&amp;nbsp;print(poly_geometry.within(point_geometry)). The point geometry should be passed as parameter to the within function.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 13 Jul 2021 14:43:32 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/finding-a-polygon-that-contains-a-point/m-p/1078252#M6346</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2021-07-13T14:43:32Z</dc:date>
    </item>
    <item>
      <title>Re: Finding a polygon that contains a point</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/finding-a-polygon-that-contains-a-point/m-p/1078258#M6347</link>
      <description>&lt;P&gt;Code Example:&lt;/P&gt;&lt;P&gt;pt = Point({"x" : -10392706.32194956, "y" : 5622351.110157302, "spatialReference" : {"wkid" : 4326 }}) //constructing point geometry&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;gf = within(pt,sr=4326) // this is&amp;nbsp;&lt;EM&gt;geometry_filter we are creating which will be passed in the query below. you will have to import within filter&amp;nbsp;from arcgis.geometry.filters import within&lt;BR /&gt;&lt;/EM&gt;&lt;BR /&gt;polygon_features = polygon_layer.query(where='1=1',geometry_filter=gf,out_fields='field1,field2',return_geometry= False) // if you want polygon geometry to be returned then remove&amp;nbsp;return_geometry= False.&lt;/P&gt;</description>
      <pubDate>Tue, 13 Jul 2021 14:53:39 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/finding-a-polygon-that-contains-a-point/m-p/1078258#M6347</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2021-07-13T14:53:39Z</dc:date>
    </item>
  </channel>
</rss>

