Hello everyone,
I have a procedure which takes a selected shape(s) from a layer and creates a copy of the shape in a different feature class. This procedure worked fine in desktop 10.5 and I am now trying to modify it to use in Pro 2.6.2.
The code works fine and it copies the shape into the other feature class but what isn't working is that the parcel which I copied the first time, continues to be copied in subsequent tests (even though I'm selecting different parcels).
Below is a picture of the layer that the parcel to be copied is selected from. I have done numerous tests, each one selecting a different parcel.
Below is a picture of the feature class where the shapes are copied to. All of the records in the attribute table have the same parcel location (even though I selected different parcels above).
Here is the script:
def CreateShapes(item, mapType, selLyr, dType, dbPath, proj, mapName, luCode=None):
arcpy.env.overwriteOutput = True
aprx = arcpy.mp.ArcGISProject(proj)
m = aprx.listMaps(mapName)[0]
inLayer = m.listLayers(selLyr)[0]
#Get a count of how many shapes are selected
resultSelShapes = arcpy.GetCount_management(inLayer)
countSelShapes = int(resultSelShapes.getOutput(0))
arcpy.AddMessage('Number of rows in inLayer: {}'.format(str(countSelShapes)))
#create in_memory feature class
tempFc = 'in_memory/tempFc'
arcpy.CreateFeatureclass_management('in_memory','tempFc','POLYGON')
if dType != 'NotRequired':
#dissolve shapes
arcpy.Dissolve_management(in_features=inLayer, out_feature_class=tempFc,
multi_part=dType, unsplit_lines="DISSOLVE_LINES")
#Get a count of how many shapes are in the post dissolve in memory feature class
resultTempFc = arcpy.GetCount_management(tempFc)
countTempFc = int(resultTempFc.getOutput(0))
arcpy.AddMessage('Number of rows in tempFc: {}'.format(str(countTempFc)))
polyGeom = []
with arcpy.da.SearchCursor(in_table=tempFc, field_names='SHAPE@') as sCur:
for row in sCur:
polyGeom.append(row[0])
arcpy.AddMessage('polyGeom.append(row[0]): {}'.format(row[0]))
del sCur
if mapType == 'Land Use Amendment':
with arcpy.da.InsertCursor(in_table=dbPath + r'\PEYTO.PLANNING.PLAN_SITE_MNT',
field_names=['APPLICATION_TYPE','ITEM','IS_VALID','SHAPE@']) as iCur:
for poly in polyGeom:
iCur.insertRow((mapType,item,'Y',poly))
del iCur
with arcpy.da.InsertCursor(in_table=dbPath + r'\PEYTO.PLANNING.PLAN_LU_BOUNDARY_MNT',
field_names=['ITEM','STATUS','LU_CODE','IS_VALID','SHAPE@']) as iCur2:
for poly in polyGeom:
iCur2.insertRow((item,'PENDING',luCode,'Y',poly))
del iCur2
arcpy.AddMessage('Create Shapes procedure passed.')
return True
else:
with arcpy.da.InsertCursor(in_table=dbPath + r'\PEYTO.PLANNING.PLAN_SITE_MNT',
field_names=['APPLICATION_TYPE','ITEM','IS_VALID','SHAPE@']) as iCur:
for poly in polyGeom:
iCur.insertRow((mapType,item,'Y',poly))
del iCur
Appreciate any ideas as to why the parcel being selected isn't the one being inserted into the other feature class.
Thanks
My guess the issue is this line:
with arcpy.da.SearchCursor(in_table=tempFc, field_names='SHAPE@') as sCur:
It appears you are making a selection on a layer and want that selection to be honored. Unfortunately, your code is passing a data set and not a layer (even though something seems odd as well with how you are creating the temporary feature class). Data sets don't contain selections, layers and table views do.
Also, in Pro, use the "memory" workspace and not "in-memory".
Thanks. I'll dig into this some more and update. Thanks for the tip on using 'memory' too.
I got this working. It seems the problem was in the parameter definition:
shapesUsedFromLayer = arcpy.Parameter(
displayName="Selected Shape(s) Will Be Used From This Layer",
name="shapesUsedFromLayer",
datatype="GPFeatureLayer", #"GPString",
parameterType="Required",
direction="Input")
I had the parameter as GPString instead of GPFeatureLayer. I also removed references to inLayer and just used selLyr (from the parameter).