0

What I want to do is to manually select two features with the mouse (e.g. point or polygon), each in separate geodatabases to then copy the attributes values from one to the other (for only these two selected objects).

So far I have only managed to copy all attributes from one to the other. I am stuck at how to make update cursor run only on selected objects

I am trying to use something like this to get the selected objects, but this selects the entire layer:

mxd = arcpy.mapping.MapDocument("CURRENT")
layers = arcpy.mapping.ListLayers(mxd)
sel_layers=[str(i.name) for i in layers if arcpy.Describe(i.name).fidSet]

And my update cursor is this:

fc_search = r'C:\PythonFolder\Othergeodatabas.gdb\Polyline'
fc_destination = r'C:\PythonFolder\LKpolygon.gdb\Avloppsbrunn'
with arcpy.da.SearchCursor(fc_search,["DIM","MATERIAL"]) as search_cur:
 for search_row in search_cur:
 with arcpy.da.UpdateCursor(fc_destination,["Tillkomst_plan","Notering"]) as upd_cur:
 for upd_row in upd_cur:
 upd_row[0] = search_cur[0]
 upd_row[1] = search_cur[1]
 upd_cur.updateRow(upd_row)

This code works, but it applies changes to attributes for the entire layer, while I would like to only change attribute values for selected objects.

asked Nov 12, 2018 at 10:16
5
  • Welcome to GIS SE. As a new user, please take the Tour. Unfortunately, you've left out the code where the error occurs, and the code you have provided is deeply flawed. If you're selecting objects with a mouse, then there is a Layer object associated with that selection. Show us how the fc_search and fc_destination variables are defined. You also don't need to double-nest the cursors -- Just load the row array in one, then use it in the other. You should also use GetCount to verify that one row is in each set. Commented Nov 12, 2018 at 11:53
  • @BERA UpdateCursor will only update selected rows of a Layer (this is the problem). Listing the layers is necessary in order to identify which layer names to use, and only the mapping module can do this. Commented Nov 12, 2018 at 12:33
  • Thanks for your comments. I have been trying both. When I run the Updatecursor in the python window, then it updates all rows in the destination table, not just the selected one Commented Nov 12, 2018 at 12:41
  • That is because you are applying the cursor to the feature class and not the layer. The layer is selected, you cant select a feature class. So both fc_search and fc_destination needs to be layers, for example from your sel_layers list, or created with MakeFeatureLayer, or if you are using the python window the layer names as you see them in table of contents (probably "Polyline" or "Avloppsbrunn"). Commented Nov 12, 2018 at 12:48
  • 1
    @BERA Thank you, this was the answer I needed. It was obviously much more complicated in my brain than in reality. Commented Nov 13, 2018 at 7:31

1 Answer 1

2

A selection can only be made on a layer (not a feature class) and if you pass a layer to the cursors only selected rows will be proccessed. You are passing the feature classes to the cursors and that is why all rows are proccessed.

So both fc_search and fc_destination needs to be layers, for example from your sel_layers list, or created with MakeFeatureLayer, or if you are using the python window the layer names as you see them in table of contents (probably "Polyline" or "Avloppsbrunn").

You can also use a feature class as input and use the where_clause parameter of the cursors:

An optional expression that limits the records returned

answered Nov 12, 2018 at 12:34

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.