Advertisement

07.07.2008 at 11:37AM PDT, ID: 23544321
[x]
Attachment Details

Modify python script - switch order of processes

Asked by mgjust in Python Scripting Language, GIS & GPS Programming

Hello,
The code below takes file(s) and clips them by another file.  I would like it to take files and clip them by one file.  I think this will work if I switch these lines of code around. So that FCstoClip is just one file, and the 'clipWith' is a list of features.  I look at this and am basically lost, I'd apperciate any advice, scripting help (annotated or otherwise). I know many here are not familiar with ArcGis but I appreciate any help you could provide me.

Thanks you kindly,
MJ

# Script arguments...
FCsToClip = gp.GetParameterAsText(0)    #a list of features
fcs = string.split(FCsToClip, ";")      #split into a list
#
clipWith = sys.argv[2]                  #clip layer
desc = gp.Describe(clipWith)
clipWithType = desc.ShapeTypeStart Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
#BatchClipFCs.py
#
#Author
#  Dan Patterson
#  Dept of Geography and Environmental Studies
#  Carleton University, Ottawa, Canada
#  Dan_Patterson@carleton.ca
#
#Purpose
#  Batch clips feature classes (eg shapefiles) in a project 
#  and writes them to a folder.
#  Many batch clip examples exist, this is compiled for demonstration
#  purpose for a class
#
#Properties (right-click on the tool and specify the following)
#General
#  Name   BatchClipFCs
#  Label  Batch Clip Feature Classes
#  Desc   Batch clip feature classes (eg shapefiles) and saves
#         them to a folder.
#
#Source  BatchClipFCs.py
#
#Parameter list
#                                                Parameter Properties
#           Display Name          Data type      Type      Direction  MultiValue
#  argv[1]  Feature(s) to clip    Feature Layer  Required  Input      Yes
#  argv[2]  Polygon clip layer    Feature Layer  Required  Input      No
#  argv[3]  Append to output      String         Optional  Input      No
#           filename
#  argv[4]  Output folder         Folder         Required  Input      No
#--------------------------------------------------------------------
#Import the standard modules
#  Get a list of feature classes (shapefiles)
#  Get the clip feature class
#  Get the optional text to append to the filename output filename
#  Get the output folder
 
# Usage: BatchClipFCs <Input_Features> <ClipWith> <optional_text> <Output_Workspace> 
# ---------------------------------------------------------------------------
#
# Import system modules, Create the Geoprocessor object and
# Load required toolboxes...
#
import sys, string, os, win32com.client
gp = win32com.client.Dispatch("esriGeoprocessing.GpDispatch.1")
gp.AddToolbox("C:/Program Files/ArcGIS/ArcToolbox/Toolboxes/Analysis Tools.tbx")
#gp.AddToolbox("C:/Program Files/ArcGIS/ArcToolbox/Toolboxes/Data Management Tools.tbx")
#
# Script arguments...
FCsToClip = gp.GetParameterAsText(0)    #a list of features
fcs = string.split(FCsToClip, ";")      #split into a list
#
clipWith = sys.argv[2]                  #clip layer
desc = gp.Describe(clipWith)
clipWithType = desc.ShapeType
#
addToName = sys.argv[3]                 #optional text to append to output name
if(addToName == "#"):
  addToName = ""
# 
outputFolder = sys.argv[4]              #a folder
#
gp.Addmessage("\n" + "Batch Clip Feature layers" + "\n" )
#
if (clipWithType == "Polygon"):
  for fc in fcs:
    try:
      fc=fc.replace("'","")             #check if a layers name has been changed
      desc = gp.Describe(fc)
      fcDataType = desc.DataType
      gp.AddMessage("Data type = " + fcDataType)
      if(fcDataType == "FeatureClass"): # Feature class on disk
        theName = str(os.path.split(fc)[1])
        outFile = outputFolder + "\\" + theName + addToName + ".shp"
      else:                             # Feature layer
        FullName = desc.CatalogPath
        theName = (os.path.split(FullName)[1]).replace(".shp","")
        theName = str(fc).replace(" ","_")
        outFile = outputFolder + "\\" + theName + addToName + ".shp"
        gp.AddMessage("Output file from layer " + outFile)
      #
      gp.AddMessage("Clipping " + str(fcDataType) + ": " + fc + " Saving to: " + outFile + "\n")
      try:
        gp.Clip_analysis(fc, clipWith, outFile, "")
      except:
        gp.AddMessage("Could not clip " + fc + " with " + clipWith + " to " + outFile)
    except:
      gp.AddMessage("cannot describe" + fc)
else:
  gp.AddMessage (clipWith + " is not a polygon layer, clipping terminated" )
#
[+][-]07.08.2008 at 07:51PM PDT, ID: 21960248

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: Python Scripting Language, GIS & GPS Programming
Sign Up Now!
Solution Provided By: mish33
Participating Experts: 1
Solution Grade: A
 
 
[+][-]07.08.2008 at 10:31PM PDT, ID: 21960806

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07.09.2008 at 12:00AM PDT, ID: 21961153

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.09.2008 at 10:46AM PDT, ID: 21966307

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]07.09.2008 at 08:58PM PDT, ID: 21970321

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07.10.2008 at 03:57PM PDT, ID: 21978088

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628