I am trying to create an empty feature class in the projects folder directory, but I get the following error.
How can use arcpy.management.CreateFeatureclass to create an empty feature class in the projects folder?
import arcpy
import os, sys
import time
from arcpy import env
t1 = time.time()
aprx = arcpy.mp.ArcGISProject("CURRENT")
m = aprx.listMaps()[0]
lyr = m.listLayers("Soil")[0]
arcpy.env.workspace = os.path.dirname(aprx.filePath)
wp = os.path.dirname(aprx.filePath)
outPath = wp
NewLyr = "Soils_New"
template = "Soil"
out_fc = arcpy.management.CreateFeatureclass(outPath, NewLyr, template)
Error
Traceback (most recent call last):
File "<string>", line 20, in <module>
File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\management.py", line 3451, in CreateFeatureclass
raise e
File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\management.py", line 3448, in CreateFeatureclass
retval = convertArcObjectToPythonObject(gp.CreateFeatureclass_management(*gp_fixargs((out_path, out_name, geometry_type, template, has_m, has_z, spatial_reference, config_keyword, spatial_grid_1, spatial_grid_2, spatial_grid_3, out_alias), True)))
File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\geoprocessing\_base.py", line 512, in <lambda>
return lambda *args: val(*gp_fixargs(args, True))
arcgisscripting.ExecuteError: ERROR 160142: The Field type is invalid or unsupported for the operation.
Failed to execute (CreateFeatureclass).
Solved! Go to Solution.
Currently you have your template in the geometry_type parameter space.
Try either of the following:
outPath = wp
NewLyr = "Soils_New"
templateFC = "Soil"
out_fc = arcpy.management.CreateFeatureclass(outPath, NewLyr, template = templateFC)
#OR
out_fc = arcpy.management.CreateFeatureclass(outPath, NewLyr, "", templateFC)
Create Feature Class (Data Management)—ArcGIS Pro | Documentation
Editing to explain farther:
arcpy.management.CreateFeatureclass(out_path, out_name, {geometry_type}, {template}, {has_m}, {has_z}, {spatial_reference}, {config_keyword}, {spatial_grid_1}, {spatial_grid_2}, {spatial_grid_3}, {out_alias})
Each parameter in brackets is optional. The order still matters, so you have to account for them (example 2) UNLESS you specifically call each one(example 1)
Currently you have your template in the geometry_type parameter space.
Try either of the following:
outPath = wp
NewLyr = "Soils_New"
templateFC = "Soil"
out_fc = arcpy.management.CreateFeatureclass(outPath, NewLyr, template = templateFC)
#OR
out_fc = arcpy.management.CreateFeatureclass(outPath, NewLyr, "", templateFC)
Create Feature Class (Data Management)—ArcGIS Pro | Documentation
Editing to explain farther:
arcpy.management.CreateFeatureclass(out_path, out_name, {geometry_type}, {template}, {has_m}, {has_z}, {spatial_reference}, {config_keyword}, {spatial_grid_1}, {spatial_grid_2}, {spatial_grid_3}, {out_alias})
Each parameter in brackets is optional. The order still matters, so you have to account for them (example 2) UNLESS you specifically call each one(example 1)
The soils layer resides in an SDE feature class/ feature dataset and I have other shapefiles and SDE feature classes in this project. No matter what sde feature class I changed the templateFC to, if it's in a feature class I get the error I posted. It works fine if the templateFC is a shapefile...?
I'm not sure what to tell you, that error that you posted before specifically relates to an error in what you gave it for parameters. Is it possible it's giving you a different error?
I just tested myself on a feature class in an enterprise gdb (accessed through an SDE file) and it worked fine.
One thing else to check is that you're using a feature class as the template, not a feature dataset.
After testing and testing, I figured out why I kept getting that error, at least I think.
You have to be the owner/creator of that of that enterprise SDE gdb. The soils feature class was from another departments enterprise SDE gdb. It works on my enterprise SDE gdb but if the template is from another enterprise SDE gdb not owned or created by you it won't work. I've tested this a dozen times, that is how it functions for me.
and you might want to add a *.shp to the output filename since the destination appears to be a folder