Wednesday, 13 February 2019

arcmap - Merging polygons that share one vertex (node neighbors) using ArcGIS Desktop?


I have a set of very large shapefiles and would like to merge polygons within these that share one vertex.



Currently they are all separate like so:


enter image description here


What tool can I use to merge all the polygons that share one vertex so in the above example the selected polygon would be part of the one to the left like so:


enter image description here



Answer



Suggestion in my comments "Convert back to integer raster, region group, to polygons, dissolve to multipart" will work for polygons derived from rasters.


However in general it's better to start with suggestion by @Polygeo . You'll need to use script though:


import arcpy
import networkx as nx
## replace 2 lines below to point to polygons and neighbours table

Nodes=r'C:\SCRATCH\SCRATCH.gdb\R2P'
Links=r'C:\SCRATCH\SCRATCH.gdb\NBRS'
fldFROM,fldTO="src_OBJECTID","nbr_OBJECTID"
G=nx.Graph()
with arcpy.da.SearchCursor(Links, (fldFROM,fldTO)) as cursor:
for f,t in cursor:
G.add_edge(int(f),int(t))
d,N=dict(),1
for group in nx.connected_components(G):
for n in group:d[n]=N

N+=1
with arcpy.da.UpdateCursor(Nodes, ("OBJECTID","GRP")) as cursor:
for k, v in cursor:
if k in d:v=d[k]
else: N+=1;v=N
cursor.updateRow((k,v))

Script assumes that your neighborhood table called NBRS, your polygons stored in file GDB and there is a field called "GRP" to store group numbers.


enter image description here


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...