# Distance from edge of each polygon to nearest point, such that points within each polygon aren't included

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 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 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 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 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 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,poly.distanceTo(point)] for point in points] #Calculate distance to all points
all_distances = [point for point in all_distances if point>0] #Remove Points with distance=0
all_distances.sort(key=lambda x: x) #Sort by distance
d[poly] = all_distances #Store Point id and distance for the point with smallest distance (first in list)
`````` If you want to add nearest point id and distance to the attribute table of the polygons you can:

``````with arcpy.da.UpdateCursor(polygon_fc,['OID@','Nearest_point_id','Nearest_point_distance']) as cursor: