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 ?

PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Jan 13, 2015 at 11:10
2
  • You attempt to use the variable "newpolygon" before it is assigned a value. Commented Jan 13, 2015 at 21:17
  • Similarly, as Leroy points out below, you attempt to use "root" before it is assigned. Commented Jan 13, 2015 at 21:50

3 Answers 3

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.

answered Jan 13, 2015 at 12:04
0
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)
answered Jan 13, 2015 at 21:44
0

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

answered Jan 13, 2015 at 21:11

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.