def geocode(TBL_NAME, id): TEMP_TBL_NAME = transferRecordToStandaloneTable(TBL_NAME, id) FEATURESET_NAME = TBL_NAME + "Geocode" ap.GeocodeAddresses_geocoding( TEMP_TBL_NAME, 'https://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer', 'Address Address', FEATURESET_NAME, 'STATIC', 'US', 'ADDRESS_LOCATION') #ap.AddXY_management(FEATURESET_NAME) #Going to add the new point to a hard-coded feature set for now searchCursor = ap.SearchCursor(FEATURESET_NAME) row = searchCursor.next() serialNo = row.getValue("USER_SerialNo") shape = row.getValue("SHAPE") #x, y = row.getValue("DisplayX"), row.getValue("DisplayY") #spatialRef = row.getValue("SHAPE").spatialReference #pointLocation = ap.Point(x,y) #pointGeo = ap.PointGeometry(pointLocation) #pointGeo = pointGeo.projectAs(spatialRef) del searchCursor del row #Copy the record into feature class pathToFC = ap.Describe("License").catalogPath ap.Append_management( TEMP_TBL_NAME, pathToFC, "NO_TEST" ) #NO_TEST is loose schema enforcement; will drop non-matching fields from input updateCursor = ap.UpdateCursor("License", f"SERIALNO = '{serialNo}'") row = updateCursor.next() row.setValue("SHAPE", shape) updateCursor.updateRow(row) ap.Delete_management(ap.Describe(TEMP_TBL_NAME).catalogPath) ap.Delete_management(ap.Describe(FEATURESET_NAME).catalogPath)
def geocodeAddresses(table): global config folder = rootDir + "BAK" conn_file_name = "arcgis_online_batch_geocoding.ags" conn_file = os.path.join(folder, conn_file_name) server_url = "https://geocode.arcgis.com/arcgis/services" username = config["username"] password = config["password"] #if not arcpy.Exists(stagingGDB): arcpy.mapping.CreateGISServerConnectionFile("USE_GIS_SERVICES", folder, conn_file_name, server_url, "ARCGIS_SERVER", username=username, password=password) #Perform batch geocoding address_locator = os.path.join(conn_file, "World.GeocodeServer") arcpy.GeocodeAddresses_geocoding( table, address_locator, "SingleLine city VISIBLE NONE", publishGDB + "/" + "GeocodedCityLeaderboard", "STATIC")
def _geocode(self, table, out_name): address_locator = self.address_locator address_fields = "Street street_1; City city; State state; Zip zip" out_feature_class = os.path.join(self.gdb_path, out_name) msg = '\nGeocoding table: %s\nOutput name: %s\nOutput Path: %s\nLocator: %s\nAddress Fields: %s' msg = msg % (table, out_name, out_feature_class, address_locator, address_fields) print(msg) arcpy.GeocodeAddresses_geocoding(table, address_locator, address_fields, out_feature_class)
def geocodeCountries(inCSV, inLocator, inField, inCountryBorders): '''This function geocodes country data, completes a spatial join, and outputs the data''' '''to a country shapefile. The input address locator and country borders feature class''' '''are packaged in the workspace environment File Geodatabase "Country_Geocoding.gdb"''' '''The four input arguments for the function consist of the input variables listed''' '''before the function. The user must identify the input csv (input_data) and field''' '''the geocode will be based on (Geocode_Field).''' # Geocoding input data based on single input (country names) Field = "'Single Line Input' %s VISIBLE NONE" % (Geocode_Field) Geocoded_Points = "\\\\Mac\\Home\\Desktop\\ArcGIS II\\Exercise 5\\Country_Geocoding.gdb\\Geocoded_Points" arcpy.GeocodeAddresses_geocoding(inCSV, inLocator, Field, Geocoded_Points, "STATIC") # Spatial Join between country borders and geocoded points Join_Output = "\\\\Mac\\Home\\Desktop\\ArcGIS II\\Exercise 5\\Country_Geocoding.gdb\\Join_Output" arcpy.SpatialJoin_analysis( inCountryBorders, Geocoded_Points, Join_Output, "JOIN_ONE_TO_ONE", "KEEP_ALL", "NAME \"NAME\" true true false 50 Text 0 0 ,First,#,\\\\Mac\\Home\\Desktop\\ArcGIS II\\Exercise 5\\Country_Geocoding.gdb\\Country_Borders,NAME,-1,-1;Shape_Length \"Shape_Length\" false true true 8 Double 0 0 ,First,#,\\\\Mac\\Home\\Desktop\\ArcGIS II\\Exercise 5\\Country_Geocoding.gdb\\Country_Borders,Shape_Length,-1,-1;Shape_Area \"Shape_Area\" false true true 8 Double 0 0 ,First,#,\\\\Mac\\Home\\Desktop\\ArcGIS II\\Exercise 5\\Country_Geocoding.gdb\\Country_Borders,Shape_Area,-1,-1;Status \"Status\" true true false 1 Text 0 0 ,First,#,\\\\Mac\\Home\\Desktop\\ArcGIS II\\Exercise 5\\Country_Geocoding.gdb\\Geocoded_Points,Status,-1,-1;Score \"Score\" true true false 0 Double 2 21 ,First,#,\\\\Mac\\Home\\Desktop\\ArcGIS II\\Exercise 5\\Country_Geocoding.gdb\\Geocoded_Points,Score,-1,-1;Match_type \"Match_type\" true true false 2 Text 0 0 ,First,#,\\\\Mac\\Home\\Desktop\\ArcGIS II\\Exercise 5\\Country_Geocoding.gdb\\Geocoded_Points,Match_type,-1,-1;Match_addr \"Match_addr\" true true false 120 Text 0 0 ,First,#,\\\\Mac\\Home\\Desktop\\ArcGIS II\\Exercise 5\\Country_Geocoding.gdb\\Geocoded_Points,Match_addr,-1,-1;X \"X\" true true false 0 Double 6 25 ,First,#,\\\\Mac\\Home\\Desktop\\ArcGIS II\\Exercise 5\\Country_Geocoding.gdb\\Geocoded_Points,X,-1,-1;Y \"Y\" true true false 0 Double 6 25 ,First,#,\\\\Mac\\Home\\Desktop\\ArcGIS II\\Exercise 5\\Country_Geocoding.gdb\\Geocoded_Points,Y,-1,-1;Xmin \"Xmin\" true true false 0 Double 6 25 ,First,#,\\\\Mac\\Home\\Desktop\\ArcGIS II\\Exercise 5\\Country_Geocoding.gdb\\Geocoded_Points,Xmin,-1,-1;Xmax \"Xmax\" true true false 0 Double 6 25 ,First,#,\\\\Mac\\Home\\Desktop\\ArcGIS II\\Exercise 5\\Country_Geocoding.gdb\\Geocoded_Points,Xmax,-1,-1;Ymin \"Ymin\" true true false 0 Double 6 25 ,First,#,\\\\Mac\\Home\\Desktop\\ArcGIS II\\Exercise 5\\Country_Geocoding.gdb\\Geocoded_Points,Ymin,-1,-1;Ymax \"Ymax\" true true false 0 Double 6 25 ,First,#,\\\\Mac\\Home\\Desktop\\ArcGIS II\\Exercise 5\\Country_Geocoding.gdb\\Geocoded_Points,Ymax,-1,-1;Addr_type \"Addr_type\" true true false 20 Text 0 0 ,First,#,\\\\Mac\\Home\\Desktop\\ArcGIS II\\Exercise 5\\Country_Geocoding.gdb\\Geocoded_Points,Addr_type,-1,-1;ARC_Single_Line_Input \"Key\" true true false 100 Text 0 0 ,First,#,\\\\Mac\\Home\\Desktop\\ArcGIS II\\Exercise 5\\Country_Geocoding.gdb\\Geocoded_Points,ARC_Single_Line_Input,-1,-1;Country \"Country\" true true false 8000 Text 0 0 ,First,#,\\\\Mac\\Home\\Desktop\\ArcGIS II\\Exercise 5\\Country_Geocoding.gdb\\Geocoded_Points,Country,-1,-1;Value \"Value\" true true false 8 Double 0 0 ,First,#,\\\\Mac\\Home\\Desktop\\ArcGIS II\\Exercise 5\\Country_Geocoding.gdb\\Geocoded_Points,Value,-1,-1", "INTERSECT", "", "")
def geocode_ffe_points_with_address_locator(not_found_path, feature_class_path): address_locator = egh_public + r"\ARCMAP_ADMIN.Streets_Geocoding_pdx_no_zone" arcpy.CalculateField_management(not_found_path, "City", "'Portland'", "PYTHON") arcpy.AlterField_management(not_found_path, "Address", "original_address") arcpy.AlterField_management(not_found_path, "conditioned_address", "Address") arcpy.GeocodeAddresses_geocoding(not_found_path, address_locator, "", feature_class_path) arcpy.AlterField_management(feature_class_path, "Address", "conditioned_address") arcpy.AlterField_management(feature_class_path, "original_address", "Address") add_text_field_to_feature_class(feature_class_path,'GeocodingNotes', 15) with arcpy.da.UpdateCursor(feature_class_path, ['GeocodingNotes', 'Status']) as cursor: for row in cursor: if row[1] =='U': row[0] = "UNMATCHED" else: row[0] = "Address Locater" cursor.updateRow(row) drop_fields = ["Status", "Score", "Match_addr", "Pct_along", "Side", "Ref_ID", "X", "Y", "Addr_type", "ARC_Street", "ARC_City", "ARC_ZIP"] if arcpy.TestSchemaLock(feature_class_path): arcpy.DeleteField_management(feature_class_path, drop_fields) else: pass
def geocode(): # Local variables: AddressLocator_Street_Centerlines = "I:\\GIS\\OASIS\\AddressLocators\\AddressLocator_Street_Centerlines" transformed_xlsx = "I:\\GIS\\OASIS\\Geocoder\\transformed.xlsx" transfomed = "I:\\GIS\\OASIS\\Geocoder\\geocoder.gdb\\transfomed" AddressLocator_Master_Address_Database = "I:\\GIS\\OASIS\\AddressLocators\\AddressLocator_Master_Address_Database" geocoded_addresses = "I:\\GIS\\OASIS\\Geocoder\\geocoder.gdb\\geocoded_addresses" geocoder_gdb = "I:\\GIS\\OASIS\\Geocoder\\geocoder.gdb" geocoded_master_successful = "geocoded_master_successful" geocoded_master_successful__2_ = geocoded_master_successful geocoded_master_successful__4_ = geocoded_master_successful__2_ geocoder_gdb__2_ = "I:\\GIS\\OASIS\\Geocoder\\geocoder.gdb" geocoded_addresses_failed = "geocoded_addresses_failed" geocoder_gdb__3_ = "I:\\GIS\\OASIS\\Geocoder\\geocoder.gdb" failed_table = "I:\\GIS\\OASIS\\Geocoder\\geocoder.gdb\\failed_table" failed_table__2_ = failed_table AddressLocator_Street_Centerlines__2_ = "I:\\GIS\\OASIS\\AddressLocators\\AddressLocator_Street_Centerlines" geocoded_street_centerlines = "I:\\GIS\\OASIS\\Geocoder\\geocoder.gdb\\geocoded_street_centerlines" geocoder_gdb__4_ = "I:\\GIS\\OASIS\\Geocoder\\geocoder.gdb" geocoded_street_centerlines_successful = "geocoded_street_centerlines_successful" geocoded_street_centerlines_successful__2_ = geocoded_street_centerlines_successful geocoded_street_centerlines_successful__3_ = geocoded_street_centerlines_successful__2_ final = "I:\\GIS\\OASIS\\Geocoder\\geocoder.gdb\\final" # Process: Excel To Table arcpy.ExcelToTable_conversion(transformed_xlsx, transfomed, "") # Process: Geocode Addresses arcpy.GeocodeAddresses_geocoding(transfomed, AddressLocator_Master_Address_Database, "Key transformed_address VISIBLE NONE", geocoded_addresses, "STATIC", "", "ROUTING_LOCATION") # Process: Make Feature Layer (2) arcpy.MakeFeatureLayer_management( geocoded_addresses, geocoded_master_successful, "Status = 'M' OR Status = 'T'", geocoder_gdb, "ObjectID OBJECTID VISIBLE NONE;Shape Shape VISIBLE NONE;Status Status VISIBLE NONE;Score Score VISIBLE NONE;Match_type Match_type VISIBLE NONE;Match_addr Match_addr VISIBLE NONE;X X VISIBLE NONE;Y Y VISIBLE NONE;Xmin Xmin VISIBLE NONE;Xmax Xmax VISIBLE NONE;Ymin Ymin VISIBLE NONE;Ymax Ymax VISIBLE NONE;Addr_type Addr_type VISIBLE NONE;ARC_SingleKey ARC_SingleKey VISIBLE NONE" ) # Process: Add Field arcpy.AddField_management(geocoded_master_successful, "geocoder", "TEXT", "", "", "20", "", "NULLABLE", "NON_REQUIRED", "") # Process: Calculate Field arcpy.CalculateField_management(geocoded_master_successful__2_, "geocoder", "\"EAS\"", "PYTHON", "") # Process: Make Feature Layer arcpy.MakeFeatureLayer_management( geocoded_addresses, geocoded_addresses_failed, "Status = 'U'", geocoder_gdb__2_, "ObjectID OBJECTID VISIBLE NONE;Shape Shape VISIBLE NONE;Status Status VISIBLE NONE;Score Score VISIBLE NONE;Match_type Match_type VISIBLE NONE;Match_addr Match_addr VISIBLE NONE;X X VISIBLE NONE;Y Y VISIBLE NONE;Xmin Xmin VISIBLE NONE;Xmax Xmax VISIBLE NONE;Ymin Ymin VISIBLE NONE;Ymax Ymax VISIBLE NONE;Addr_type Addr_type VISIBLE NONE;ARC_SingleKey ARC_SingleKey VISIBLE NONE" ) # Process: Table to Table arcpy.TableToTable_conversion( geocoded_addresses_failed, geocoder_gdb__3_, "failed_table", "", "Status \"Status\" true true false 1 Text 0 0 ,First,#,I:\\GIS\\OASIS\\Geocoder\\geocoder.gdb\\geocoded_addresses,Status,-1,-1;Score \"Score\" true true false 8 Double 0 0 ,First,#,I:\\GIS\\OASIS\\Geocoder\\geocoder.gdb\\geocoded_addresses,Score,-1,-1;Match_type \"Match_type\" true true false 2 Text 0 0 ,First,#,I:\\GIS\\OASIS\\Geocoder\\geocoder.gdb\\geocoded_addresses,Match_type,-1,-1;Match_addr \"Match_addr\" true true false 120 Text 0 0 ,First,#,I:\\GIS\\OASIS\\Geocoder\\geocoder.gdb\\geocoded_addresses,Match_addr,-1,-1;X \"X\" true true false 8 Double 0 0 ,First,#,I:\\GIS\\OASIS\\Geocoder\\geocoder.gdb\\geocoded_addresses,X,-1,-1;Y \"Y\" true true false 8 Double 0 0 ,First,#,I:\\GIS\\OASIS\\Geocoder\\geocoder.gdb\\geocoded_addresses,Y,-1,-1;Xmin \"Xmin\" true true false 8 Double 0 0 ,First,#,I:\\GIS\\OASIS\\Geocoder\\geocoder.gdb\\geocoded_addresses,Xmin,-1,-1;Xmax \"Xmax\" true true false 8 Double 0 0 ,First,#,I:\\GIS\\OASIS\\Geocoder\\geocoder.gdb\\geocoded_addresses,Xmax,-1,-1;Ymin \"Ymin\" true true false 8 Double 0 0 ,First,#,I:\\GIS\\OASIS\\Geocoder\\geocoder.gdb\\geocoded_addresses,Ymin,-1,-1;Ymax \"Ymax\" true true false 8 Double 0 0 ,First,#,I:\\GIS\\OASIS\\Geocoder\\geocoder.gdb\\geocoded_addresses,Ymax,-1,-1;Addr_type \"Addr_type\" true true false 20 Text 0 0 ,First,#,I:\\GIS\\OASIS\\Geocoder\\geocoder.gdb\\geocoded_addresses,Addr_type,-1,-1;ARC_Single_Line_Input \"Key\" true true false 100 Text 0 0 ,First,#,I:\\GIS\\OASIS\\Geocoder\\geocoder.gdb\\geocoded_addresses,ARC_Single_Line_Input,-1,-1;ST_NAME \"ST_NAME\" true true false 255 Text 0 0 ,First,#,I:\\GIS\\OASIS\\Geocoder\\geocoder.gdb\\geocoded_addresses,ST_NAME,-1,-1;EXT \"EXT\" true true false 255 Text 0 0 ,First,#,I:\\GIS\\OASIS\\Geocoder\\geocoder.gdb\\geocoded_addresses,EXT,-1,-1;NUM \"NUM\" true true false 4 Long 0 0 ,First,#,I:\\GIS\\OASIS\\Geocoder\\geocoder.gdb\\geocoded_addresses,NUM,-1,-1;LETTER \"LETTER\" true true false 255 Text 0 0 ,First,#,I:\\GIS\\OASIS\\Geocoder\\geocoder.gdb\\geocoded_addresses,LETTER,-1,-1;UNIT \"UNIT\" true true false 255 Text 0 0 ,First,#,I:\\GIS\\OASIS\\Geocoder\\geocoder.gdb\\geocoded_addresses,UNIT,-1,-1;Address \"Address\" true true false 255 Text 0 0 ,First,#,I:\\GIS\\OASIS\\Geocoder\\geocoder.gdb\\geocoded_addresses,Address,-1,-1;DBA \"DBA\" true true false 255 Text 0 0 ,First,#,I:\\GIS\\OASIS\\Geocoder\\geocoder.gdb\\geocoded_addresses,DBA,-1,-1;OWNER \"OWNER\" true true false 255 Text 0 0 ,First,#,I:\\GIS\\OASIS\\Geocoder\\geocoder.gdb\\geocoded_addresses,OWNER,-1,-1;ZIP \"ZIP\" true true false 4 Long 0 0 ,First,#,I:\\GIS\\OASIS\\Geocoder\\geocoder.gdb\\geocoded_addresses,ZIP,-1,-1;LONGITUDE \"LONGITUDE\" true true false 8 Double 0 0 ,First,#,I:\\GIS\\OASIS\\Geocoder\\geocoder.gdb\\geocoded_addresses,LONGITUDE,-1,-1;LATITUDE \"LATITUDE\" true true false 8 Double 0 0 ,First,#,I:\\GIS\\OASIS\\Geocoder\\geocoder.gdb\\geocoded_addresses,LATITUDE,-1,-1;DISTRICT \"DISTRICT\" true true false 4 Long 0 0 ,First,#,I:\\GIS\\OASIS\\Geocoder\\geocoder.gdb\\geocoded_addresses,DISTRICT,-1,-1;LOC_ID_NO \"LOC_ID_NO\" true true false 4 Long 0 0 ,First,#,I:\\GIS\\OASIS\\Geocoder\\geocoder.gdb\\geocoded_addresses,LOC_ID_NO,-1,-1;CERT_NO \"CERT_NO\" true true false 4 Long 0 0 ,First,#,I:\\GIS\\OASIS\\Geocoder\\geocoder.gdb\\geocoded_addresses,CERT_NO,-1,-1;H31_ACCOUNT_NO \"H31_ACCOUNT_NO\" true true false 255 Text 0 0 ,First,#,I:\\GIS\\OASIS\\Geocoder\\geocoder.gdb\\geocoded_addresses,H31_ACCOUNT_NO,-1,-1;PERMIT_NO \"PERMIT NO\" true true false 255 Text 0 0 ,First,#,I:\\GIS\\OASIS\\Geocoder\\geocoder.gdb\\geocoded_addresses,PERMIT_NO,-1,-1;BOE_NO \"BOE_NO\" true true false 4 Long 0 0 ,First,#,I:\\GIS\\OASIS\\Geocoder\\geocoder.gdb\\geocoded_addresses,BOE_NO,-1,-1;DATE_ISSUED \"DATE_ISSUED\" true true false 8 Date 0 0 ,First,#,I:\\GIS\\OASIS\\Geocoder\\geocoder.gdb\\geocoded_addresses,DATE_ISSUED,-1,-1;LAST_ROUTINE \"LAST_ROUTINE\" true true false 8 Date 0 0 ,First,#,I:\\GIS\\OASIS\\Geocoder\\geocoder.gdb\\geocoded_addresses,LAST_ROUTINE,-1,-1;transformed_address \"transformed_address\" true true false 255 Text 0 0 ,First,#,I:\\GIS\\OASIS\\Geocoder\\geocoder.gdb\\geocoded_addresses,transformed_address,-1,-1;geocoder \"geocoder\" true true false 20 Text 0 0 ,First,#,I:\\GIS\\OASIS\\Geocoder\\geocoder.gdb\\geocoded_addresses,geocoder,-1,-1;ARC_SingleKey \"Key\" true true false 100 Text 0 0 ,First,#,I:\\GIS\\OASIS\\Geocoder\\geocoder.gdb\\geocoded_addresses,ARC_SingleKey,-1,-1", "") # Process: Delete Field arcpy.DeleteField_management(failed_table, "ARC_Single_Line_Input") # Process: Geocode Addresses (2) arcpy.GeocodeAddresses_geocoding( failed_table__2_, AddressLocator_Street_Centerlines__2_, "'Full Address' transformed_address VISIBLE NONE", geocoded_street_centerlines, "STATIC", "", "") # Process: Make Feature Layer (3) arcpy.MakeFeatureLayer_management( geocoded_street_centerlines, geocoded_street_centerlines_successful, "", geocoder_gdb__4_, "ObjectID OBJECTID VISIBLE NONE;Shape Shape VISIBLE NONE;Status Status VISIBLE NONE;Score Score VISIBLE NONE;Match_type Match_type VISIBLE NONE;Match_addr Match_addr VISIBLE NONE;Side Side VISIBLE NONE;Ref_ID Ref_ID VISIBLE NONE;User_fld User_fld VISIBLE NONE;Addr_type Addr_type VISIBLE NONE;ARC_Single_Line_Input ARC_Single_Line_Input VISIBLE NONE;Status_1 Status_1 VISIBLE NONE;Score_1 Score_1 VISIBLE NONE;Match_type_1 Match_type_1 VISIBLE NONE;Match_addr_1 Match_addr_1 VISIBLE NONE;X X VISIBLE NONE;Y Y VISIBLE NONE;Xmin Xmin VISIBLE NONE;Xmax Xmax VISIBLE NONE;Ymin Ymin VISIBLE NONE;Ymax Ymax VISIBLE NONE;Addr_type_1 Addr_type_1 VISIBLE NONE;ST_NAME ST_NAME VISIBLE NONE;EXT EXT VISIBLE NONE;NUM NUM VISIBLE NONE;LETTER LETTER VISIBLE NONE;UNIT UNIT VISIBLE NONE;Address Address VISIBLE NONE;DBA DBA VISIBLE NONE;OWNER OWNER VISIBLE NONE;ZIP ZIP VISIBLE NONE;LONGITUDE LONGITUDE VISIBLE NONE;LATITUDE LATITUDE VISIBLE NONE;DISTRICT DISTRICT VISIBLE NONE;LOC_ID_NO LOC_ID_NO VISIBLE NONE;CERT_NO CERT_NO VISIBLE NONE;H31_ACCOUNT_NO H31_ACCOUNT_NO VISIBLE NONE;PERMIT_NO PERMIT_NO VISIBLE NONE;BOE_NO BOE_NO VISIBLE NONE;DATE_ISSUED DATE_ISSUED VISIBLE NONE;LAST_ROUTINE LAST_ROUTINE VISIBLE NONE;transformed_address transformed_address VISIBLE NONE;geocoder geocoder VISIBLE NONE;ARC_SingleKey ARC_SingleKey VISIBLE NONE" ) # Process: Add Field (2) arcpy.AddField_management(geocoded_street_centerlines_successful, "geocoder", "TEXT", "", "", "", "", "NULLABLE", "NON_REQUIRED", "") # Process: Calculate Field (2) arcpy.CalculateField_management( geocoded_street_centerlines_successful__2_, "geocoder", "classifyGeocoder(!Status!)", "PYTHON", "def classifyGeocoder(Status):\\n if Status == \"M\" or Status == \"T\":\\n return \"SC\"\\n else:\\n return \"U\"" ) # Process: Merge arcpy.Merge_management( "geocoded_master_successful;geocoded_street_centerlines_successful", final, "Status \"Status\" true true false 1 Text 0 0 ,First,#,geocoded_master_successful,Status,-1,-1,geocoded_street_centerlines_successful,Status,-1,-1;Score \"Score\" true true false 8 Double 0 0 ,First,#,geocoded_master_successful,Score,-1,-1,geocoded_street_centerlines_successful,Score,-1,-1;Match_type \"Match_type\" true true false 2 Text 0 0 ,First,#,geocoded_master_successful,Match_type,-1,-1,geocoded_street_centerlines_successful,Match_type,-1,-1;Match_addr \"Match_addr\" true true false 120 Text 0 0 ,First,#,geocoded_master_successful,Match_addr,-1,-1,geocoded_street_centerlines_successful,Match_addr,-1,-1;X \"X\" true true false 8 Double 0 0 ,First,#,geocoded_master_successful,X,-1,-1,geocoded_street_centerlines_successful,X,-1,-1;Y \"Y\" true true false 8 Double 0 0 ,First,#,geocoded_master_successful,Y,-1,-1,geocoded_street_centerlines_successful,Y,-1,-1;Xmin \"Xmin\" true true false 8 Double 0 0 ,First,#,geocoded_master_successful,Xmin,-1,-1,geocoded_street_centerlines_successful,Xmin,-1,-1;Xmax \"Xmax\" true true false 8 Double 0 0 ,First,#,geocoded_master_successful,Xmax,-1,-1,geocoded_street_centerlines_successful,Xmax,-1,-1;Ymin \"Ymin\" true true false 8 Double 0 0 ,First,#,geocoded_master_successful,Ymin,-1,-1,geocoded_street_centerlines_successful,Ymin,-1,-1;Ymax \"Ymax\" true true false 8 Double 0 0 ,First,#,geocoded_master_successful,Ymax,-1,-1,geocoded_street_centerlines_successful,Ymax,-1,-1;Addr_type \"Addr_type\" true true false 20 Text 0 0 ,First,#,geocoded_master_successful,Addr_type,-1,-1,geocoded_street_centerlines_successful,Addr_type,-1,-1;ARC_Single_Line_Input \"Key\" true true false 100 Text 0 0 ,First,#,geocoded_master_successful,ARC_Single_Line_Input,-1,-1,geocoded_street_centerlines_successful,ARC_Single_Line_Input,-1,-1;ST_NAME \"ST_NAME\" true true false 255 Text 0 0 ,First,#,geocoded_master_successful,ST_NAME,-1,-1,geocoded_street_centerlines_successful,ST_NAME,-1,-1;EXT \"EXT\" true true false 255 Text 0 0 ,First,#,geocoded_master_successful,EXT,-1,-1,geocoded_street_centerlines_successful,EXT,-1,-1;NUM \"NUM\" true true false 4 Long 0 0 ,First,#,geocoded_master_successful,NUM,-1,-1,geocoded_street_centerlines_successful,NUM,-1,-1;LETTER \"LETTER\" true true false 255 Text 0 0 ,First,#,geocoded_master_successful,LETTER,-1,-1,geocoded_street_centerlines_successful,LETTER,-1,-1;UNIT \"UNIT\" true true false 255 Text 0 0 ,First,#,geocoded_master_successful,UNIT,-1,-1,geocoded_street_centerlines_successful,UNIT,-1,-1;Address \"Address\" true true false 255 Text 0 0 ,First,#,geocoded_master_successful,Address,-1,-1,geocoded_street_centerlines_successful,Address,-1,-1;DBA \"DBA\" true true false 255 Text 0 0 ,First,#,geocoded_master_successful,DBA,-1,-1,geocoded_street_centerlines_successful,DBA,-1,-1;OWNER \"OWNER\" true true false 255 Text 0 0 ,First,#,geocoded_master_successful,OWNER,-1,-1,geocoded_street_centerlines_successful,OWNER,-1,-1;ZIP \"ZIP\" true true false 4 Long 0 0 ,First,#,geocoded_master_successful,ZIP,-1,-1,geocoded_street_centerlines_successful,ZIP,-1,-1;LONGITUDE \"LONGITUDE\" true true false 8 Double 0 0 ,First,#,geocoded_master_successful,LONGITUDE,-1,-1,geocoded_street_centerlines_successful,LONGITUDE,-1,-1;LATITUDE \"LATITUDE\" true true false 8 Double 0 0 ,First,#,geocoded_master_successful,LATITUDE,-1,-1,geocoded_street_centerlines_successful,LATITUDE,-1,-1;DISTRICT \"DISTRICT\" true true false 4 Long 0 0 ,First,#,geocoded_master_successful,DISTRICT,-1,-1,geocoded_street_centerlines_successful,DISTRICT,-1,-1;LOC_ID_NO \"LOC_ID_NO\" true true false 4 Long 0 0 ,First,#,geocoded_master_successful,LOC_ID_NO,-1,-1,geocoded_street_centerlines_successful,LOC_ID_NO,-1,-1;CERT_NO \"CERT_NO\" true true false 4 Long 0 0 ,First,#,geocoded_master_successful,CERT_NO,-1,-1,geocoded_street_centerlines_successful,CERT_NO,-1,-1;H31_ACCOUNT_NO \"H31_ACCOUNT_NO\" true true false 255 Text 0 0 ,First,#,geocoded_master_successful,H31_ACCOUNT_NO,-1,-1,geocoded_street_centerlines_successful,H31_ACCOUNT_NO,-1,-1;PERMIT_NO \"PERMIT NO\" true true false 255 Text 0 0 ,First,#,geocoded_master_successful,PERMIT_NO,-1,-1,geocoded_street_centerlines_successful,PERMIT_NO,-1,-1;BOE_NO \"BOE_NO\" true true false 4 Long 0 0 ,First,#,geocoded_master_successful,BOE_NO,-1,-1,geocoded_street_centerlines_successful,BOE_NO,-1,-1;DATE_ISSUED \"DATE_ISSUED\" true true false 8 Date 0 0 ,First,#,geocoded_master_successful,DATE_ISSUED,-1,-1,geocoded_street_centerlines_successful,DATE_ISSUED,-1,-1;LAST_ROUTINE \"LAST_ROUTINE\" true true false 8 Date 0 0 ,First,#,geocoded_master_successful,LAST_ROUTINE,-1,-1,geocoded_street_centerlines_successful,LAST_ROUTINE,-1,-1;transformed_address \"transformed_address\" true true false 255 Text 0 0 ,First,#,geocoded_master_successful,transformed_address,-1,-1,geocoded_street_centerlines_successful,transformed_address,-1,-1;geocoder \"geocoder\" true true false 20 Text 0 0 ,First,#,geocoded_master_successful,geocoder,-1,-1,geocoded_street_centerlines_successful,geocoder,-1,-1;ARC_SingleKey \"Key\" true true false 100 Text 0 0 ,First,#,geocoded_master_successful,ARC_SingleKey,-1,-1,geocoded_street_centerlines_successful,ARC_SingleKey,-1,-1;Side \"Side\" true true false 1 Text 0 0 ,First,#,geocoded_street_centerlines_successful,Side,-1,-1;Ref_ID \"Ref_ID\" true true false 50 Text 0 0 ,First,#,geocoded_street_centerlines_successful,Ref_ID,-1,-1;User_fld \"User_fld\" true true false 120 Text 0 0 ,First,#,geocoded_street_centerlines_successful,User_fld,-1,-1;Status_1 \"Status\" true true false 1 Text 0 0 ,First,#,geocoded_street_centerlines_successful,Status_1,-1,-1;Score_1 \"Score\" true true false 8 Double 0 0 ,First,#,geocoded_street_centerlines_successful,Score_1,-1,-1;Match_type_1 \"Match_type_1\" true true false 2 Text 0 0 ,First,#,geocoded_street_centerlines_successful,Match_type_1,-1,-1;Match_addr_1 \"Match_addr\" true true false 120 Text 0 0 ,First,#,geocoded_street_centerlines_successful,Match_addr_1,-1,-1;Addr_type_1 \"Addr_type\" true true false 20 Text 0 0 ,First,#,geocoded_street_centerlines_successful,Addr_type_1,-1,-1" ) del geocoded_addresses_failed del geocoded_street_centerlines_successful del geocoded_master_successful
address_fields = "Street address;City city;State state" points30 = dir_path + name + "_Points30_2.shp" pblk_points = dir_path + name + "_1930_Pblk_Points_2.shp" print "Working On: " + name + " Creating Physical Blocks" ##### #Create Physical Blocks# ##### #First Dissolve St_Grid lines arcpy.Dissolve_management(grid, dissolve_grid, "FULLNAME") #Second Split Lines at Intersections arcpy.FeatureToLine_management(dissolve_grid, split_grid) #Third Create Physical Blocks using Feature to Polygon arcpy.FeatureToPolygon_management(split_grid, pblocks) #Finally Add a Physical Block ID expression = "!FID! + 1" arcpy.AddField_management(pblocks, "pblk_id", "LONG", 4, "", "", "", "", "") arcpy.CalculateField_management(pblocks, "pblk_id", expression, "PYTHON_9.3") print "Working On: " + name + " Geocode" ##### #Geocode Points# ##### #Create Address Locator arcpy.CreateAddressLocator_geocoding("US Address - Dual Ranges", reference_data, in_field_map, add_locator, "") #Geocode Points arcpy.GeocodeAddresses_geocoding(addresses, add_locator, address_fields, points30) #Attach Pblk ids to points arcpy.SpatialJoin_analysis(points30, pblocks, pblk_points, "JOIN_ONE_TO_MANY", "KEEP_ALL", "#", "INTERSECT") print "Finished: " + name
#if units are miles... arcpy.SelectLayerByAttribute_management(inputfc, "NEW_SELECTION", "AT_ROAD_KDOT_DIST_UOM = 'M'") arcpy.CalculateField_management(inputfc, offField, "[AT_ROAD_KDOT_DISTANCE]&' miles'", "VB", "#") #arcpy.SelectLayerByAttribute_management(inputfc,"NEW_SELECTION","[AccidentOffset] IS NOT NULL") #clear the selction arcpy.SelectLayerByAttribute_management(inputfc, "CLEAR_SELECTION") #hard set the coordinate system and to operate in WGS84 Lat Long datum wgs = r"\\GISDATA\ArcGIS\GISDATA\Layers\CoordSys\WGS 1984.prj" arcpy.env.outputCoordinateSystem = wgs #do the geocode - address match the intersections arcpy.GeocodeAddresses_geocoding( inputfc, Geocoder, "Street Intersection VISIBLE NONE;City City_name VISIBLE NONE;State State VISIBLE NONE;ZIP <None> VISIBLE NONE", GCContainer + "//" + GCresult, "STATIC") #This is the first result - if the At road offset is null, this is the final location. If the at road offset is not null, then continue... #this is a good time to rematch the addresses that tied or missed, or scored below an acceptable threshold. This is a manual process. #depending on how we want this process to run, I might need to add some automation code or a subroutine here #arcpy.RematchAddresses_geocoding() inlyr = GCresult + "Buffer" #Doing this buffer gets our offset to a common datum - units are now in DD arcpy.Buffer_analysis(GCresult, inlyr, "AccidentOffset", "FULL", "ROUND", "NONE", "#") #add the XY fields for accidents that need to be offset from the intersection arcpy.AddField_management(inlyr, "Accident_X", "DOUBLE", "#", "#", "#", "#", "NULLABLE", "NON_REQUIRED", "#") arcpy.AddField_management(inlyr, "Accident_Y", "DOUBLE", "#", "#", "#", "#",
### Basic code for geocoding within ArcMap (for replicability - doesn't produce report) import arcpy #Set workspace location arcpy.env.workspace = "C:/Users/aramille/Documents/ArcGIS/Geocoding" #Set geocoding variables address_table = "addresses_tt_editExcel.csv" address_locator = "USA" address_fields = "Address addr1plain" geocode_result = "geocode_result.shp" #Run geocoding arcpy.GeocodeAddresses_geocoding(address_table, address_locator, address_fields, geocode_result)
cursor = arcpy.da.InsertCursor(tblMGRS, ("ObjectID", "MGRS")) for x in xrange(0, 1): cursor.insertRow((x, strMGRS)) # call address locator arcpy.AddMessage("call address locator") time.sleep(.06) arcpy.AddMessage("\n") adlMGRS = "Address Locators\MGRS" # geocode table to shpMgrsPoint.shp arcpy.AddMessage("geocode table to pt_imMgrsPoint.shp") time.sleep(.06) arcpy.AddMessage("\n") geocode_out = str(os.path.join("in_memory", "shpMgrsPoint.shp")) arcpy.GeocodeAddresses_geocoding(tblMGRS, adlMGRS, "MGRS MGRS VISIBLE NONE", geocode_out) # create buffer on geocode result arcpy.AddMessage("create buffer") time.sleep(.06) arcpy.AddMessage("\n") arcpy.Buffer_analysis(geocode_out, "fcBuf", strBuffDistance + " Kilometers") # X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X-X # Calculating pie segments from input. Takes the circle geometry, creates a "cutting line" based on the bearing points and centroid, then cuts the circle geometry, returning the resulting pie segment in the 'finalpies' list. # Source credited to Alex Tereshenkov, "https://gis.stackexchange.com/users/14435/alex-tereshenkov" # set pie parameters arcpy.AddMessage("set pie parameters") time.sleep(.06) arcpy.AddMessage("\n")
'Side' <None> VISIBLE NONE;'Prefix Direction' <None> VISIBLE NONE; 'Prefix Type' <None> VISIBLE NONE;'*Street Name' Loc_new VISIBLE NONE; 'Suffix Type' <None> VISIBLE NONE;'Suffix Direction' <None> VISIBLE NONE; 'City or Place' CITY VISIBLE NONE;'ZIP Code' ZIP VISIBLE NONE; 'State' <None> VISIBLE NONE;'Street ID' <None> VISIBLE NONE; 'Display X' <None> VISIBLE NONE;'Display Y' <None> VISIBLE NONE; 'Min X value for extent' <None> VISIBLE NONE;'Max X value for extent' <None> VISIBLE NONE; 'Min Y value for extent' <None> VISIBLE NONE;'Max Y value for extent' <None> VISIBLE NONE; 'Additional Field' <None> VISIBLE NONE;'Altname JoinID' <None> VISIBLE NONE""" print "Working on Locator" #arcpy.CreateAddressLocator_geocoding(in_address_locator_style="US Address - Single House", in_reference_data=Orig_Parcel_Table, in_field_map=field_map, out_address_locator=add_locator) #Geocode Addresses to Parcel Data print "Working on Geocoding" arcpy.GeocodeAddresses_geocoding(Addresses, add_locator, address_fields, Stu_Points_int) #Output to DBF for Analysis in R print "Converting to DBF" arcpy.TableToDBASE_conversion([Stu_Points_int], dbf) #Run R Script to Get Unmatched Count print "Running Initial_Geocode.R" script_secondgeo= rpath + "Initial_Geocode.R" process= subprocess.call([script_path, script_secondgeo], shell=True) print "Finished Geocode 1" #Start Your count and count_f values as not equal. The script will update when necessary. The count originates #from the initial geocode. Start iteration at 2 since the initial geocode is considered iteration 1. iteration=2
import arcpy workspace = r"ワークスペースのパス" arcpy.env.workspace = workspace # arcpy を使用してジオプロセシングツールを実行:(https://pro.arcgis.com/ja/pro-app/tool-reference/geocoding/geocode-addresses.htm) in_table = r"CSV ファイルのパス" address_fields = '住所 所在地 VISIBLE NONE' geocode_result = "ジオコーディングで作成されるフィーチャ クラス名" address_locator = "住所テーブルのジオコーディングに使用する住所ロケーター" arcpy.GeocodeAddresses_geocoding(in_table, address_locator, address_fields, geocode_result, 'STATIC')
# Delete the old table. if arcpy.Exists(scratch_table): arcpy.Delete_management(scratch_table) # Create the table and associated view to hold the address arcpy.CreateTable_management(arcpy.env.scratchGDB, "addr_table") arcpy.MakeTableView_management(scratch_table, addr_table_view) # Add the address field and copy the address to the table arcpy.AddField_management(addr_table_view, address_field, "TEXT", 200) with arcpy.da.InsertCursor(addr_table_view, address_field) as ic: ic.insertRow((address, )) # Geocode the address arcpy.GeocodeAddresses_geocoding( addr_table_view, locator, "'Single Line Input' {} VISIBLE NONE".format(address_field), address_point_fc) # Make sure we have a match, translate point to Web Mercator arcpy.MakeFeatureLayer_management(address_point_fc, address_point_layer) with arcpy.da.SearchCursor(address_point_layer, ["Status", "SHAPE@XY", "SHAPE@"]) as point_sc: for row in point_sc: match_info = "Type: {}, Location: {}".format(row[0], str(row[1])) if row[0] not in ['M', 'T']: raise ValueError("Address not found: {}".format(address)) # If it is a valid match, translate to web mercator web_point = row[2].projectAs(arcpy.SpatialReference(3857)) xy = (web_point.centroid.X, web_point.centroid.Y)
def initial_geocode( city_info, geo_path, hn_ranges=['MIN_LFROMA', 'MIN_RFROMA', 'MAX_LTOADD', 'MAX_RTOADD']): city_name, state_abbr, decade = city_info city_name = city_name.replace(' ', '') state_abbr = state_abbr.upper() min_l, max_l, min_r, max_r = hn_ranges # Files add_locator = geo_path + city_name + "_addloc_" + str(decade) addresses = geo_path + city_name + "_" + str(decade) + "_Addresses.csv" address_fields = "Street address; City city; State state" points = geo_path + city_name + "_" + str(decade) + "_Points.shp" reference_data = geo_path + city_name + state_abbr + "_" + str( decade) + "_stgrid_edit_Uns2.shp 'Primary Table'" # Fix addresses for use in geocoding try: arcpy.Delete_management(geo_path + "temp.gdb") except: pass arcpy.CreateFileGDB_management(geo_path, "temp.gdb") df_addresses_csv = pd.read_csv(addresses) df_addresses_dbf = df_addresses_csv.replace(np.nan, '', regex=True) x = np.array(np.rec.fromrecords(df_addresses_dbf.values)) names = df_addresses_dbf.dtypes.index.tolist() x.dtype.names = tuple(names) arcpy.da.NumPyArrayToTable( x, geo_path + "temp.gdb/" + city_name + "_" + str(decade) + "_Addresses") arcpy.env.workspace = geo_path + "temp.gdb" field_map = "'Feature ID' FID VISIBLE NONE; \ '*From Left' %s VISIBLE NONE; \ '*To Left' %s VISIBLE NONE; \ '*From Right' %s VISIBLE NONE; \ '*To Right' %s VISIBLE NONE; \ 'Prefix Direction' <None> VISIBLE NONE; \ 'Prefix Type' <None> VISIBLE NONE; \ '*Street Name' FULLNAME VISIBLE NONE; \ 'Suffix Type' '' VISIBLE NONE; \ 'Suffix Direction' <None> VISIBLE NONE; \ 'Left City or Place' CITY VISIBLE NONE; \ 'Right City or Place' CITY VISIBLE NONE; \ 'Left ZIP Code' <None> VISIBLE NONE; \ 'Right ZIP Code' <None> VISIBLE NONE; \ 'Left State' STATE VISIBLE NONE; \ 'Right State' STATE VISIBLE NONE; \ 'Left Street ID' <None> VISIBLE NONE; \ 'Right Street ID' <None> VISIBLE NONE; \ 'Display X' <None> VISIBLE NONE; \ 'Display Y' <None> VISIBLE NONE; \ 'Min X value for extent' <None> VISIBLE NONE; \ 'Max X value for extent' <None> VISIBLE NONE; \ 'Min Y value for extent' <None> VISIBLE NONE; \ 'Max Y value for extent' <None> VISIBLE NONE; \ 'Left parity' <None> VISIBLE NONE; \ 'Right parity' <None> VISIBLE NONE; \ 'Left Additional Field' <None> VISIBLE NONE; \ 'Right Additional Field' <None> VISIBLE NONE; \ 'Altname JoinID' <None> VISIBLE NONE" % (min_l, max_l, min_r, max_r) #Make sure address locator doesn't already exist - if it does, delete it add_loc_files = [ geo_path + '/' + x for x in os.listdir(geo_path) if x.startswith(city_name + "_addloc") ] for f in add_loc_files: if os.path.isfile(f): os.remove(f) print("The script is executing the 'CreateAddressLocator' tool") #Create Address Locator arcpy.CreateAddressLocator_geocoding( in_address_locator_style="US Address - Dual Ranges", in_reference_data=reference_data, in_field_map=field_map, out_address_locator=add_locator, config_keyword="") #Change side offset to maximize quality of physical block matches locator_fn = geo_path + '/' + city_name + '_addloc_' + str(decade) + '.loc' locator_file = open(locator_fn, 'a') # open for appending locator_file.writelines('SideOffset = 1') locator_file.writelines('SideOffsetUnits = Feet') locator_file.close() print( "The script has finished executing the 'CreateAddressLocator' tool and has begun executing the 'GeocodeAddress' tool" ) #Geocode Points arcpy.GeocodeAddresses_geocoding(in_table=city_name + "_" + str(decade) + "_Addresses", address_locator=add_locator, in_address_fields=address_fields, out_feature_class=points) #Delete temporary.gdb arcpy.Delete_management(geo_path + "temp.gdb/" + city_name + "_" + str(decade) + "_Addresses") arcpy.Delete_management(geo_path + "temp.gdb") print( "The script has finished executing the 'GeocodeAddress' tool and has begun executing the 'SpatialJoin' tool" )
def geocode(geo_path, city_name, add, sg, vm, sg_vm, fl, tl, fr, tr, cal_street, cal_city, cal_state, addfield, al, g_address, g_city, g_state, gr, no_side_offset=True): print( "Step 1 of 3: The 'Geocode' function has started and the program has started to implement the intersect function" ) arcpy.Intersect_analysis(in_features=[sg, vm], out_feature_class=sg_vm, join_attributes="ALL") print( "Step 2 of 3: The program has finished implementing the intersect function and has begun creating the address locator" ) #Make sure address locator doesn't already exist - if it does, delete it add_loc_files = [ geo_path + '\\' + x for x in os.listdir(geo_path) if x.startswith(al.split('/')[-1] + '.') ] for f in add_loc_files: if os.path.isfile(f): os.remove(f) arcpy.CreateAddressLocator_geocoding( in_address_locator_style="US Address - Dual Ranges", in_reference_data=sg_vm, in_field_map="'Feature ID' <None> VISIBLE NONE;\ '*From Left' " + fl + " VISIBLE NONE;\ '*To Left' " + tl + " VISIBLE NONE;\ '*From Right' " + fr + " VISIBLE NONE;\ '*To Right' " + tr + " VISIBLE NONE;\ 'Prefix Direction' <None> VISIBLE NONE;\ 'Prefix Type' <None> VISIBLE NONE;\ '*Street Name' " + cal_street + " VISIBLE NONE;\ 'Suffix Type' <None> VISIBLE NONE;\ 'Suffix Direction' <None> VISIBLE NONE;\ 'Left City or Place' " + cal_city + " VISIBLE NONE;\ 'Right City or Place' " + cal_city + " VISIBLE NONE;\ 'Left ZIP Code' <None> VISIBLE NONE;\ 'Right ZIP Code' <None> VISIBLE NONE;\ 'Left State' " + cal_state + " VISIBLE NONE;\ 'Right State' " + cal_state + " VISIBLE NONE;\ 'Left Street ID' <None> VISIBLE NONE;\ 'Right Street ID' <None> VISIBLE NONE;\ 'Display X' <None> VISIBLE NONE;\ 'Display Y' <None> VISIBLE NONE;\ 'Min X value for extent' <None> VISIBLE NONE;\ 'Max X value for extent' <None> VISIBLE NONE;\ 'Min Y value for extent' <None> VISIBLE NONE;\ 'Max Y value for extent' <None> VISIBLE NONE;\ 'Left parity' <None> VISIBLE NONE;\ 'Right parity' <None> VISIBLE NONE;\ 'Left Additional Field' " + addfield + " VISIBLE NONE;\ 'Right Additional Field' " + addfield + " VISIBLE NONE;\ 'Altname JoinID' <None> VISIBLE NONE", out_address_locator=al) if no_side_offset: locator_fn = al + '.loc' locator_file = open(locator_fn, 'a') # open for appending locator_file.writelines('SideOffset = 0') locator_file.writelines('SideOffsetUnits = Feet') locator_file.close() print( "Step 3 of 3: The program has finished creating the address locator and has started the geocoding process" ) arcpy.GeocodeAddresses_geocoding(in_table=add, address_locator=al, in_address_fields="Street " + g_address + " VISIBLE NONE;\ City " + g_city + " VISIBLE NONE;\ State " + g_state + " VISIBLE NONE", out_feature_class=gr, out_relationship_type="STATIC") print( "The program has finished the geocoding process and 'Geocode' function is complete" )
username=username, password=password) print "Log on to ARCGIS Server Successful" #Build field mappings from the input table input_mappings = { "Address": "well_address", "City": "well_city", "Region": "well_state", "Postal": "zip" } field_mappings = arcpy.FieldInfo() for field in input_mappings: field_mappings.addField(field, input_mappings[field], "VISIBLE", "NONE") print "field mapping built" #Perform batch geocoding address_locator = os.path.join(conn_file, "World.GeocodeServer") arcpy.GeocodeAddresses_geocoding(input_table, address_locator, field_mappings, output_feature_class) print arcpy.GetMessages()
def main(config_file, *args): """ Import the incidents to a feature class, filtering out duplicates if necessary, assign geometry using addresses or XY values, and publish the results usign AGOL or ArcGIS for Server. Output is an updated feature class, processign reports, and optionally a service """ # Current date and time for file names fileNow = dt.strftime(dt.now(), prefix) if isfile(config_file): cfg = ConfigParser.ConfigParser() cfg.read(config_file) else: raise Exception(e1.format("Configuration file", config_file, "")) # Get general configuration values incidents = cfg.get('GENERAL', 'spreadsheet') inc_features = cfg.get('GENERAL', 'incident_features') id_field = cfg.get('GENERAL', 'incident_id') report_date_field = cfg.get('GENERAL', 'report_date_field') reports = cfg.get('GENERAL', 'reports') loc_type = cfg.get('GENERAL', 'loc_type') summary_field = cfg.get('GENERAL', 'summary_field') transform_method = cfg.get('GENERAL', 'transform_method') pub_status = cfg.get('GENERAL', 'pub_status') delete_duplicates = cfg.get('GENERAL', 'delete_duplicates') if delete_duplicates in ('true', 'True', True): delete_duplicates = True if report_date_field == "": raise Exception(e16) if delete_duplicates in ('false', 'False'): delete_duplicates = False # Log file if exists(reports): rptLog = join(reports, "{0}_{1}.log".format(fileNow, log_name)) else: raise Exception(e1.format("Report location", reports, w5)) # Scratch workspace tempgdb = arcpy.env.scratchGDB with open(rptLog, "w") as log: try: # Log file header log.write(l1.format(fileNow)) log.write(l2.format(getpass.getuser())) log.write(l3.format(incidents)) log.write(l4.format(inc_features)) if loc_type == "ADDRESSES": log.write(l5.format(cfg.get('ADDRESSES', 'locator'))) # Validate output feature class geometry type desc = arcpy.Describe(inc_features) if not desc.shapeType == "Point": raise Exception(e6.format(inc_features)) # Identify field names in both fc and csv if arcpy.Exists(incidents): csvfieldnames = [f.name for f in arcpy.ListFields(incidents)] else: raise Exception(e1.format("Spreadsheet", incidents, "")) if arcpy.Exists(inc_features): incfieldnames = [ f.name for f in arcpy.ListFields(inc_features) ] else: raise Exception(e1.format("Feature Class", inc_features, "")) matchfieldnames = [] for name in csvfieldnames: if name in incfieldnames: matchfieldnames.append(name) # If data is to be geocoded if loc_type == "ADDRESSES": # Get geocoding parameters address_field = cfg.get('ADDRESSES', 'address_field') city_field = cfg.get('ADDRESSES', 'city_field') state_field = cfg.get('ADDRESSES', 'state_field') zip_field = cfg.get('ADDRESSES', 'zip_field') locator = cfg.get('ADDRESSES', 'locator') # Geocoding field names reqFields = [address_field, id_field] #, report_date_field] opFields = [ city_field, state_field, zip_field, summary_field, report_date_field ] if locator == "": raise Exception(e13) # Test geolocator fields loc_address_fields = [ loc_address_field, loc_city_field, loc_zip_field, loc_state_field ] for a in loc_address_fields: if not a == "": if not a in all_locator_fields: raise Exception(e14) # If data has coordinate values else: # Get coordinate parameters lg_field = cfg.get('COORDINATES', 'long_field') lt_field = cfg.get('COORDINATES', 'lat_field') coord_system = cfg.get('COORDINATES', 'coord_system') remove_zeros = cfg.get('COORDINATES', 'ignore_zeros') if remove_zeros in ('true', 'True'): remove_zeros = True if remove_zeros in ('false', 'False'): remove_zeros = False # Coordinate field names reqFields = [id_field, lg_field, lt_field] #, report_date_field] opFields = [summary_field, report_date_field] # Validate required field names field_test(incidents, reqFields, csvfieldnames, True) field_test(inc_features, reqFields, incfieldnames, True) # Validate optional field names field_test(incidents, opFields, csvfieldnames) field_test(inc_features, opFields, incfieldnames) # Validate basic publishing parameters if not pub_status == "": # Get general publishing parameters mxd = cfg.get('PUBLISHING', 'mxd') username = cfg.get('PUBLISHING', 'user_name') password = cfg.get('PUBLISHING', 'password') # Test for required inputs if not arcpy.Exists(mxd): raise Exception(e1.format("Map document", mxd, "")) if splitext(mxd)[1] != ".mxd": raise Exception(e3) # Test for required inputs if username == "" or password == "": if pub_status == "ARCGIS_ONLINE": raise Exception(e8) # Get address fields for geocoding if loc_type == "ADDRESSES": addresses = "" loc_fields = [] adr_string = "{0} {1} VISIBLE NONE;" for loc_field in all_locator_fields: if loc_field == loc_address_field: addresses += adr_string.format(loc_field, address_field) loc_fields.append(address_field) elif loc_field == loc_city_field and city_field != "": addresses += adr_string.format(loc_field, city_field) loc_fields.append(city_field) elif loc_field == loc_state_field and state_field != "": addresses += adr_string.format(loc_field, state_field) loc_fields.append(state_field) elif loc_field == loc_zip_field and zip_field != "": addresses += adr_string.format(loc_field, zip_field) loc_fields.append(zip_field) else: addresses += adr_string.format(loc_field, "<None>") # Get coordinate fields else: loc_fields = [lg_field, lt_field] total_records = len(field_vals(incidents, id_field)) messages(m17.format(total_records, incidents), log) if not summary_field == "": SumVals = field_vals(incidents, summary_field) listSumVals = [val for val in SumVals if val != None] if not len(SumVals) == len(listSumVals): print m19.format(len(SumVals) - len(listSumVals)) log.write(m19.format(len(SumVals) - len(listSumVals))) listSumVals.sort() log.write(l10.format(summary_field)) dateCount = 1 i = 0 n = len(listSumVals) while i < n: try: if listSumVals[i] == listSumVals[i + 1]: dateCount += 1 else: log.write(l11.format(listSumVals[i], dateCount)) dateCount = 1 except: log.write(l11.format(listSumVals[i], dateCount)) i += 1 log.write("\n") # Remove duplicate incidents if delete_duplicates: timeNow = dt.strftime(dt.now(), time_format) messages(m13.format(timeNow), log) incidents, req_nulls, countUpdate, countDelete = remove_dups( tempgdb, incidents, inc_features, matchfieldnames, id_field, report_date_field, loc_fields) if not req_nulls == "": req_nulls = "{}\n".format(req_nulls) messages(w3.format(req_nulls), log, 1) if not countUpdate == 0: messages(m14.format(countUpdate, inc_features), log) if countDelete > 0: messages(m15.format(countDelete, inc_features), log) # Create features tempFC = join(tempgdb, "tempDataLE") # Create point features from spreadsheet timeNow = dt.strftime(dt.now(), time_format) messages(m1.format(timeNow), log) if loc_type == "ADDRESSES": timeNow = dt.strftime(dt.now(), time_format) messages(m3.format(timeNow), log) # Geocode the incidents arcpy.GeocodeAddresses_geocoding(incidents, locator, addresses, tempFC, "STATIC") # Initiate geocoding report counts countMatch = 0 countTrueMatch = 0 countUnmatch = 0 # Create geocoding reports rptUnmatch = join(reports, "{0}_{1}.csv".format(fileNow, unmatch_name)) fieldnames = [f.name for f in arcpy.ListFields(tempFC)] # Sort incidents based on match status statusIndex = fieldnames.index(status) locIndex = fieldnames.index(addr_type) # Write incidents that were not well geocoded to file and # delete from temp directory with open(rptUnmatch, "wb") as umatchFile: unmatchwriter = csv.writer(umatchFile) unmatchwriter.writerow(fieldnames) # Delete incidents that were not Matched countUnmatch = sort_records(tempFC, unmatchwriter, statusIndex, match_value, False, True) if not countUnmatch == 0: messages(w6.format(countUnmatch, rptUnmatch), log, 1) # Incidents that were not matched to an acceptable accuracy countMatch = sort_records(tempFC, unmatchwriter, locIndex, addrOK, False, True) if not countMatch == 0: messages(w7.format(countMatch, addrOK, rptUnmatch), log, 1) countTrueMatch = len(field_vals(tempFC, "OBJECTID")) messages(m16.format(countTrueMatch, inc_features), log) else: # Create temporary output storage tempFL = arcpy.MakeXYEventLayer_management( incidents, lg_field, lt_field, "tempLayerLE", coord_system) # Convert the feature layer to a feature class to prevent # field name changes arcpy.CopyFeatures_management(tempFL, tempFC) arcpy.Delete_management(tempFL) timeNow = dt.strftime(dt.now(), time_format) messages(m4.format(timeNow, inc_features), log) # Fields that will be copied from geocode results to final fc copyfieldnames = [] copyfieldnames.extend(matchfieldnames) copyfieldnames.append("SHAPE@XY") # Fields for error reporting errorfieldnames = [] errorfieldnames.extend(matchfieldnames) errorfieldnames.insert(0, errorfield) errorfieldnames += [long_field, lat_field] # Reproject the features sr_input = arcpy.Describe(tempFC).spatialReference sr_output = arcpy.Describe(inc_features).spatialReference if sr_input != sr_output: proj_out = "{}_proj".format(tempFC) arcpy.Project_management(tempFC, proj_out, sr_output, transform_method) tempFC = proj_out # Append geocode results to fc rptNoAppend = join(reports, "{0}_{1}.csv".format(fileNow, noappend_name)) with arcpy.da.SearchCursor(tempFC, copyfieldnames) as csvrows: with arcpy.da.InsertCursor(inc_features, copyfieldnames) as incrows: # Open csv for un-appended records with open(rptNoAppend, "wb") as appendFile: appendwriter = csv.writer(appendFile) appendwriter.writerow(errorfieldnames) # Index of field with incident ID record = errorfieldnames.index(id_field) # Initiate count of successfully appended records countAppend = 0 # List of ids of records not successfully appended errorRecords = [] for csvrow in csvrows: try: if loc_type == "COORDINATES": if remove_zeros: lt_index = copyfieldnames.index( lt_field) lg_index = copyfieldnames.index( lg_field) ltVal = csvrow[lt_index] lgVal = csvrow[lg_index] if ltVal == 0 and lgVal == 0: raise Exception( "invalid_coordinates") # If the row can be appended incrows.insertRow(csvrow) countAppend += 1 except Exception as reason: # e.g. 'The value type is incompatible with the # field type. [INCIDENTDAT]' # Alternatively, the exception # 'invalid_coordinates' raised by the # remove_zeros test above # Get the name of the problem field badfield = reason[0].split(" ")[-1] badfield = badfield.strip(" []") # Append field name to start of record csvrow = list(csvrow) csvrow.insert(0, badfield) # Split the coordinate tuple into X and Y lng, lat = list(csvrow[-1]) csvrow[-1] = lng csvrow.append(lat) csvrow = tuple(csvrow) # Write the record out to csv appendwriter.writerow(csvrow) # Add id and field to issue list errorRecords.append( w4.format(csvrow[record], badfield)) # If issues were reported, print them if len(errorRecords) != 0: messages( w1.format(len(errorRecords), inc_features, rptNoAppend), log, 1) messages(m18.format(countAppend, inc_features), log) del incrows, csvrows # Convert times to UTC if publishing to AGOL if pub_status == "ARCGIS_ONLINE": # Get date fields date_fields = [ f.name for f in arcpy.ListFields(inc_features) if f.type == "Date" and f.name in matchfieldnames ] # Convert from system timezone to UTC convert_to_utc(inc_features, date_fields) # Publish incidents if not pub_status == "": timeNow = dt.strftime(dt.now(), time_format) messages(m5.format(timeNow), log) errors = serviceutils.publish_service(cfg, pub_status, mxd, username, password) # Print analysis errors if errors: raise Exception(e4.format(errors)) # Convert times from UTC to system timezone if pub_status == "ARCGIS_ONLINE": convert_from_utc(inc_features, date_fields) timeNow = dt.strftime(dt.now(), time_format) messages(m8.format(timeNow), log) except arcpy.ExecuteError: print("{}\n{}\n".format(gp_error, arcpy.GetMessages(2))) timeNow = dt.strftime(dt.now(), "{} {}".format(date_format, time_format)) arcpy.AddError("{} {}:\n".format(timeNow, gp_error)) arcpy.AddError("{}\n".format(arcpy.GetMessages(2))) log.write("{} ({}):\n".format(gp_error, timeNow)) log.write("{}\n".format(arcpy.GetMessages(2))) for msg in range(0, arcpy.GetMessageCount()): if arcpy.GetSeverity(msg) == 2: code = arcpy.GetReturnCode(msg) print("Code: {}".format(code)) print("Message: {}".format(arcpy.GetMessage(msg))) except Exception as ex: print("{}: {}\n".format(py_error, ex)) timeNow = dt.strftime(dt.now(), "{}".format(time_format)) arcpy.AddError("{} {}:\n".format(timeNow, py_error)) arcpy.AddError("{}\n".format(ex)) log.write("{} {}:\n".format(timeNow, py_error)) log.write("{}\n".format(ex)) finally: # Clean up try: arcpy.Delete_management(tempgdb) except: pass
output_shape = sys.argv[2] #use this to name what the script returns add_field = sys.argv[ 3] #use these to specify what columns of input data table are used within GeocodeAddresses #handle the components if provided if len(sys.argv) > 4: city_field = sys.argv[4] state_field = sys.argv[5] else: city_field = None state_field = None #for runs where we want to use a single field if (city_field is None and state_field is None): in_add_string = "'Single Line Input' " + add_field + " VISIBLE NONE" #for normal runs else: in_add_string = "Address " + add_field + " VISIBLE NONE;City " + city_field + " VISIBLE NONE;Region " + state_field + " VISIBLE NONE;Postal <None> VISIBLE NONE" #run the geocoding based on the current best-working setup arcpy.GeocodeAddresses_geocoding( in_table=input_filename, address_locator= "R:/Data/GIS/Geocoding/2019 Business Analyst Data/Geocoding Data/USA", in_address_fields=in_add_string, out_feature_class=output_shape, out_relationship_type="STATIC", country="", location_type="ROUTING_LOCATION")
"TMax 90%", "TMin 90%"] def project_path(path): if path == "current": return path return gdb_path + "\\" + path gis_project = arcpy.mp.ArcGISProject(aprx_path) gis_map = gis_project.listMaps()[0] gis_layout = gis_project.listLayouts()[0] table = gis_map.addDataFromPath(csv_url) arcpy.GeocodeAddresses_geocoding(table, geocodeUrl, "SingleLine City VISIBLE NONE", project_path(city_data_layer_name), 'STATIC', 'US', 'ADDRESS_LOCATION', 'City') arcpy.Buffer_analysis(project_path(city_data_layer_name), project_path(city_buffer_layer_name), circle_size, 'FULL', 'ROUND', 'NONE') city_buffer_layer = gis_map.addDataFromPath(project_path(city_buffer_layer_name)) symbol_template_layer = gis_map.listLayers(symbol_layer_template_name)[0] for data_index in range(len(data_ranges)): for date_index in range(len(date_ranges)): data_field = "USER_" + data_ranges[data_index] + "_" + date_ranges[date_index] arcpy.ApplySymbologyFromLayer_management(city_buffer_layer, symbol_template_layer, [["VALUE_FIELD", "USER_Field2", data_field]], 'MAINTAIN') t_text = gis_layout.listElements("TEXT_ELEMENT", "title_text")[0] t_text.text = data_ranges_text[data_index] + " " + date_ranges_text[date_index] gis_layout.exportToPNG(export_path + data_ranges[data_index] + "_" + date_ranges[date_index] + ".png")
) arcpy.TableSelect_analysis( in_table="FACILITIES_TABLE", out_table="FACILITIESw_GEOM", where_clause= "(GIS_LATITUDE IS NOT NULL) AND (GIS_LONGITUDE IS NOT NULL ) AND (Upper(NAME) NOT LIKE '%DO NOT USE%')" ) ############################################################################### # GEOCODE NECESSARY ROWS #address_fields = "Street ConcatAddress;ZIP ZIP" address_fields = "'Street or Intersection' ConcatAddress VISIBLE NONE;'City or Placename' CITY VISIBLE NONE;State STATE VISIBLE NONE;'ZIP Code' ZIP VISIBLE NONE" if arcpy.Exists("GeocodedAddresses"): arcpy.Delete_management("GeocodedAddresses") arcpy.GeocodeAddresses_geocoding(in_table="nullGeoRows", address_locator=locator, in_address_fields=address_fields, out_feature_class="GeocodedAddresses") # RECLASS '0' LAT AND LON TO NONE def GEO0(inputfc): with arcpy.da.UpdateCursor(inputfc, field_names=["Y", "X"]) as gcursor: for arow in gcursor: if arow[0] == 0: arow[0] = None arow[1] = None gcursor.updateRow(arow) del gcursor del arow GEO0("GeocodedAddresses")
def geocode(): try: # Local variables: transformed_xlsx = "I:\\GIS\\OASIS\\Geocoder\\transformed.xlsx" transfomed = "I:\\GIS\\OASIS\\Geocoder\\geocoder.gdb\\transfomed" AddressLocator_Master_Address_Database = "I:\\GIS\\OASIS\\AddressLocators\\AddressLocator_Master_Address_Database" geocoded_addresses = "I:\\GIS\\OASIS\\Geocoder\\geocoder.gdb\\geocoded_addresses" geocoder_gdb = "I:\\GIS\\OASIS\\Geocoder\\geocoder.gdb" geocoded_addresses_failed = "geocoded_addresses_failed" unmatched_xls = "I:\\GIS\\OASIS\\Geocoder\\unmatched.xls" unmatched = "I:\\GIS\\OASIS\\Geocoder\\geocoder.gdb\\unmatched" unmatched__3_ = unmatched AddressLocator_Street_Centerlines__2_ = "I:\\GIS\\OASIS\\AddressLocators\\AddressLocator_Street_Centerlines" geocoded_street_centerlines = "I:\\GIS\\OASIS\\Geocoder\\geocoder.gdb\\geocoded_street_centerlines" geocoded_street_centerlines_successful = "geocoded_street_centerlines_successful" geocoded_street_centerlines_successful__2_ = geocoded_street_centerlines_successful geocoded_street_centerlines_successful__3_ = geocoded_street_centerlines_successful__2_ geocoder_gdb__2_ = "I:\\GIS\\OASIS\\Geocoder\\geocoder.gdb" geocoded_master_successful = "geocoded_master_successful" geocoded_master_successful__2_ = geocoded_master_successful geocoded_master_successful__4_ = geocoded_master_successful__2_ geocoder_eas = "I:\\GIS\\OASIS\\Geocoder\\geocoder.gdb\\geocoder_eas" final = "I:\\GIS\\OASIS\\Geocoder\\geocoder.gdb\\final" # Process: Excel To Table try: arcpy.ExcelToTable_conversion(transformed_xlsx, transfomed) except Exception as e: print(e) # Process: Geocode Addresses try: arcpy.GeocodeAddresses_geocoding(transfomed, AddressLocator_Master_Address_Database, "Key transformed_address VISIBLE NONE", geocoded_addresses, "STATIC", "", "") except Exception as e: print(e) # Process: Make Feature Layer try: arcpy.MakeFeatureLayer_management(geocoded_addresses, geocoded_addresses_failed, "Status = 'U'", geocoder_gdb, "ObjectID OBJECTID VISIBLE NONE;Shape Shape VISIBLE NONE;Status Status VISIBLE NONE;Score Score VISIBLE NONE;Match_type Match_type VISIBLE NONE;Match_addr Match_addr VISIBLE NONE;X X VISIBLE NONE;Y Y VISIBLE NONE;Xmin Xmin VISIBLE NONE;Xmax Xmax VISIBLE NONE;Ymin Ymin VISIBLE NONE;Ymax Ymax VISIBLE NONE;Addr_type Addr_type VISIBLE NONE;ARC_Single_Line_Input ARC_Single_Line_Input VISIBLE NONE") except Exception as e: print(e) # Process: Table To Excel try: arcpy.TableToExcel_conversion(geocoded_addresses_failed, unmatched_xls, "NAME", "CODE") except Exception as e: print(e) # Process: Excel To Table (2) arcpy.ExcelToTable_conversion(unmatched_xls, unmatched, "") # Process: Delete Field arcpy.DeleteField_management(unmatched, "OBJECTID_1;Status;Score;Match_type;Match_addr;X;Y;Xmin;Xmax;Ymin;Ymax;Addr_type;ARC_Single_Line_Input;ARC_SingleKey") # Process: Geocode Addresses (2) arcpy.GeocodeAddresses_geocoding(unmatched__3_, AddressLocator_Street_Centerlines__2_, "'Full Address' transformed_address VISIBLE NONE", geocoded_street_centerlines, "STATIC", "", "") # Process: Make Feature Layer (3) arcpy.MakeFeatureLayer_management(geocoded_street_centerlines, geocoded_street_centerlines_successful, "", "", "ObjectID ObjectID VISIBLE NONE;Shape Shape VISIBLE NONE;Status Status VISIBLE NONE;Score Score VISIBLE NONE;Match_type Match_type VISIBLE NONE;Match_addr Match_addr VISIBLE NONE;Side Side VISIBLE NONE;Ref_ID Ref_ID VISIBLE NONE;User_fld User_fld VISIBLE NONE;Addr_type Addr_type VISIBLE NONE;ARC_Single_Line_Input ARC_Single_Line_Input VISIBLE NONE") # Process: Add Field (2) arcpy.AddField_management(geocoded_street_centerlines_successful, "geocoder", "TEXT", "", "", "", "", "NULLABLE", "NON_REQUIRED", "") # Process: Calculate Field (2) arcpy.CalculateField_management(geocoded_street_centerlines_successful__2_, "geocoder", "classifyGeocoder(!Status!)", "PYTHON", "def classifyGeocoder(Status):\\n if Status == \"M\" or Status == \"T\":\\n return \"SC\"\\n else:\\n return \"U\"") # Process: Make Feature Layer (2) arcpy.MakeFeatureLayer_management(geocoded_addresses, geocoded_master_successful, "Status = 'M' OR Status = 'T'", geocoder_gdb__2_, "ObjectID OBJECTID VISIBLE NONE;Shape Shape VISIBLE NONE;Status Status VISIBLE NONE;Score Score VISIBLE NONE;Match_type Match_type VISIBLE NONE;Match_addr Match_addr VISIBLE NONE;X X VISIBLE NONE;Y Y VISIBLE NONE;Xmin Xmin VISIBLE NONE;Xmax Xmax VISIBLE NONE;Ymin Ymin VISIBLE NONE;Ymax Ymax VISIBLE NONE;Addr_type Addr_type VISIBLE NONE;ARC_Single_Line_Input ARC_Single_Line_Input VISIBLE NONE") # Process: Add Field arcpy.AddField_management(geocoded_master_successful, "geocoder", "TEXT", "", "", "20", "", "NULLABLE", "NON_REQUIRED", "") # Process: Calculate Field arcpy.CalculateField_management(geocoded_master_successful__2_, "geocoder", "\"EAS\"", "PYTHON", "") # Process: Copy Features arcpy.CopyFeatures_management(geocoded_master_successful__4_, geocoder_eas, "", "0", "0", "0") # Process: Merge print("SUCCEDED") fieldmappings = arcpy.FieldMappings() fieldmappings.addTable(transfomed) arcpy.Merge_management( "I:\\GIS\\OASIS\\Geocoder\\geocoder.gdb\\geocoder_eas; geocoded_street_centerlines_successful", final, fieldmappings) print("GEOCODING SUCCESSFUL") except Exception as e: print("ERROR") print(e)
# Geocode street addresses # # Author: Nathaniel Price # Date: May 22, 2018 # NOTE: # Excute script from within ArcMap # execfile('C://Users//nprice3//PycharmProjects//merge_shapefiles//5_geocode_street.py') # Setup --------------------------------------------------------------------------------------------------------------- # Import system modules import csv import glob import os import arcpy from arcpy import env # Overwrite existing files arcpy.env.overwriteOutput = True # Geocode streets arcpy.GeocodeAddresses_geocoding(in_table="E:/QGIS/geocoding2018/geocoding.gdb/address", address_locator="C:/Users/nprice3/Documents/ArcGIS/Address_Locators/streets/national_streets", in_address_fields="Street Street VISIBLE NONE;ZIP ZIP VISIBLE NONE", out_feature_class="E:/QGIS/geocoding2018/geocoding.gdb/geocodeResultStreet", out_relationship_type="STATIC")
with arcpy.da.UpdateCursor(facility_table, field_names=[ 'OBJECTID', 'GIS_LATITUDE', 'GIS_LONGITUDE', 'STREET_NUMBER', 'STREET_DIRECTION', 'STREET_NAME', 'STREET_TYPE', 'ZIP', 'STREETADDRESS' ]) as cursor: for row in cursor: row[-1] = addrcat(row[3], row[4], row[5], row[6]) cursor.updateRow(row) # GEOCODE - PAY CLOSE ATTENTION TO ADDRESS FIELDS SYNTAX facility_fields = "Street STREETADDRESS;ZIP ZIP" arcpy.GeocodeAddresses_geocoding(in_table="nullGeoRows", address_locator=locator, in_address_fields=facility_fields, out_feature_class="GeocodedAddresses", out_relationship_type="STATIC") # RECLASS '0' LAT AND LON TO NONE with arcpy.da.UpdateCursor("GeocodedAddresses", field_names=["Y", "X"]) as gcursor: for arow in gcursor: if arow[0] == 0: arow[0] = None arow[1] = None gcursor.updateRow(arow) # NESTED CURSORS TO UPDATE THE ORIGINAL FACILITY TABLE
in_table=facility_table, out_table="nullGeoRows", where_clause="GIS_LATITUDE IS NULL OR GIS_LONGITUDE IS NULL") arcpy.AddField_management("nullGeoRows", "STREETADDRESS", "TEXT") arcpy.CalculateField_management( in_table="nullGeoRows", field="STREETADDRESS", expression= "addrcat( !STREET_NUMBER!, !STREET_DIRECTION!, !STREET_NAME!, !STREET_TYPE!)", expression_type="PYTHON_9.3", code_block= 'def addrcat(snum, sdir, sname, stype):\n snum = str(snum)\n sdir = str(sdir)\n sname = str(sname)\n stype = str(stype)\n streetaddress = ""\n if len(snum)>0 and snum != "None":\n streetaddress = streetaddress+snum\n if len(sdir)>0 and sdir != "None":\n streetaddress = streetaddress+" "+sdir\n if len(sname)>0 and sname != "None":\n streetaddress = streetaddress + " " + sname\n if len(stype)>0 and stype != "None":\n streetaddress = streetaddress + " " + stype\n return streetaddress' ) facility_fields = "STREETADDRESS;CITY;STATE;ZIP" arcpy.GeocodeAddresses_geocoding("nullGeoRows", locator, facility_fields, "Facility_geocode", 'STATIC') ''' I:\IEPH\Software\ESRI\ESRI_StreetMap\Locators\USA_StreetAddress.loc ''' ''' fields to update Geocode State - text field
maximoCapacity = arcpy.GetParameterAsText(8) metodoTerritorios = arcpy.GetParameterAsText(9) quantidadeAreas = arcpy.GetParameterAsText(10) goodBalance = arcpy.GetParameterAsText(11) layerOutput = "{0}\\{1}\\tdlayer.lyr".format(pastaProjeto, territoryName) try: arcpy.AddMessage("Iniciando Processo") arcpy.AddMessage("Convertendo Tabela de Enderecos") arcpy.GeocodeAddresses_geocoding( tabelaGeocode, addressLocator, "Address 'ENDERECO CLIE' VISIBLE NONE;Neighborhood 'BAIRRO CLIE' VISIBLE NONE;City 'CIDADE CLIE' VISIBLE NONE;Region 'ESTADO CLIE' VISIBLE NONE;Postal <None> VISIBLE NONE", saidafeaturePontos, "STATIC", "", "" ) arcpy.AddMessage("Enderecos convertidos com sucesso. Iniciando processo - Criar Solucao de Territorio") gp.CreateTerritorySolution_TD( camadaEntrada, displayField, "Territories", pastaProjeto, territoryName )
fieldCalc(needFields3, activeDict, msagEDIT, "HIGH_ADD") print("Added and calculated FULL_ST_NM, LOW_ADD, and HIGH_ADD") #geocode the low and high address ranges to create two new files addrLocIn = schemaIn.iloc[9] addrLoc = addrLocIn[1] addrLoc = addrLoc.replace("\\", "/") print(addrLoc) #specify and set the spatial reference #spatial reference change see webpage for WKID: #https://desktop.arcgis.com/en/arcmap/10.3/analyze/arcpy-classes/pdf/projected_coordinate_systems.pdf arcpy.env.outputCoordinateSystem = arcpy.SpatialReference(2965) #geocode the low and high addresses lowOut = "lowGeo1" addrFields = "Street LOW_ADD; ZIP Zip" arcpy.GeocodeAddresses_geocoding(msagEDIT, addrLoc, addrFields, lowOut, 'STATIC') highOut = "highGeo1" addrFields = "Street HIGH_ADD; ZIP Zip" arcpy.GeocodeAddresses_geocoding(msagEDIT, addrLoc, addrFields, highOut, 'STATIC') print("Geocoding complete") answer = raw_input("Would you like to geocode again? Enter \"y\" to continue. Otherwise, press enter ") #if user wants to repeat geocoding, go through this loop counter = 2 while answer == "y": print("Should a different address locator be used? Enter the path if yes, otherwise hit enter") addrInput = raw_input("Input: ") if len(addrInput) > 0: addrLoc = addrInput.replace("\\", "/") addrLoc = addrLoc.replace("\\", "/") lowOut = "lowGeo" + str(counter)
def fishbone(address, centerlines, schemaFile): #determine the coordinate system of the files env.outputCoordinateSystem = arcpy.Describe(address + ".shp").spatialReference #run make feature layer so processes can be performed on the files addFC = "addressFC" centerFC = "centerFC" arcpy.MakeFeatureLayer_management(address + ".shp", addFC) arcpy.MakeFeatureLayer_management(centerlines + ".shp", centerFC) print("Created address and centerlines feature layers") #convert dbf of the address file to csv inTable = "addressFC.dbf" outTable = "addressdbfConvert1.csv" outLoc = schemaFile.iloc[0, 2] arcpy.TableToTable_conversion(inTable, outLoc, outTable) print("Converted address dbf") #remove the OID field that sometimes gets added from converting to csv os.chdir(outLoc) df = pd.read_csv(outTable, delimiter=",") outCSV = schemaFile.iloc[0, 5] try: df = df.drop("OID", axis=1) except: print("OID does not exist in the table") df.to_csv(outLoc + "/" + outCSV) print("Created csv file") #geocode the csv file using a premade address locator addressField = schemaFile.iloc[0, 6] zipField = schemaFile.iloc[0, 7] addressLocator = schemaFile.iloc[0, 8] addressFields = "Street " + addressField + ";ZIP " + zipField geocodeResult = "geocodeResult1" arcpy.GeocodeAddresses_geocoding(outCSV, addressLocator, addressFields, geocodeResult) print("Finished geocoding") #repair geometry on the addresses in the geocode output file tempLayer3 = "geocodeFC" arcpy.MakeFeatureLayer_management("geocodeResult1.shp", tempLayer3) arcpy.RepairGeometry_management(tempLayer3, "KEEP_NULL") #export to shapefile repairResult = "repairResult1" arcpy.CopyFeatures_management(tempLayer3, repairResult) print("Created new file with the repaired geometry") #check for scores that aren't 100 and save them in a file fc = outputLoc + "/" + repairResult + ".shp" fields = ["Score", "FID"] expression = "Score < 100" unmatchValues = arcpy.da.UpdateCursor(fc, fields, expression) for record in unmatchValues: print("Record number " + str(record[1]) + " has a match score of " + str(record[0])) f = open(str(outputLoc) + "/fishNotMatch.csv", "a") f.write(str(record[1]) + "," + str(record[0]) + ",\n") if int(record[0]) < 80: unmatchValues.deleteRow() #add x, y, and join fields in the results and address files repairResult = "repairResult1.shp" arcpy.AddField_management(repairResult, "NewXField1", "FLOAT", 9, 2) arcpy.AddField_management(repairResult, "NewYField1", "FLOAT", 9, 2) arcpy.AddField_management(repairResult, "JoinField1", "TEXT", "", "", 100) arcpy.AddField_management(addFC, "NewXField2", "FLOAT", 9, 2) arcpy.AddField_management(addFC, "NewYField2", "FLOAT", 9, 2) arcpy.AddField_management(addFC, "JoinField2", "TEXT", "", "", 100) print("Added fields") #calculate geometry on x and y arcpy.CalculateField_management(repairResult, "NewXField1", "!shape.extent.XMax!", "PYTHON_9.3") arcpy.CalculateField_management(repairResult, "NewYField1", "!shape.extent.YMax!", "PYTHON_9.3") arcpy.CalculateField_management(addFC, "NewXField2", "!shape.extent.XMax!", "PYTHON_9.3") arcpy.CalculateField_management(addFC, "NewYField2", "!shape.extent.YMax!", "PYTHON_9.3") #calculate join to equal the full address and zip code repairAddress = schemaFile.iloc[0, 9] repairZipcode = schemaFile.iloc[0, 10] expression1 = '!' + str(repairAddress) + '! + " " + !' + str( repairZipcode) + '!' expression2 = '!' + str(addressField) + '! + " " + !' + str(zipField) + '!' arcpy.CalculateField_management(repairResult, "JoinField1", expression1, "PYTHON_9.3") arcpy.CalculateField_management(addFC, "JoinField2", expression2, "PYTHON_9.3") print("Calculated fields") #join the two datasets and export them arcpy.MakeFeatureLayer_management(repairResult, "repairFC") arcpy.AddJoin_management("repairFC.shp", "JoinField1", "addressFC.dbf", "JoinField2") joinFile = "joinFile1" arcpy.CopyFeatures_management("repairFC", joinFile) print("Joined tables and exported file") #run xy to line arcpy.XYToLine_management("joinFile1.dbf", "outFish", "NewXField1", "NewYField1", "NewXField2", "NewYField2") print("XY to Line is complete") #delete temporary files arcpy.Delete_management("centerEDIT.shp") arcpy.Delete_management("addressdbfConvert1.csv") arcpy.Delete_management("geocodeTable.csv") arcpy.Delete_management("geocodeResult1.shp") arcpy.Delete_management("repairResult1.shp") arcpy.Delete_management("addressEDIT.shp") arcpy.Delete_management("joinFile1.shp") return
locComposite = createLocator() # Process: Excel To Table ScriptUtils.AddMsgAndPrint("\tCreating a connection to {0}...".format(tblAddress), 0) arcpy.ExcelToTable_conversion(tblAddress, dbfName) # Process: Make Table View (2) expression = "\"State\" = 'KY'" arcpy.MakeTableView_management(dbfName, tmpMotaxView, expression) # Process: Copy Rows arcpy.CopyRows_management(tmpMotaxView, tmpMotaxSub, "") # Process: Geocode Addresses ScriptUtils.AddMsgAndPrint("\tGeocoding addresses...", 0) arcpy.GeocodeAddresses_geocoding(tmpMotaxSub, locComposite, "Street Address VISIBLE NONE", tmpMotaxGeocoded, "STATIC") # Process: Spatial Join ScriptUtils.AddMsgAndPrint("\tProcessing the first spatial join...", 0) arcpy.SpatialJoin_analysis(tmpMotaxGeocoded, Personal_Property_Taxing_Districts, tmpMotaxJoin1, "JOIN_ONE_TO_ONE", "KEEP_ALL", "", "INTERSECT", "", "") # Process: Copy (4) arcpy.Copy_management(PVA_Parcel, parcels, "FeatureClass") # Process: Spatial Join (2) ScriptUtils.AddMsgAndPrint("\tProcessing the second spatial join...", 0) arcpy.SpatialJoin_analysis(tmpMotaxJoin1, parcels, tmpMotaxJoin2, "JOIN_ONE_TO_ONE", "KEEP_ALL", "", "INTERSECT", "", "") # Process: Make Table View arcpy.MakeTableView_management(tmpMotaxJoin2, tmpMotaxView2)
try: # Delete rows from MISSING_GIS_GEOCODE in AUTOWORKSPACE (prep existing FC, by cleaning out rows) arcpy.DeleteRows_management(MISSING_GIS_GEOCODE) except: print("\n Unable to delete rows from MISSING_GIS_GEOCODE") write_log("\n Unable to delete rows from MISSING_GIS_GEOCODE", logfile) logging.exception( 'Got exception on delete rows from MISSING_GIS_GEOCODE logged at:' + str(Day) + " " + str(Time)) raise sys.exit() try: # Geocode VIS_REALMAST_TBL in AUTOWORKSPACE against CC_PARCEL_Locator, into "memory" VIS_REALMAST_GEOCODE = arcpy.GeocodeAddresses_geocoding( VISION_REALMAST_TBL_SDE, CC_PARCEL_LOC, "'Single Line Input' REM_PID VISIBLE NONE", "in_memory/VIS_REALMAST_GEOCODE", "STATIC", "", "ROUTING_LOCATION") print("\n Geocoding REALMAST Table into memory") write_log("\n Geocoding REALMAST Table into memory", logfile) except: print( "\n Unable to Geocode VIS_REALMAST_TBL in AUTOWORKSPACE against CC_PARCEL_Locator, into memory" ) write_log( "\n Unable to Geocode VIS_REALMAST_TBL in AUTOWORKSPACE against CC_PARCEL_Locator, into memory", logfile) logging.exception( 'Got exception on Geocode VIS_REALMAST_TBL in AUTOWORKSPACE against CC_PARCEL_Locator, into memory logged at:' + str(Day) + " " + str(Time)) raise sys.exit()
'Side' <None> VISIBLE NONE;'Prefix Direction' <None> VISIBLE NONE; 'Prefix Type' <None> VISIBLE NONE;'*Street Name' LocName_ne VISIBLE NONE; 'Suffix Type' <None> VISIBLE NONE;'Suffix Direction' <None> VISIBLE NONE; 'City or Place' CITY VISIBLE NONE;'ZIP Code' ZIP VISIBLE NONE; 'State' <None> VISIBLE NONE;'Street ID' <None> VISIBLE NONE; 'Display X' <None> VISIBLE NONE;'Display Y' <None> VISIBLE NONE; 'Min X value for extent' <None> VISIBLE NONE;'Max X value for extent' <None> VISIBLE NONE; 'Min Y value for extent' <None> VISIBLE NONE;'Max Y value for extent' <None> VISIBLE NONE; 'Additional Field' <None> VISIBLE NONE;'Altname JoinID' <None> VISIBLE NONE""" print "Working on Locator" arcpy.CreateAddressLocator_geocoding(in_address_locator_style="US Address - Single House", in_reference_data=Orig_Parcel_Table, in_field_map=field_map, out_address_locator=add_locator) #Geocode Addresses to Parcel Data print "Working on Geocoding" arcpy.GeocodeAddresses_geocoding(Addresses, add_locator, address_fields, Stu_Parcel_Points) #After Geocode, Attach Points to Parcel Flood Data print "Working on Attaching Points to Parcels" Stu_Parcel_Damage= path + "\Shapefiles\Results_92517.gdb\Stu_Parcel_Damage" #Attach Student points to Parcel Damage arcpy.SpatialJoin_analysis(Orig_Parcel, Stu_Parcel_Points, Stu_Parcel_Damage, "JOIN_ONE_TO_ONE", "KEEP_ALL", "#", "INTERSECT") #Select Only Geocoded Features print "Working on Creating New Selection" arcpy.env.workspace= path + "\Shapefiles\Results_92517.gdb" arcpy.MakeFeatureLayer_management(Stu_Parcel_Damage, "temp") #First Make Temp Layer to Select Features arcpy.SelectLayerByAttribute_management("temp", "", '"Status" = \'M\' OR "Status" = \'T\'') #Then Select Features arcpy.CopyFeatures_management("temp", "Stu_Parcel_Damage_1") #Finally Save New Feature Class