<?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 arcpy points in polygon check in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/arcpy-points-in-polygon-check/m-p/758125#M58516</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hello, &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I have a list of points and a list of polygons both in a separate shape files.&amp;nbsp; I am trying to write a ArcPy script to loop through the points, and then the polygons respectively and tell me when a point is within a polygon.&amp;nbsp; I have tried both the contains and touches methods on polygons but haven't had any luck.&amp;nbsp; Any help would be appreciated. So far I have the following:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;import arcpy&amp;nbsp; pointFeatureClass = "C:\\Data\\POINTS2.dbf" pointFieldList = arcpy.ListFields(pointFeatureClass) for field in pointFieldList:&amp;nbsp; curField = field.name&amp;nbsp;&amp;nbsp; arcpy.AddMessage(curField)&amp;nbsp; ptRows = arcpy.SearchCursor("C:\\Data\\POINTS2.dbf", "", "", "CID;Shape","CID;Shape")&amp;nbsp; for ptRow in ptRows:&amp;nbsp;&amp;nbsp; #Now loop through poly's testing for intersect&amp;nbsp; shpRows = arcpy.SearchCursor("C:\\Data\\counties.shp", "", "", "NAME;Shape","NAME;Shape")&amp;nbsp; for shpRow in shpRows:&amp;nbsp; &amp;nbsp; #arcpy.AddMessage("Checking for point "+ str(ptRow.CID) +" in: " + shpRow.NAME) &amp;nbsp; if shpRow.Shape.touches(ptRow.Shape): &amp;nbsp;&amp;nbsp; arcpy.AddMessage("Got it: " + shpRow.NAME)&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Tue, 26 Feb 2013 13:51:39 GMT</pubDate>
    <dc:creator>JohnMorgan</dc:creator>
    <dc:date>2013-02-26T13:51:39Z</dc:date>
    <item>
      <title>arcpy points in polygon check</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-points-in-polygon-check/m-p/758125#M58516</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hello, &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I have a list of points and a list of polygons both in a separate shape files.&amp;nbsp; I am trying to write a ArcPy script to loop through the points, and then the polygons respectively and tell me when a point is within a polygon.&amp;nbsp; I have tried both the contains and touches methods on polygons but haven't had any luck.&amp;nbsp; Any help would be appreciated. So far I have the following:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;import arcpy&amp;nbsp; pointFeatureClass = "C:\\Data\\POINTS2.dbf" pointFieldList = arcpy.ListFields(pointFeatureClass) for field in pointFieldList:&amp;nbsp; curField = field.name&amp;nbsp;&amp;nbsp; arcpy.AddMessage(curField)&amp;nbsp; ptRows = arcpy.SearchCursor("C:\\Data\\POINTS2.dbf", "", "", "CID;Shape","CID;Shape")&amp;nbsp; for ptRow in ptRows:&amp;nbsp;&amp;nbsp; #Now loop through poly's testing for intersect&amp;nbsp; shpRows = arcpy.SearchCursor("C:\\Data\\counties.shp", "", "", "NAME;Shape","NAME;Shape")&amp;nbsp; for shpRow in shpRows:&amp;nbsp; &amp;nbsp; #arcpy.AddMessage("Checking for point "+ str(ptRow.CID) +" in: " + shpRow.NAME) &amp;nbsp; if shpRow.Shape.touches(ptRow.Shape): &amp;nbsp;&amp;nbsp; arcpy.AddMessage("Got it: " + shpRow.NAME)&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 26 Feb 2013 13:51:39 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-points-in-polygon-check/m-p/758125#M58516</guid>
      <dc:creator>JohnMorgan</dc:creator>
      <dc:date>2013-02-26T13:51:39Z</dc:date>
    </item>
    <item>
      <title>Re: arcpy points in polygon check</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-points-in-polygon-check/m-p/758126#M58517</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Is there some reason &lt;/SPAN&gt;&lt;A class="jive-link-external-small" href="http://resources.arcgis.com/en/help/main/10.1/index.html#//00080000000q000000" rel="nofollow" target="_blank"&gt;Spatial Join&lt;/A&gt;&lt;SPAN&gt; doesn't work for you?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 26 Feb 2013 14:08:19 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-points-in-polygon-check/m-p/758126#M58517</guid>
      <dc:creator>MathewCoyle</dc:creator>
      <dc:date>2013-02-26T14:08:19Z</dc:date>
    </item>
    <item>
      <title>Re: arcpy points in polygon check</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-points-in-polygon-check/m-p/758127#M58518</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks for you response. I am trying that now.&amp;nbsp; I should also point out that I am starting with a list of points in a dbf and then converting them to point geometry with &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;point = arcpy.Point(ptRow.xpos, ptRow.ypos)
 ptGeometry = arcpy.PointGeometry(point) &lt;/PRE&gt;&lt;BR /&gt;&lt;SPAN&gt;And that I am dealing with about 17 million points and 3000 polygons.&amp;nbsp; Therefore, I was assuming that this would be something to script.&amp;nbsp; But if the join works then that would be great.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Derek&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 08:08:23 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-points-in-polygon-check/m-p/758127#M58518</guid>
      <dc:creator>JohnMorgan</dc:creator>
      <dc:date>2021-12-12T08:08:23Z</dc:date>
    </item>
    <item>
      <title>Re: arcpy points in polygon check</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-points-in-polygon-check/m-p/758128#M58519</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;That is quite a few points to get through but should be doable. Is this something you will be doing with some frequency? If so you would definitely want to write a script for it, however basic. If it is something you want to optimize and/or will be doing quite often you can get right down to the math and bypass arcpy completely (albeit reinventing the wheel a bit). It follows the concept of the Jordan curve theorem to determine whether a point is inside or outside a polygon. You can utilize the code for the even-odd rule here to get started. &lt;/SPAN&gt;&lt;A href="http://en.wikipedia.org/wiki/Even-odd_rule"&gt;http://en.wikipedia.org/wiki/Even-odd_rule&lt;/A&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 26 Feb 2013 14:50:16 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-points-in-polygon-check/m-p/758128#M58519</guid>
      <dc:creator>MathewCoyle</dc:creator>
      <dc:date>2013-02-26T14:50:16Z</dc:date>
    </item>
    <item>
      <title>Re: arcpy points in polygon check</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-points-in-polygon-check/m-p/758129#M58520</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;That is quite a few points to get through but should be doable. Is this something you will be doing with some frequency? If so you would definitely want to write a script for it, however basic. If it is something you want to optimize and/or will be doing quite often you can get right down to the math and bypass arcpy completely (albeit reinventing the wheel a bit). It follows the concept of the Jordan curve theorem to determine whether a point is inside or outside a polygon. You can utilize the code for the even-odd rule here to get started. &lt;A href="http://en.wikipedia.org/wiki/Even-odd_rule"&gt;http://en.wikipedia.org/wiki/Even-odd_rule&lt;/A&gt;&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks Mathew.&amp;nbsp; That is interesting reading.&amp;nbsp; The spatial join did end up working for me best though as you first replied.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 28 Feb 2013 00:25:02 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-points-in-polygon-check/m-p/758129#M58520</guid>
      <dc:creator>JohnMorgan</dc:creator>
      <dc:date>2013-02-28T00:25:02Z</dc:date>
    </item>
    <item>
      <title>Re: arcpy points in polygon check</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-points-in-polygon-check/m-p/758130#M58521</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I came across this method some time ago. This method does not use arcpy and is much faster. The only issue is that you have to import the geometry of your polygons.&lt;/P&gt;&lt;P&gt;&lt;A href="http://geospatialpython.com/2011/08/point-in-polygon-2-on-line.html" title="http://geospatialpython.com/2011/08/point-in-polygon-2-on-line.html"&gt;GeospatialPython.com: Point in Polygon 2: Walking the line&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE __default_attr="python" __jive_macro_name="code" class="jive_macro_code _jivemacro_uid_14091775809407271 jive_text_macro" jivemacro_uid="_14091775809407271"&gt;
