Just how easy is Python programming in ArcGIS Pro? You only need 7+2 commands for this tool!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hey everyone dr. Chris you today's video I'm gonna show you just how easy it is to do something that you should be doing every single time within your GIS and that is create a Python tool it's only seven Python commands and two arc PI commands here we go all right Here I am back in my basement office where once again I'm a free agent in GIS Geoscience graphic design and in video production today's video I'm gonna be talking about Python coding within ArcGIS pro and I'm gonna show you just how easy it is just how fast you can do it and if you're lucky I'll leave the links down below for you to download to the Python code all for yourself or you get going if you want to support my channel I've got a teespring store down below where you can get some GIS and Geoscience t-shirts all designed by yours truly also I've got a web site geographic information success.com and sign up for my newsletter they're pretty new to this blogging stuff but let's have fun with it I'm going to do a little bit of an explanation about the coding right now again I'll be talking about ArcGIS for personal use and a reminder that ESRI is not paying for this but they can if they like this is part of my series on build a bulletproof a GIS you can need a few things here you of course gonna need ArcGIS Pro you're gonna need a Python IDE that's an integrated development environment development environment I'm using Spyder and that comes with anaconda I believe you don't really need to install anaconda I'm gonna suggest you do because that gives you a whole bunch of libraries that you may never use because I'm gonna show you just as easy it is to create a bit of power creates a Python code using a little bit of snippet set you can use over and over and over and over and over again within ArcGIS pro now why would we want to do programming within our ji GIS brought well we actually don't have to there's a lot of things we all you have to do is click and stuff happens click stuff happens click stuff happened big stuff happens me I'm pretty lazy I like doing things only once that's why I like creating systems of tools that are integrated and repeat themselves over and over again that makes me a little bit lazy but it's some pretty cool stuff going a little bit of code that we're going to do today is in is has to do with do databases now when you start an ArcGIS project it automatically creates a geo database with your project name now for me I like having at least two other your databases that being a wretch geo database and a rest or geo database I'll probably talk about my rationale for those in the future but you're going to initially need is you import got that out of order to Python library libraries just o OS and art PI you only get art PI if you've got ArcGIS Rho I'm sorry probably probably map as well I mean these mapping salon I assume so you probably could use it oh that's not quite true and I remember now some of the commands are slightly different between map and our GIS pro I sense a slightly they're completely different we'll just talk about our GIS pro there are only seven Python commands that you're going to need to use in this little program these Python commands are def which creates helps us create a function helps us it creates a function that we can call over and over again we're also going to need yep CWD that's within OS this is so we can get the working directory because from that working directory that is where we're gonna stick the your database and that is where we also want to pull out the project name up from our mapping from our arcgis project we also need a loop this is a for loop go around a couple times because we're gonna have multiple names for different to your databases and go from you can do this once twice a million times when you get this I'll show it to you in a bit we're also gonna need split we're gonna need split because we're gonna take what we we're gonna take what we get from get a CWD and then we're gonna split it up so we can pull out the project name so we can use it on the next one and that is format or we're going to recombine project name with the geo databases that we want to create that was seven Python commands that we're gonna need we need two art PI commands these are pretty basic this is just commands or just the magic of editing fixed we're gonna need two commands ah it doesn't matter get parameter as text that's what we're saying is we're going to get the parameters from the ArcGIS tool this case there is only going to be one the second one is add message now I like to add a message because I'd like to remind myself with what I'm actually doing with the tool my more complex tools have many many messages just so I know what's going on while the codes running do that there was only seven Python commands and two arc PI commands here we go Here I am an ArcGIS pro I've already created project I have called it library that's something I'm going to talk about in a future video that says it's a library where I like to keep all my domains all my subtypes all my reused features but I don't have to build them and I can just go into the library grab what I need bring it into another project talk about that in the future right now all we've got is one do database library right here me I like to add two you know three you can add 10 I'm gonna show you how many you can you could add a thousand of them if you want by the way I'm going to show you how to create a tool let's start off by adding a new script my library I'm gonna call this script because I want to create a new geo database or some some new geo databases I'm going to call it gdb builder the name the name here hast can't be a kind of an under scare can of spaces but on this label we can so we're gonna call this yo data base builder now we have to aim this at a script file this is where we go back to our IDE that in my case is spider spider come over here what I've got set up already is a pimp plate I'll leave that down below to where I've already got some of the stuff in there I consistently use in all my little Python tools got a bunch of stuff commented out at the beginning I've got the first line of code called import OS and arc PI I've got a definition that's ready to go the name has to change it will change that name we also have it argument from the tool that we're going to be using in our QGIS pro and then a call to be function right here I'm going to save this template now as a properly named Python tool save as I thought tool I've already actually built one but I'm gonna call this different we call this pipe your database builder but Chris yeah Chris Dave I'm just going to fill in some this documentation of God here the file is your database builder dr. risk version 0.1 little Oh we'll get a running first try and guarantee it we've got a tool we can link back into art back in ArcGIS Pro though that's right here the script well within the new script though I'm gonna link back in their folders Python tools and we link it to geo database builder dr. Chris now we could save it here but it's nothing's gonna happen because I haven't put any parameters I like saving it right here just in case I screw up the parameters I'm gonna press save I'm gonna go back into the geo database builder I click on properties and then go to parameters now in this case we're going to only need one parameter and that is some geo database names that we want to name our multiple geo database so I'm just going to call this gdb names and the function is going to be think variable is going to be gdb names and the data type it is going to be a string and we're going to make it multiple values they okay by this on over to default I'm gonna have to defaults now let's make it three defaults just so you can see it working each and these are the names that are gonna be tacked on at the end because I want library underscore scratch library underscore raster's and as well have library underscore backup and we even add one ourselves when we run the tool okay so the first one is called scratch and then it's separated by semicolon and brass or city cooling and backup that's all we need to do so the parameters are now yeah I could run the tool right now still nothing's gonna happen I will show you so I click on G database builder right now these are the only parameters I've got scratch raster back up but I do know that it is gonna go back to my Python program here and what it actually is going to happen is it's only gonna execute this actually I'm gonna change right now is we'll make some changes within Orkut you guys right now sorry within Spyder right now we're going to call this IDI the Builder that migrating in my hand EVP builder that's going to be called the argument going to be the function and the argument coming out are going to be the names and this will work or should work so we run it details right there it says starting burn function current function is a jet database builder remember we're going to use seven commands or this little program we're gonna go back here so what happens is we want to we're now these arguments are actually the geo database here D names all of the names we're going to send them into our to your database builder function we know that the tool is going to run but what we now need build a geo database we can copy the Python code and put it into our little Python tool now close this I'm gonna go back up into analysis click on tools we're going to find the create geo database geoprocessing right there create file geodatabase and ask for the geo database location we're going to go in we'll give it the location which is right there and the name the name is going to be just a placeholder phone that will call this library do not use and run it okay that's run if I go into my fat folder there it is library do not use what we want now is the Python snippet from here so we go open history right here create file geodatabase I'm gonna right-click on it copy Python command I'm gonna go back to my spider code I'm gonna put it in here as a placeholder now there's a bunch of stuff we want to get rid of this is obviously do the directory that say pin and this is the name these are the two things that we're going to want to put in our for loop let's start off we want to get this directory right here and I've already showed you the little snippet of code that is OS dot it s CWD or get curve directly so directory so I'll call this urn directory people - OS thought yet and that's always a brilliant thing when it suggests it there we go get current directory now another thing we need is the name check this out what do you see here at the current directory where we've made the file to your database project name is right there and that is what we want how do we get that out of the directory that's where we use like if you remember that from what I talked about before so here is yet here's another little snippet of code that we're going to use we call this project name is equal to current directory got split blitz no but what quotes and I have to use I have to double the backslash because it's a special character backsplash or forward slash just forget backslash and we want to get the last drug that's been split so we have to go where brackets minus one got the current directory we've got the project name and now we just have to cycle through those geo database names that we've put in there we use a for loop because we want to get to each individual little each individual name so I go for gdb name in gdb names now we have to split that up again we actually want to split it based on the semicolons that were within that are being exported out of ArcGIS Pro those split comes in two again so we go split based on : so what we now have got we've got career directory project names and recycling through and grabbing each individual name that we've put in now we have to construct a new name we're down here and this part right here library do not use this is where the format comes into effect so we call this let's call this new pdb name equal to location curly brackets underscore curly brackets is with four format we want current directory whoops current rectory I love when they do that tab and gdb name we have to remember to now put this little step fix this little snippet of code we're going to put it within the for loop you have to get rid of this little chunk of information here in favor of the current directory rid of this name right here and use our OS on current directory we're sorry project name cut that one and we're gonna put new geo database name into year now we're gonna make sure that the argument is now geo database names and if we're really really lucky this will work on the first shot let's clean this up a bit more all this starting you know database builder now I like putting in other stuff but this is a really short snippet of code that you can use in future projects it'll make your geo databases over and over and over again let's give this to try let's go back to our GI Anse Pro I'm gonna go back to the catalog I'm gonna run the geo database builder I've got three names in there scratch raster backup and if I'm lucky it's gonna work on the first try run and let's see if it's run go back into my folder there we go get out of library underscore geodatabase raster and scratch now you can add any number of geodatabases you want let's put in will call this geo bad red based one two three and just run it and you're now going to have something that builds geodatabases just like that go back to the folder there they are one two three you can make hundreds of them this way if you like for me I would chain this into the other stuff that I do at the beginning of projects but don't have to keep on making these do databases over and over again now you'd have to be careful because you don't want to run this over geo databases that are populated because I'm pretty sure it'll start destroying data but you can get that's where you get a little more clever back in your code when you start using stuff like if exists don't do this if it exists don't do that so you don't lose your geo database that was my tutorial on creating a Python tool within ArcGIS pro as he saw it was pretty easy now I've got something can work over and over and over again for all your future projects and you can stick it into other little programs so that it does it automatically I'm gonna leave the Python tool down below fully documented if I remember to do that for your future use as a reminder I've got a teespring store we can get some geo science and T is shirts to support my channel that would be awesome also I've got a web site geographic information success comm where you can sign up to my newsletter get some valuable information every week thanks for watching if you like this video please give me a big thumbs up subscribe to my channel add me to your LinkedIn or even better share my videos through your networks until next time I'm dr. Gris keep Rocking [Music] [Music]
Info
Channel: Dr. Chris Geoscience
Views: 10,777
Rating: 4.8929768 out of 5
Keywords: Geophyics, Geology, Geoscience, GIS, Geographic Information Systems, Science, Environment, Seismic, Magnetics
Id: tdpZYJdasho
Channel Id: undefined
Length: 18min 13sec (1093 seconds)
Published: Mon Jun 22 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.