Select to view content in your preferred language

Input user address into table for geocoding using python?

1732
11
05-03-2010 04:45 AM
PaulMcBride
Occasional Contributor
My goal is to have the user input their address and have that be stored into a table for the geocode_address geoprocessing tool to work.  I can get the user input to write to any file type except a database table.  Any help would be great.

My end goal is to have the voters polling location decided by the users address.

Thanks!
0 Kudos
11 Replies
RDHarles
Regular Contributor
You can use an InsertCursor something like this:


import arcgisscripting, sys, os
gp = arcgisscripting.create()

gp.workspace = os.getcwd()

# Get input from the user at the command line.
input = raw_input("\nWhat is your address?\n")

# Set the output table
table = "poll.gdb/Voters"

# Call the InsertCursor method 
cur = gp.InsertCursor(table)
# Create the row
row = cur.NewRow()
# Add input to field 'Address'
row.Address = input
print "Updating "+table+" with '"+input+"'..."
# Insert the row
cur.InsertRow(row)
# Delete cursor
del cur
0 Kudos
PaulMcBride
Occasional Contributor
Thanks for the help, that worked great.

Now on to getting the address locator to work.  It works perfectly in the find tool, but for some reason it will not work when using the table.

Thanks again.
0 Kudos
BruceHarold
Esri Regular Contributor
Hi

Locators recognize field names in tables to use as input fields, like address, city, state, zip.  You might have better luck creating a script tool with a recordset input parameter and using an empty template table with these fields already set up; then the user will be able to input multiple records and the script will have an in-memory table to geocode.

Regards
0 Kudos
PaulMcBride
Occasional Contributor
I'm able to get the geocode address tool to work correctly based on the user input, but my python experience is very limited and am having an issue with getting the database to empty after the geocoding is completed.  Purpose behind this is so that only one address is geocoded each time the script will run.  My other issue is when using the geocode address tool it creates an output fc and I'm not sure how to allow overwriting for the feature everytime the tool is used.

Thanks for the help!
0 Kudos
JoelCalhoun
Deactivated User
My other issue is when using the geocode address tool it creates an output fc and I'm not sure how to allow overwriting for the feature everytime the tool is used.


The following is taken from ArcGIS Desktop Help:

Gets the overwrite output flag (False, True). Default is False.


You can set your environment to allow or not allow the overwriting of existing outputs. If it is set to True, and a specified command output already exists, it will be overwritten when the command executes. The previous features and attributes will be lost, and the new results maintained. If it is set to False, then command results will not overwrite existing features and attributes. The existing dataset will be maintained, and the command output will be renamed.


This property will tell you whether overwrites are allowed.


Can be set using either Boolean values (True, False) or integers (1, 0).


Syntax:

variable = object.OverwriteOutput


Return value:  Boolean


Examples:


import arcgisscripting
gp = arcgisscripting.create(9.3)

# Setting OverWriteOutput to True allows geoprocessing tools to overwrite
#    the output if it already exists.
gp.OverWriteOutput = 1

gp.Workspace = "H:/Tongass/forestry.mdb"
gp.Erase_analysis("housing", "income", "low_income")
0 Kudos
PaulMcBride
Occasional Contributor
Thanks that works great for my output fc, one more question for everyone.  What would be the best way to get rid of the records in the table used for the geocoding?  I only want to tool to geocode the single address that the user inputs.
0 Kudos
BruceHarold
Esri Regular Contributor
The Delete Rows GP tool will truncate the table.

Regards
0 Kudos
PaulMcBride
Occasional Contributor
Thanks again for all the help everyone, but I still cannot get this to work even when trying to customize a script that i start in model builder.  Here is my code, hopefully this helps.

THANKS!

# Polling_Location.py
# Created on: Wed May 12 2010 10:49:48 AM
#   (generated by ArcGIS/ModelBuilder)
# Usage: Polling_Location <User_Address> <Selected_Precinct> <Output_Layer>
# Description:
# Selects the voting precinct by a user address.
# ---------------------------------------------------------------------------

# Import system modules
import sys, string, os, arcgisscripting
# Create the Geoprocessor object
gp = arcgisscripting.create()
gp.OverwriteOutput = 1

# Load required toolboxes...
gp.AddToolbox("C:/arcgis/ArcTutor/GP Service Examples/SelectingData/SelectionUtilities.tbx")
gp.AddToolbox("C:/Program Files (x86)/ArcGIS/ArcToolbox/Toolboxes/Data Management Tools.tbx")
gp.AddToolbox("C:/Program Files (x86)/ArcGIS/ArcToolbox/Toolboxes/Geocoding Tools.tbx")



# Script arguments...
User_Address = raw_input("Enter Address:")
table = "\\\\coggis01\\gis\\MapServices\\Tasks\\Geoprocessing\\Address.gdb\\Address"

# Call the InsertCursor method
cur = gp.InsertCursor(table)
# Create the row
row = cur.NewRow()
# Add input to field 'Address'
row.Address = User_Address

# Insert the row
cur.InsertRow(row)

# Delete cursor
del cur



# Local variables...
Matched_address = "in_memory\\matched_address"
Address_Locator = "\\\\coggis01\\gis\\MapServices\\Tasks\\Address_Composite_Locator"
Success = "false"
Precincts = "Precincts"
Precincts__4_ = "Precincts"
Polling_Places = "Polling Places"
Polling_Places__3_ = "Polling Places"

# Process: Geocode Addresses...
gp.GeocodeAddresses_geocoding(table, Address_Locator, "STREET_ADDRESS Address VISIBLE NONE;NAME <None> VISIBLE NONE", Matched_address, "STATIC")

# Process: Check Match Results...
gp.toolbox = "C:/arcgis/ArcTutor/GP Service Examples/SelectingData/SelectionUtilities.tbx";
gp.CheckMatchResults(Matched_address, "Address", "0")

# Process: Select Layer By Location...
gp.SelectLayerByLocation_management(Precincts__4_, "INTERSECT", Matched_address, "", "NEW_SELECTION")

# Process: Make Feature Layer...
gp.MakeFeatureLayer_management(Precincts, Selected_Precinct, "", "", "Old_Dist Old_Dist VISIBLE NONE;Old_Numb Old_Numb VISIBLE NONE;New_Numb New_Numb VISIBLE NONE;Township Township VISIBLE NONE;Precinct Precinct VISIBLE NONE;Ward Ward VISIBLE NONE;Full_Name Full_Name VISIBLE NONE;GlobalID GlobalID VISIBLE NONE;Shape.area Shape.area VISIBLE NONE;Shape.len Shape.len VISIBLE NONE")

# Process: Add Join...
gp.AddJoin_management(Polling_Places__3_, "TOWNSHIP", Selected_Precinct, "Full_Name", "KEEP_COMMON")

# Process: Select by attributes...
gp.SelectLayerByAttributes_management(Polling_Places_3_, "NEW_SELECTION", "Madison_Web.DBO.PollPlace.TOWNSHIP = Madison_Web.DBO.Precincts.Full_Name")
0 Kudos
RDHarles
Regular Contributor
If you're only going to update one record over and over, use an UpdateCursor instead of a InsertCursor.


# Create update cursor for feature class.
rows = gp.UpdateCursor(table)
row = rows.Next()
while row: 

    # Add User_Address to field Address    
    row.Address = User_Address
       
    # Execute the new value to the table
    rows.UpdateRow(row)
    # Go to the next
    row = rows.Next()
del rows
0 Kudos