3

We are working with parcel data that includes a single polygon for each individual tax parcel. We need to develop a list that includes an attribute for each residential address. Some tax parcels could have multiple residential addresses, e.g. condos, apartments, etc. Although this could be done in a spreadsheet, it would likely be time consuming. The though is to copy each polygon x number of times dependent on the number of units in order to begin this list, this would allow for all the attribute data to be copied as well. In researching a way to do this is we came across Exploding features based on numerical field in QGIS?. I provides a Python script and minimal directions to run the script.The script is as follows:

layer.startEditing()
for f in layer.getFeatures():
    # Create X-1 new features since we already have the original
    for i in range(f['X']-1):
        layer.addFeature(f)
layer.commitChanges()

I am actually trying to replicate this in ArcGIS. I have translated this to the following:

import arcpy
mxd = arcpy.mapping.MapDocument(r"G:\LUCA_2020\WorkingMap_4.19.mxd")
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
lyr = arcpy.mapping.ListLayers(mxd, "newburgh_city_parcels_master_april2012_joinRPS", df)[0]
lyrfile = arcpy.mapping.layer(r"G:\GIS_Chad\Scratch\Delete\test.lyr")
arcpy.mapping.UpdateLayer(df, lyr, lyrfile, True)
layer.startEditing('newburgh_city_parcels_master_april2012_joinRPS')
for f in layer.getFeatures('total_units'):
    # Create X-1 new features since we already have the original
    for i in range(f['X']-1):
        layer.addFeature(f)
layer.commitChanges('newburgh_city_parcels_master_april2012_joinRPS')

When I run the script in the Python window, i get the following error:

Runtime error 
Traceback (most recent call last):
  File "<string>", line 5, in <module>
AttributeError: 'module' object has no attribute 'layer'

I assume there may be other, put I am not sure. I am looking for guidance on correcting and running the script in ArcGIS 10.3.

  • Is there anything before "layer.startEditing()"? Based on the error it appears that you have not yet defined what exactly "layer" refers to. Layer Example 5 from ArcGIS Support: lyr = arcpy.mapping.Layer(r'C:\Project\Data\Time\TemperatureWithTime.lyr') – smiller Apr 20 '18 at 15:05
  • There isn't, newbie here, updated post. – LandArch Apr 20 '18 at 15:12
  • You can create a layer directly from a feature class or shapefile, or select a layer out of a MXD file (Example 4 in my earlier link, or mimic some of the code in this post). It's probably easier to do the former -- create a layer immediately from a shapefile or feature class. – smiller Apr 20 '18 at 15:24
  • Duplicate geometries is bad idea. Use related table instead. – FelixIP Apr 21 '18 at 22:35
  • I need to duplicate the geometries in order to create the initial table for the relate. – LandArch Apr 22 '18 at 12:26
2

I haven't yet found a tool that does precisely what you need (duplicating items within the same feature class). Arcpy uses SearchCursors and UpdateCursors to parse through and update a list of records, but a more generic approach may be to duplicate the records the specified amount of times then merge together all of the files, since you need all of the fields to be copied over.

# 1. access the layer
# Import system modules
import arcpy
# Set the workspace
env.workspace = "c:/data/mexico.gdb"
# Make a layer from the feature class
arcpy.MakeFeatureLayer_management("cities", "lyr")

# 2. create a list of possible values in your polygon copy field
# 2a. You can do this manually as below, or select it from unique values in the field
numrepeats = [0,1,2,3,4,5]

# 3. loop through all of the values. Select by attributes for each value.
for i in numrepeats:
    sqlquery = '"repeatfieldname" = %d' % i
    # Select all cities with the specified value 
    arcpy.SelectLayerByAttribute_management("lyr", "NEW_SELECTION", sqlquery) 
# 4. Use CopyFeatures to copy to a new file. Repeat the number of times specified.
    n = 1
    while n < i:
        outFeatureClass = "polgyoncopy{}".format(n)
        arcpy.CopyFeatures_management("lyr", outFeatureClass)
        n += 1 #increment n

# 5. After all values have been processed, merge the resulting files

References mentioned in codeblock:

  1. Unique Values code from @Aaron
  2. Select by Attributes
  3. CopyFeatures
  4. Merge
  5. InsertCursor
0

I love the idea to use multipart shape that can be exploded afterwards. The workflow below creates multipoint point shape in order to reduce the size of output feature class:

arcpy.AddGeometryAttributes_management("Export_Output", "CENTROID_INSIDE")
arcpy.MakeXYEventLayer_management("Export_Output", "INSIDE_X","INSIDE_Y", "Export_Output_Layer")
arcpy.Dissolve_management("Export_Output_Layer", "../m_point.shp", "Label", statistics_fields="N FIRST;INSIDE_X FIRST;INSIDE_Y FIRST", multi_part="MULTI_PART")

Use field calculator expression on field Shape:

from math import sin, cos
def multiplyMe(n,xC,yC):
 angle,array=3.141593*2/n,arcpy.Array()
 for i in range(n):
   X=xC+50*cos(angle*i);Y=yC+50*sin(angle*i)
   array.add(arcpy.Point(X,Y))
 mPoint=arcpy.Multipoint(array)
 return mPoint
#-------------
multiplyMe( !FIRST_N!, !FIRST_INSI!, !FIRST_IN_1!)

INPUT: enter image description here

OUTPUT:

enter image description here

To create initial table for the relate simply join parent polygon table to multipoint table, export it and use multipart to single part tool.

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.