Create A Python Script Tool | ArcGIS Scripting

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Applause] [Music] [Applause] okay welcome back to my channel and we are going to continue from where we left off able to just go through a practical question of what you have done to see how might you have grasped throughout the process all the way from geoprocessing exploring or exploiting the geospatial data manipulating it with the use of cars yeah so let's see what you have lands we are going to write a script that converts a text file to a feature class so I am this text file is delimited by tabs or spaces and then you are going to convert it to a feature class which will contain three fields it will contain the X the Y that is the longitude latitude and then the confidence level so let's get right into it first we'll begin by importing the works the site package okay geez and you need to import occupy and OS so you can run that let's go line by line then now we are going to work on try/except and finally and I have mentioned in the previous tutorial which I live in the link did indeed leave a link in the description box I had mentioned that they try except finally used for error handling and it will help us to know in case there is an error in the try block then of course it will just keep and return the accept and then the finally so what I mean is just create a try block then you put some code then and except when he puts on code then finally so let's use an example an example is let's say want to print let's print yellow yeah then we also wanted to say except so print want to print the error something went wrong okay this is not a good way of debugging but let's just use it something went wrong then finally finally will always be executed print you have come to an end so if we run this you'll notice that like I just ran it then I explain what's happening you'll see something went wrong we have come to an end it means that the try block was not ran successfully because I said green yellow and yellow is not defined we have not defined yellow anywhere area trying to call it so um if it was string it would have just sucked fine let me show you what loved them it could have painted yellow and you have come to an end but since now you have defined it as a variable you shall never not defined or declared then forget that error but you can see the political handling errors so let's apply that knowledge in what you are going to do so we are going to try to first of all define our space then open my text file after we open the text file we'll inside a Casa and and then I looked through the the field that I've selected from the Casa and then we will feed it with the X Y and the confidence level from the text files but if that process done successfully treat should return an occupy error message and then finally it should delete the Casa and close the file so let's do that quickly so they try so let's start by defining our workspace then the children arc space should be a tab just a workspace firstly let's create will be hard coding first of all then when everything works well will now change this to some dynamic code so for now Jesse occupy do it env don't work space then we are saying it uh-huh so I expect a long talk on this and we just steal it from access reduce this geoprocessing that NDB so just properties when you copy that path then paste it and paste it here we like to put the roof on my team then after I've defined a locksmith we can now open the file you want to read which is a text file open text file so to open it we lose just a F which is the variable and then L will open then now we declare the name of the file which is can just give the path I have the patria so it's a text file then copy part like that sorry and just put reform I think there then wanted to be in read to be in read access that is you can only read then huh we want to read the file line-by-line and convert it to a list so just released then you can call it list of schools then say F dot read lines just like that you say read line by line just commenting it out then next we need to create our Casa so we are going to create an inside castle open for read and write access this is a casa Casa okay so to create a castle just name it krauser then we'll give it occupy dot inside kartha and then the name of the file we choose in the job geoprocessing got MDB now there is something we're supposed to do in occupy are supposed to create this geodatabase and then they are supposed to create within the geo database as opposed to create feature class so let me just show you that quickly but I've already made a tutorial on how to work with a catalog so on whatever I want your J database to appear just right click when you say new let me just delete the runner that exists okay so I think there's a problem to just create another new then personal geodatabase let's do name it through something like geoprocessing one because i already have another name to geoprocessing then you can change the pattern of today you can change to geoprocessing one like that then mm-hmm within the geo database we need to create a feature class so a new feature class and a feature class the name we just call it school schools and then it's a point feature type then I can click Next so just relax Geographic wild then 1984 you just use that on for now then click Next then here we need to add a field and this field we'll call it confidence level we'll see how we use it in the code confidence make sure to spell it correctly confidence level no confidence value then it's going to be an integer a short integer then finish now you have this course but it's an empty feature class now wants to populate it to the points that you have in our our text file and yes I text file that I made I can just open it so that you see and as you can see I've just made a sample text file with separated by tabs is the first one second one like that and so on and so forth so the Sun should be populated into the feature class as points and confidence level so you have opened the file which is schools dot txt then we have read it land by length when you have opened an inside cancer kasa and now we want to specify inside the wants to look at the schools the one that you created the feature class so it's called schools schooled yeah if it was not you to suffer that with our sales goes dot SHP then next after have created our Casa now let's do our for loop will be looping through on the school text file file and inside that should then long it you don't need you through school feature class yes alike that that's a comment then tablets aha so fall for school in for school in lists of schools this one this variable we want to fast rate an if statement to check if there is evil attitude field exists in school then we should continue if not then we should stop the whole process or continue the time is okay then you can move at our tab backwards then now we need to search the values that is the latitude longitude and the confident level and remember that our text file is split by tabs so set values you just split byte also you can say for example Val's is equal strong school dot split when you're going to split it by tabs I think it's I think I tell these two spaces let me just confirm actually needs three spaces the next are going to me just at the comment so that can get to follow split by the next one through the states the latitude and also the longitude I hope you're following through so the longitude the longitude value in our in our text file let's look at the text file and studied a little bit in our text file we have three columns we have the longitude the latitude the latitude the longitude and the confidence level so the confidence level is at index 0 the latitude is at the longitude is at index 1 and the latitude desert z-index - so let's just do that the same way so it will be we just converted him to float first then Vance so the latitude is index 2 then the longitude is in index 1 and finally the confidence level is in we just convert it to an integer because the short integer we had set the confidence level remember we had set it in when you are creating the feature class and we had added the new column with a value of confidence level confidence Riley so just said it as the Valles and it's at index 0 then next you want to convert want to convert actually want to create points in AK by and it's very easy to con to create points just say for example pointers the variable point to use act by dot point if you wanted to create a polygon and find iterate it will be occupied polygon a polyline act by dot polyline so now it's our point and then the point can take him to values that's the X and y axis the longitude which are specified idea and then you can just copy it and paste it there then comma latitude test next we're going to create a new row and you're going to call it feet can call it whatever you want so feet is equal to the Casa which you created earlier dot new row if you remember I had taught you about creating Casas and then creating rows so within a casa it's a row so the new row just it just be an empty row then now we want to feed it so if we did with the ship feet dot ship and filter ship will give us the point that is the latitude and the longitude and then remember the shape I talked about when I was talking about tokens that is the shape at XY shape at area so by default the shape will give us the XY then now you can have the feet but set value and here you said the confidence level comes in remember we created a a new column called confidence level confidence value sorry so first you need to create the column called confidence value before you can actually set the value in the problem in the in the prop so if you say feet set value then you give it something like let's see Lake ma'am when you don't create it in the columns in access it won't be reflected the coddled actually run so you can leave it like that then finally we can inside so it's Casa then let's inside it supposed to because also Casa inside in fact true and elderoy are going to are inside his feet now that we have populated it so fast ik we say Casa donor oh the nose job would let indeed Casa got inside to inside the feet feet contains the points and the confidence value as easy as that now we are done let's go to the except block now for the except to just print out let's return the error in case something goes wrong so just print back by dot get messages then finally we need to firstly release the lock so releasing the locks means to delete the kartha then now we also close the file remember I had mentioned about closing a file so um so far so good I think I can try it out and see anywhere that you went wrong so let's run it and give it time to spin so let me explain what will happen once it's finished it should look really it should populate the school's feature class with their new points so as you can see there is no era which is a very good thing so let's go to achma and we had saved it in and i geoprocessing one so let's just drag and drop it and true into the into this area to view so let's view the attributes table as you can see it seems like it's empty so I've noticed that there was a mistake somewhere here I wrote config instead of confid you can just change that and then rerun it now let's check occupy refresh then you can't drag this into just remove this so as you can see let's open the attribute table we have ten points and they are just similar to the ten points that we had in the text file so voila it is done now what is left is just to take this script make it dynamic and then convert it into a tool that can be shared so let's do that quickly go back to the code then I'm going to make it dynamic and I had said you can make it dynamic by using the get parameter as text from act I so able to change all of this and we will refer to just creates new variables so maybe for the workspace will name it as occupy dot get parameter as text parameter parameter as text text then we'll give it to zero the next one we also add to the text file text file which is act by again 38 parameters test text then I'll give it one then next one it's the the feature class will just say FC and it's octa-pie again that get parameter our text will give it to so now we can change this hard-coded paths to the correct values Rackspace yeah then this one we can change it to text file quickly then you can also change this the inside cuz I true FC so now we have made our code dynamic we now want to convert it into a tool so I'll just create a dot py file I can add a Python roger service as a text file then I'll rename it to text to feature dot py yeah then now we can transfer the code we just ran straight till they step and also we have John then let's paste that truth oh that's nice you can save it these are supposed to be wax placed I don't know it wasn't saved when this was supposed to be they text file and then these are not supposed to be FC so I think everything is okay right now you can just save it then let's head on to act by quickly and wants to create our tool box so I told bak are going to create it and the that's created in this box in this folder so create click and then new and are going to create a new tool box and then let's rename this tool works to text text to feature each tool box then now that in the little box you need to add script so we'll add our script they never were script is text true feature then the label will now build a long explanation geez text file to feature class and it will be the label that appears such as the one here for ad script then the description converts text files to feature class you can skip this with a side sheet then the skill file you need to import it so let's find it it's it was called text to feature and it was and just under document your processing so it's this one the dot Pugh I failed open it and then can just click on next so here we need to set the the parameters that the user needs to input so we need first just input workspace it's like the label and then the Sun is like the data type so just would work space it can either be expressed or a feature class so just roll down which are data set the next input we just follow this procedure just follow this procedure next text file then next FC and FC teams had not changed its FZ was supposed to be also act by dot getparameter think something went wrong yes of that so next it's text file then the feature class so back to act by input text file when you want it to be a text file next input feature class then you wanted to be a feature class so deities like that then you can click finish and now you have our our script our tool now this tool we can add it into actual books so click on app actual box it should be loading then you're just going to click at the root of actual box at the top and I'm going to say add toolbox and I just added the toolbox you have created so it's called within this extension this one just open it now it comes the final fight the meet the chunk where we need to test to see if everything went well so as you can see if we double click on the tool on the script it brings a user interface with the form that you have already created the labels and our two just need to do is just add our data so you need to input our workspace I'll just use the workspaces they have your processing one you can add it then text file text file will be the text file you chose in geoprocessing it's called schools then next the feature class depending on where you saved us the feature class its schools add then click OK firstly the first completed successfully we can now let me delete this we can now import that data and see if it's worked well so it was in job reducing one just drop it and as you can see guys he had the points you can open the attribute table to check since you have run this command twice it has brought the values twice because I concede reached and then it started again counting so if you run the script again at a time for the same feature class it will just add them on top it will add the values on top so it it turns over right so and that's it guys thank you for watching remember to follow me on youtube subscribe hit that red button just below there to support me and then like the video share it with others and yeah see you guys bye [Music] [Applause] [Music] [Applause]
Info
Channel: The Geospatials
Views: 8,832
Rating: 4.9083967 out of 5
Keywords: Working with Geometry, Manipulate Spatial Data with python (Cursors), List Feature Classes, Make Script Dynamic, Geoprocessing tools, Create Workspace, Convert txt file to feature class, ArcGIS Scripting, Create A Python Script Tool, python, arcgis, jupyter, anaconda, arcpy, arcpy scripting with python
Id: Sguwt5kup-M
Channel Id: undefined
Length: 31min 39sec (1899 seconds)
Published: Sat Mar 02 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.