2

I'm in the process of developing a script that will create individual shapefiles for schools, churches, and hospitals in El Salvador based off of OpenStreetMap data. For some reason, my loop is creating empty shapefiles rather than selecting the pre-existing shapefile points and moving them into their own shapefile.

import arcpy
import os
arcpy.env.workspace = "D:/GIS/Lesson3/Project3/Project3.gdb"
arcpy.env.overWriteOutput = True
outPath = "D:/GIS/Lesson3/Project3/Project3.gdb"
points = "OSMpoints"
countries = "CentralAmerica"
amenities = ['school','hospital','place_of_worship']
amenityLayers = []

country = 'EL Salvador'

stateSelectionString = '"NAME" = \'{}\''.format(country)

Delete any extra layers that might be hanging around and prevent code from executing

if arcpy.Exists("ElSalvadorLayer"):
  arcpy.Delete_management("ElSalvadorLayer")
if arcpy.Exists("amenitiesLayer"):
  arcpy.Delete_management("amenitiesLayer")

Create a feature layer that I can use to query my amenities with later

arcpy.MakeFeatureLayer_management(countries,"ElSalvadorLayer",\
                              stateSelectionString)

for amenity in amenities:
  amenitySelectionString = '"amenity" = \'{}\''.format(amenity)    
  outShp = os.path.join(outPath,"{}".format(amenity))

  arcpy.MakeFeatureLayer_management(points,"amenitiesLayer",\
                                  amenitySelectionString)

getCount method used to test for whether any amenities have been selected, provides 3 0's to pywin interactive window each time.

  selectedAmenitiesCount = arcpy.GetCount_management("amenitiesLayer")
  amenitiesCount = int(selectedAmenitiesCount.getOutput(0))
  print amenitiesCount

  arcpy.SelectLayerByLocation_management("amenitiesLayer",\
                                       "WITHIN",\
                                       "ElSalvadorLayer")

  arcpy.CopyFeatures_management("amenitiesLayer",\
                              outShp)
  arcpy.Delete_management("amenitiesLayer")

1 Answer 1

3

Remove the quotes around your field name in your where clause.

stateSelectionString = "NAME = '{}'".format(country)

Additionally, triple quoting strings can make it easier to type them. Triple quoting allows you to use any quote mark within the string without needing to escape them. This works well with SQL query string.s

stateSelectionString = """ NAME = '{}' """.format(country)

Update as per Paul's comment

stateSelectionString = """ {0} = '{1}' """.format(arcpy.AddFieldDelimiters("ElSalvadorLayer", 'NAME'),  country)
3
  • 2
    AddFieldDelimiters removes the guess work of: do I need quotes, brackets, or nothing?
    – Paul
    Mar 21, 2017 at 18:07
  • Interesting. Thank you for the tip, however I am still encountering the same issue after formatting both my stateSelectionString and amenitySelectionString as you have suggested. Mar 22, 2017 at 21:30
  • @taco_wednesday I have updated my answer to include the addFieldDelimiters
    – Midavalo
    Mar 23, 2017 at 19:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.