Population Density in Census Tracts

1102
2
12-03-2013 12:37 PM
VikJasrotia
New Contributor
I am quite new to Python and I am attempting to practice by making a simple program with ArcGIS. I wanted to make a script that automated the calculation of population density of selected census tracts within the city I live in. I also wanted to give the ability for the user to choose the census tract.

I then wanted to provide a map layout in ArcMap of the specific tract with the population, area, and population density being shown.

I realize most of this can be done using ModelBuilder, but I needed practice with Python and thought this would be a good simple program to start with. Anyone have any pointers on the script and also how I could possibly make it better?

Thanks
Tags (2)
0 Kudos
2 Replies
MaxSquires
Occasional Contributor
One useful thing to try is running the tools that your are interested in scripting from arccatalog. Make sure the results tab is enabled.  After running the tool you can right click the result object for the tool and select "copy as python snippet."  This copies the exact command that was passed to the arcpy interpreter. you can paste it into your python editor and inspect it further. 

One thing you should also look into is using the traceback, and os libraries (import os, traceback, etc.)  to help you debug your script.
simple error handling helps a lot when working with arcpy. something as easy as

try:
    arcpy.mapping.ListLayers(mxd)
except:
    print "there was an exception"
    # from arcgis python editor or toolbox execution:
    arcpy.GetMessages()
    # from idle or the like:
    print arcpy.GetMessages()

can prevent you from banging your head against the wall for too long.

You should also define the input parameters (separated by commas after the method call (i.e. arcpy.CopyFeatures_management (in_features, out_feature_class, {config_keyword}, {spatial_grid_1}, {spatial_grid_2}, {spatial_grid_3})) so you can attach each of your processes to one another dynamically. 

http://resources.arcgis.com/en/help/main/10.1/index.html#//001700000035000000

ESRI also has some great examples of how to set up your python script as a tool box script using the input parameters, output parameters, and data types.

http://resources.arcgis.com/en/help/main/10.1/index.html#/A_quick_tour_of_creating_tools_with_Python...

It's fun.
0 Kudos
VikJasrotia
New Contributor
So this is what I was able to get for my code for the user input.

import arcpy
import os
from arcpy import env
env.workspace = r"C:\Users\dallascowboy83\Desktop\final project 2\final project\final project.gdb"
env.overwriteOutput = True

#set local variables
tracts = "plano_tract"

#ask user for tract to search
tract = raw_input("Please type Tract Number: ")

#user selects tract, Select by Attribute tool runs
arcpy.MakeFeatureLayer_management("plano_tract", "plano_tract_lyr")
arcpy.SelectLayerByAttribute_management("plano_tract_lyr", "NEW_SELECTION", "Tract_Number = " + tract)
arcpy.CopyFeatures_management("plano_tract", "plano_tract_feat")

#add field
arcpy.AddField_management("plano_tract_feat", "density", "DOUBLE")

#calculate field
arcpy.CalculateField_management("plano_tract_select", "density", "[DP0010001] / [sq_mile_1]", "PYTHON", "")
print "executed successfully"

It does end by saying "executing successfully" but for some reason it just copies the original plano_tract shapefile instead of selecting the attribute and copying the features of the inputed tract. Do I have to use a where clause for this kind of function or is there a mistake somewhere in my code?

Victor
0 Kudos