Select to view content in your preferred language

Create feature class in project folder directory

889
6
Jump to solution
06-14-2023 07:42 AM
CCWeedcontrol
Regular Contributor

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).
0 Kudos
1 Solution

Accepted Solutions
AlfredBaldenweck
MVP Regular Contributor

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)

View solution in original post

6 Replies
AlfredBaldenweck
MVP Regular Contributor

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)

CCWeedcontrol
Regular Contributor

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...?

0 Kudos
AlfredBaldenweck
MVP Regular Contributor

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.

0 Kudos
AlfredBaldenweck
MVP Regular Contributor

One thing else to check is that you're using a feature class as the template, not a feature dataset.

0 Kudos
CCWeedcontrol
Regular Contributor

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.

0 Kudos
DanPatterson
MVP Esteemed Contributor

and you might want to add a *.shp to the output filename since the destination appears to be a folder


... sort of retired...