Python coding for point distance

1834
14
03-31-2014 10:18 AM
MatthewIsbell
New Contributor
I am a bit stuck and hoping someone can help.   I have some python experience, but am getting stuck on the best rout forward for a specific function I am trying to run.

I have two point shapefiles.  The first is a list of polling locations for the county I live, with the precinct number that corresponds to that location.  the second shapefile is a geocode of all the voters in that county.  Each voter has their precinct number in the attribute table.

What I am hoping to do is run a code that will calculate the distance each voter is from their designated polling spot.  I am assuming Ishould use point distance and a combination of an IF or/and FOR loop to run the function when the precinct numbers from the two distinct shapefiles match.  However, I am unsure how to move forward in writing that code.  If anyone has any suggestions, or a better method, the help would be appreciated.

Matthew
Tags (2)
0 Kudos
14 Replies
MathewCoyle
Frequent Contributor
Are you looking for a simple geographic distance or using a transportation network of some kind?
0 Kudos
MattI
by
New Contributor
Simple geographic distance is good for me. Just not sure how to proceed
0 Kudos
markdenil
Occasional Contributor III
in a loop
create a feature layer of each polling place and
another of the voters associated with that place.

Run GenerateNearTable_analysis with the ALL option

This will give you a table of the distances of each voter to his polling place.

PointDistance_analysis might also do the trick for you

Of course, you need an Advanced license to run either of these tools.
0 Kudos
MattI
by
New Contributor
thanks!  I am trying to write the loop but having a few problems.  Have had trouble getting it right before.  my code is below, if anyone can give any guidance it would be appreciated it

import arcpy
from arcpy import env

env.workspace = "C:\work"
env.overwriteOutput = True

# Local variables:
Voters = "Voters.shp"  # these are the voters, point file
PollingSite = "Polling.shp"  # this is the point file of the voter sites
distance = "distance.DBF" # this will be the output of the distance table

#need to define rows, updatecursor seems to be required
rows = arcpy.UpdateCursor(PollingSite)
rows2 = arcpy.UpdateCursor(Voters)

#run a for loop to go to each row in attribute table.
for row in rows:  #rows is the PollingSite shapefile
    p_precinct = row.Pre #this is the precinct column for PollingSite
    v_precinct = rows2.PrecinctNa # this is the precinct column for Voters
    if p_precinct == v_precinct:
        PointDistance_analysis(Voters, PollingSite, distance)
0 Kudos
MattI
by
New Contributor
the indents dont show up in the copy paste, but they are indented as needed.  i get no errors on that
0 Kudos
JamesCrandall
MVP Frequent Contributor
the indents dont show up in the copy paste, but they are indented as needed.  i get no errors on that


Use the "#" Code Wrap function on the toolbar when you make a post


import arcpy
from arcpy import env
 
env.workspace = "C:\work"
env.overwriteOutput = True
 

# Local variables
Voters = "Voters.shp" # these are the voters, point file
PollingSite = "Polling.shp" # this is the point file of the voter sites
distance = "distance.DBF" # this will be the output of the distance table
 

#need to define rows, updatecursor seems to be required
rows = arcpy.UpdateCursor(PollingSite)
rows2 = arcpy.UpdateCursor(Voters)
 

#run a for loop to go to each row in attribute table.
for row in rows: #rows is the PollingSite shapefile
   p_precinct = row.Pre #this is the precinct column for PollingSite
   v_precinct = rows2.PrecinctNa # this is the precinct column for Voters
   if p_precinct == v_precinct:
      PointDistance_analysis(Voters, PollingSite, distance) 

0 Kudos
MattI
by
New Contributor
thanks.  the error I keep getting at this point is

AttributeError: 'Cursor' object has no attribute 'PrecinctNa'

I cannot understand why I get that vote the second cursor object, but not the first
0 Kudos
markdenil
Occasional Contributor III
Well, there is no PrecinctNa in row2  because
rows2 is a cursor, and contains the table, not an individual row.

You don't really want to use cursors to do this
and you need layers to drive PointDistance

import arcpy
from arcpy import env

env.workspace = "C:\work"
env.overwriteOutput = True

# Local variables:
Voters = "Voters.shp" # these are the voters, point file
PollingSite = "Polling.shp" # this is the point file of the voter sites
distance = "distance.DBF" # this will be the output of the distance table

rows = arcpy.SearchCursor(PollingSite)

##  build a list of sites with a search cursor
sitesList = []
for row in rows: #rows is the PollingSite shapefile
    p_precinct = row.Pre #this is the precinct column for PollingSite

    if p_precinct not in sitesList:
        sitesList.append(p_precinct)

for site in sitesList:
    Votewhere = '"PrecinctNa" = %s' % (site)
    Sitewhere = '"Pre" = %s' % (site)

    ##      don't depend on env.overwriteOutput
    if arcpy.Exists("PollPlace_Lyr"):
        arcpy.Delete_management("PollPlace_Lyr")
    if arcpy.Exists("Voter_Lyr"):
        arcpy.Delete_management("Voter_Lyr")
    if arcpy.Exists("Point_Tbl"):
        arcpy.Delete_management("Point_Tbl")

    arcpy.MakeFeatureLayer_management(Voters, "Voter_Lyr", Votewhere)
    arcpy.MakeFeatureLayer_management(
                            PollingSite, "PollPlace_Lyr", Sitewhere)

    arcpy.PointDistance_analysis("Voter_Lyr",
                                 "PollPlace_Lyr",
                                 "Point_Tbl")

    arcpy.Append_management("Point_Tbl", distance)
    ##  this assumes distance.DBF exists and has the
    ##  correct schema
    


untested: for illustration only: no warranties expressed or implied
0 Kudos
MattI
by
New Contributor
thank you for the info boost!  I am still getting used to python so this definitely helped.  I do have a few quick questions. 

i am getting an error (message below) regarding the "Point_Tbl" part of the code.  And indeed I am little confused there as well.  Is the Point_tbl supposed to be the eventual output of distances, (called distance.dbf).  does another MakeFeatureLayer code need to be written to create the Point_tbl layer? 

one other question, further up in the code, in the for site in siteList loop, the Votewhere = PrecinctNa is referencing the field from the Voters shapefile.  However, a searchcursor is never established for that shapefile.  How does the code know that 'PrecinctNa' is referencing the field in Voters and not a field in PollingPlace?  Sorry if this is an obvious answer, just a little confused.

again thank you for the help, I feel like I am much closer

Traceback (most recent call last):
  File "C:\Work\Code.py", line 43, in <module>
    "Point_Tbl")
  File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\analysis.py", line 941, in PointDistance
    raise e
ExecuteError: ERROR 999999: Error executing function.
Failed to execute (PointDistance).
0 Kudos