&lt;P&gt;def point_in_poly(x,y,poly):&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; # check if point is a vertex&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; if (x,y) in poly: return "Point " + str(x) + "," + str(y) + " is IN"&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; # check if point is on a boundary&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; for i in range(len(poly)):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; p1 = None&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; p2 = None&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if i==0:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; p1 = poly[0]&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; p2 = poly[1]&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; p1 = poly[i-1]&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; p2 = poly&lt;I&gt;&lt;/I&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if p1[1] == p2[1] and p1[1] == y and x &amp;gt; min(p1[0], p2[0]) and x &amp;lt; max(p1[0], p2[0]):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return "Point " + str(x) + "," + str(y) + " is IN"&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; n = len(poly)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; inside = False&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; p1x,p1y = poly[0]&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; for i in range(n+1):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; p2x,p2y = poly[i % n]&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if y &amp;gt; min(p1y,p2y):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if y &amp;lt;= max(p1y,p2y):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if x &amp;lt;= max(p1x,p2x):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if p1y != p2y:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; xints = (y-p1y)*(p2x-p1x)/(p2y-p1y)+p1x&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if p1x == p2x or x &amp;lt;= xints:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; inside = not inside&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; p1x,p1y = p2x,p2y&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; if inside: return "Point " + str(x) + "," + str(y) + " is IN"&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; else: return "Point " + str(x) + "," + str(y) + " is OUT"&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;# Test a vertex for inclusion&lt;/P&gt;
&lt;P&gt;poligono = [(-33.416032,-70.593016), (-33.415370,-70.589604),&lt;/P&gt;
&lt;P&gt;(-33.417340,-70.589046), (-33.417949,-70.592351),&lt;/P&gt;
&lt;P&gt;(-33.416032,-70.593016)]&lt;/P&gt;
&lt;P&gt;lat= -33.416032&lt;/P&gt;
&lt;P&gt;lon= -70.593016&lt;/P&gt;
&lt;P&gt;print point_in_poly(lat, lon, poligono)&lt;/P&gt;
&lt;P&gt;# test a boundary point for inclusion&lt;/P&gt;
&lt;P&gt;poly2 = [(1,1), (5,1), (5,5), (1,5), (1,1)]&lt;/P&gt;
&lt;P&gt;x = 10&lt;/P&gt;
&lt;P&gt;y = 1&lt;/P&gt;
&lt;P&gt;print point_in_poly(x, y, poly2)&lt;/P&gt;

&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 27 Aug 2014 22:13:18 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-points-in-polygon-check/m-p/758130#M58521</guid>
      <dc:creator>NettoChad</dc:creator>
      <dc:date>2014-08-27T22:13:18Z</dc:date>
    </item>
  </channel>
</rss>

