I'm writing a script that will cycle through a shapefile of building footprints for an entire state, create a folder for the individual counties in that state, and then intersect the building footprints file with a county polygon stored.
Because I'm working with multiple states and hundreds of counties, I've created two variables that will take the state and county names of the current iteration- I keep running into an issue when it comes to writing a query that uses those variables. My goal is to create the following query:
"ST_NAME = 'Colorado' AND "POLYGON_NM" = 'Weld'
I'm not familiar with writing sql queries in arcpy and the syntax is throwing me. Here's what I currently have as my query:
dictionary = set(zip(statenames, countynames)) for item in dictionary: try: stname = str(item) coname = str(item.replace(r' ', '_')) except: stname = str(item) coname = str(item) if stname == 'Colorado': coco = arcpy.CreateFolder_management((basefile + '/Colorado/Counties'), coname) query = '"ST_NAME" =' + "'" + stname + "'" + " AND" + '"POLYGON_NM" =' + "'" + coname + "'" fl = arcpy.MakeFeatureLayer_management(counties, 'in_memory/counties_lyr') coselect = arcpy.SelectLayerByAttribute_management(fl, '', query) fc = arcpy.FeatureClassToFeatureClass_conversion(str(coselect), 'in_memory', coname) arcpy.Intersect_analysis([Colorado, fc], (str(coco) + '/' + coname))
I keep getting the following error for the Make Feature Layer tool:
ERROR 000358: Invalid expression
In the past I used a different syntax, but it only produced empty shapefiles, if no errors:
query = "ST_NAME" = ('"' + stname + '"') AND "POLYGON_NM" = ('"' + coname + '"')