Tuesday, September 30, 2014

How to write Python Scripts for ArcGIS

We can write python Scripts to the ArcGIS in-order to skip large number of complex steps in ArcGIS. Once we created script users just have to run the script and assign the inputs for the script. In ArcGIS we can create model builder and then created model builder can be export as a python script. Then we have to Edit the script in-order to modify and run as user requirement.  



Step 1,

In this exercise first we create model builder in ArcGIS and the export it as a python script.  Check my following blog for more details about model builder,

http://effective-gis.blogspot.com/2014/07/modelbuilder-in-arcgis.html


Create Personal GeoDataBase - Right Click - New ToolBox - Right Click - New Model



Drag Select Tool, Which required for selecting Main Roads from the Transport layer in this exercise. Add data as required. 



Add buffer tool, which will create buffer for the selected road type from the select tool.


Then add clip tool



Step 2,

Then we have to export model builder in to python script

Model - Export - To Python Script


Save script as a Python Script


Step 3,

Once you save your script you can open it with either python IDLE (command line) or Note pad, It is better to open with python IDLE.


you can open python IDLE manually by

All programs - ArcGIS - Python 2.7 - IDLE

This is the python script generated by the model builder



Step 4,

You can run the Script by Run - Run Module or by pressing F5


Once we run this script again it will generate error message (only if you have run the model builder earlier and having outputs ) saying particular output is already exists. Then we have to edit our script otherwise all the time before run this tool we have to delete the outputs. 



Step 5,

As you know import arcpy keyword is to import ArcGIS functions and also we need "sys" keyword,

for this script we need to assign out data in Geo-Database, for that we have to use "import = sys.argy[1]"



Then change the local variable path as follows,
"input+"\\WorkingData\\Transport (Here as input we assign out Geo-Database) 


Step 6,

when we run our script it gave us error message saying that data already exsists. Here we are going to remove those outputs in the Geo-Database (only if available)

if arcpy.Exists(trans_li_selected): arcpy.Delete_management(trans_li_selected,"")

here we have used Delete_management funtion in the python script. apply this for the required outputs. 



Step 7,

We can Add messages to be shown while script is running, for that we have to use following code,

arcpy.AddMessage ("Deleting exsisting files on Progress")

Withing brackets you type your message to be shown, You can Add any number of messages as you required to be shown when running your script..


Step 8,

Now you have complete your script, We have to add this script to ArcGIS, for that right click on your created ToolBox - Add - Script

This window will appear, Then you can give a name to youe script, Label for the script and Description, you do not required any stylesheet for this, the press 'Next'


The we have to add label for user interface, this appear when running this tool, therefore we have to add meaning full name, here we need input as a GeoDatabase, therefore I have assign "Input DataBase" you can add what ever you required,


Since we need input as a GeoDatabase we have to assign Data Type as "Workspace", Then press "Finish"


Then we have to add our script, brows to the script and press Open.




Step 9,

Once you complete adding script, your added script appear in your ToolBox. 



Step 10,

Now we have to run our script, for that just double click on the script, Then select your Goedatabase where your data available for this practical (data used to create bodybuilder), Press "Add", After you add Geodatabase run the script.

Step 11,

Once you run the script you can see the messages which you have added to the script is show in the each process, 


While you run this script you will get this error message "Building Polygon Does not exists", mean we do not have required data for clipping process, When you get this kind of errors inpython it will show you the error line even, so we can solve the problem easily, 


Once we go through our coding we can see that we are deleting the required shape file also from the beginning which only required for the clipping, so we have to remove that line from the script. then add that deleted shapefile to your database again manually. Therefore make sure not to delete data which required for the other process in the latter part of the process. once you edit your script make sure to save it. 


Step 12, 

Again run your script. Now its working properly, once it complete it generate completed message. you can check your output in the Geo-database.




1 comment: