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... 




8 comments: