Note that if you want the "first" city to trump subsequent ones (in the case of overlap), you would want to sort the cityFC search cursor by OID in decending order (by default the sort order is by OID in ascending order).
An invalid SQL statement was used.
clause = "\"'NAME10' = " + cityname + '"'
clause = "NAME10 = " + "'" + cityname + "'"
How about something like this:Note that if you want the "first" city to trump subsequent ones (in the case of overlap), you would want to sort the cityFC search cursor by OID in decending order (by default the sort order is by OID in ascending order).
import arcpy from arcpy import env arcpy.env.workspace = "C:\E1B8\Grid_Create_Update_11-2013" # Create the search cursor # tablecur = arcpy.SearchCursor("SampleCityTable.dbf") for row in tablecur: cityname = row.City clause = "\"'NAME10' = " + cityname + '"' arcpy.AddMessage (clause) arcpy.AddMessage (cityname) # Make new feature for current city arcpy.MakeFeatureLayer_management("state_places.shp","thecity",clause) # Create layer for selection arcpy.MakeFeatureLayer_management("CityGrids.shp", "cityGridsLyr") # Select grid that intersect with new feature arcpy.SelectLayerByLocation_management("cityGridsLyr", "INTERSECT", "thecity", "", "NEW_SELECTION") # Update field in grid layer with city name arcpy.CalculateField_management("cityGridsLyr", "CITY_GRID", cityname, "PYTHON", "") # Remove selection from grid arcpy.SelectLayerByAttribute_management("cityGridsLyr", "CLEAR_SELECTION", "") # Delete newly made city layer arcpy.Delete_management("thecity", "") arcpy.Delete_management("cityGridsLyr", "")
import arcpy cityFC = r"C:\temp\city_polygons.shp" indexFC = r"C:\temp\index_polygons.shp" arcpy.MakeFeatureLayer_management(indexFC, "index_fl") searchRows = arcpy.da.SearchCursor(cityFC, ["SHAPE@","CITY_NAME"]) for searchRow in searchRows: geomObj, cityName = searchRow arcpy.SelectLayerByLocation_management("index_fl", "INTERSECT", geomObj, "", "NEW_SELECTION") arcpy.CalculateField_management("index_fl", "CITY_GRID", "'" + cityName + "'", "PYTHON", "")
What if one of your grid cells has multiple cities within it?
arcpy.MakeFeatureLayer_management("CityGrids.shp", "cityGridsLyr") arcpy.SelectLayerByLocation_management("cityGridsLyr", "INTERSECT", "thecity", "", "NEW_SELECTION")
import arcpy from arcpy import env #arcpy.AddMessage ("1") arcpy.env.workspace = "C:\E1B8\Grid_Create_Update_11-2013" #arcpy.AddMessage ("2") # Create the search cursor # tablecur = arcpy.SearchCursor("SampleCityTable") for row in tablecur: cityname = row.City sql = "\"'NAME10' = " + cityname + '"' # arcpy.AddMessage (sql) # arcpy.AddMessage (cityname) # Make new feature for current city arcpy.MakeFeatureLayer_management("state_places.shp","thecity",sql) # arcpy.AddMessage ("3") # Select grid that intersect with new feature arcpy.SelectLayerByLocation_management("CityGrids.shp", "INTERSECT", "thecity", "", "NEW_SELECTION") # arcpy.AddMessage ("4") # Update field in grid layer with city name arcpy.CalculateField_management("CityGrids.shp", "CITY_GRID", cityname, "PYTHON", "") # Remove selection from grid arcpy.SelectLayerByAttribute_management("CityGrids.shp", "CLEAR_SELECTION", "") # Delete newly made city layer arcpy.Delete_management("thecity", "") # arcpy.AddMessage ("5")
I have the following information:A table with a list of citiesA cities layerA grid layerI am trying to write a code that goes through the list of cities and searches for each grid that intersects each city in the list. So if I have a city of 'somewhereville' python will find all the grids that intersect the 'somewhereville' feature and then updates a field in the grid's attribute with the city name ('somewhereville'). I have tried a few ways but I can't quite figure it out. Any direction would be great.
Přihlášení členové mohou přispívat, sledovat aktualizace a další. Jste tu noví? Zaregistrujte si bezplatný účet.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.