0

I am trying to make a selection of my "layer" and create a "new_layer" of the selection. Next I want to append data to the memory layer. But nothing get appended with this script, nor get I any messages. After the data is appended I will write it to a permanent layer. Can I append data to a memory layer like I am trying to do or what seem to be wrong?

try:
# Make memory layer from subset of feature class
    new_layer = arcpy.MakeFeatureLayer_management("layer","sub_layer","Contract = AB23" )

except:
   print(arcpy.GetMessages())

DataToAppend = "c:/data.gdb/MyLayer"
appendData = []
appendData.append(DataToAppend)

# Append data to layer from input
  arcpy.Append_management(
    inputs=appendData,
    target=new_layer,
    schema_type="NO_TEST",
)

# save layer permanently
arcpy.CopyFeatures_management("sub_layer", out_location)

-----------------------EDIT---------------------------

This script work. But its feels a bit bloated and I am need to delete the copied feature class if appendData fails somehow. I will make this dynamic and add it as a script to a toolbox

# Execute CopyFeatures to make a new copy of the feature class
arcpy.CopyFeatures_management(inFeatures, outFeatures)

# Create temporary layer based on the copied FC
arcpy.MakeFeatureLayer_management(outFeatures, "tempLayer")
print("Created feature layer")

# Select Subset of temp layer
arcpy.SelectLayerByAttribute_management("tempLayer", "NEW_SELECTION", "Contract IN ('AB123')")

if int(arcpy.GetCount_management("tempLayer")[0]) > 0:
    print("Selected items")
    arcpy.DeleteFeatures_management("tempLayer")

new_data = c:/data.gdb/MyLayer
appendData = []
appendData.append(new_data)

# Append data to layer from input
arcpy.Append_management(
    inputs=appendData,
    target=outFeatures,
    schema_type="NO_TEST",
)
5
  • Interesting, thank I will look into that and try it. I assumed I could do it this way since the layer written to the gdb is a feature class Commented Nov 21, 2018 at 16:17
  • 1
    Yes you should be able to do it this way, but I get strange errors with feature layers. After looking at this some more, I think your problem could also be the sql expression in make feature layer. if 'Contract' is a text field, it should be "Contract = '23'" Commented Nov 21, 2018 at 16:49
  • Sorry if its unclear, the field type is text in the data I am using. Trying your approach too, but takes me a some time to get it setup (new to arcpy) Commented Nov 21, 2018 at 16:55
  • the expressions can be tricky in arcpy. If you open a feature class and enter a definition query it will look something like Field = 'BlahBlah'. That entire string needs to be brought into Python like so - "Field = 'BlahBlah'" - (Wrap it in double quotes to make it a string of text). Double Quote - Field Name - Operator - Single Quote - Value - Single Quote - Double Quote Commented Nov 21, 2018 at 17:33
  • When presenting code here and when testing I think it is almost always better to remove any try/except statements because they can mask otherwise helpful error messages. Commented Nov 21, 2018 at 22:14

1 Answer 1

1

You're creating a feature layer, which is not a feature class. You need to create a feature class and set the output location to in_memory/yourfeatureclass and use feature class to feature class or copy features instead of make feature layer.

Try the edited code below. If you get an error you may need to add the path to new_layer explicitly (after you create it) - new_layer_path = r'in_memory/new_layer':

try:
# Make memory layer from subset of feature class
    infc = arcpy.mapping.Layer("layer")
    infc.definitionQuery = "Contract in ('AB123')"
    mem_fc = arcpy.CopyFeatures_management(infc,r"in_memory/new_layer")
    new_layer = arcpy.MakeFeatureLayer_management(mem_fc,"new_layer")

except:
   print(arcpy.GetMessages())

DataToAppend = "c:/data.gdb/MyLayer"
appendData = []
appendData.append(DataToAppend)

# Append data to layer from input
  arcpy.Append_management(
    inputs=appendData,
    target=new_layer,
    schema_type="NO_TEST",
)

# save layer permanently
arcpy.CopyFeatures_management("sub_layer", out_location)
3
  • Edited my answer to a working example, not sure if that is what you had in mind? If I interpreted your suggestion properly Commented Nov 21, 2018 at 17:34
  • 1
    I was just suggesting that you copy the subset into memory rather than directly creating a feature layer. You can set the output path as "in_memory/YourLayerName" to write to memory. Then you don't have the data to delete at the end. Commented Nov 21, 2018 at 17:37
  • Oh I see what you mean now, sorry. Yes that is a more logical way of doing it. arcpy.mapping.layer was new to me. Thats a better approach. Thank you Commented Nov 21, 2018 at 18:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.