1

i want to create a python scipt query using arpy in arcgis i have one polyline shapefile and one polygon shapefile i want to choose(select) the polygons where contained within in the polylines shapefiles or there intersections and finaly i want that select polygons to export to new shapefile layer. i use arcmap 10.1

my code is

# Import arcpy module
import arcpy
from arcpy.sa import *


arcpy.CheckOutExtension("spatial")
arcpy.env.overwriteOutput = True
try:
arcpy.env.workspace = root
root = arcpy.GetParameterAsText(0)
polygon = arcpy.GetParameterAsText(1)
line =  arcpy.GetParameterAsText(2)
except:
arcpy.AddMessage(arcpy.GetMessages())
try:
arcpy.SelectLayerByLocation_management(polygon, "INTERSECT", line, "", "NEW_SELECTION")
arcpy.FeatureClassToShapefile_conversion("polygon", newpolygon)
newpolygon = root + "\\newpolygon"
except:
arcpy.AddMessage(arcpy.GetMessages())

i use that code but i dont take Correct Results my root workspace is empty

any idea ?

  • You attempt to use the variable "newpolygon" before it is assigned a value. – phloem Jan 13 '15 at 21:17
  • Similarly, as Leroy points out below, you attempt to use "root" before it is assigned. – phloem Jan 13 '15 at 21:50
1

This script can be further edited but commonly this script should work

import arcpy

# Script arguments
Search_Distance = arcpy.GetParameterAsText(0)

Selection_type = arcpy.GetParameterAsText(1)
if Selection_type == '#' or not Selection_type:
    Selection_type = "NEW_SELECTION" # provide a default value if unspecified

Source_Layer = arcpy.GetParameterAsText(2)

Relationship = arcpy.GetParameterAsText(3)
if Relationship == '#' or not Relationship:
    Relationship = "INTERSECT" # provide a default value if unspecified

Target_Layers__Select_From_ = arcpy.GetParameterAsText(4)

Output_Layer_Name = arcpy.GetParameterAsText(5)

Output_Layer = arcpy.GetParameterAsText(6)

Workspace_or_Feature_Dataset = arcpy.GetParameterAsText(7)

# Local variables:

# Process: Select Layer By Location
arcpy.SelectLayerByLocation_management(Target_Layers__Select_From_, Relationship, Source_Layer, Search_Distance, Selection_type)

# Process: Make Feature Layer
arcpy.MakeFeatureLayer_management(Output_Layer_Name, Output_Layer, "", Workspace_or_Feature_Dataset, "")

Thanks.

  • thank you but not help me that :( – hellas Jan 13 '15 at 20:12
1
import arcpy

polygon = arcpy.GetParameterAsText(0) # input polygon feature class
line =  arcpy.GetParameterAsText(1) # input line feature class
output = arcpy.GetParameterAsText(2) # output polygon feature class

# make polygon feature layer
arcpy.MakeFeatureLayer_management(polygon, 'polygon_lyr') 
# select polygons
arcpy.SelectLayerByLocation_management('polygon_lyr', "INTERSECT", line, "", "NEW_SELECTION")
# save selection to output
arcpy.CopyFeatures_management('polygon_lyr', output)
0

You need to place 'root = arcpy.GetParameterAsText(0)' before 'arcpy.env.workspace = root' and that should sort it. Watch your script indentation too :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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