import arcpy, sys from arcpy import env layerSelection = sys.argv[1] featureSet = sys.argv[2] scratch_ws = arcpy.env.scratchWorkspace arcpy.AddMessage(scratch_ws) try: arcpy.env.overwriteOutput = True arcpy.MakeFeatureLayer_management(layerSelection,'lyr') arcpy.AddMessage('Running Spatial Intersect...') arcpy.SelectLayerByLocation_management('lyr', 'INTERSECT', featureSet, 0, r'NEW_SELECTION') arcpy.AddMessage('Copying Selected Features to Output...') arcpy.CopyFeatures_management('lyr', scratch_ws + r'\selection.shp') arcpy.Delete_Management('lyr') except: print arcpy.GetMessages()
Solved! Go to Solution.
import arcpy, sys from arcpy import env ##Input Variables and setting scratch workspace variable layerSelection = sys.argv[1] featureSet = sys.argv[2] scratch_ws = arcpy.env.scratchWorkspace arcpy.env.overwriteOutput = True ##Creating Variables for model derived Buffer = scratch_ws + r'\scratch.gdb\buffer' TempLayer = scratch_ws + r'\scratch.gdb\layer' selection = scratch_ws + r'\scratch.gdb\selection' try: ##Create a buffer of input feature set. This will be used to set the extent of the model. ##If this is not used the script will try to process the entire 36 million feature CLU dataset. ##This works because we are only interested in returning one feature to the geoprocessing service. arcpy.Buffer_analysis(featureSet, Buffer, "100 feet") desc = arcpy.Describe(Buffer) arcpy.env.extent = Buffer ##Copy the selected features to a temporary layer. This will respect your environment setting, ##so only records within 100ft of your point will be written to disk. arcpy.CopyFeatures_management(layerSelection, TempLayer) ##Create a temporary feature layer for use in select by location arcpy.MakeFeatureLayer_management(TempLayer, 'lyr') arcpy.AddMessage('Running Spatial Intersect...') ##Select features that intersect the user input point. arcpy.SelectLayerByLocation_management('lyr', 'INTERSECT', featureSet, 0, r'NEW_SELECTION') arcpy.AddMessage('Copying Selected Features to Output...') ##Copy the selected features to the arcgisjobs scratch directory arcpy.CopyFeatures_management('lyr', selection) ##Delete Temporary Data arcpy.Delete_management(Buffer) arcpy.Delete_management(TempLayer) ##Will count the number of features in output featureclass result = arcpy.GetCount_management(selection) ##If there are no records in the output featureclass a custom error will be returned to the user if int(result.getOutput(0)) == 0: arcpy.AddError ("No Features in " + layerSelection + " at selected location") ##Delete empty featureclass so that the server will retun an empty result arcpy.Delete_management(selection) else: pass except: print arcpy.GetMessages()
import arcpy, sys from arcpy import env ##Input Variables and setting scratch workspace variable layerSelection = sys.argv[1] featureSet = sys.argv[2] scratch_ws = arcpy.env.scratchWorkspace arcpy.env.overwriteOutput = True ##Creating Variables for model derived Buffer = scratch_ws + r'\scratch.gdb\buffer' TempLayer = scratch_ws + r'\scratch.gdb\layer' selection = scratch_ws + r'\scratch.gdb\selection' try: ##Create a buffer of input feature set. This will be used to set the extent of the model. ##If this is not used the script will try to process the entire 36 million feature CLU dataset. ##This works because we are only interested in returning one feature to the geoprocessing service. arcpy.Buffer_analysis(featureSet, Buffer, "100 feet") desc = arcpy.Describe(Buffer) arcpy.env.extent = Buffer ##Copy the selected features to a temporary layer. This will respect your environment setting, ##so only records within 100ft of your point will be written to disk. arcpy.CopyFeatures_management(layerSelection, TempLayer) ##Create a temporary feature layer for use in select by location arcpy.MakeFeatureLayer_management(TempLayer, 'lyr') arcpy.AddMessage('Running Spatial Intersect...') ##Select features that intersect the user input point. arcpy.SelectLayerByLocation_management('lyr', 'INTERSECT', featureSet, 0, r'NEW_SELECTION') arcpy.AddMessage('Copying Selected Features to Output...') ##Copy the selected features to the arcgisjobs scratch directory arcpy.CopyFeatures_management('lyr', selection) ##Delete Temporary Data arcpy.Delete_management(Buffer) arcpy.Delete_management(TempLayer) ##Will count the number of features in output featureclass result = arcpy.GetCount_management(selection) ##If there are no records in the output featureclass a custom error will be returned to the user if int(result.getOutput(0)) == 0: arcpy.AddError ("No Features in " + layerSelection + " at selected location") ##Delete empty featureclass so that the server will retun an empty result arcpy.Delete_management(selection) else: pass except: print arcpy.GetMessages()