Introduction to Python in ArcGIS Pro

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello welcome to this key ideas video on python in gis in this video we will be introducing you to python from small single lines of code to be able to run batch processing jobs you don't need to know python to use arcgis pro and to produce um really worthwhile uh outputs however python can be a really useful skill um just to to improve your um your workflow maybe you're wasting time on repetitive jobs maybe you yearn for more customization whatever it be knowing a little bit of python can really help you with your efficiency so we'll start with looking um in the arcgis pro and we're going to run a demo on writing a label expression just helping you customize a an attribute table command for example so i've got a project in arcgis pro using the same data set we looked at for counties and i've got national parks around the uk if we click on one of them and i'll show you sort of some of the um attribute uh information okay so there you go this is the cairngolds national park you can see it's all in capitals uh that's what the uh attribute table looks like so it's got a uh name field is it just going to draw this yeah it's got the name field and you can see it's got all the uh national parks and they're in capital letters so let's say i wanted to label my national parks on the map i could do that by label button and this will draw the labels on the map like so but i might not be so happy with the way it appears i might not want to stand out so much and be capitalized so i can go into the um labeling properties i mean one option is to go into the attribute table just to rename that oh my goodness that would be exhausting you rename them all and then actually you change your mind and then you have to do it again well here we go we can change the language here to python we can pull in the name field and we can start to use python syntax like this hyphen dot title we can press that to verify expression is valid and if we press apply it will apply a python syntax command to the labeling of the data set and rather than having uh capitalized letters now it's it's um it's it's labeled them in a in a a way that has the um first word as a capital dartmoor national park like that and so knowing these little tricks can really help you um having to change the labels in an attribute table without having to go through it so i'm just showing you another example where we can change all of the text to lower case so that should change there we go so now it's all uh there's another one for upper but that's how it appears in the attribute table um anyway so probably for this one title is what we need so that's a bit of a time saver okay we've got another example now this time looking at english town centers you can see i've got the expression for just the name being shown on the map which creates an awful lot of clutter so now we can write a python expression where we just select which names to uh to draw so we'll look at the actual table and see how we might uh do that we know that we want the name field because that contains the name we don't have to mess around actually with uppercase lowercase and they're already sort of in a nice title format but but we can see we've got other attributes here we've got shape area and shape length so we've got some way of knowing the size of these town centers based on the shape area that would be useful attribute maybe we just want to enable the big ones so let's do that so in the expression box here i'm actually going to write a python function a function is a block of code which runs when it's called so i'm defining first um or you know using the def to to create this function so def define find label name and shape area those are the two attributes that i want to call and then i'm running an if statement an if statement uses the um or supports the usual logical conditions from mathematics so in this case if shape area is bigger or equal to 15 150 000 then we'll get on to that in a minute but notice first of all that we're saying float so i need to tell the tell the code that uh shape area is a number not a text before we're looking at text now this is a number so if that number is bigger or equal to 150 000 then we're going to return something we're going to say right we're going to color it black which is a bit of code that 255 says pure black and then the font size is 10 and plus so we're concatenating this using plus we're going to give it a name of upper so actually we're going to change that lowercase text or title text to uppercase and and then um we'll call in the font and color no if i press analyze here the expression is valid um so i'm going to go ahead and run that this will update there we go so now we've labeled our map with only the significant um english town centers or so significant being bigger than 150 000 whatever that shapefile is notice that python requires this indentation if i went like this and then press analyze or the expression is the word if i went like this and pressed analyze oh it's not because there's an indentation error indentation is is forced on us when we're using python it forces us to be neat and tidy with our code so we have these indentations which shows us when certain things are taking place it's a bit difficult to visualize in this window which is a bit compressed thus a bit better so we're saying if the float shape is this then return this and then we've seen that what happens there beauty about running an if statement is we can also specify um what to do if that condition isn't met so i'm going to copy and paste a little bit of code i prepared earlier this is the beauty again of python you hardly have to write code if you've got something that exists you can you can steal that and reuse it so if uh to find the function uh name and shape area so if the float shape area is a 150 000 or more equal or more then do what we've done over here capitalize it give it a nice black else or otherwise if it's not so if it's smaller than we'll give it a blue and we'll give it a much smaller and we're not going to do a um uh yeah we'll just give it a name title so uppercase and black and then title so that just gives an uppercase at the first letter of the word and then blue so we'll check that that works is valid and we'll press apply okay cool i mean we can do further customization we can get this looking how we want that's just an example but we've done something there in our label without having to change anything in our attribute table we've now got labels for significant towns and um small blue ones for for the smaller size and then we can change that and adapt that to to suit our needs so it's a powerful little um bit of code that we've just written okay so for my next demo i'd like to show you python snippets in our pro so we'll go back to pro this time i've got the same strategy coordinate survey data set i've got a rivers shape file for the county of devon and i'm going to show you how to get a um python snippet from a geoprocessing tool it could be any geoprocessing tool um so first we'll run it here and then we'll get the code so input feature i'm going to run it on the devon river line i want the output to be the same as the input so i'm just going to check the uh source for this i'm going to copy that label demo strategy devon riversland um let's go label demo okay so yeah not where i want so i'm gonna paste that in and then i'm gonna call it um i'm going to give it a name of output just for simplicity when we're looking at the um python code we know that's the output yeah that's the input output oh this would output 100 because i want to give it a um value of 100 meters it's giving me a warning here um already exists i've run this i've practiced this already that's okay we'll overwrite it and we'll press run leaving all of the default settings so it's run a buffer analysis you can see river you can see the output 100 right clicking on the um result window goes i can copy python command wow so i can copy that to any uh any sort of area where i'm writing code i can just paste that in and there is a line of python code but more importantly here is i can send it to the python window now in arc pro you can do you can use your analysis in python you can open in a notebook which is brand new to pro and we'll be doing that in practical or you can open the python window so that's how you can open the python window there i'm going to um i'm going to try docking this um over here so i'm going to go center python window so now i've got my one line of code and here in the python window i can get rid of i'll leave the geoprocessing toolbox there just while i talk it through so this is the um the command analysis buffer it's there's the input then rivers line there's the output so output 100 i'm going to change that to output 200 i'm going to change the buffer to 200 and leave all the other defaults as they are look there they are with defaults and i'm going to press return and it's going to run the code get rid of the geoprocessing window and you can see the beauty of running in the python window in our pro is the output comes into the map so there's the output there output 200. so you can see if i wanted to run that tool a few times just changing the snippet here is super in easy if i press up then i get the command i've just run change up to 300 change that to 300 press return it's doing it again so a nice tool to sort of run the same thing a few times um but what i want to show you next is how to perform a uh for loop so kind of what we just did but on multiple files so for loop is when you um you write a it was for iterating over a sequence so you might have a list of commands like 100 200 300 400 buffers maybe you've got the um rivers in shapefiles for all sorts of different counties and so you can um apply the um buffer to all those different files so for these files or for these buffers apply the code to them it allows you to be really efficient so let's go through an example of that i've written some code here so i'm going to do is copy and paste into the command window and we'll see what it says so first we're setting in environment workspace so c local data label demo strategy i think actually i need to update that because i'm actually in local data week nine label demo now strategy yeah i am see that so i'll copy that and i'll update the environment workspace enabled damage strategy set up variables so a variable is when we are um setting a container to store something interesting some files what numbers whatever it might be this container so whenever we write we say in this case rivers that's our variable so rivers now is a variable here where we're calling in this other variable our workspace and we're concatenating so we're adding something to it we're adding river's line.shape actually we want um devon rivers now so let's update that devon rivers underscore line dot shape buff distances we're gonna go 100 500 1 000 and then we're going to loop through the different buffer distances and create buffers using each button notice that i've got these hashtags in front of the code that just allows us to create a comment so i can press return and add another comment and that makes it easy to remember when we're revisiting code what it does it allows us to put in some text into the code so we can understand what it does if you put a hashtag in front of the line it doesn't it doesn't recognize it as code so just it's there for us so here's the four loop look four now we decided to find this buff in buff discs we've got buff discs here so so basically we're gonna run through this for 100 for 500 per thousand we're going gonna now do something notice again it's um we've got an indent here so everything within this indent will be the four okay so four buff in buff discs print the string buff so it's going to be text recognize this as string is processing so 100 is processing 500 is processing a thousand is purchasing so rivers out equals arc by environment workspace rivers line plus the buffer shape so this is good the output but we're using the buff as a name in the file so the name is going to be appended to the end of the file that the number of the buffer is going to be appended to the name of the file here and now we're using our um python um snippet but i've just noticed that that's updated there's our python snippet we used earlier here's the one from my existing code so i'm just going to change that very subtle change but different version of artpro apply analysis buffer so rivers that's our variable which is here then rivers line the rivers out we've defined the other variable here rivers out is this with the name added to it and then we're going buff 100 500 000. it's just going to call one of these each meters full round now those are our defaults plainer write a print statement so users know when the script is complete script is complete that is out of this indentation so it's not in the for loop so if i press return twice it's going to run so 100 is processing that's the first one there goes out of it 500 is processing but it's going to have it there it is but 1000 is processing script is complete hooray so it's gone through a for loop there and it's added them to the map rivers line 100 500 100 500 000 to the map oh stunning so we've only had to do it for three files there but yeah imagine if we had to run that over you know multiple different ones we could do it again just to show you 1500 i could change the name of the output to with this line demo it's going to still add that on uh press script is complete i could put change that again press return twice there you go 100 is processing there it is riversland demo 100 okay you get the idea 500 100 000 1500 and then it's going to print that again script is complete again very okay so that's a really powerful for loop there it's not finished there we right click on this and we go save transcript and then we've got a little um python script that we've say we can save to our wherever we like and we can call that in and it's there ready waiting for us for when we have to run a buffer analysis um again so some really powerful tools indeed so we've used the python window we've run some python code we have evaluated the results we've looked at the buffer analysis we've adjusted the code and then run it again and that's the beauty about running in a python window so in practical we're going to be doing um more uh much bigger workflow working in a jupyter notebook so join me then and i shall see you in the practical thanks very much
Info
Channel: Damien Mansell
Views: 849
Rating: 5 out of 5
Keywords:
Id: QzkhXUU8VQQ
Channel Id: undefined
Length: 19min 27sec (1167 seconds)
Published: Fri Mar 05 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.