Select to view content in your preferred language

Attributing Railways with Surrounding Population

812
1
09-06-2011 08:51 PM
AshleyBrown
New Contributor
Hi,

I???ve tried to write a script to take a file of railway lines and find the towns within 2 kilometres of each railway line, sum the populations of those selected towns and return the result to a field in the railway lines file.

I???ve been successful in getting bits of the script to work in isolation (copied from other???s scripts), but not as a whole.  The script is below and I would really appreciate it if anyone can offer advice as to why it doesn???t work, or how it could be improved.

I only have an ArcView license (9.3.1) with Python 2.5 and I'm very new to Python.

Thank you,
Ash

# Import system modules
import sys, string, os, arcgisscripting

# Create the Geoprocessor object
gp = arcgisscripting.create(9.3)

# Load required toolboxes.
gp.AddToolbox("C:/Program Files/ArcGIS/ArcToolbox/Toolboxes/Data Management Tools.tbx")

# Script arguments...
inFC = r"c:\data\Towns.shp"         # Input towns.
inFCSel = r"c:\data\railways.shp"      # Input railways.

# Local variables...
tempLayer = "tempLayer" # the towns
tempLayer2 = "tempLayer2" # the Railways
counter = 0

try:
    # Process: Make Feature Layer for input feature class (the towns).
    gp.MakeFeatureLayer_management(inFC, tempLayer, "", "", "")
        
    # Process: Make Feature Layer for Selecting features (the railways).
    gp.MakeFeatureLayer_management(inFCSel, tempLayer2, "", "", "")
    
    # Create a row and rows object to update the field (Railways).
    rows = gp.UpdateCursor(tempLayer2)
    row = rows.Next()

    # Iteration to loop through the railways
    while row:
        query = "\"FID\" = " + str(counter)

        # Process: Select the railway
        gp.SelectLayerByAttribute_management(tempLayer2, "NEW_SELECTION", query)
        
        # Process: Select towns within 2kms of the railway
        tempLayer3 = gp.SelectLayerByLocation_management(tempLayer, "WITHIN_A_DISTANCE", tempLayer2, "2 Kilometers" , "NEW_SELECTION")

        # Create row and rows object to sum the population (Towns).
        rows2 = gp.SearchCursor(tempLayer3)
        row2 = rows2.Next()
        rsum = 0

        # Iteration to loop through the selected towns
        while row2:
            rsum = rsum + row2.GetValue("Total_pop") # Sum the populations of the selected towns
            row2 = rows2.Next()
        
        row.SetValue("Pop_Sum",rsum)
        rows.UpdateRow(row) # update the population field for the selected railway

        counter = counter + 1
        row = rows.Next()
        
    gp.AddMessage("Processing done...")   

    # release locks...    
    del row
    del rows
    del row2
    del rows2
    del gp        
except:
    print "Error found.."
    gp.AddMessage("Error found..")
    print gp.getmessage(2)
    del tempLayer
    del tempLayer2
    del tempLayer3
    del gp
Tags (2)
0 Kudos
1 Reply
MarcNakleh
Regular Contributor
Hi there,

What kind of error messages are you receiving?
I tried out your code on my computer, and once I had created the appropriate columns, it worked fine.

As a couple of pointers:
- I would advise giving your layers and shp's more explicit names to help in coding and debugging (ex. layer_town, fc_railway)
- You don't actually need a tempLayer3, as the SelectLayerByLocation modifies the temp_layer1 directly.
- I would put the try...except around the geoprocessing block of code. That way, you don't end up catching more mistakes and issues than you might be expecting (like issues due to naming conflicts, references or other Python aspects of Python programming itself.)

The code I used to test was as follows:
import sys, string, os, arcgisscripting

gp = arcgisscripting.create(9.3)
gp.AddToolbox("C:/Program Files/ArcGIS/ArcToolbox/Toolboxes/Data Management Tools.tbx")

# Variables
shp_town = r"c:\data\Towns.shp"
shp_railway = r"c:\data\railways.shp'
layer_town = "tempLayer"
layer_railway = "tempLayer2"
counter = 0

# Execution    
gp.MakeFeatureLayer_management(shp_town, layer_town)
rows = gp.UpdateCursor(shp_railway)
row = rows.Next()

while row:
    rsum = 0
    query = '\"FID\" = ' + str(counter)

    try:    
        gp.MakeFeatureLayer_management(shp_railway, layer_railway, query)    
        gp.SelectLayerByLocation_management(layer_town, "WITHIN_A_DISTANCE", layer_railway, "2 Kilometers" , "NEW_SELECTION")
    except:
        print "Error found.."
        gp.AddMessage("Error found..")
        print gp.getmessages()
        
    rows_town = gp.SearchCursor(layer_town)
    row_town = rows_town.Next()
    
    while row_town:
        rsum = rsum + row_town.GetValue('Total_pop')
        row_town = rows_town.Next()

    del row_town, rows_town
    row.setvalue('Pop_Sum', rsum)
    rows.UpdateRow(row)
    
    counter = counter + 1
    row = rows.Next()

gp.AddMessage("Processing done...")
del row, rows
del layer_town, layer_railway
del gp    


I hope this helps!!
0 Kudos