Saturday, 6 February 2016

arcmap - Using Replace Geometry to replace selected feature with circle in ArcGIS Desktop?


I have a polygon shapefile and I want replace every irregular polygon with a circle.



I think I can do it with the Replace Geometry tool. But once the Replace Geometry tool is selected, draw a polygon mode is immediately entered. If I select the circle tool in the Draw tool window, the Replace Geometry tool becomes inactive.


Is there any way to use the Replace Geometry tool to draw a circle polygon?



Answer



The script below is using following naming convention of layers in the table of content:



  1. "Target" - feature layer with geometries to be replaced

  2. "chops" - feature layer with correct geometries


It works on selection, i.e. 1 feature selected in each layer.


import arcpy, traceback, os, sys


try:
def showPyMessage():
arcpy.AddMessage(str(time.ctime()) + " - " + message)
def CheckBothLayers(infc,infc2):
d=arcpy.Describe(infc); theType1=d.shapeType
d=arcpy.Describe(infc2); theType2=d.shapeType

if theType1!=theType2:
arcpy.AddWarning("\nTool designed to work with same geometries!")

raise NameError, "Wrong input\n"
return d
mxd = arcpy.mapping.MapDocument("CURRENT")
layers = arcpy.mapping.ListLayers(mxd)

destLR, sourceLR="chops","target"
destLR = arcpy.mapping.ListLayers(mxd,destLR)[0]
sourceLR = arcpy.mapping.ListLayers(mxd,sourceLR)[0]
a = CheckBothLayers(destLR,sourceLR)
g=arcpy.Geometry()

geometryList=arcpy.CopyFeatures_management(destLR,g)
nLines=len(geometryList)
if nLines!=1:
arcpy.AddWarning("\nSelect only 1 feature!")
raise NameError, "Wrong input\n"
line=geometryList[0]

tbl=arcpy.da.TableToNumPyArray(sourceLR,"OID@")
if len(tbl)!=1:
arcpy.AddWarning("\nSelect only 1 feature!")

raise NameError, "Wrong input\n"
with arcpy.da.UpdateCursor(sourceLR, ("SHAPE@")) as rows:
for row in rows:
row[0]=line
rows.updateRow(row)
arcpy.SelectLayerByAttribute_management(destLR, "CLEAR_SELECTION")
arcpy.SelectLayerByAttribute_management(sourceLR, "CLEAR_SELECTION")
arcpy.RefreshActiveView()

except NameError, theMessage:

arcpy.AddMessage (theMessage)
except:
message = "\n*** PYTHON ERRORS *** "; showPyMessage()
message = "Python Traceback Info: " + traceback.format_tb(sys.exc_info()[2])[0]; showPyMessage()
message = "Python Error Info: " + str(sys.exc_type)+ ": " + str(sys.exc_value) + "\n"; showPyMessage()

The best way to use it - attach to a button on Editor toolbar:


enter image description here


Don't forget to save edits


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...