0

I have a small script to search through a featureclass and copy and entire row to another featureclass. Its working well, in that I get all of the attributes for the row, but there is no geometry for the layer and no shapes appear. It is a POLYLINE layer and all of my shape_lengths = 0

    import arcpy
from arcpy import env

arcpy.env.overwriteOutput = True


env.workspace = r"C:\temp\gamma\Gamma_Survey_With_Lines.gdb"


Detector_Line = env.workspace + "/Detector_Line"
GammaDetectionLocation = env.workspace + "/GammaDetectionLocation"

arcpy.MakeFeatureLayer_management(GammaDetectionLocation, "GammaDetectionLocation1") 


#lyr2Explode = arcpy.CreateFeatureclass_management("in_memory", "CopyOfGammaDetectionLocation", "POLYLINE",GammaDetectionLocation,"DISABLED","DISABLED",GammaDetectionLocation)
lyr2Explode = arcpy.CreateFeatureclass_management(env.workspace, "CopyofDetectorLine", "POLYLINE",Detector_Line,"DISABLED","DISABLED",Detector_Line)


cur = arcpy.InsertCursor(lyr2Explode)

field = "GlobalID"
cursor = arcpy.SearchCursor(Detector_Line)
for row in cursor:
    #print(row.getValue(field))
    globalid = row.getValue(field)
    arcpy.SelectLayerByAttribute_management("GammaDetectionLocation1", "NEW_SELECTION", "SourceTrack = '" + globalid + "'")
    count = int(arcpy.GetCount_management("GammaDetectionLocation1").getOutput(0))
    if(count == 0):
        print count
        feat = cur.newRow()
        feat.shape = row.Shape
        feat.setValue("UserID","doodie")
        cur.insertRow(feat)
1

It looks like the fields are identical between the two layers. I'd try listing all fields and adding the "SHAPE@" token to make sure you snag the geometry.

Maybe something like this:

#Fields list
#Get all fields
flds = [f.name for f in ListFields (Detector_Line)]
#Add shape@ token
flds += ["SHAPE@"]

with arcpy.da.SearchCursor(Detector_Line, flds) as sCur:
    with arcpy.da.InsertCursor(lyr2Explode, flds) as iCur:
        for row in sCur:
            print(row[17])
            globalid = row[17]
            arcpy.SelectLayerByAttribute_management("GammaDetectionLocation1", "NEW_SELECTION", "SourceTrack = '" + globalid + "'")
            count = int(arcpy.GetCount_management("GammaDetectionLocation1").getOutput(0))
            if(count == 0):
                print count
                iCur.insertRow(row)

if there's trouble with the OID field you can modify it as such:

oidFld = arcpy.Describe (Detector_Line).OIDFieldName
flds = [f.name for f in ListFields (Detector_Line) if f.name != oidFld]
...
  • The shape field was there, not sure what the issue is. I have reworked the code, updated above, to apply the row.Shape, but now I must set each attribute individually. Not a bad thing, but a bit more tedious. You would think "*" would bring over everything, but the geometry is empty still. – ericoneal Sep 23 '15 at 13:44
  • Did you give my method a shot? The SHAPE@ token is different from the shape field name. See data access search cursor help. – Emil Brundage Sep 23 '15 at 15:14
  • Sorry, yes I did, it made no difference. – ericoneal Sep 23 '15 at 15:45

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.