-1

My target is to create a script tool using python where I will select location by area and export the area in the directory. To do so I have used this script

import arcpy
arcpy.env.overwriteOutput = True

try:
    inPath = arcpy.GetParameterAsText(0)
    inpath2 = arcpy.GetParameterAsText(1)
    outPath = arcpy.GetParameterAsText(2)
    selection = arcpy.SelectLayerByLocation_management("inPath", "WITHIN_A_DISTANCE", "inpath2", "50 meter","NEW_SELECTION","NOT_INVERT")
    copySelection = arcpy.CopyFeatures_management(selection,outPath)
    arcpy.GetMessage("All Done")
except:
    arcpy.AddError("Could not colplete the process")
    arcpy.AddMessage(arcpy.GetMessages())

but running this code in scripting tool toll look like thisgive this error messageError given after running the tool. :Please help me to solve this issue. Thanks in advance.

  • 2
    [Images](//meta.stackoverflow.com/q/285551/90527) should not be used for textual data, such as code or error messages. Please look over the [help], especially the "[ask]" article. – outis Dec 15 '22 at 04:09
  • Does this answer your question? "[Get Exception type + message as string in except](/q/68098658/90527)", "[How to get exception message in Python properly](/q/33239308/90527)" – outis Dec 15 '22 at 04:12
  • #outis No this is not answer of my questions – Emranul Islam Dec 26 '22 at 03:39

2 Answers2

1

I belive you are mistaking the syntax, although the data you are working with is not clear to me, and you may need to provide more details. Anyway try this code out and let me know if it helped you with your project.

import arcpy
arcpy.env.overwriteOutput = True

try:
    inPath = arcpy.GetParameterAsText(0)
    inpath2 = arcpy.GetParameterAsText(1)
    outPath = arcpy.GetParameterAsText(2)
    selection = arcpy.management.SelectLayerByLocation("inPath", "WITHIN_A_DISTANCE", "inpath2", "50 meter","NEW_SELECTION","NOT_INVERT")
    copySelection = arcpy.management.CopyFeatures(selection,outPath)
    arcpy.GetMessage("All Done")
except:
    arcpy.AddError("Could not colplete the process")
    arcpy.AddMessage(arcpy.GetMessages())

if it didn't work for you your problem is copying the features and instead of copying just try

arcpy.conversion.FeatureClassToFeatureClass(in_features, out_path, out_name, {where_clause}, {field_mapping}, {config_keyword})

That will solve your error.

sanitizedUser
  • 1,723
  • 3
  • 18
  • 33
Erfan
  • 9
  • 3
  • Please note that it is not enough to indent only the unindented lines to make a code block. You need to indent ALL lines in the code block by 4 spaces. (In the Stack Overflow editor, select your code and click the code-block button) If you don't want to mess with indent, use code fences, which are created by adding a line containing three backticks (`\`\`\``) before and after your code. See [formatting help](/help/formatting) – Pranav Hosangadi Jan 05 '23 at 15:48
0

There are a few issues with the script that need to be discussed:

  1. You shouldn't put your variables in quotes.
  2. The SelectLayerByLocation tool, as the name says, works with layers. Therefore, You need to convert the path of your feature-class to a layer. One way to do this is by using the MakeFeatureLayer tool.

The working method should look similar to this:

fc = arcpy.GetParameterAsText(0)
lyr = "lyr" # Just some string

arcpy.management.MakeFeatureLayer(fc, lyr)

arcpy.management.SelectLayerBy…(lyr, …)

On top of all this I'm not sure about how you are using certain functions (for example, the arcpy.GetMessage() method). I suggest the script should look like this:

import arcpy
arcpy.env.overwriteOutput = True

in_path1 = arcpy.GetParameterAsText(0)
in_path2 = arcpy.GetParameterAsText(1)
out_path = arcpy.GetParameterAsText(2)

in_lyr1 = "lyr1"
in_lyr2 = "lyr2"

arcpy.management.MakeFeatureLayer(in_path1, in_lyr1)
arcpy.management.MakeFeatureLayer(in_path2, in_lyr2)

arcpy.management.SelectLayerByLocation(in_lyr1, "WITHIN_A_DISTANCE", in_lyr2, "50 Meters")
arcpy.management.CopyFeatures(in_lyr1, out_path)

arcpy.AddMessage("All Done")
Itay Dvash
  • 40
  • 9