<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Programmatically Creating a Feature Class in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/programmatically-creating-a-feature-class/m-p/272806#M21076</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Mainly the two types of feature classes we will be creating are either a shapefile or a layer in a file geodatabase.&amp;nbsp; It's always going to be a file on the user's machine. No versioning or SDE.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I'm not setting a scratch workspace as far as I know.&amp;nbsp; At least not in the python code.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Im using ArcMap/ArcDesktop 10.0 Build 2414.&amp;nbsp; No service packs.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I can't use the template because I'm getting the list of fields from a service my python code calls and the fields can be different for every call so I don't have a predetermined format to base a template off of.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Here is some test code that replicates the issue.&amp;nbsp; I had to hand type this since the machine I'm working on doesn't have internet access so please excuse any typos.&amp;nbsp; It runs on the work machine.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
import arcpy
import time

fc_workspace = "c:/test"
fc_name = "test.shp"
fc_fields = (
 ("a", "TEXT", None, None, 50, "", "NULLABLE", "NON_REQUIRED"),
 ("b", "LONG", 8, None, None, "", "NULLABLE", "NON_REQUIRED"),
 ("c", "SHORT", 4, None, None, "", "NULLABLE", "NON_REQUIRED"),
 ("d", "DOUBLE", 11, 8, None, "", "NULLABLE", "NON_REQUIRED"),
 ("e", "FLOAT", 5, 2, None, "", "NULLABLE", "NON_REQUIRED"),
 ("f", "DATE", None, None, None, "", "NULLABLE", "NON_REQUIRED")
 )

arcpy.env.workspace = fc_workspace

total_start = time.clock()

start = time.clock()
fc = arcpy.CreateFeatureclass_management(fc_workspace, fc_name, "POINT", spatial_reference="c:/Program Files/ArcGIS/Desktop10.0/Coordinate Systems/Geographic Coordinate Systems/World/WGS 1984.prj")
end = time.clock()
arcpy.AddMessage("Create Feature Class %.3f" % (end - start))

start = time.clock()
for fc_field in fc_fields:
 arcpy.AddField_management(fc, *fc_field)
end = time.clock()
arcpy.AddMessage("Create Fields %.3f" % (end - start))

start = time.clock()
arcpy.DeleteField_management(fc, "Id")
end = time.clock()
arcpy.AddMessage("Delete Id Field %.3f" % (end - start))

total_end = time.clock()
arcpy.AddMessage("Total %.3f" % (total_end - total_start))
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Output when I run it just as a regular python script:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
Create Feature Class 0.313
Create Fields 0.313
Delete Id Field 0.044
Total 0.670
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Output when I bind the script to a ArcToolbox Tool.&amp;nbsp; I just create a standard script tool with no parameters.&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
Executing: testcreatefc
Start Time: Mon Dec 13 19:42:04 2010
Running script testcreatefc...
Create Feature Class 0.628
Create Fields 2.713
Delete Id Field 0.627
Total 3.968
Completed script testcreatefc...
Succeeded at Mon Dec 13 19:42:09 2010
(Elapsed Time: 5.00 seconds)
&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sat, 11 Dec 2021 13:19:27 GMT</pubDate>
    <dc:creator>DanielLozier</dc:creator>
    <dc:date>2021-12-11T13:19:27Z</dc:date>
    <item>
      <title>Programmatically Creating a Feature Class</title>
      <link>https://community.esri.com/t5/python-questions/programmatically-creating-a-feature-class/m-p/272804#M21074</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I'm trying to programmatically create a feature class by doing the following:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Calling CreateFeatureclass_management() to create the feature class&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Calling AddField_management() in a loop to add all of the fields. (About 50 in total)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Calling DeleteField_management() to remove the default 'Id' field created by the CreateFeatureclass method.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;When I run the script with the above code from the command line it runs 'relatively' fast (takes about 5 secs) but, once I bind the script to an ArcToolbox Tool (no changes to the script) it takes significantly longer (3min 20sec).&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;One thing that might point as to why its taking so long is that if I add the line 'arcpy.env.workspace="c:/workspace"' it speeds up some (1min 10sec) but still nowhere near the command line.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Also in general is there a better/faster way to programmatically create a feature class from scratch? I can't use the template parameter to the createFeatureclass_management method.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Dec 2010 04:34:26 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/programmatically-creating-a-feature-class/m-p/272804#M21074</guid>
      <dc:creator>DanielLozier</dc:creator>
      <dc:date>2010-12-10T04:34:26Z</dc:date>
    </item>
    <item>
      <title>Re: Programmatically Creating a Feature Class</title>
      <link>https://community.esri.com/t5/python-questions/programmatically-creating-a-feature-class/m-p/272805#M21075</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;It must be the different default environment settings.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;We need more detail of your setup to guess the issues.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Where are you creating the featureclass? SDE, versioning, local geodatabase?&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Have you set your scratch workspace too?&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;These will all have defaults when you add a tool&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;What version and service pack?&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Why can't you use a template?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2010 06:03:14 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/programmatically-creating-a-feature-class/m-p/272805#M21075</guid>
      <dc:creator>KimOllivier</dc:creator>
      <dc:date>2010-12-11T06:03:14Z</dc:date>
    </item>
    <item>
      <title>Re: Programmatically Creating a Feature Class</title>
      <link>https://community.esri.com/t5/python-questions/programmatically-creating-a-feature-class/m-p/272806#M21076</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Mainly the two types of feature classes we will be creating are either a shapefile or a layer in a file geodatabase.&amp;nbsp; It's always going to be a file on the user's machine. No versioning or SDE.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I'm not setting a scratch workspace as far as I know.&amp;nbsp; At least not in the python code.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Im using ArcMap/ArcDesktop 10.0 Build 2414.&amp;nbsp; No service packs.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I can't use the template because I'm getting the list of fields from a service my python code calls and the fields can be different for every call so I don't have a predetermined format to base a template off of.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Here is some test code that replicates the issue.&amp;nbsp; I had to hand type this since the machine I'm working on doesn't have internet access so please excuse any typos.&amp;nbsp; It runs on the work machine.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
