# --------------------------------------------------------------------------- # adjacent.py # Created on: 2012-07-12 15:14:17.00000 # (generated by ArcGIS/ModelBuilder) # Usage: adjacent <countyName> # Description: # --------------------------------------------------------------------------- # Import arcpy module import arcpy # Set Geoprocessing environments arcpy.env.scratchWorkspace = "C:\\ESRItest\\model\\process.gdb" arcpy.env.workspace = "C:\\ESRItest\\model\\process.gdb" # Script arguments countyName = arcpy.GetParameterAsText(0) if countyName == '#' or not countyName: countyName = "Boone" # provide a default value if unspecified # Local variables: selected_County = countyName Adjacent_Counties = selected_County county = "county" # Process: Select Layer By Attribute arcpy.SelectLayerByAttribute_management(county, "NEW_SELECTION", "\"NAME\" = '%countyName%'") # Process: Select Layer By Location result = arcpy.SelectLayerByLocation_management(selected_County, "BOUNDARY_TOUCHES", "", "", "NEW_SELECTION") ## NOW WHAT?
Solved! Go to Solution.
# --------------------------------------------------------------------------- # adjacent.py # Created on: 2012-07-12 15:14:17.00000 # (generated by ArcGIS/ModelBuilder) # Usage: adjacent <countyName> # Description: # --------------------------------------------------------------------------- # Import arcpy module import arcpy # Set Geoprocessing environments arcpy.env.scratchWorkspace = "C:\\ESRItest\\model\\process.gdb" arcpy.env.workspace = "C:\\ESRItest\\model\\process.gdb" #overwrite pre-existing files arcpy.env.overwriteOutput = True # Script arguments #countyName = arcpy.GetParameterAsText(0) countyName = raw_input('What County do you want to select?') # Local variables: county = "county" county_Layer = "county_Layer" countyList = [] txt_list = "" #Output_Feature_Class = "C:\\ESRItest\\model\\process.gdb\\county_CopyFeaturesOutput" # Process: Make Feature Layer county_Layer = arcpy.MakeFeatureLayer_management("C:\ESRItest\model\process.gdb\county", county_Layer, "", "", "OBJECTID OBJECTID VISIBLE NONE;Shape Shape VISIBLE NONE;NAME NAME VISIBLE NONE;CNTY_FIPS CNTY_FIPS VISIBLE NONE;FIPS FIPS VISIBLE NONE;POP2005 POP2005 VISIBLE NONE;POP05_SQMI POP05_SQMI VISIBLE NONE;SQMI SQMI VISIBLE NONE;NAME2 NAME2 VISIBLE NONE;Shape_Length Shape_Length VISIBLE NONE;Shape_Area Shape_Area VISIBLE NONE") #Process: Select Layer By Attributes #print countyName whereClause=" \"NAME\" = " + "'"+countyName+"'" #print whereClause countySelection = arcpy.SelectLayerByAttribute_management(county_Layer, "NEW_SELECTION", whereClause) # Process: Select Layer By Location results = arcpy.SelectLayerByLocation_management(countySelection, "BOUNDARY_TOUCHES", "", "", "NEW_SELECTION") # Process: Copy Features #arcpy.CopyFeatures_management(county_Layer, Output_Feature_Class, "", "0", "0", "0") ## Step through the selection to get the values from the name field rows = arcpy.SearchCursor(results,"","","NAME","NAME") for row in rows: neighborVal = row.getValue("NAME") if neighborVal != countyName: countyList.append(neighborVal) txt_list = ','.join(countyList) print txt_list
list_of_districts = [] #creates an empty list of the districts that you will fill rows = arcpy.SearchCursor(select_by_location_layer,"","","DistrictNum") # sets up a search cursor that allows you to move row by row for row in rows: dist_id = row.DistrictNum list_of_districts.append(dist_id)
dist_num_file = open(r'c:\districts.txt','a') #creates and opens a file on disk for appending to for x in list_of_districts: dist_num_file.write(x + '\n') #the \n places a carriage return so each district value is put on a separate line dist_num_file.close()The key concepts you'll want to master for this sort of thing are search cursors, iteration concepts, use the python for loop, file objects (creating, reading, writing, etc.), and text string manipulation.
result = arcpy.SelectLayerByLocation_management(selected_County, "BOUNDARY_TOUCHES", "", "", "NEW_SELECTION")
rows = arcpy.SearchCursor(result,"","","DistrictNum")
PAZ_lyr = 'feature_layer' # feature layer created as a global variable def make_paz_layer(keyhole): paz_units = [] SelPaz = arcpy.SelectLayerByLocation_management (PAZ_lyr, 'Intersect', keyhole) rows = arcpy.SearchCursor(SelPaz," \"POWERPLANT\" = 'PlantName'", "", "PAZ;PAZ_NUM;PAZ_ALPHA","PAZ_NUM A;PAZ_ALPHA A") for row in rows: unit = str(row.PAZ) unum = "'" + unit + "'" #wraps each value in single quotes paz_units.append(unum) txt_paz = ','.join(paz_units) #creates a comma separated string out of a ist paz_list = '(' + txt_paz + ')' #encloses the string inside parentheses to be used in the definition query paz_qry = ' "POWERPLANT" = '+ station_name + ' AND "PAZ" in ' + paz_list new_paz = arcpy.MakeFeatureLayer_management(paz, keyhole + '_paz', paz_qry) arcpy.SaveToLayerFile_management(new_paz, outfolder + '\\' + keyhole + '_paz.lyr')
# --------------------------------------------------------------------------- # adjacent.py # Created on: 2012-07-12 15:14:17.00000 # (generated by ArcGIS/ModelBuilder) # Usage: adjacent <countyName> # Description: # --------------------------------------------------------------------------- # Import arcpy module import arcpy # Set Geoprocessing environments arcpy.env.scratchWorkspace = "C:/ESRItest/model/process.gdb" arcpy.env.workspace = "C:/ESRItest/model/process.gdb" # Script arguments countyName = arcpy.GetParameterAsText(0) if countyName == '#' or not countyName: countyName = "Boone" # provide a default value if unspecified # Local variables: selected_County = countyName Adjacent_Counties = selected_County county = "county" countList = [] # I added this countyField = "NAME" #I added this # Process: Select Layer By Attribute arcpy.SelectLayerByAttribute_management(county, "NEW_SELECTION", "\"NAME\" = '%countyName%'") # Process: Select Layer By Location results = arcpy.SelectLayerByLocation_management(selected_County, "BOUNDARY_TOUCHES", "", "", "NEW_SELECTION") ## I added this section 7-13-12 rows = arcpy.SearchCursor(results,"","","NAME") for row in rows: neighborVal = row.NAME print(neighborVal) countyList.append(neighborVal) txt_list = ','.join(countyList) print txt_list
def GetDistricts(args): #where args are values you'd pass in if there were any ...code to do stuff txt_list = ','.join(countyList) return txt_list
# --------------------------------------------------------------------------- # adjacent.py # Created on: 2012-07-12 15:14:17.00000 # (generated by ArcGIS/ModelBuilder) # Usage: adjacent <countyName> # Description: # --------------------------------------------------------------------------- # Import arcpy module import arcpy # Set Geoprocessing environments arcpy.env.scratchWorkspace = "C:/ESRItest/model/process.gdb" arcpy.env.workspace = "C:/ESRItest/model/process.gdb" #overwrite pre-existing files arcpy.env.overwriteOutput = True # Script arguments countyName = arcpy.GetParameterAsText(0) if countyName == '#' or not countyName: countyName = "Boone" # provide a default value if unspecified # Local variables: selected_County = countyName Adjacent_Counties = selected_County #county = "county" county = "C:/ESRItest/model/process.gdb/county" Output_Layer = "county_Layer" countList = [] # I added this countyField = "NAME" #I added this county_lyr = "C:/ESRItest/model/county.lyr" # Process: Make Feature Layer arcpy.MakeFeatureLayer_management(county, Output_Layer, "", "", "OBJECTID OBJECTID VISIBLE NONE;Shape Shape VISIBLE NONE;NAME NAME VISIBLE NONE;CNTY_FIPS CNTY_FIPS VISIBLE NONE;FIPS FIPS VISIBLE NONE;POP2005 POP2005 VISIBLE NONE;POP05_SQMI POP05_SQMI VISIBLE NONE;SQMI SQMI VISIBLE NONE;NAME2 NAME2 VISIBLE NONE;Shape_Length Shape_Length VISIBLE NONE;Shape_Area Shape_Area VISIBLE NONE") # Process: Select Layer By Attribute selection = arcpy.SelectLayerByAttribute_management(Output_Layer, "NEW_SELECTION", "\"NAME\" = '%countyName%'") # Process: Select Layer By Location results = arcpy.SelectLayerByLocation_management(selection, "BOUNDARY_TOUCHES", "", "", "NEW_SELECTION") # Process: Select Layer By Attribute #arcpy.SelectLayerByAttribute_management("C:/ESRItest/model/process.gdb/county", "NEW_SELECTION", "\"NAME\" = '%countyName%'") ## I added this section 7-13-12 rows = arcpy.SearchCursor(results,"","","NAME","NAME") for row in rows: neighborVal = row.getValue("NAME") print(neighborVal) countyList.append(neighborVal) print countyList #txt_list = ','.join(countyList)
{ "attributes": { "OID": 1, "LABNO": "C-117267", "CATEGORY": "NURE", "DATASET": "NURE Coastal 98", "TYPEDESC": "STRM-SED-DRY", "COUNT_": 118, "PB_ICP40": 12 }
## try this version by ct 7/18/12 countyList = [] # this creates an empty python list for you to append values to # creating this outside the loop makes sure that it can be used outside the loop because of scope issues rows = arcpy.SearchCursor(results,"","","NAME","NAME") for row in rows: neighborVal = row.NAME print(neighborVal) countyList.append(neighborVal) print countyList #txt_list = ','.join(countyList) mydir = r'XXXXX' # put in a directory path here that you can write a text file to outdoc = open(mydir + '\\results.txt','a') outdoc.write(txt_list + '\n') outdoc.close()