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












1














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
    Dec 5 '18 at 1:17






  • 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
    Dec 5 '18 at 1:22










  • desc.FIDSet is checking if I have any features selected. If there is no selection, there is nothing to update.
    – lida
    Dec 5 '18 at 2:03










  • 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
    Dec 5 '18 at 3:43
















1














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
    Dec 5 '18 at 1:17






  • 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
    Dec 5 '18 at 1:22










  • desc.FIDSet is checking if I have any features selected. If there is no selection, there is nothing to update.
    – lida
    Dec 5 '18 at 2:03










  • 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
    Dec 5 '18 at 3:43














1












1








1







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 Dec 5 '18 at 1:10









lidalida

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
    Dec 5 '18 at 1:17






  • 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
    Dec 5 '18 at 1:22










  • desc.FIDSet is checking if I have any features selected. If there is no selection, there is nothing to update.
    – lida
    Dec 5 '18 at 2:03










  • 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
    Dec 5 '18 at 3:43


















  • 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
    Dec 5 '18 at 1:17






  • 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
    Dec 5 '18 at 1:22










  • desc.FIDSet is checking if I have any features selected. If there is no selection, there is nothing to update.
    – lida
    Dec 5 '18 at 2:03










  • 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
    Dec 5 '18 at 3:43
















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
Dec 5 '18 at 1:17




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
Dec 5 '18 at 1:17




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
Dec 5 '18 at 1:22




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
Dec 5 '18 at 1:22












desc.FIDSet is checking if I have any features selected. If there is no selection, there is nothing to update.
– lida
Dec 5 '18 at 2:03




desc.FIDSet is checking if I have any features selected. If there is no selection, there is nothing to update.
– lida
Dec 5 '18 at 2:03












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
Dec 5 '18 at 3:43




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
Dec 5 '18 at 3:43










1 Answer
1






active

oldest

votes


















4














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
    Dec 5 '18 at 2:50






  • 1




    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
    Dec 5 '18 at 3:28











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',
autoActivateHeartbeat: false,
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









4














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
    Dec 5 '18 at 2:50






  • 1




    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
    Dec 5 '18 at 3:28
















4














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
    Dec 5 '18 at 2:50






  • 1




    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
    Dec 5 '18 at 3:28














4












4








4






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 Dec 5 '18 at 1:33









Marc PfisterMarc Pfister

3,17289




3,17289












  • that works, except that the loop never ends. what is the best way to stop it going once done?
    – lida
    Dec 5 '18 at 2:50






  • 1




    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
    Dec 5 '18 at 3:28


















  • that works, except that the loop never ends. what is the best way to stop it going once done?
    – lida
    Dec 5 '18 at 2:50






  • 1




    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
    Dec 5 '18 at 3:28
















that works, except that the loop never ends. what is the best way to stop it going once done?
– lida
Dec 5 '18 at 2:50




that works, except that the loop never ends. what is the best way to stop it going once done?
– lida
Dec 5 '18 at 2:50




1




1




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
Dec 5 '18 at 3:28




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
Dec 5 '18 at 3:28


















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

Berounka

Sphinx de Gizeh

Different font size/position of beamer's navigation symbols template's content depending on regular/plain...