try: # Iterates through each of the input text files for v in list(table): # Removes the file extension tableName = os.path.splitext(v)[0] # Variables tableName_line = tableName + str("_line.shp") tableName_vctor = tableName + str("_vctor.shp") tableName_e = tableName + str ("_e.shp") gate = r"C:\gis\gate.shp" tableName_sim = tableName + str("_sim.shp") # Creating vectors from xy coordinates gp.Toolbox = r"C:\Program Files\ArcGIS\ArcToolbox\Toolboxes\Military Analyst Tools.tbx" gp.TableToLine_ma(v, tableName_line, "Decimal Degrees", "sLat", "sLon", "eLat", "eLon", "False", "False") gp.Project_management(tableName_line, tableName_vctor, "PROJCS['LCC']") #Selecting vectors that intersect a line, then points that intersect the vector, then removing the initial selection gp.SelectLayerByLocation(tableName_vctor, "intersect", gate, "NEW_SELECTION") gp.SelectLayerByLocation(tableName_e, "intersect", tableName_vctor, "ADD_TO_SELECTION") gp.SelectLayerByLocation(tableName_vctor, "intersect", gate, "REMOVE_FROM_SELECTION") # Write the selected features to a new feature class in the folder gp.CopyFeatures(tableName_sim, r"C:\gis\temp") except: #If an error occurred while running the script, then print the messages print gp.GetMessages()
Solved! Go to Solution.
# Make a layer from the shapefiles gp.MakeFeatureLayer("tableName_e.shp", "tableName_e_lyr") gp.MakeFeatureLayer("tableName_vctor.shp", "tableName_vctor_lyr")
# Make a layer from the feature class gp.MakeFeatureLayer_management(gate, "gate_lyr") gp.MakeFeatureLayer_management(tableName_e, "tableName_e_lyr") gp.MakeFeatureLayer_management(tableName_vec, "tableName_vec_lyr") print "Feature Layers created." #Selecting the vectors that intersect the gate gp.SelectLayerByLocation_management(tableName_vec_lyr, "intersect", gate_lyr) # Add to selection, points that intersect with the previously selected vectors gp.SelectLayerByLocation_management(tableName_e_lyr, "intersect", tableName_vec_lyr, "ADD_TO_SELECTION") # Remove initial vector selection gp.SelectLayerByLocation_management(tableName_vec_lyr, "intersect", gate_lyr, "REMOVE_FROM_SELECTION") # Write the selected features to a new feature class gp.CopyFeatures(tableName_e_lyr, tableName_sim)
# Make a layer from the feature class gp.MakeFeatureLayer_management(gate, "gate_lyr") gp.MakeFeatureLayer_management(tableName_e, "tableName_e_lyr") gp.MakeFeatureLayer_management(tableName_vec, "tableName_vec_lyr") print "Feature Layers created." #Selecting the vectors that intersect the gate gp.SelectLayerByLocation_management(tableName_vec_lyr, "intersect", gate_lyr) # Add to selection, points that intersect with the previously selected vectors gp.SelectLayerByLocation_management(tableName_e_lyr, "intersect", tableName_vec_lyr, "ADD_TO_SELECTION") # Remove initial vector selection gp.SelectLayerByLocation_management(tableName_vec_lyr, "intersect", gate_lyr, "REMOVE_FROM_SELECTION") # Write the selected features to a new feature class gp.CopyFeatures(tableName_e_lyr, tableName_sim)
The feature layers are successfully created, and the first "select layer by location " works, but it doesn't seem to perform the 2 subsequent "select layer by location"'s.
You need to use the proper syntax.
SelectLayerByLocation_management (in_layer, overlap_type, select_features, search_distance, selection_type)
http://webhelp.esri.com/arcgisdesktop/9.3/body.cfm?id=1877&pid=1865&topicname=Select%20Layer%20By%20...
You are trying to insert "ADD_TO_SELECITON" in the search distance parameter.
gp.SelectLayerByLocation_management(tableName_e_lyr, "intersect", tableName_vec_lyr, "", "ADD_TO_SELECTION") gp.SelectLayerByLocation_management(tableName_vec_lyr, "intersect", gate_lyr, "#","REMOVE_FROM_SELECTION")
gp.SelectLayerByLocation_management(tableName_e_lyr, "intersect", tableName_vec_lyr, "", "ADD_TO_SELECTION")
try: gp.SelectLayerByLocation_management(tableName_e_lyr, "intersect", tableName_vec_lyr, "", "ADD_TO_SELECTION") except: print gp.GetMessages(2)
# Variables... tableName_e = tableName + str("_e.shp") tableName_vec = tableName + str("_vec.shp") tableName_e = tableName + str ("_e.shp") tableName_sim = tableName + str("_sim.shp") gate = r"E:\test\gate.shp" end_lyr = tableName + str("_e.lyr") vec_lyr = tableName + str("_vec.lyr") # Make a layer from the feature class gp.MakeFeatureLayer_management(gate, "gate_lyr") gp.MakeFeatureLayer_management(tableName_e, "end_lyr") gp.MakeFeatureLayer_management(tableName_vec, "vec_lyr") print "Feature Layers created." #Selecting the vectors that intersect the gate print "Selecting vectors that intersect the gate for:" + str(tableName) # Process: Select Layer By Location... gp.SelectLayerByLocation_management(vec_lyr, "INTERSECT", gate_lyr, "", "NEW_SELECTION") # Process: Select Layer By Location (2)... gp.SelectLayerByLocation_management(end_lyr, "INTERSECT", vec_lyr, "", "ADD_TO_SELECTION") # Process: Select Layer By Location (3)... gp.SelectLayerByLocation_management(vec_lyr, "INTERSECT", gate_lyr, "", "REMOVE_FROM_SELECTION") # Write the selected features to a new feature class gp.CopyFeatures_management(end_lyr, tableName_sim, "", "0", "0", "0")