Using ArcGIS 10.5, I have a cluster of polygons that represent districts (together this cluster of polygons creates a nation). I also have thousands of points that represent offices.

I would like to calculate the distance from the edge of each polygon (market) to the nearest point (office) outside of that polygon (market).

My goal is to calculate the distance of the nearest point (office) from each polygon (district) - such that a value is recorded next to each polygon (office) FID. I used the Near tool in toolkit, but it calculates a distance of 0 if there are points (offices) within the polygons (districts). I only want to calculate the distance from the nearest point (office) OUTSIDE of the district (polygon), but not INSIDE of it.

Could you help me with this?

To clarify, every polygon needs a value that is larger than zero. That value should represent the distance from the edge of that polygon to the nearest point OUTSIDE (and never inside) of that polygon.

  • How many polygons and Points do you have? – BERA Jan 10 '19 at 6:13
  • There are approximately 3,000 points (these are office locations) and there are 526 polygons (these are districts that form a country). – DrJV Jan 10 '19 at 6:20
  • Just delete the records with a distance of 0. Though I suspect you want to use Generate Near Table to get a distance from each polygon to every point then clean out for each polygon the distances equal to 0. Do you have an advanced license? If you can use generate near table you can then use Summary Statistics to find the minimum distance to an outside point with a case field of polygon ID to ensure each polygon is represented. – Michael Stimson Jan 10 '19 at 6:20
  • Yes, I have an advanced license. If I take this approach, how will I then calculate the distances from the edges of the polygons that were given a distance of 0 to the nearest branch OUTSIDE of that polygon? – DrJV Jan 10 '19 at 6:23
  • Read the docs on Generate Near Table resources.arcgis.com/en/help/main/10.2/index.html#//… it tabulates the distance for every combination up to the maximum tolerance. Remove the rows with a distance of 0 then filter with Summary Statistics resources.arcgis.com/en/help/main/10.2/index.html#//… – Michael Stimson Jan 10 '19 at 6:23

One option is to use arcpy and the distanceTo method. It will be a bit slower than using Near Tools, about 15s when i try it on 2365 Points and 21 polygons:

import arcpy

point_fc = 'bt_riks' #Change
polygon_fc = 'an_riks' #Change

d = {} #Dictionary to hold polygons objectid as key and nearest point (outside the polygon) id and distance as value

#Store objectids and geometries of points and polygons in lists
points = [i for i in arcpy.da.SearchCursor(point_fc,['OID@','SHAPE@'])] 
polys = [i for i in arcpy.da.SearchCursor(polygon_fc,['OID@','SHAPE@'])]

#For each polygon, check all Point distances and find closest one
for poly in polys:
    all_distances = [[point[0],poly[1].distanceTo(point[1])] for point in points] #Calculate distance to all points
    all_distances = [point for point in all_distances if point[1]>0] #Remove Points with distance=0
    all_distances.sort(key=lambda x: x[1]) #Sort by distance
    d[poly[0]] = all_distances[0] #Store Point id and distance for the point with smallest distance (first in list)

enter image description here

If you want to add nearest point id and distance to the attribute table of the polygons you can:

  1. Add Fields 'Nearest_point_id','Nearest_point_distance'
  2. Execute code:
with arcpy.da.UpdateCursor(polygon_fc,['OID@','Nearest_point_id','Nearest_point_distance']) as cursor:
    for row in cursor:
        if row[0] in d:
            row[1], row[2] = d[row[0]]
| improve this answer | |
  • @DrJV nice! If it solved your problem could you accept my answer with the checkbox? – BERA Jan 11 '19 at 6:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.