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]
arcpy
add a comment |
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]
arcpy
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
add a comment |
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]
arcpy
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
arcpy
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
add a comment |
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
add a comment |
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
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
add a comment |
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
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
add a comment |
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
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
add a comment |
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
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
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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