Arcpy Getting Started (Python in ArcGIS)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello this is a this is going to be a quick video I'm going to try to keep it under 10 minutes on the title is getting started with Python within ArcGIS or getting started with the arc PI module first part I'm going to show you just just kind of you know how do you get started learning the you know the Python syntax using the Python window in arcmap and then I'll show you how to save that out to a Python file so you can run a script outside of work map all right so let's get started the you know the first first thing you do when you when you start learning Python is you have your have arcmap open and you want to add the Python window you can find that up here right here alright I'm in the clear all this alright so this is what it looks like by default so you have a cursor here your Python window but you're kind of like alright I want to do like a select by attribute I want to do something with this what can I do alright so you think like alright sequel I can do a select select and I don't get anything so select all from table name and nothing this is not sequel it's it's Python so it's a completely different syntax alright so how to learn it I think the best way is to use your toolbox and let's go over here to the toolbox and I will locate the select by attribute tool select layer by attribute and it's very simple you just drag-and-drop all right so that kind of gets you started all right here's the tool you see it has the arc PI here then dot the tool name and then open parentheses and then by default it's going to read your tio your table of contents here's your layer so you can select that and then you can see over here on the right it's kind of giving you a prompt comma alright this looks familiar yeah I want a new selection let's say I want to select all the counties within one state so I'm a new selection and after that comma alright now I got to enter in my where clause and this is where you really you really start getting lost here you can think about sequel but you know like alright sequels not going to work so what do you do all right let's just remove all this and let's open up the tool select layer by attribute and I'm going to choose layer new selection and then I'm going to enter in my expression field name equals so I'm going to choose in North Carolina numbers the state FP number is 37 hit OK run that alright and it performed the selection by attribute all right so what next how do you do that in Python all right so up here you want to go to your geoprocessing menu open up the results expand your current session and right here you have this this is the this was the last it's your processing tool that was run I'll have to do is drag this into your into the Python window and there you go you got it so a selection clear selected feature let me close this and here's the syntax for doing exactly what we did here but within Python so how do you run this all you have to do is just hit enter and that work so let's let's let's copy and paste that and make some changes so I'm going to copy copy paste now let's say I want to choose this a different state so I know New York is 36 so make that change there this is your where clause syntax alright hit enter and these are all the New York State counties all right so let's say we want to change the field field name let's look at I know I did this earlier you have quite a few Adams counties in the United States so I know the when we just I'll just identify one of these all right so here field name is name I'm going to choose Adams and so I'm going to paste a new line and I'm going to change this to name the field name is name and then the county name I'm going to query his Adams hit enter and you can see they completed and I have about a dozen there's about a dozen Adams counties in the US just to confirm that identify and here you go Adams County alright let's go back selection clear all right so that's great let's say we want to do a select instead of a select by attribute let's do a select and we're going to take that select selection set of counties and save that to a feature class alright first of all let's right click in our Python window and clear all let's go back into our do processing results now let's clear all these delete yep close that and the tool I'm going to use is under analysis extract select so once again I can bring that in here and I can choose the layer one point form the select on and then this is where I get stuck I got to choose I got I got to name the output feature class and let's say if I put in test OOP I'll put in test comma where clause once again I don't have that memorized I'm stuck so what I do I'm just going to clear this open up the select tool choose the layer I'm just going to name this one a temporary name so I'm going to call it in C X and then put in my expression state at P equals unique values 37 okay okay okay just ran that and you can see these are the this is the result north colonic North Carolina counties are red and the data source that was written to my profile the default geodatabase and the feature class name is right here in C in C X okay so what about if I want to do that from Python remember what do you do you go to geoprocessing results expand this drag and drop now let's change it to New York 36 and then I'm going to change this to in Y hit enter yeah there you go so you can imagine if if you have to do this let's say you have to break out all of your counties by state you know using this one over all US counties layer you know that would take quite a bit of time to do it manually and so you can really see the power of Python allows you to automate this so so where do we go from now we you know we we ran this from in with in arcmap but let's say we want to create this we want to save this to a Python file and run this tool outside of arcmap okay so we just ran this this Python line here successfully so you can just right click and save as and I'm going to call this cell NC County's dot py pay attention to the file extension yes all right and I'm going to minimize this it's on my desktop and I'm going to open it with my with the idle and all right for someone who's just getting started you're like once again you're kind of like all right I got one line of code here I can't imagine that's going to work but I'll try it so I'm going to run module and I get an error right here named mark pi is not defined well that's because you have to import the arc PI module you can do that up at the top from with an arcmap you don't have to do it it's already it's already it's there it's ready to go so import arc PI file save I'm gonna run this again that's too easy you know it's just not that's not going to work there's going to be another issue this will probably run a lot quicker on a faster machine this is a really inexpensive Dell Inspiron 15 3000 series laptop with an AMD a6 processor I I just got it so I can play around with arcmap all right so that that completed and the error is saying that it cannot find the input feature dataset CB 2015 u.s. counties does not exist well that's because in arcmap this works fine within the Python window because it by default it knows to look at the table of contents so it's looking at your layers and your table of contents so how do you fix that well you just have to put in the path to the the file location so let's just go we'll open up that location chi and copy it see and you will have to add the extension if you're working with shapefile so dot is HP so here's the path to the shape the overall County shapefile and then this is the output now I wanted to select North Carolina so I'm going to change this to NC and then my state FP code is going to be 37 and let me go back here alright that's it file save run on module now when you run this this is a very simple Python script and so it's not printing anything to this shell so you could print the output the results you could do some add some debugging error handling that kind of stuff so to see if it worked we'll go back into arcmap let me turn off these layers and I'm going to add there it is right there CB 2015 us County 500k underscore NC that's the feature class that was created make it make it blue all right there it is so you can imagine what you can do with this let's say you just copy this line here copy it create a new line and we'll name this New York 30 36 and so you imagine you know you could put in 50 lines here and quickly run through and pull out all the counties by state and have those added to this location right here you'd have a feature class for each state so let's test this out file save and I'm gonna I don't know if it's gonna there's going to be a problem overriding it so I'm just going to call this NCAA in Y file save run and let's see how quickly that works okay there we are alright so yeah I got the NCAA and in New York I'd both of those all right well that's it it really is just what I wanted to put together really just an introduction to Python using Python with an arc arc map and just getting familiar with the syntax and you know just kind of how I get started with it I hope to do some more of these videos if you if you if you go on YouTube and you look for any kind of Python videos regarding ArcGIS our arc PI this is different formats usually like like a conference somebody is giving a presentation and they're quite long it's about an hour so I like this format like to try it try and keep it just really really short under under 10 to 12 minutes or so and yeah I hope you enjoyed it I hope this is helpful to someone if you have any questions please feel free to make make a comment below thank you
Info
Channel: Jido Herox
Views: 36,137
Rating: 4.8270268 out of 5
Keywords: arcgis, python, arcpy, gis
Id: MdFB4psq42A
Channel Id: undefined
Length: 18min 44sec (1124 seconds)
Published: Fri Aug 12 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.