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.




Tuesday, September 23, 2014

Python Scripts for ArcGIS to Print Spatial Reference (Coordinate System) of a Shape File

Python is a powerful Object Oriented Programming language which can write scripts for ArcGIS also. Here I have use IDLE (Python GUI) in the ArcGIS 10.1 (Python 2.7) which automatically install computer while installing ArcGIS, but there are many other tools you can use to write python scripts like PythonWin etc... or you can use the "Python" in ArcGIS.

In this exercise  we are going to write a Python Script out side the ArcGIS to get the Spatial Reference of a Shape-file.

Step 1

Start - All Programs - ArcGIS - Python 2.7 - IDLE (double click)


Step 2

This is the Python Shell where you have to write your scripts


Step 3

In first line we have to write "Import arcpy" which inform to Python Interpreter that we are going to use ArcGIS tool for our Script. 

In second line I have created String Variable call "Fetureclass"you can use your own variable name, Then assign the shape file path to the variable. 

"fetureclass = "F:/AdvanceGIS/Ex12/GISEX_12.mdb/Working_data/hydro_arc""

Shape file which I have used here is stored in Geo Database so I have assign my path as above...(make sure you assign path inside the " ")

In next line I have used the method of this script. here method is to describe the variable we have created (fetureclass)

desc = arcpy.Describe(fetureclass)

In next line we have to assign the already described object to a variable. 

spatialRef = desc.SpatialReference



Step 4

Now we have to Print the property of this spatial reference. Here the property is the Name, so I have use following command to print the spatial reference of this shape file. 

print spatialRef.Name

Once you press "Enter" python print the spatial reference of the shape file.

"SLD-Kandawala"

Step 5

We can use the ArcGIS "Python" option also 

Geo-processing - Python


Type the same code and you can see the same out-put... 




Wednesday, September 10, 2014

Data Driven Pages in ArcGIS

When we need to create large scale maps such as 1:1000, 1:5000 etc... it is not possible always to export entire map into a single A4 or A3 size page. Therefore we can use Data Driven Page option in the ArcGIS which include Grid Map Index as a key to the Map.

Step 1,

Right Click on the ArcGIS Tool Bars area - Add Data Driven Pages Tool bar to your ArcGIS Desktop.



Step 2,

Add data you prefer for this project and make sure to change the scale in to large scale such as 1:5000 1:2000 etc... The change your view to 'Layout View', You can see your entire map in not visible withing the map area.


Step 3,

Go to Arc ToolBox - Cartography Tools - Data Driven Pages - Grid Index Features


Step 4,

Assign output feature class and select Input features as you need, you can add any amount of data layers. Then Tick both check boxes (Generate polygon Grid that intersects input feature layers or datasets and Use page unit and Scale). Then Map scale will automatically assign the layers map scale (Here I have used 1:5000 for layers hence Grid Index Features also automatically assigned 1:5000 scale). Then assign the size of your map sheet need. Here I have assign 20 cm for both Polygon Width and Height. Use drop down box to assign the unit you need, (Make sure for your layers also you have use the same unit such as km, m or cm etc...)Then assign number of Rows and Columns you required for your gird. Keep other optional features as it is. then Press 'OK'


Step 5,

Now your map Index has been created, 



Step 6,

This is the attribute table of the created Map Index, you can see it has given Page Name and the Page number for each Grid. 



Step 7,

Now we have to use Data Driven Pages Tool bar to assign our created Map Index to our data layers.
Click on Set Up data Driven Pages then in the Index layer "layer" select created Map Index, for the Name field select "Page name" of the Map index attribute.  and assign Page Number for the Sort field.



Step 8,

In the "Set Up Data Driven Pages" select Extent Tab, and then assign 100% as the Best Fit. Press "OK"



Step 9,

Now if you click on the Next page option you map shift to the next page, 


Step 10,

Now we have to show the map page in the Grid Index, for that add new data frame, 


Step 11, 

Add your map index to the new data frame.



Add Extended Indicator for new layer, 



Step 12,

Now you can see its indicate what page of the map on the Map Index, now we can add Data Driven Page Name to the Map to improve the readability of the map. 


Step 13,

Now we have to export Data Driven Pages to  PDF, for that simply go to Files- Export map  then select PDF, once you select PDF you have to tick on the All pages option the "Options". 


Once you exported to PDF you can see your map how it reefers to the Map Index. 







Monday, July 21, 2014

ModelBuilder in ArcGIS

ModelBuilder is an application use to create, edit, and manage models.Models are workflows that string together sequences of geoprocessing tools, feeding the output of one tool into another tool as input. ModelBuilder can also be thought of as a visual programming language for building workflows.

In this exercise I have used ModelBuilder for our last  exercise Create Stream Line Network Using Digital Elevation Model in ArcMap. (http://effective-gis.blogspot.com/2014/07/create-stream-line-network-using_16.html)

Step 1,

Right Click Your Geo Database - New - ToolBox to create new modelbuilder and name as you required. 


Step 2,

 Right Click on the created toolbox - New - Model


Step 3, 

this is the Model window of the ModelBuilder, once you create a model you have to save it and you can Edit it when you need. 


Step 4,

Go to ArcGIS ToolBox and select the Tool you need and drag in to the Model. Here I have used Terrain to Raster Tool. 


Step 5,

Now you can see that Terrain to Raster Tool have been added to the Model. Now we have to give the inputs to the Tool. For that Double click on the Tool. 

Same as in ArcMap you have to just the data you required and enter required parameters. 



Now you Tool containing data which can process to get a output. 


Step 6,

Add Flow direction tool, for this as a Input we have to assign Terrain to Raster's output. Same as we did earlier double click on the Flow Direction Tool.


 For this as a Input we have to assign Terrain to Raster's output. Same as we did earlier double click on the Flow Direction Tool. (we do not required data for output drop raster)


Now both Tools are connected since we have use "Terrain to Raster" output as a input to the "Flow Direction" (If you press the "Auto Layout" option in the Model's tool bar your Model will automatically arrange in to a order)


You have to run the model to get the output to use as inputs for the other Tools (Before Running the model you can validate the model)



Step 7,

Now we have to use Flow Accumulation Tool to this Model. 


Select Flow Direction's output as a Input.



All three Tools are connected together 


Step 8,

Now Add "CON" tool to the ModelBuilder


Add Input as Flow Accumulation's output.



Step 9,

Now we have to add stream line tool to the Model, Select CON's output as input to the Stream Line.





Step 10,

Now we have to add Stream to Feature Tool for the model. 


Add inputs to the Stream to Feature Tool


Step 11,

Run the model. you will get the output to your Geo database. Add to ArcMap.