Select to view content in your preferred language

Clipping by view extent

751
2
06-28-2012 12:30 PM
DevonJenkins
Deactivated User
Hi everyone,

This is the second post I have made on this subject.....I couldnt find the old post. Here is my script. Everything seems to be in order however when the clip function runs, it clips features by the window extent...however it produces and empty shape file. Any ideas?

Thanks!



#DESCRIPTION:
#-clips Hydro and trapper features by data frame window extent
#-dissolves hyrdro layer to simplify line work
#-densify's both features to create more detailed line work
#-matches the trapper lines accordingly with the water features

#Import arcpy modules
import arcpy
#import operating system-level tools - usefull when working with paths
import os

#displays location of python script
home = os.getcwd()

#Declare Water feature variable
Hydro = "G:/Data/GIS/ArcGIS_Mapping/Hydrography.gdb/Watercourse_Poly"

#Provides access to map document
mxd = arcpy.mapping.MapDocument("CURRENT")

#Directs output shapefile to specified folder
arcpy.env.workspace = "C:/Users/DJenkins/Desktop/Temp_Folder Apr2012 - Present/Python/Trapper Script/Output Shape"

#Allows data to be overwritten
arcpy.env.overwriteOutput = True

#Makes a copy of original Trapper Line Data
arcpy.CopyFeatures_management("LINE DATA & DISPOSITIONS/Trapper Lines","Trapper_copy")

df = arcpy.mapping.ListDataFrames(mxd,"Layers")[0]

extent = df.extent
array = arcpy.Array()

array.add(extent.lowerLeft)
array.add(extent.lowerRight)
array.add(extent.upperRight)
array.add(extent.upperLeft)
array.add(extent.lowerLeft)

poly = arcpy.Polygon(array)

#Clip Trapper feature by window extent
arcpy.Clip_analysis("Trapper_copy",poly,"Trapper_Extent")

#Clip Hydro feature by window extent
arcpy.Clip_analysis(Hydro,poly,"Hydro_extent")

#Dissolve Hydro_copy
arcpy.Dissolve_management("Hydro_extent","Hydro_Dissolve","FID")

#Find the number of features in the the trapper_extent & Hydro_copy
arcpy.GetCount_management("Trapper_Extent")
arcpy.GetCount_management("Hydro_Dissolve")


try:
    #Add more points along trapper/Hydro features
    arcpy.Densify_edit("Trapper_Extent", "DISTANCE", "25 meters")
    arcpy.Densify_edit("Hydro_Dissolve", "DISTANCE", "25 meters")
    #Snap the trapper points to match the Hydro features
    arcpy.Snap_edit("Trapper_Extent", [["Hydro_Dissolve","EDGE", "250 meters"]])
    
except:    
    #Print the results
    print arcpy.GetMessages(2)
else:
    print "Congradulations! The script finished successfully. Your trapper lines now match with the hydro features."

del mxd    
Tags (2)
0 Kudos
2 Replies
JakeSkinner
Esri Esteemed Contributor
It doesn't look like you are specifying the .shp extension when you are executing the 'CopyFeatures' function.  Since your workspace is set to a folder, you will need to specify the extension.  Try the following and see if it works:

arcpy.CopyFeatures_management("LINE DATA & DISPOSITIONS/Trapper Lines","Trapper_copy.shp")
0 Kudos
JakeSkinner
Esri Esteemed Contributor
Have you tried using a different workspace.  The number of spaces that are in your current workspace may be causing issues.  Attached is a File Geodatabase and MXD that you can test with.  Create a folder called 'Temp' on you C: drive and extract the contents of the zip there, then execute the following within ArcMap while the MXD is open:

import arcpy
import os

mxd = arcpy.mapping.MapDocument("CURRENT")

arcpy.env.workspace = "C:/Temp"
arcpy.env.overwriteOutput = True

arcpy.CopyFeatures_management("Trapper Lines","Trapper_copy")

df = arcpy.mapping.ListDataFrames(mxd,"Layers")[0]

extent = df.extent
array = arcpy.Array()

array.add(extent.lowerLeft)
array.add(extent.lowerRight)
array.add(extent.upperRight)
array.add(extent.upperLeft)
array.add(extent.lowerLeft)

poly = arcpy.Polygon(array)

arcpy.Clip_analysis("Trapper_copy",poly,"Trapper_Extent")
0 Kudos