arcpy.da.searchCursor - need to go through all records, but only go through one. What am I missing?











up vote
1
down vote

favorite












My script supposed to go through all rows in one dataset, fl1:




  • select one row at a time,

  • based on that selection, select spatially from another dataset, fl2

  • if spatially identical records were found, update some columns from fl1 to fl2.


My script only does through one record. What am I missing to get going through every row in fl1? I thought that SearchCursor goes from one row to the next.



with arcpy.da.SearchCursor(fc1, fields1) as search_cur:
for search_row in search_cur:
with arcpy.da.UpdateCursor(fc2, fields2) as upd_cur:
for upd_row in upd_cur:
# select this record in fc1
whereClause = "ObjectID_1 = {0}".format(search_row[0])
#fl1 = rcpy.MakeFeatureLayer_management(fl1, 'fl1') # creating feature layer
arcpy.SelectLayerByAttribute_management(fl1, "NEW_SELECTION", whereClause)
# check for spatial intersect
#fl2 = arcpy.MakeFeatureLayer_management(fl2, 'fl2') # creating feature layer
arcpy.SelectLayerByLocation_management(fl2, "ARE_IDENTICAL_TO", fl1, "", "NEW_SELECTION")
# check for selection in fl2
desc=arcpy.Describe("fl2")
if not desc.FIDSet:
# if selection is not empty
print search_row[1]









share|improve this question






















  • Do you get any error messages? What is if not desc.FIDSet: trying to do? I notice you are using a mix of fl2 (variable) and "fl2" (literal) this might cause problems depending on the value of the fl2 variable.
    – Michael Stimson
    2 hours ago






  • 2




    Rather than run an update cursor within a search cursor, I think you will get much better performance by using your search cursor to create a dictionary, and then using your update cursor separately to retrieve values from the dictionary.
    – PolyGeo
    2 hours ago










  • desc.FIDSet is checking if I have any features selected. If there is no selection, there is nothing to update.
    – lida
    1 hour ago










  • To answer your original question, I think the reason it only runs once is that cursors will limit themselves to selected records. In the first row of the searchcursor, you select that same row. Since that is now the only record to loop over, the search cursor thinks it is finished.
    – Marc Pfister
    13 mins ago















up vote
1
down vote

favorite












My script supposed to go through all rows in one dataset, fl1:




  • select one row at a time,

  • based on that selection, select spatially from another dataset, fl2

  • if spatially identical records were found, update some columns from fl1 to fl2.


My script only does through one record. What am I missing to get going through every row in fl1? I thought that SearchCursor goes from one row to the next.



with arcpy.da.SearchCursor(fc1, fields1) as search_cur:
for search_row in search_cur:
with arcpy.da.UpdateCursor(fc2, fields2) as upd_cur:
for upd_row in upd_cur:
# select this record in fc1
whereClause = "ObjectID_1 = {0}".format(search_row[0])
#fl1 = rcpy.MakeFeatureLayer_management(fl1, 'fl1') # creating feature layer
arcpy.SelectLayerByAttribute_management(fl1, "NEW_SELECTION", whereClause)
# check for spatial intersect
#fl2 = arcpy.MakeFeatureLayer_management(fl2, 'fl2') # creating feature layer
arcpy.SelectLayerByLocation_management(fl2, "ARE_IDENTICAL_TO", fl1, "", "NEW_SELECTION")
# check for selection in fl2
desc=arcpy.Describe("fl2")
if not desc.FIDSet:
# if selection is not empty
print search_row[1]









share|improve this question






















  • Do you get any error messages? What is if not desc.FIDSet: trying to do? I notice you are using a mix of fl2 (variable) and "fl2" (literal) this might cause problems depending on the value of the fl2 variable.
    – Michael Stimson
    2 hours ago






  • 2




    Rather than run an update cursor within a search cursor, I think you will get much better performance by using your search cursor to create a dictionary, and then using your update cursor separately to retrieve values from the dictionary.
    – PolyGeo
    2 hours ago










  • desc.FIDSet is checking if I have any features selected. If there is no selection, there is nothing to update.
    – lida
    1 hour ago










  • To answer your original question, I think the reason it only runs once is that cursors will limit themselves to selected records. In the first row of the searchcursor, you select that same row. Since that is now the only record to loop over, the search cursor thinks it is finished.
    – Marc Pfister
    13 mins ago













