[GIS] Select Layer by Location using ArcPy

arcgis-10.1arcpy

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 ?

Best Answer

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.