AnsweredAssumed Answered

AddField_management to shapefiles created through SelectLayerByLocation

Question asked by nmichelg on Nov 11, 2015
Latest reply on Nov 12, 2015 by nmichelg

I am new to scripting and to Python/arcpy. My intention with this script is to:

  1. select points from a featureclass that fall within a target polygon and that meet an attribute query specified by the 'amenities' variable.
  2. create three new featureclasses of these points.
  3. add a new textfield to each of the featureclasses.
  4. update that field.

I am stuck at the step where I would add the new field to each new fc. I'm not sure if I'm on the right track as the for loop in which I selected the points was iterated based on an attribute query and now I am working on three new fc so perhaps I have to start a new for loop?

Any advice would be helpful.




import arcpy
arcpy.env.workspace = "C:/Test1/GIS/output"
arcpy.env.overwriteOutput = True

# set local variables
centralAmerica = "C:/Test1/GIS/CentralAmerica.shp"
targetCountry = "El Salvador"
points = "C:/Test1/GIS/OSMpoints.shp"
pointsField = "amenity"
sourceField = "Source"

amenities = ['school', 'hospital', 'place_of_worship']

# isolate El Salvador polygon from CentralAmerica dataset: select it by attribute "NAME"
# writing a Query 
countryQuery = '"NAME" = ' +"'" + targetCountry +  "'"
print countryQuery

# make a feature layer using the selection criteria from countryQuery
arcpy.MakeFeatureLayer_management(centralAmerica, "targetCountrylyr", countryQuery)
print arcpy.Exists("targetCountrylyr")

# establish a selection of points in an attribute table individually based on a Query
for amenity in amenities:
    amenityQuery = ' "amenity" = ' + "'" + amenity + "'"
    print amenityQuery
    # make a feature layer for the selected points
    arcpy.MakeFeatureLayer_management(points, "pointslyr", amenityQuery)
    # select points from above selection that fall within targetCountry
    arcpy.SelectLayerByLocation_management("pointslyr", "CONTAINED_BY", "targetCountrylyr")
    arcpy.CopyFeatures_management("pointslyr", amenity + ".shp")
    # add a field to each of the new shapefiles