3

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?

4

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

  • There was a minor error, fixed now – FelixIP Jul 21 '15 at 21:48
1

Not that I can find. I've played with it a bit and you're right, the polygon/feature creation/construction tools don't seem to be accessible from within Replace Geometry. I can think of a couple of alternatives.

  • Create a new feature, drawing your circle so that it completely covers the poly you want to replace, then choose Edit > Merge and pick the original poly as the attribute source.
  • Same as the first option but circle inside the poly. With the circle selected, choose Edit > Clip, and you'll cut the poly down to the size/shape of the circle (note this will clip all visible/editable layers and shapes with the selected polygon, so if you have overlaps or other layers turned on it won't work). You can then delete the circle template, as the original shape will have been replaced/clipped to it.
  • Draw a new circle(s) where/as you want it(them), and then use the Attribute Transfer tool on the Spatial Adjustment toolbar to copy the attributes from the original poly to the new circle. Delete the original poly.

There are other ways you could do this with exporting and centroids and joins, but they're more involved processes than the above methods. There might be additional methods with scripting to update the geometry of a record with a predefined shape or replace it from another file (which could also be done with a Spatial Join if you create a new layer of circles over your polys and then use that to join/transfer attributes via spatial join with intersect method - again assuming no overlap).

A few different options available, depends on how many you have and how often you do this as to which route to go.

  • Thanks. These three methods are good way, I usually use third method. But because a lot of times want to replace the features sometime, the above method still a bit cumbersome.So I would like to ask if there is any way that I do not know the operation can achieve by one-step operation. – T J Jul 21 '15 at 8:48
0

FelixIP,Thank you.

You provides a good solution to solve the problem with the script, I tried to write a script that has achieved the functionality I want to achieve.

import arcpy,os.path as op,arcpy.mapping as arcmap

mxd=arcmap.MapDocument("CURRENT")
lyr=arcmap.ListLayers(mxd,"Target")[0]
workspace=op.dirname(lyr.dataSource)

result=arcpy.GetCount_management(lyr)
count=int(result.getOutput(0))

# Only run when two feature is selected
if count==2:
    desc=arcpy.Describe(lyr)
    OID=desc.OIDFieldName
    Shape=desc.ShapeFieldName
    fields=desc.fields

    fieldNames=[field.name for field in fields if field.name not in [desc.OIDFieldName,desc.ShapeFieldName]]

    with arcpy.da.Editor(workspace):
        rows=arcpy.UpdateCursor(lyr)
        fieldValues={}
        minFid=-1
        # Save old feature fields value
        for row in rows:
            FID=row.getValue(OID)
            if minFid==-1 or FID<minFid:
                for fieldName in fieldNames:
                    fieldValues[fieldName]=row.getValue(fieldName)
                minFid=FID

        rows=arcpy.UpdateCursor(lyr)
        for row in rows:
            # Delete old feature
            if row.getValue(OID)==minFid:
                rows.deleteRow(row)
            # Set old feature values to new feature
            else:
                for fieldName in fieldNames:
                    row.setValue(fieldName,fieldValues[fieldName])

    arcpy.RefreshActiveView()

else:
    arcpy.AddError("Please select two feature.")

del row,rows

enter image description here

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.