up vote
1
down vote

favorite









up vote
1
down vote

favorite











My script supposed to go through all rows in one dataset, fl1:




  • select one row at a time,

  • based on that selection, select spatially from another dataset, fl2

  • if spatially identical records were found, update some columns from fl1 to fl2.


My script only does through one record. What am I missing to get going through every row in fl1? I thought that SearchCursor goes from one row to the next.



with arcpy.da.SearchCursor(fc1, fields1) as search_cur:
for search_row in search_cur:
with arcpy.da.UpdateCursor(fc2, fields2) as upd_cur:
for upd_row in upd_cur:
# select this record in fc1
whereClause = "ObjectID_1 = {0}".format(search_row[0])
#fl1 = rcpy.MakeFeatureLayer_management(fl1, 'fl1') # creating feature layer
arcpy.SelectLayerByAttribute_management(fl1, "NEW_SELECTION", whereClause)
# check for spatial intersect
#fl2 = arcpy.MakeFeatureLayer_management(fl2, 'fl2') # creating feature layer
arcpy.SelectLayerByLocation_management(fl2, "ARE_IDENTICAL_TO", fl1, "", "NEW_SELECTION")
# check for selection in fl2
desc=arcpy.Describe("fl2")
if not desc.FIDSet:
# if selection is not empty
print search_row[1]









share|improve this question













My script supposed to go through all rows in one dataset, fl1:




  • select one row at a time,

  • based on that selection, select spatially from another dataset, fl2

  • if spatially identical records were found, update some columns from fl1 to fl2.


My script only does through one record. What am I missing to get going through every row in fl1? I thought that SearchCursor goes from one row to the next.



with arcpy.da.SearchCursor(fc1, fields1) as search_cur:
for search_row in search_cur:
with arcpy.da.UpdateCursor(fc2, fields2) as upd_cur:
for upd_row in upd_cur:
# select this record in fc1
whereClause = "ObjectID_1 = {0}".format(search_row[0])
#fl1 = rcpy.MakeFeatureLayer_management(fl1, 'fl1') # creating feature layer
arcpy.SelectLayerByAttribute_management(fl1, "NEW_SELECTION", whereClause)
# check for spatial intersect
#fl2 = arcpy.MakeFeatureLayer_management(fl2, 'fl2') # creating feature layer
arcpy.SelectLayerByLocation_management(fl2, "ARE_IDENTICAL_TO", fl1, "", "NEW_SELECTION")
# check for selection in fl2
desc=arcpy.Describe("fl2")
if not desc.FIDSet:
# if selection is not empty
print search_row[1]






arcpy






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 2 hours ago









lida

728




728












  • Do you get any error messages? What is if not desc.FIDSet: trying to do? I notice you are using a mix of fl2 (variable) and "fl2" (literal) this might cause problems depending on the value of the fl2 variable.
    – Michael Stimson
    2 hours ago






  • 2




    Rather than run an update cursor within a search cursor, I think you will get much better performance by using your search cursor to create a dictionary, and then using your update cursor separately to retrieve values from the dictionary.
    – PolyGeo
    2 hours ago










  • desc.FIDSet is checking if I have any features selected. If there is no selection, there is nothing to update.
    – lida
    1 hour ago










  • To answer your original question, I think the reason it only runs once is that cursors will limit themselves to selected records. In the first row of the searchcursor, you select that same row. Since that is now the only record to loop over, the search cursor thinks it is finished.
    – Marc Pfister
    13 mins ago


















  • Do you get any error messages? What is if not desc.FIDSet: trying to do? I notice you are using a mix of fl2 (variable) and "fl2" (literal) this might cause problems depending on the value of the fl2 variable.
    – Michael Stimson
    2 hours ago






  • 2




    Rather than run an update cursor within a search cursor, I think you will get much better performance by using your search cursor to create a dictionary, and then using your update cursor separately to retrieve values from the dictionary.
    – PolyGeo
    2 hours ago










  • desc.FIDSet is checking if I have any features selected. If there is no selection, there is nothing to update.
    – lida
    1 hour ago










  • To answer your original question, I think the reason it only runs once is that cursors will limit themselves to selected records. In the first row of the searchcursor, you select that same row. Since that is now the only record to loop over, the search cursor thinks it is finished.
    – Marc Pfister
    13 mins ago
















