Multiple selections with a python loop The 2019 Stack Overflow Developer Survey Results Are InMaking multiple separate selections from attributesArcPy nested loop problemLoop with arcpy.ListFilesCreate a fishnet in a 'for' loop with ArcpyLoop through columns Python arcpy.ListFieldsSpatial join for multiple shapefiles (loop)Checking whether file already exists using while loop with ArcPy?Having trouble replaceDataSource loop in PythonBuffer loop with Python QGISMerge and save results after selections using loop
Is it ok to offer lower paid work as a trial period before negotiating for a full-time job?
Worn-tile Scrabble
Output the Arecibo Message
Inverse Relationship Between Precision and Recall
Falsification in Math vs Science
Can a rogue use sneak attack with weapons that have the thrown property even if they are not thrown?
What information about me do stores get via my credit card?
Why was M87 targeted for the Event Horizon Telescope instead of Sagittarius A*?
Why can't devices on different VLANs, but on the same subnet, communicate?
How to obtain a position of last non-zero element
Ubuntu Server install with full GUI
What to do when moving next to a bird sanctuary with a loosely-domesticated cat?
Why does the nucleus not repel itself?
Keeping a retro style to sci-fi spaceships?
Old scifi movie from the 50s or 60s with men in solid red uniforms who interrogate a spy from the past
Using xargs with pdftk
The phrase "to the numbers born"?
Is it okay to consider publishing in my first year of PhD?
What could be the right powersource for 15 seconds lifespan disposable giant chainsaw?
Balance problems for leveling up mid-fight?
Is bread bad for ducks?
Why is ParallelDo slower than Do?
Landlord wants to switch my lease to a "Land contract" to "get back at the city"
writing variables above the numbers in tikz picture
Multiple selections with a python loop
The 2019 Stack Overflow Developer Survey Results Are InMaking multiple separate selections from attributesArcPy nested loop problemLoop with arcpy.ListFilesCreate a fishnet in a 'for' loop with ArcpyLoop through columns Python arcpy.ListFieldsSpatial join for multiple shapefiles (loop)Checking whether file already exists using while loop with ArcPy?Having trouble replaceDataSource loop in PythonBuffer loop with Python QGISMerge and save results after selections using loop
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I am very new to Python and have no idea about how to write a python loop to do selections for ArcGIS. I already finished writing selection codes with different conditions. Is there anyone helping me revise my codes with a python loop?
import arcpy
arcpy.env.workspace = "D:/STAR/STARproject.gdb"
arcpy.MakeFeatureLayer_management('wqi_Conshp_clip27171','wqi_Conshp_clip27171')
#Select level 1 gridcode<=14.3
selection1 = '"gridcode"<=14.3'
arcpy.SelectLayerByAttribute_management('wqi_Conshp_clip27171','NEW_SELECTION',selection1)
arcpy.Statistics_analysis('wqi_Conshp_clip27171','stats27171wqi_Conlevel1',[["Shape_Area","sum"]])
#summarize the selected features
arcpy.AddField_management('stats27171wqi_Conlevel1','rate','DOUBLE')
arcpy.CalculateField_management('stats27171wqi_Conlevel1','rate','!SUM_Shape_Area!/'.format(Sum_Area27171),'PYTHON_9.3')
#Sum_Area27171 is a variable and already defined in the workspace
#select level 2 14.3<gridcode<=25.6
selection2 = '"gridcode">14.3 AND "gridcode"<=28.6'
arcpy.SelectLayerByAttribute_management('wqi_Conshp_clip27171','NEW_SELECTION',selection2)
arcpy.Statistics_analysis('wqi_Conshp_clip27171','stats27171wqi_Conlevel2',[["Shape_Area","sum"]])
arcpy.AddField_management('stats27171wqi_Conlevel2','rate','DOUBLE')
arcpy.CalculateField_management('stats27171wqi_Conlevel2','rate','!SUM_Shape_Area!/'.format(Sum_Area27171),'PYTHON_9.3')
#select level 3 28.6<gridcode<=42.9
selection3 = '"gridcode">28.6 AND "gridcode"<=42.9'
arcpy.SelectLayerByAttribute_management('wqi_Conshp_clip27171','NEW_SELECTION',selection3)
arcpy.Statistics_analysis('wqi_Conshp_clip27171','stats27171wqi_Conlevel3',[["Shape_Area","sum"]])
arcpy.AddField_management('stats27171wqi_Conlevel3','rate','DOUBLE')
arcpy.CalculateField_management('stats27171wqi_Conlevel3','rate','!SUM_Shape_Area!/'.format(Sum_Area27171),'PYTHON_9.3')
#select level 4 42.9<gridcode<=57.2
selection4 = '"gridcode">42.9 AND "gridcode"<=57.2'
arcpy.SelectLayerByAttribute_management('wqi_Conshp_clip27171','NEW_SELECTION',selection4)
arcpy.Statistics_analysis('wqi_Conshp_clip27171','stats27171wqi_Conlevel4',[["Shape_Area","sum"]])
arcpy.AddField_management('stats27171wqi_Conlevel4','rate','DOUBLE')
arcpy.CalculateField_management('stats27171wqi_Conlevel4','rate','!SUM_Shape_Area!/'.format(Sum_Area27171),'PYTHON_9.3')
#merge these results
arcpy.Merge_management(['stats27171wqi_Conlevel1','stats27171wqi_Conlevel2','stats27171wqi_Conlevel3',
'stats27171wqi_Conlevel4'],'stats27171wqi_ConMerge')
As you can see, there are four conditions above. Actually, I have another three similar conditions. I am wondering how to combine these selections together with a loop in python to make the work as simple as possible.
arcpy arcgis-10.0 loop
add a comment |
I am very new to Python and have no idea about how to write a python loop to do selections for ArcGIS. I already finished writing selection codes with different conditions. Is there anyone helping me revise my codes with a python loop?
import arcpy
arcpy.env.workspace = "D:/STAR/STARproject.gdb"
arcpy.MakeFeatureLayer_management('wqi_Conshp_clip27171','wqi_Conshp_clip27171')
#Select level 1 gridcode<=14.3
selection1 = '"gridcode"<=14.3'
arcpy.SelectLayerByAttribute_management('wqi_Conshp_clip27171','NEW_SELECTION',selection1)
arcpy.Statistics_analysis('wqi_Conshp_clip27171','stats27171wqi_Conlevel1',[["Shape_Area","sum"]])
#summarize the selected features
arcpy.AddField_management('stats27171wqi_Conlevel1','rate','DOUBLE')
arcpy.CalculateField_management('stats27171wqi_Conlevel1','rate','!SUM_Shape_Area!/'.format(Sum_Area27171),'PYTHON_9.3')
#Sum_Area27171 is a variable and already defined in the workspace
#select level 2 14.3<gridcode<=25.6
selection2 = '"gridcode">14.3 AND "gridcode"<=28.6'
arcpy.SelectLayerByAttribute_management('wqi_Conshp_clip27171','NEW_SELECTION',selection2)
arcpy.Statistics_analysis('wqi_Conshp_clip27171','stats27171wqi_Conlevel2',[["Shape_Area","sum"]])
arcpy.AddField_management('stats27171wqi_Conlevel2','rate','DOUBLE')
arcpy.CalculateField_management('stats27171wqi_Conlevel2','rate','!SUM_Shape_Area!/'.format(Sum_Area27171),'PYTHON_9.3')
#select level 3 28.6<gridcode<=42.9
selection3 = '"gridcode">28.6 AND "gridcode"<=42.9'
arcpy.SelectLayerByAttribute_management('wqi_Conshp_clip27171','NEW_SELECTION',selection3)
arcpy.Statistics_analysis('wqi_Conshp_clip27171','stats27171wqi_Conlevel3',[["Shape_Area","sum"]])
arcpy.AddField_management('stats27171wqi_Conlevel3','rate','DOUBLE')
arcpy.CalculateField_management('stats27171wqi_Conlevel3','rate','!SUM_Shape_Area!/'.format(Sum_Area27171),'PYTHON_9.3')
#select level 4 42.9<gridcode<=57.2
selection4 = '"gridcode">42.9 AND "gridcode"<=57.2'
arcpy.SelectLayerByAttribute_management('wqi_Conshp_clip27171','NEW_SELECTION',selection4)
arcpy.Statistics_analysis('wqi_Conshp_clip27171','stats27171wqi_Conlevel4',[["Shape_Area","sum"]])
arcpy.AddField_management('stats27171wqi_Conlevel4','rate','DOUBLE')
arcpy.CalculateField_management('stats27171wqi_Conlevel4','rate','!SUM_Shape_Area!/'.format(Sum_Area27171),'PYTHON_9.3')
#merge these results
arcpy.Merge_management(['stats27171wqi_Conlevel1','stats27171wqi_Conlevel2','stats27171wqi_Conlevel3',
'stats27171wqi_Conlevel4'],'stats27171wqi_ConMerge')
As you can see, there are four conditions above. Actually, I have another three similar conditions. I am wondering how to combine these selections together with a loop in python to make the work as simple as possible.
arcpy arcgis-10.0 loop
It looks like for each condition you are performing a selection, adding fields, and calculating fields. Because of this I'd create a function in which you pass the query and fieldname, and then loop through calls to this function.
– smiller
Apr 5 at 20:49
@smiller, thanks for your help. Writing a function is a kind of complex for me. Do you know where I can find similar examples for looping or functions? Appreciate it!
– Zhenyu
Apr 5 at 20:53
add a comment |
I am very new to Python and have no idea about how to write a python loop to do selections for ArcGIS. I already finished writing selection codes with different conditions. Is there anyone helping me revise my codes with a python loop?
import arcpy
arcpy.env.workspace = "D:/STAR/STARproject.gdb"
arcpy.MakeFeatureLayer_management('wqi_Conshp_clip27171','wqi_Conshp_clip27171')
#Select level 1 gridcode<=14.3
selection1 = '"gridcode"<=14.3'
arcpy.SelectLayerByAttribute_management('wqi_Conshp_clip27171','NEW_SELECTION',selection1)
arcpy.Statistics_analysis('wqi_Conshp_clip27171','stats27171wqi_Conlevel1',[["Shape_Area","sum"]])
#summarize the selected features
arcpy.AddField_management('stats27171wqi_Conlevel1','rate','DOUBLE')
arcpy.CalculateField_management('stats27171wqi_Conlevel1','rate','!SUM_Shape_Area!/'.format(Sum_Area27171),'PYTHON_9.3')
#Sum_Area27171 is a variable and already defined in the workspace
#select level 2 14.3<gridcode<=25.6
selection2 = '"gridcode">14.3 AND "gridcode"<=28.6'
arcpy.SelectLayerByAttribute_management('wqi_Conshp_clip27171','NEW_SELECTION',selection2)
arcpy.Statistics_analysis('wqi_Conshp_clip27171','stats27171wqi_Conlevel2',[["Shape_Area","sum"]])
arcpy.AddField_management('stats27171wqi_Conlevel2','rate','DOUBLE')
arcpy.CalculateField_management('stats27171wqi_Conlevel2','rate','!SUM_Shape_Area!/'.format(Sum_Area27171),'PYTHON_9.3')
#select level 3 28.6<gridcode<=42.9
selection3 = '"gridcode">28.6 AND "gridcode"<=42.9'
arcpy.SelectLayerByAttribute_management('wqi_Conshp_clip27171','NEW_SELECTION',selection3)
arcpy.Statistics_analysis('wqi_Conshp_clip27171','stats27171wqi_Conlevel3',[["Shape_Area","sum"]])
arcpy.AddField_management('stats27171wqi_Conlevel3','rate','DOUBLE')
arcpy.CalculateField_management('stats27171wqi_Conlevel3','rate','!SUM_Shape_Area!/'.format(Sum_Area27171),'PYTHON_9.3')
#select level 4 42.9<gridcode<=57.2
selection4 = '"gridcode">42.9 AND "gridcode"<=57.2'
arcpy.SelectLayerByAttribute_management('wqi_Conshp_clip27171','NEW_SELECTION',selection4)
arcpy.Statistics_analysis('wqi_Conshp_clip27171','stats27171wqi_Conlevel4',[["Shape_Area","sum"]])
arcpy.AddField_management('stats27171wqi_Conlevel4','rate','DOUBLE')
arcpy.CalculateField_management('stats27171wqi_Conlevel4','rate','!SUM_Shape_Area!/'.format(Sum_Area27171),'PYTHON_9.3')
#merge these results
arcpy.Merge_management(['stats27171wqi_Conlevel1','stats27171wqi_Conlevel2','stats27171wqi_Conlevel3',
'stats27171wqi_Conlevel4'],'stats27171wqi_ConMerge')
As you can see, there are four conditions above. Actually, I have another three similar conditions. I am wondering how to combine these selections together with a loop in python to make the work as simple as possible.
arcpy arcgis-10.0 loop
I am very new to Python and have no idea about how to write a python loop to do selections for ArcGIS. I already finished writing selection codes with different conditions. Is there anyone helping me revise my codes with a python loop?
import arcpy
arcpy.env.workspace = "D:/STAR/STARproject.gdb"
arcpy.MakeFeatureLayer_management('wqi_Conshp_clip27171','wqi_Conshp_clip27171')
#Select level 1 gridcode<=14.3
selection1 = '"gridcode"<=14.3'
arcpy.SelectLayerByAttribute_management('wqi_Conshp_clip27171','NEW_SELECTION',selection1)
arcpy.Statistics_analysis('wqi_Conshp_clip27171','stats27171wqi_Conlevel1',[["Shape_Area","sum"]])
#summarize the selected features
arcpy.AddField_management('stats27171wqi_Conlevel1','rate','DOUBLE')
arcpy.CalculateField_management('stats27171wqi_Conlevel1','rate','!SUM_Shape_Area!/'.format(Sum_Area27171),'PYTHON_9.3')
#Sum_Area27171 is a variable and already defined in the workspace
#select level 2 14.3<gridcode<=25.6
selection2 = '"gridcode">14.3 AND "gridcode"<=28.6'
arcpy.SelectLayerByAttribute_management('wqi_Conshp_clip27171','NEW_SELECTION',selection2)
arcpy.Statistics_analysis('wqi_Conshp_clip27171','stats27171wqi_Conlevel2',[["Shape_Area","sum"]])
arcpy.AddField_management('stats27171wqi_Conlevel2','rate','DOUBLE')
arcpy.CalculateField_management('stats27171wqi_Conlevel2','rate','!SUM_Shape_Area!/'.format(Sum_Area27171),'PYTHON_9.3')
#select level 3 28.6<gridcode<=42.9
selection3 = '"gridcode">28.6 AND "gridcode"<=42.9'
arcpy.SelectLayerByAttribute_management('wqi_Conshp_clip27171','NEW_SELECTION',selection3)
arcpy.Statistics_analysis('wqi_Conshp_clip27171','stats27171wqi_Conlevel3',[["Shape_Area","sum"]])
arcpy.AddField_management('stats27171wqi_Conlevel3','rate','DOUBLE')
arcpy.CalculateField_management('stats27171wqi_Conlevel3','rate','!SUM_Shape_Area!/'.format(Sum_Area27171),'PYTHON_9.3')
#select level 4 42.9<gridcode<=57.2
selection4 = '"gridcode">42.9 AND "gridcode"<=57.2'
arcpy.SelectLayerByAttribute_management('wqi_Conshp_clip27171','NEW_SELECTION',selection4)
arcpy.Statistics_analysis('wqi_Conshp_clip27171','stats27171wqi_Conlevel4',[["Shape_Area","sum"]])
arcpy.AddField_management('stats27171wqi_Conlevel4','rate','DOUBLE')
arcpy.CalculateField_management('stats27171wqi_Conlevel4','rate','!SUM_Shape_Area!/'.format(Sum_Area27171),'PYTHON_9.3')
#merge these results
arcpy.Merge_management(['stats27171wqi_Conlevel1','stats27171wqi_Conlevel2','stats27171wqi_Conlevel3',
'stats27171wqi_Conlevel4'],'stats27171wqi_ConMerge')
As you can see, there are four conditions above. Actually, I have another three similar conditions. I am wondering how to combine these selections together with a loop in python to make the work as simple as possible.
arcpy arcgis-10.0 loop
arcpy arcgis-10.0 loop
edited Apr 5 at 23:20
PolyGeo♦
53.9k1781246
53.9k1781246
asked Apr 5 at 20:44
ZhenyuZhenyu
113
113
It looks like for each condition you are performing a selection, adding fields, and calculating fields. Because of this I'd create a function in which you pass the query and fieldname, and then loop through calls to this function.
– smiller
Apr 5 at 20:49
@smiller, thanks for your help. Writing a function is a kind of complex for me. Do you know where I can find similar examples for looping or functions? Appreciate it!
– Zhenyu
Apr 5 at 20:53
add a comment |
It looks like for each condition you are performing a selection, adding fields, and calculating fields. Because of this I'd create a function in which you pass the query and fieldname, and then loop through calls to this function.
– smiller
Apr 5 at 20:49
@smiller, thanks for your help. Writing a function is a kind of complex for me. Do you know where I can find similar examples for looping or functions? Appreciate it!
– Zhenyu
Apr 5 at 20:53
It looks like for each condition you are performing a selection, adding fields, and calculating fields. Because of this I'd create a function in which you pass the query and fieldname, and then loop through calls to this function.
– smiller
Apr 5 at 20:49
It looks like for each condition you are performing a selection, adding fields, and calculating fields. Because of this I'd create a function in which you pass the query and fieldname, and then loop through calls to this function.
– smiller
Apr 5 at 20:49
@smiller, thanks for your help. Writing a function is a kind of complex for me. Do you know where I can find similar examples for looping or functions? Appreciate it!
– Zhenyu
Apr 5 at 20:53
@smiller, thanks for your help. Writing a function is a kind of complex for me. Do you know where I can find similar examples for looping or functions? Appreciate it!
– Zhenyu
Apr 5 at 20:53
add a comment |
0
active
oldest
votes
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
);
);
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%2f317945%2fmultiple-selections-with-a-python-loop%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
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%2f317945%2fmultiple-selections-with-a-python-loop%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
It looks like for each condition you are performing a selection, adding fields, and calculating fields. Because of this I'd create a function in which you pass the query and fieldname, and then loop through calls to this function.
– smiller
Apr 5 at 20:49
@smiller, thanks for your help. Writing a function is a kind of complex for me. Do you know where I can find similar examples for looping or functions? Appreciate it!
– Zhenyu
Apr 5 at 20:53