I have been trying to create a command to perform geocoding of address data with local resources (i.e. Streetmap Premium address locators) via arcpy integration with Stata. However, I have been running into a problem where the arcpy geocode command throws an error, stating that the output already exists. The trouble is that: 1) the program deletes the File Geodatabase where the output should be stored prior to this step and 2) you can verify that, immediately prior to the geocoding step, the .gdb contains no such feature class. This may be an arcpy/ArcGIS issue, but I have gotten the geocoding command to run in at the python command prompt (and even in Stata under certain circumstances). But this particular implementation is giving me trouble. The program assumes there is a dataset in memory in Stata that contains columns named "address", "city", "state", and "zip". The only argument to this minimal example is "using" which gives the path to where you want the
geodatabase (.gdb) file with the geocoded output stored. I have also tried manually deleting the output feature class immediately before the geocode command is run. Any ideas would be greatly appreciated!
Mike LeGower
geodatabase (.gdb) file with the geocoded output stored. I have also tried manually deleting the output feature class immediately before the geocode command is run. Any ideas would be greatly appreciated!
Mike LeGower
Code:
program define minimal_example, rclass version 16.1 syntax using/ /* These are set by the syntax command in the real program, hard-coded here for demonstration */ local locator_path "<path to ArcGIS locators>" local locator "<name of locator>" local address "address" local city "city" local state "state" local zip "zip" /* Simple tranformations of above */ local full_locator "`locator_path'/`locator'" /* This is an argument to the arcpy geocoding function */ local cmd "'Street or Intersection' `address' VISIBLE NONE;'City or Placename' `city' VISIBLE NONE;'State' `state' VISIBLE NONE; 'ZIP Code' `zip' VISIBLE NONE" python: import arcpy python: arcpy.ResetEnvironments() python: arcpy.env.overwriteOutput = True mata { pathsplit(st_local("using"), db_path="", db_name="") st_local("db_path", db_path) st_local("db_name", db_name) } capture confirm new file "`using'" if _rc != 0 { display as text "Deleting existing .gdb..." python: import shutil python: shutil.rmtree("`using'") } pause Just deleted `using' tempfile original input_table output_table local input_table: subinstr local input_table "\" "/", all local output_table: subinstr local output_table "\" "/", all local input_table: subinstr local input_table ".tmp" ".csv", all local output_table: subinstr local output_table ".tmp" ".csv", all qui save `original', replace keep `address' `city' `state' `zip' duplicates drop `address' `city' `state' `zip', force export delimited `address' `city' `state' `zip' using `input_table', quote replace local mrg `address' `city' `state' `zip' /* These are extraneous variables produced by the geocode */ local drp "objectid match_type side-langcode distance displayx-arc_zip" display as text "Exporting data to arcpy..." python: arcpy.CreateFileGDB_management("`db_path'", "`db_name'") python: arcpy.env.workspace = "`using'" python: arcpy.CopyRows_management("`input_table'", "geocode_input") display as text "Geocoding addresses..." /* Here you can check that the .gdb just created does not contain "geocode_output" python: import arcpy python: arcpy.ListFeatureClasses() should return an empty list */ pause About to geocode python: arcpy.GeocodeAddresses_geocoding("geocode_input", "`full_locator'", "`cmd'", "geocode_result") /* ERROR HERE: "geocode_output" already exists */ python: arpy.CopyRows_management("`geocode_result'", "`output_table'") display as text "Importing to Stata..." import delimited using `output_table', clear stringcols(_all) drop `drp' /* arcpy can add suffixes to the original columns; rename back here for merge */ cap rename `address' `address' cap rename `city' `city' cap rename `state' `state' cap rename `zip' `zip' merge 1:m `mrg' using `original', nogen qui destring x y, replace force qui replace x = . if status != "M" qui replace y = . if status != "M" end /* Here we need to make sure the right version of python is initialized */ version 16.1 qui python query if r(initialized)==0 { qui python set exec "<path to 64bit ArcGIS python install>" } else if r(initialized) == 1 & r(execpath) != "<path to 64bit ArcGIS python install>" { display as err "python initialized with wrong version; please restart Stata" exit 7101 }
Comment