13 Replies Latest reply on Jun 13, 2012 5:02 AM by lavpizzo

    Selecting Features by Location

    lavpizzo
      Hello,

      I am trying to select features by location from the "tableName_vctor" layer as defined (and highlighted in red), then export them to a new shapefile and am receiving the following errors (refer to the blue section):

      ERROR 000622: Failed to execute (Select Layer By Location). Parameters are not valid.
      ERROR 000628: Cannot set input into parameter search_distance.

      any insight as to how to fix these errors is much appreciated

      thank-you in advance!

      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()
        • Re: Selecting Features by Location
          mzcoyle
          You can only do selections on layers, not feature classes.

          Also, this is the syntax.
          SelectLayerByLocation_management (in_layer, {overlap_type}, {select_features}, {search_distance}, {selection_type})
          • Re: Selecting Features by Location
            lavpizzo
            When creating the feature layer using the following code,


            # Make a layer from the shapefiles
            gp.MakeFeatureLayer("tableName_e.shp", "tableName_e_lyr")
            gp.MakeFeatureLayer("tableName_vctor.shp", "tableName_vctor_lyr")


            I am trying to call on shapefiles that were made earlier in the code (the files in blue), yet I am getting this error:
            Failed to execute. Parameters are not valid.
            ERROR 000732: Input Features: Dataset tableName_e.shp does not exist or is not supported
            Failed to execute (MakeFeatureLayer).
            • Re: Selecting Features by Location
              m.gasior
              The shapefile you specified is not found in geoprocessing workspace or gp.workspace was not declared.

              So you have to:
              - declare gp.workspace which contains your shapefiles
              or
              - provide full path (or variable with path) in gp.MakeFeatureLayer_management() tool
              • Re: Selecting Features by Location
                lavpizzo
                # 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.
                • Re: Selecting Features by Location
                  mzcoyle
                  # 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%20Location%20%28Data%20Management%29&tocVisable=0

                  You are trying to insert "ADD_TO_SELECITON" in the search distance parameter.
                  • Re: Selecting Features by Location
                    lavpizzo
                    It says that the search distance is optional so I have tried to set it as "None", "Null", "No_Value", "", " ", "0", as I do not want to create a buffer, but rather only select the points that intersect with the specified vector... I can't seem to find any other alternative placeholder to setting it as blank.

                    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%20Location%20%28Data%20Management%29&tocVisable=0

                    You are trying to insert "ADD_TO_SELECITON" in the search distance parameter.
                    • Re: Selecting Features by Location
                      mzcoyle
                      Either of these should work.
                      "" or "#"

                      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")


                      Are you getting an error or anything when these execute? Or just unexpected behaviour?
                      • Re: Selecting Features by Location
                        nahmed-esristaff
                        After you set an empty string (red) for the search_distance parameter:

                        gp.SelectLayerByLocation_management(tableName_e_lyr, "intersect", tableName_vec_lyr, "", "ADD_TO_SELECTION")


                        Please enclose the call with try-except block as follows:


                        try:
                            gp.SelectLayerByLocation_management(tableName_e_lyr, "intersect", tableName_vec_lyr, "", "ADD_TO_SELECTION")
                        
                        except:
                            print gp.GetMessages(2)


                        What error message do you get now?
                        • Re: Selecting Features by Location
                          lavpizzo
                          I added the try and except statement around the select layers by location by did not get any error messages generated.

                          This is what printed in the python shell:
                          Feature Layers created. (suggesting that the feature layers were created)
                          Selecting vectors that intersect the gate for:20041115011426_20041122011013 (this is the start of the select by location commands)
                          (i'm not sure why this is occuring after the feature layers should have aExecuting: MakeFeatureLayer E:\temp\5464648_vec.shp vec_lyr # # "CP CP VISIBLE NONE;startX startX VISIBLE NONE;startY startY VISIBLE NONE;dispX dispX VISIBLE NONE;dispY dispY VISIBLE NONE;sLat sLat VISIBLE NONE;sLon sLon VISIBLE NONE;theta theta VISIBLE NONE;pCorr pCorr VISIBLE NONE;pkStr pkStr VISIBLE NONE;cCorr cCorr VISIBLE NONE;conf conf VISIBLE NONE;velX velX VISIBLE NONE;velY velY VISIBLE NONE;endX endX VISIBLE NONE;endY endY VISIBLE NONE;eLat eLat VISIBLE NONE;eLon eLon VISIBLE NONE"
                          Start Time: Tue Jun 12 12:57:06 2012
                          Executed (MakeFeatureLayer) successfully.
                          End Time: Tue Jun 12 12:57:06 2012 (Elapsed Time: 0.00 seconds)

                          I know that the select by location should work because when I do it manually in ArcMap, i'm able to select several features through following the similar set of commands but the automation for this step doesn't seem to run.

                                  # 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")
                          
                          
                          
                          • Re: Selecting Features by Location
                            nahmed-esristaff
                            Which version of ArcGIS you are on?

                            Please add another print statement in the try block and see whether you get any success message.

                            try:
                                gp.SelectLayerByLocation_management(tableName_e_lyr, "intersect", tableName_vec_lyr, "", "ADD_TO_SELECTION")
                                print gp.GetMessages()
                            except:
                                print gp.GetMessages(2)
                            • Re: Selecting Features by Location
                              lavpizzo
                              Hello NobbirAhmed,

                              I am using ArcGIS 9.3.

                              when I added the additional print message, and the try/except block this error occured:

                              Executing: SelectLayerByLocation 11013_e.lyr INTERSECT 11013_vec.lyr # ADD_TO_SELECTION 11013_e.lyr
                              Start Time: Tue Jun 12 14:18:24 2012
                              Failed to execute. Parameters are not valid.
                              ERROR 000732: Input Feature Layer: Dataset 20041115011426_20041122011013_e.lyr does not exist or is not supported
                              ERROR 000732: Selecting Features: Dataset 20041115011426_20041122011013_vec.lyr does not exist or is not supported
                              Failed to execute (SelectLayerByLocation).
                              • Re: Selecting Features by Location
                                mzcoyle
                                You are referencing layer files that were never created it looks like. MakeFeatureLayer creates a temporary layer in memory, not a layer file.
                                • Re: Selecting Features by Location
                                  lavpizzo
                                  Thanks Mathew!