Quantcast
Channel: Question and Answer » arcpy
Viewing all articles
Browse latest Browse all 767

ArcGIS python “add field” and “calculate field” workflow

$
0
0

I am new in Python and I am creating a Script. I’ve crafted a functional code, but I’d like to know if there is an easier way to do it, just for curiosity (And learning new ways!).

The purpose of the Script is to create 5 fields in a polygon shape (previously drawn) and to calculate their values.

My code is this:

import arcpy

env.workspace = "C:/Users/...."

#First, create the fieldlist with a loop for. With this I'll create the table
fieldlist = ["Area","Longitude","Latitude","MaxDepth","MinDepth"]
for field in fieldlist:
    arcpy.AddField_management(My_shapefile,field,"dobule")

#And now calculate fields
arcpy.CalculateField_management(My_shapefile,"Area","!shape.area@squarekilometers!","PYTHON_9.3")
arcpy.CalculateField_management(My_shapefile,"Longitude","!shape.extent.XMax!","PYTHON_9.3")
arcpy.CalculateField_management(My_shapefile,"Latitude","!shape.extent.YMax!","PYTHON_9.3")
arcpy.CalculateField_management(My_shapefile,"MaxDepth","!shape.extent.ZMin!","PYTHON_9.3")
arcpy.CalculateField_management(My_shapefile,"MinDepth","!shape.extent.ZMax!","PYTHON_9.3")

Viewing all articles
Browse latest Browse all 767

Trending Articles