Do you get any error messages? What is if not desc.FIDSet: trying to do? I notice you are using a mix of fl2 (variable) and "fl2" (literal) this might cause problems depending on the value of the fl2 variable.
– Michael Stimson
2 hours ago




Do you get any error messages? What is if not desc.FIDSet: trying to do? I notice you are using a mix of fl2 (variable) and "fl2" (literal) this might cause problems depending on the value of the fl2 variable.
– Michael Stimson
2 hours ago




2




2




Rather than run an update cursor within a search cursor, I think you will get much better performance by using your search cursor to create a dictionary, and then using your update cursor separately to retrieve values from the dictionary.
– PolyGeo
2 hours ago




Rather than run an update cursor within a search cursor, I think you will get much better performance by using your search cursor to create a dictionary, and then using your update cursor separately to retrieve values from the dictionary.
– PolyGeo
2 hours ago












desc.FIDSet is checking if I have any features selected. If there is no selection, there is nothing to update.
– lida
1 hour ago




desc.FIDSet is checking if I have any features selected. If there is no selection, there is nothing to update.
– lida
1 hour ago












To answer your original question, I think the reason it only runs once is that cursors will limit themselves to selected records. In the first row of the searchcursor, you select that same row. Since that is now the only record to loop over, the search cursor thinks it is finished.
– Marc Pfister
13 mins ago




To answer your original question, I think the reason it only runs once is that cursors will limit themselves to selected records. In the first row of the searchcursor, you select that same row. Since that is now the only record to loop over, the search cursor thinks it is finished.
– Marc Pfister
13 mins ago










1 Answer
1






active

oldest

votes

















up vote
3
down vote













You don't need to use selections in your search cursor, you can grab the geometry of each feature in FC1:



with arcpy.da.SearchCursor(fc1, ["SHAPE@", "MY_DEBUG_FIELD", "SOME_OTHER_FIELD") as search_cur:
for search_row in search_cur:
# SHAPE@ is a magic token that gets the geometry of the feature
geom = search_row[0]
arcpy.SelectLayerByLocation_management(fl2, "ARE_IDENTICAL_TO", geom, "", "NEW_SELECTION")
# do what you need to do with the selected feature
# as suggested above, you could populate a dictionary with changes
# and then use an UpdateCursor to apply those changes





share|improve this answer





















  • that works, except that the loop never ends. what is the best way to stop it going once done?
    – lida
    1 hour ago










  • The loop should end when it is finished with all the rows. You may want to add a print statement to track what it's working on - perhaps a count of rows that have been searched. Then you can see if it's running, or if it's somehow stuck.
    – Marc Pfister
    28 mins ago











Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "79"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f305004%2farcpy-da-searchcursor-need-to-go-through-all-records-but-only-go-through-one%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
3
down vote













You don't need to use selections in your search cursor, you can grab the geometry of each feature in FC1:



with arcpy.da.SearchCursor(fc1, ["SHAPE@", "MY_DEBUG_FIELD", "SOME_OTHER_FIELD") as search_cur:
for search_row in search_cur:
# SHAPE@ is a magic token that gets the geometry of the feature
geom = search_row[0]
arcpy.SelectLayerByLocation_management(fl2, "ARE_IDENTICAL_TO", geom, "", "NEW_SELECTION")
# do what you need to do with the selected feature
# as suggested above, you could populate a dictionary with changes
# and then use an UpdateCursor to apply those changes





share|improve this answer





















  • that works, except that the loop never ends. what is the best way to stop it going once done?
    – lida
    1 hour ago










  • The loop should end when it is finished with all the rows. You may want to add a print statement to track what it's working on - perhaps a count of rows that have been searched. Then you can see if it's running, or if it's somehow stuck.
    – Marc Pfister
    28 mins ago















up vote
3
down vote













You don't need to use selections in your search cursor, you can grab the geometry of each feature in FC1:



with arcpy.da.SearchCursor(fc1, ["SHAPE@", "MY_DEBUG_FIELD", "SOME_OTHER_FIELD") as search_cur:
for search_row in search_cur:
# SHAPE@ is a magic token that gets the geometry of the feature
geom = search_row[0]
arcpy.SelectLayerByLocation_management(fl2, "ARE_IDENTICAL_TO", geom, "", "NEW_SELECTION")
# do what you need to do with the selected feature
# as suggested above, you could populate a dictionary with changes
# and then use an UpdateCursor to apply those changes





share|improve this answer





















  • that works, except that the loop never ends. what is the best way to stop it going once done?
    – lida
    1 hour ago










  • The loop should end when it is finished with all the rows. You may want to add a print statement to track what it's working on - perhaps a count of rows that have been searched. Then you can see if it's running, or if it's somehow stuck.
    – Marc Pfister
    28 mins ago













up vote
3
down vote










up vote
3
down vote









You don't need to use selections in your search cursor, you can grab the geometry of each feature in FC1:



with arcpy.da.SearchCursor(fc1, ["SHAPE@", "MY_DEBUG_FIELD", "SOME_OTHER_FIELD") as search_cur:
for search_row in search_cur:
# SHAPE@ is a magic token that gets the geometry of the feature
geom = search_row[0]
arcpy.SelectLayerByLocation_management(fl2, "ARE_IDENTICAL_TO", geom, "", "NEW_SELECTION")
# do what you need to do with the selected feature
# as suggested above, you could populate a dictionary with changes
# and then use an UpdateCursor to apply those changes





share|improve this answer












You don't need to use selections in your search cursor, you can grab the geometry of each feature in FC1:



with arcpy.da.SearchCursor(fc1, ["SHAPE@", "MY_DEBUG_FIELD", "SOME_OTHER_FIELD") as search_cur:
for search_row in search_cur:
# SHAPE@ is a magic token that gets the geometry of the feature
geom = search_row[0]
arcpy.SelectLayerByLocation_management(fl2, "ARE_IDENTICAL_TO", geom, "", "NEW_SELECTION")
# do what you need to do with the selected feature
# as suggested above, you could populate a dictionary with changes
# and then use an UpdateCursor to apply those changes






share|improve this answer












share|improve this answer



share|improve this answer










answered 2 hours ago









Marc Pfister

2,57778




2,57778












  • that works, except that the loop never ends. what is the best way to stop it going once done?
    – lida
    1 hour ago










  • The loop should end when it is finished with all the rows. You may want to add a print statement to track what it's working on - perhaps a count of rows that have been searched. Then you can see if it's running, or if it's somehow stuck.
    – Marc Pfister
    28 mins ago


















  • that works, except that the loop never ends. what is the best way to stop it going once done?
    – lida
    1 hour ago










  • The loop should end when it is finished with all the rows. You may want to add a print statement to track what it's working on - perhaps a count of rows that have been searched. Then you can see if it's running, or if it's somehow stuck.
    – Marc Pfister
    28 mins ago
















that works, except that the loop never ends. what is the best way to stop it going once done?
– lida
1 hour ago




that works, except that the loop never ends. what is the best way to stop it going once done?
– lida
1 hour ago












The loop should end when it is finished with all the rows. You may want to add a print statement to track what it's working on - perhaps a count of rows that have been searched. Then you can see if it's running, or if it's somehow stuck.
– Marc Pfister
28 mins ago




The loop should end when it is finished with all the rows. You may want to add a print statement to track what it's working on - perhaps a count of rows that have been searched. Then you can see if it's running, or if it's somehow stuck.
– Marc Pfister
28 mins ago


















draft saved

draft discarded




















































Thanks for contributing an answer to Geographic Information Systems Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f305004%2farcpy-da-searchcursor-need-to-go-through-all-records-but-only-go-through-one%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Understanding the information contained in the Deep Space Network XML data?

Ross-on-Wye

Eastern Orthodox Church