<?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 Re: Using &amp;quot;near&amp;quot; with python only on objects with the same ID in 2 shp in Mapping Questions</title>
    <link>https://community.esri.com/t5/mapping-questions/using-quot-near-quot-with-python-only-on-objects/m-p/1220344#M8480</link>
    <description>&lt;P&gt;Thanks a lot Johannes, I am going to look at your code and try to modify it to my files.&lt;/P&gt;&lt;P&gt;I was also on the way to (try to) use "Distance to" but you have been much quicker &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
    <pubDate>Mon, 10 Oct 2022 13:27:55 GMT</pubDate>
    <dc:creator>MaxenceMELIN</dc:creator>
    <dc:date>2022-10-10T13:27:55Z</dc:date>
    <item>
      <title>Using "near" with python only on objects with the same ID in 2 shp</title>
      <link>https://community.esri.com/t5/mapping-questions/using-quot-near-quot-with-python-only-on-objects/m-p/1220312#M8478</link>
      <description>&lt;P&gt;Hi guys,&lt;/P&gt;&lt;P&gt;I want to calculate distance between polygons and points which are in 2 SHP using "near" function.&lt;/P&gt;&lt;P&gt;in my 2 SHP, each point is related to a specific polygon, there is a link thanks to the attributes.&lt;/P&gt;&lt;P&gt;"Near" function seem to calculate the distance between the closest objects (in my case : point to the closest boundary of the polygon)&amp;nbsp;whereas I just want to calculate for each point distance between this point and the closest boundary of THE polygon with THE SAME ID (where point.polygon_id = polygon.id).&lt;/P&gt;&lt;P&gt;Do you see a simple way to do that in python ?&lt;/P&gt;</description>
      <pubDate>Mon, 10 Oct 2022 10:33:12 GMT</pubDate>
      <guid>https://community.esri.com/t5/mapping-questions/using-quot-near-quot-with-python-only-on-objects/m-p/1220312#M8478</guid>
      <dc:creator>MaxenceMELIN</dc:creator>
      <dc:date>2022-10-10T10:33:12Z</dc:date>
    </item>
    <item>
      <title>Re: Using "near" with python only on objects with the same ID in 2 shp</title>
      <link>https://community.esri.com/t5/mapping-questions/using-quot-near-quot-with-python-only-on-objects/m-p/1220333#M8479</link>
      <description>&lt;P&gt;It's probably doable with some tool (eg GenerateNearTable and then joining the output to the inputs somehow), but you can also rig up a quick script like this:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;in_features = "Points"  # distance field will be added to this fc
near_features = "Polygons"
in_key = "polygon_id"
near_key = "id"
distance_field = "DISTANCE"


# read near_features as dictionary {key: geometry}
near_geometries = {key: shp for key, shp in arcpy.da.SearchCursor(near_features, [near_key, "SHAPE@"])}

# add field to in_features
arcpy.management.AddField(in_features, distance_field, "DOUBLE")

# calculate
with arcpy.da.UpdateCursor(in_features, [in_key, distance_field, "SHAPE@"]) as cursor:
    for key, dist, shp in cursor:
        try:
            dist = shp.distanceTo(near_geometries[key])
        except KeyError:
            dist = None
            print(f"key {key} could not be found in {near_features}.")
        cursor.updateRow([key, dist, shp])&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 10 Oct 2022 12:28:44 GMT</pubDate>
      <guid>https://community.esri.com/t5/mapping-questions/using-quot-near-quot-with-python-only-on-objects/m-p/1220333#M8479</guid>
      <dc:creator>JohannesLindner</dc:creator>
      <dc:date>2022-10-10T12:28:44Z</dc:date>
    </item>
    <item>
      <title>Re: Using "near" with python only on objects with the same ID in 2 shp</title>
      <link>https://community.esri.com/t5/mapping-questions/using-quot-near-quot-with-python-only-on-objects/m-p/1220344#M8480</link>
      <description>&lt;P&gt;Thanks a lot Johannes, I am going to look at your code and try to modify it to my files.&lt;/P&gt;&lt;P&gt;I was also on the way to (try to) use "Distance to" but you have been much quicker &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 10 Oct 2022 13:27:55 GMT</pubDate>
      <guid>https://community.esri.com/t5/mapping-questions/using-quot-near-quot-with-python-only-on-objects/m-p/1220344#M8480</guid>
      <dc:creator>MaxenceMELIN</dc:creator>
      <dc:date>2022-10-10T13:27:55Z</dc:date>
    </item>
    <item>
      <title>Re: Using "near" with python only on objects with the same ID in 2 shp</title>
      <link>https://community.esri.com/t5/mapping-questions/using-quot-near-quot-with-python-only-on-objects/m-p/1256466#M8494</link>
      <description>&lt;P&gt;Hmm, there was a post here this morning, but when I refreshed right now, it disappeared, maybe you deleted it?&lt;/P&gt;&lt;P&gt;Anyway, I remember that there was a problem with the key fields you assigned in your code.&lt;/P&gt;&lt;P&gt;So, just to be clear (which I wasn't in my original answer):&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;near_features&lt;/STRONG&gt; is the polygon fc. It has a field that acts as primary key: it uniquely identifies each polygon (like OBJECTID). Assign this field's name to &lt;STRONG&gt;near_key&lt;/STRONG&gt;. In my example script, this field would be Polygons.id&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;in_features&lt;/STRONG&gt; is the point fc. It has a field that links to the primary key of the polygon fc. Assign this field's name to &lt;STRONG&gt;in_key&lt;/STRONG&gt;. In my example script, this field would be Points.polygon_id&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I think I read "point_id" somewhere in your code, that seemed wrong. Sorry that I wasn't very clear on that point. Feel free to ask questions, of course.&lt;/P&gt;</description>
      <pubDate>Thu, 09 Feb 2023 13:29:09 GMT</pubDate>
      <guid>https://community.esri.com/t5/mapping-questions/using-quot-near-quot-with-python-only-on-objects/m-p/1256466#M8494</guid>
      <dc:creator>JohannesLindner</dc:creator>
      <dc:date>2023-02-09T13:29:09Z</dc:date>
    </item>
    <item>
      <title>Re: Using "near" with python only on objects with the same ID in 2 shp</title>
      <link>https://community.esri.com/t5/mapping-questions/using-quot-near-quot-with-python-only-on-objects/m-p/1259992#M8498</link>
      <description>&lt;P&gt;Hi Johannes,&lt;/P&gt;&lt;P&gt;You're right, I deleted the post I wrote too quickly&amp;nbsp;&lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt; , sorry...&lt;BR /&gt;Since that, I took time to modify &amp;amp; test your arcpy code on my data (and took some vacation also &lt;span class="lia-unicode-emoji" title=":snow_capped_mountain:"&gt;🏔&lt;/span&gt;) and everything works fine now : it does exactly what I want, thanks a lot !!!&amp;nbsp;&lt;/P&gt;&lt;P&gt;Have a great day too !&lt;/P&gt;&lt;P&gt;Maxence&lt;/P&gt;</description>
      <pubDate>Tue, 21 Feb 2023 08:46:02 GMT</pubDate>
      <guid>https://community.esri.com/t5/mapping-questions/using-quot-near-quot-with-python-only-on-objects/m-p/1259992#M8498</guid>
      <dc:creator>MaxenceMELIN</dc:creator>
      <dc:date>2023-02-21T08:46:02Z</dc:date>
    </item>
  </channel>
</rss>

