0

I want to write a scrpit that automatize the process of clip and project (in one GCS) to many .shp in folders and subfolders (row downloaded data), with 2 different GCS (CH1903+ or Lambert93).

For that, my script creates a new gdb with 2 feature_class: "Row" and "Clip_Project" then, I'm trying to create a loop to iterate all the feature class in my folders and subfolders.

I understood that the equivalent of "iterate feature class" in python is to define a list with

fcList=arcpy.ListFeatureClasses()

and then you create a loop: for fc in fcList

and add your function

The problem is that this function creates only a list of the .shp in the defined workspace . Since all my raw datas are classified in different subfolders, I want to find a way to get all .shp in all subfolders.

For now, my script looks like this:

# Import arcpy module
import arcpy, sys, string, os

# allow overwrite 
arcpy.env.overwriteOutput = True

# Set Geoprocessing environments
workspace=arcpy.env.workspace = "C:\\PROG"

# Local variables:

Test_classement = "C:\\PROG\\MB_PROG\\Test_classement"  
Coordinate_System = "PROJCS['RGF_1993_Lambert_93',GEOGCS['GCS_RGF_1993',DATUM['D_RGF_1993',SPHEROID['GRS_1980',6378137.0,298.257222101]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]],PROJECTION['Lambert_Conformal_Conic'],PARAMETER['False_Easting',700000.0],PARAMETER['False_Northing',6600000.0],PARAMETER['Central_Meridian',3.0],PARAMETER['Standard_Parallel_1',44.0],PARAMETER['Standard_Parallel_2',49.0],PARAMETER['Latitude_Of_Origin',46.5],UNIT['Meter',1.0]];-35597500 -23641900 10000;-100000 10000;-100000 10000;0.001;0.001;0.001;IsHighPrecision"
Output_Coordinate_System_LV95 = "PROJCS['CH1903+_LV95',GEOGCS['GCS_CH1903+',DATUM['D_CH1903+',SPHEROID['Bessel_1841',6377397.155,299.1528128]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]],PROJECTION['Hotine_Oblique_Mercator_Azimuth_Center'],PARAMETER['false_easting',2600000.0],PARAMETER['false_northing',1200000.0],PARAMETER['scale_factor',1.0],PARAMETER['azimuth',90.0],PARAMETER['longitude_of_center',7.439583333333333],PARAMETER['latitude_of_center',46.95240555555556],UNIT['Meter',1.0]]"

Brute_gdb = "C:\\PROG\\MB_PROG\\Test_classement\\Brute.gdb"
Brute = "C:\\PROG\\MB_PROG\\Test_classement\\Brute.gdb\\Brute"
ClipLV95 = "C:\\PROG\\MB_PROG\\Test_classement\\Brute.gdb\\ClipLV95"

# Process: Create File GDB
arcpy.CreateFileGDB_management(Test_classement, "Brute", "CURRENT")

# Process: Create Feature Dataset
arcpy.CreateFeatureDataset_management(Brute_gdb, "Brute", Coordinate_System)

# Process: Create Feature Dataset (2)
arcpy.CreateFeatureDataset_management(Brute_gdb, "ClipLV95", Output_Coordinate_System_LV95)

#Loop->iterator
fcs = []
walk = arcpy.da.Walk(workspace,datatype="FeatureClass",type="All")
for dirpath, dirnames, filenames in walk:
    for filename in filenames:
    fcs.append(os.path.join(dirpath, filename))

        for fc in fcs:
        root, ext = os.path.splitext(fc)
        arcpy.FeatureClassToFeatureClass_conversion(fc , Brute, root + "_brute" + ext, "")

Execpt for typing and indentation errors, my loop didnt work to rename new feature classes because the root was all the path.

I fixed the last part like this:

workspace_brute=arcpy.env.workspace="C:\\PROG\\MB_PROG\\DONNEES\\"

fcs = []

walk = arcpy.da.Walk(workspace_brute,
                     datatype="FeatureClass",
                     type="All")

for dirpath, dirnames, filenames in walk:
    for filename in filenames:
        fcs.append(os.path.join(dirpath, filename))
        for fc in fcs:
            name=os.path.splitext(filename)[0] # Here we remove the extension from filename
            arcpy.FeatureClassToFeatureClass_conversion(fc, Brute_fd, name + "_brute")
  • 1
    The error is telling you that os.path has no function splittext because you've got an extra "t" in there. The function is called splitext – user2856 Aug 28 '18 at 21:30
  • 1
    Your indentation appears to be incorrect, look at the help file on walk and look at the code samples to see the correct syntax. – Hornbydd Aug 28 '18 at 21:30
  • The error message splittext doesn't match your code splitext – user2856 Aug 29 '18 at 2:12
  • Make sure to indent the for filename in filenames. – Aaron Aug 29 '18 at 2:18
  • Yes it is correct. i created the feature dataset Brute_fd before the loop and I defined it's path as variable, same for the GCS. The script works like this and I have my feature classes all classed in different datasets. I have many shapefiles from different sources, that's why I wanted it in different feature datasets. Thank you for your help :) – Calder Aug 30 '18 at 15:50
2

Some of these points have already been mentioned by others in comments (and you have subsequently fixed some of them already in your updates):

  • your indenting needs fixing (fixed)
  • make sure that your splitext does not have a double 't' (fixed)
  • ensure that your feature dataset ('Brute' or 'Brute_fd'?) exists and has the same coordinate system as the Shapefiles and feature classes - you cannot add a feature class to a feature dataset with a different coordinate system
  • check to make sure that the feature class does not already exist (same workspace, feature dataset, feature class name)
  • make sure that there are no locks on the feature dataset (eg, close all instances of all ArcGIS software, python editors, services, etc, then re-run your Python script - as a test, see if you can create a new feature class in the same location with the same name manually)

Additionally, you don't need the extra for loop, or the fcs list. Just do it all within the existing for loops that you already have.

So something like:

for dirpath, dirnames, filenames in arcpy.da.Walk(workspace):
    for filename in filenames:
        fc = os.path.join(dirpath, filename)
        fcName = os.path.splitext(filename)[0]
        arcpy.FeatureClassToFeatureClass_conversion(fc, Brute, fcName + "_brute")

I prefer to use the arcpy.da.Walk() inline with the for-loop, as I find that quite readable, but that's just personal taste. Others may prefer to use a separate variable walk = arcpy.da.Walk() and then use walk in the for loop line.

  • Hello,Thank you very much for your answers. I made a mistake on the error. The one I don't understand is: Runtime error Traceback (most recent call last): File "<string>", line 16, in <module> File "c:\program files (x86)\arcgis\desktop10.2\arcpy\arcpy\conversion.py", line 1675, in FeatureClassToFeatureClass raise e ExecuteError: ERROR 999999: Error executing function. Failed to execute (FeatureClassToFeatureClass). I tried to run your script but the problem is still the same... Sorry I'm pretty new to python... – Calder Aug 29 '18 at 14:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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