import arcpy
import time

fc_workspace = "c:/test"
fc_name = "test.shp"
fc_fields = (
 ("a", "TEXT", None, None, 50, "", "NULLABLE", "NON_REQUIRED"),
 ("b", "LONG", 8, None, None, "", "NULLABLE", "NON_REQUIRED"),
 ("c", "SHORT", 4, None, None, "", "NULLABLE", "NON_REQUIRED"),
 ("d", "DOUBLE", 11, 8, None, "", "NULLABLE", "NON_REQUIRED"),
 ("e", "FLOAT", 5, 2, None, "", "NULLABLE", "NON_REQUIRED"),
 ("f", "DATE", None, None, None, "", "NULLABLE", "NON_REQUIRED")
 )

arcpy.env.workspace = fc_workspace

total_start = time.clock()

start = time.clock()
fc = arcpy.CreateFeatureclass_management(fc_workspace, fc_name, "POINT", spatial_reference="c:/Program Files/ArcGIS/Desktop10.0/Coordinate Systems/Geographic Coordinate Systems/World/WGS 1984.prj")
end = time.clock()
arcpy.AddMessage("Create Feature Class %.3f" % (end - start))

start = time.clock()
for fc_field in fc_fields:
 arcpy.AddField_management(fc, *fc_field)
end = time.clock()
arcpy.AddMessage("Create Fields %.3f" % (end - start))

start = time.clock()
arcpy.DeleteField_management(fc, "Id")
end = time.clock()
arcpy.AddMessage("Delete Id Field %.3f" % (end - start))

total_end = time.clock()
arcpy.AddMessage("Total %.3f" % (total_end - total_start))
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Output when I run it just as a regular python script:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
Create Feature Class 0.313
Create Fields 0.313
Delete Id Field 0.044
Total 0.670
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Output when I bind the script to a ArcToolbox Tool.&amp;nbsp; I just create a standard script tool with no parameters.&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
Executing: testcreatefc
Start Time: Mon Dec 13 19:42:04 2010
Running script testcreatefc...
Create Feature Class 0.628
Create Fields 2.713
Delete Id Field 0.627
Total 3.968
Completed script testcreatefc...
Succeeded at Mon Dec 13 19:42:09 2010
(Elapsed Time: 5.00 seconds)
&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 13:19:27 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/programmatically-creating-a-feature-class/m-p/272806#M21076</guid>
      <dc:creator>DanielLozier</dc:creator>
      <dc:date>2021-12-11T13:19:27Z</dc:date>
    </item>
    <item>
      <title>Re: Programmatically Creating a Feature Class</title>
      <link>https://community.esri.com/t5/python-questions/programmatically-creating-a-feature-class/m-p/272807#M21077</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I got exactly the opposite result.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I ran the example on my laptop, Windows 7, ArcGIS 10 SP1, Intel Core I5.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Running from Pythonwin:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Total 0.751 (Units??)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Elapsed time 00:00:02.999500 ie 3 seconds&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Running from ArcMap as a tool, &lt;/SPAN&gt;&lt;STRONG&gt;foreground,in process&lt;/STRONG&gt;&lt;SPAN&gt;:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Total 0.913&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Elapsed time&amp;nbsp; 0:00:00.95100 ie less than 1 second&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Turning off foreground process&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Total 0.335&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Turning off inprocess&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Total 0.338&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Just for fun I pasted it into the interactive Python window:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Total 10.395&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Elapsed time 10.434 seconds&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;This seems to be because each command was executed separately&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;with the result log echoed to the window&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;It will make a big difference if you run the script out of process.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 14 Dec 2010 20:21:44 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/programmatically-creating-a-feature-class/m-p/272807#M21077</guid>
      <dc:creator>KimOllivier</dc:creator>
      <dc:date>2010-12-14T20:21:44Z</dc:date>
    </item>
    <item>
      <title>Re: Programmatically Creating a Feature Class</title>
      <link>https://community.esri.com/t5/python-questions/programmatically-creating-a-feature-class/m-p/272808#M21078</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Alright so I think I've figured out the problem. Or at least a large chunk of it.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;All of the geoprocessing tools in this script seem to do something with the arcpy.env.workspace and arcpy.env.scratchWorkspace when the script is attached to a geoprocessing tool even when the feature class I'm working with is not in either of these workspaces.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;It makes sense to me why CreateFeatureclass, AddField, and DeleteField could use arcpy.env.workspace but I don't understand why they would use scratchWorkspace.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Since I wasn't explicitly setting the scratchWorkspace in the script the tools seem to do "more work" in the default geodatabase for my current map document in ArcMap.&amp;nbsp; In my case the default Geodatabase was the default one that ESRI creates, My Documents/ArcGIS/Default.gdb.&amp;nbsp; The corporate environment I'm working in puts our My Documents folder on a network drive so that is why it was running so slow.&amp;nbsp; I said "more work" because the 3 arcpy tools still seem to do something with the default geodatabase even when I explicitly set the workspace and scratchWorkspace to the location where the feature class should be created. (i.e. in the script above when I added the line arcpy.env.scratchWorkspace = fc_workspace it only sped up the total time from 4 seconds to 2 seconds.&amp;nbsp; The units in the output are seconds)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;If I set the default geodatabase to one on my local drive and add "arcpy.env.scratchWorkspace = fc_workspace" to the script I got the same results you're seeing where when attached to a geoprocessing tool the script is 20-25% slower.&amp;nbsp; I'm not looking at the total time this sample script takes to run (i.e. the "Elapsed time XX:XX:XX") because that ~2 second difference between it and "Total X.XXX" that occurs when the script runs standalone is a fixed amount.&amp;nbsp; The 2 seconds of additional time happens on the "import arcpy" line and is the same no matter how many calls to arcpy geoprocessing tools I make.&amp;nbsp; My guess is that the standalone script has to check my ArcDesktop license and/or fire up some process which is already done when running from ArcMap.&amp;nbsp; My real code makes hundreds of arcpy calls so those 2 seconds get drowned out.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I tried turning off inprocess but that caused the call to AddField_management in the script to fail with an error of "(&amp;lt;class 'arcgisscripting.ExecuteError'&amp;gt;, ExecuteError('ERROR 000582: Error occurred during execution.\n',), &amp;lt;traceback object at 0x0516D030&amp;gt;)".&amp;nbsp; I'm guessing that's a bug that was fixed in sp1.&amp;nbsp; Once I can get a hold of sp1 I will try it again.&amp;nbsp; Just fired off the request to the bureaucracy.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 15 Dec 2010 04:04:56 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/programmatically-creating-a-feature-class/m-p/272808#M21078</guid>
      <dc:creator>DanielLozier</dc:creator>
      <dc:date>2010-12-15T04:04:56Z</dc:date>
    </item>
  </channel>
</rss>

