1

I have 2 GDBs. The first one (let's name it X.gdb) contains feature classes (A,B,C...n) that are to be clipped. The second gdb (Y.gdb) contains fc (let's name it border1, border2, border3...) with only one polygonal feature (municipal district) in each. These are clipping features. What I need to do is to create model/python script, that creates separate gdb for every clipping feature (border1.gdb, border2.gdb, border3.gdb...) with clipped features from X.gdb


enter image description here

What kind of mistake did I make?

11
  • 1
    I think I have something that does this but can you provide some kind of effort in putting together a script (even if it's pseudo code) to get things started?
    – geoJshaun
    Commented Jan 5, 2017 at 16:11
  • I'm not good in python, but I guess there might be something like this:
    – Vadim
    Commented Jan 5, 2017 at 17:23
  • import arcpy from arcpy import env env.workspace = "D:\data\X.gdb" outws = "D:\data” clip_features = "D:\data\Y.gdb" fcs = arcpy.ListFeatureClasses() i=0 for fc in fcs: out_feature_class = "D:\data\border.gdb" + str(i) i+=1 arcpy.Clip_analysis(fc, clip_features, out_feature_class)
    – Vadim
    Commented Jan 5, 2017 at 17:23
  • Okay. Does Y.gdb only have one featurclass representing one municipal district, or are there multiple featureclasses representing various municipal districts?
    – geoJshaun
    Commented Jan 5, 2017 at 18:01
  • there are several feature classes in Y.gdb. Each FC contains one polygon representing sertain municipal district.
    – Vadim
    Commented Jan 5, 2017 at 18:12

3 Answers 3

0

Merge your municipal polygon features into a single file. Make sure there is a name field in your merged out put to specify the polygon representing it's respective municipal district. This code should do the trick once you replace the proper parameters and file locations:

import arcpy
import os

outputFolder = r"D:/test"
arcpy.env.workspace = r"D:/test/data_3857.gdb"
arcpy.env.overwriteOutput = True

#create clip tool parameters
ClipFeatures = r"D:/test/Y.gdb/borders"
fcs = arcpy.ListFeatureClasses()

# use a search cursor to cycle through each municipal district in your merged file, make sure there is a field representing municipal district name, I'm using "Municipality_name" here
with arcpy.da.SearchCursor(ClipFeatures, ["SHAPE@", "NAME"]) as rows:
    for row in rows:
        # create fgdb in outputFolder variable, this will create a folder with   the Municipal district name and a file gdb named muni_features.gdb to store the clipped features 
        outGDB = os.path.join("MD_" + row[1], "Muni_features.gdb")
        arcpy.CreateFileGDB_management(outputFolder,outGDB)
        print "created", outGDB
        geom = row[0]
        #now clip out each feature in
        for fc in fcs:
            out_feature_class=os.path.join(outputFolder, outGDB,"MD_" + row[1] +  os.path.basename(fc))
            print out_feature_class
            arcpy.Clip_analysis(fc,geom,out_feature_class)
        del row
4
  • I try to run the script several times, but couldn't make it work. Is there any mistake&
    – Vadim
    Commented Jan 5, 2017 at 21:04
  • @Vadim I have added your error messages from you answer as an edit to your question
    – Midavalo
    Commented Jan 5, 2017 at 21:52
  • @ShaunO This is my last attempt to run the code. I've got "Parsing error IndentationError: unexpected indent (line 15)" error message. Here is the code:
    – Vadim
    Commented Jan 5, 2017 at 22:07
  • @Midavalo Actually I failed to paste the code in a readable format.
    – Vadim
    Commented Jan 5, 2017 at 22:22
0

Your indentaion is still off. Copy this code. Paste it into a text file. Name it MyClip.py. In the python window in ArcMap, right click in the interactive window. Choose load. Upload MyClip.py. Press enter. Let the code run.enter image description here

import arcpy
import os

outputFolder = r"D:/test"
arcpy.env.workspace = r"D:/test/data_3857.gdb"
arcpy.env.overwriteOutput = True

#create clip tool parameters
ClipFeatures = r"D:/test/Y.gdb/borders"
fcs = arcpy.ListFeatureClasses()

# use a search cursor to cycle through each municipal district in your merged file, make sure there is a field representing municipal district name, I'm using "Municipality_name" here
with da.SearchCursor(ClipFeatures, ["SHAPE@", "NAME"]) as rows:
    for row in rows:
        # create fgdb in outputFolder variable, this will create a folder with   the Municipal district name and a file gdb named muni_features.gdb to store the clipped features 
        outGDB = os.path.join(outputFolder, row[1], "Muni_features.gdb")
        print "created ", outGDB
        geom = row[0]
        #now clip out each feature in
        for fc in fcs:
            out_feature_class=os.path.join(outGDB,row[1] +  os.path.basename(fc))
            arcpy.Clip_analysis(fc,geom,out_feature_class)
            del fc
        del row
17
  • I did as you said. After loading the code i pressed "Enter" but it just put the cursor down and nothing happend. Than I press "Enter" again and further mistaked appeared: SyntaxError: Missing parentheses in call to 'print' (<string>, line 17) Please, see the picture: i91.fastpic.ru/big/2017/0106/ab/…
    – Vadim
    Commented Jan 6, 2017 at 18:19
  • Are you using python 3x? If so then you need parentheses around the print statement. If you load in the arcmap python window I don't think this should be a problem. The IDE in the picture you provided does not appear the python window in ArcMap. As far as I know, ArcMap desktop uses python 2.7 so if you load it in this environment you should't have this issue. I will edit my most recent answer with a pic.
    – geoJshaun
    Commented Jan 6, 2017 at 18:34
  • additionally you can remove the print statement but be careful not to alter the indentation
    – geoJshaun
    Commented Jan 6, 2017 at 18:36
  • Last time I used ArcGIS Pro (run on python 3.4). Now I tried to paste in ArcMap python window. I pressed Enter twice (it was too long to wait and I wasn't sure this code is actually running). And i got this mistake: "Runtime error Traceback (most recent call last): File "<string>", line 5, in <module> NameError: name 'env' is not defined" The pic is here: i91.fastpic.ru/big/2017/0106/54/…
    – Vadim
    Commented Jan 6, 2017 at 19:11
  • Almost there. I modified the code to get rid of some module imports specific to my tasks. Added arcpy in front of env as I was importing this specifically from the arcpy library in my earlier version. Should work now.
    – geoJshaun
    Commented Jan 6, 2017 at 19:52
-1
    >>> #Creates clips features based on the selected features from a polygon featureclass and exports the clipped features to a specified GDB
... 
... import arcpy
... import os
... 
... outputFolder = r"D:/test"
... env.workspace = r"D:/test/data_3857.gdb"
... env.overwriteOutput = True
... 
... #create clip tool parameters
... ClipFeatures = r"D:/test/Y.gdb/borders"
... fcs = arcpy.ListFeatureClasses()
... 
... #use a search cursor to cycle through each municipal district in your     merged file, make sure there is a field representing municipal district name,   I'm using "Municipality_name" here
... with da.SearchCursor(ClipFeatures, ["SHAPE@", "NAME"]) as rows:
... for row in rows:
... # create fgdb in outputFolder variable, this will create a folder with   the Municipal district name and a file gdb named muni_features.gdb to store the   clipped features 
... outGDB = os.path.join(outputFolder, row[1], "Muni_features.gdb")
... print "created ", outGDB
... geom = row[0]
... #now clip out each feature in
... for fc in fcs:
... out_feature_class=os.path.join(outGDB,row[1] +  os.path.basename(fc))
... arcpy.Clip_analysis(fc,geom,out_feature_class)
... del fc
... del row
... 
Parsing error IndentationError: expected an indented block (line 16)
>>> 
4
  • @ShaunO The above code still doesn't work. The mistake is "Parsing error IndentationError: expected an indented block (line 16)". What it could mean?
    – Vadim
    Commented Jan 6, 2017 at 16:39
  • Did you see my comment above?
    – geoJshaun
    Commented Jan 6, 2017 at 17:40
  • You mean this: sorry, everything from the line introducing the search cursor was accidentally indented. Also, are you importing into ArcMap python window from a text file or are you copying and pasting into the window? – ShaunO 18 hours ago @ Vadim I edited my original code to not have the indent. – ShaunO 18 hours ago Try moving everything from "with.da.SearchCursor...." over to the left four spaces or one back indent. – ShaunO 18 hours ago
    – Vadim
    Commented Jan 6, 2017 at 17:43
  • @ShaunO I pasted the code into python window in arcmap. Also I pasted it in arcgis pro python window. But failed to get it work in both cases. i91.fastpic.ru/big/2017/0106/b0/… i89.fastpic.ru/big/2017/0106/45/…
    – Vadim
    Commented Jan 6, 2017 at 17:48

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.