I am new to Python.
I am trying to create a new Feature Class within a long script but before I do so, I am running it separately to make sure I have the syntax correct. The code below mimics the one in the ESRI help documentation.
# create new featureclass in which to put points
import arcpy
arcpy.env.workspace = "C:/Test2"
out_path = "C:/Test2/GIS"
out_name = "RhinoLines.shp"
geometry_type = "POLYLINE"
spatial_reference = arcpy.Describe("C:/Test2/GIS/RhinoTracks.shp").spatialReference
print spatial_reference.Name
if arcpy.Exists(out_name):
arcpy.Delete_management(out_name)
else:
arcpy.CreateFeatureclass_management(out_path, out_name, geometry_type, spatial_reference)
But I keep getting this error:
GCS_WGS_1984
Traceback (most recent call last):
File "C:\Python27\ArcGIS10.3\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 326, in RunScript
exec codeObject in __main__.__dict__
File "C:\Test2\Scripts\stupid.py", line 20, in <module>
arcpy.CreateFeatureclass_management(out_path, out_name, geometry_type, spatial_reference)
File "C:\Program Files (x86)\ArcGIS\Desktop10.3\ArcPy\arcpy\management.py", line 1807, in CreateFeatureclass
raise e
RuntimeError: Object: Error in executing tool
-
Welcome to GIS SE! As a new user be sure to take the Tour where you will see that there should normally be only one question asked per question. Add Field is done in a separate line of code. I removed some of the words from your question that fall under chit chat but otherwise it was well structured and it is good to see that you understand the need to present a working code snippet.PolyGeo– PolyGeo ♦2015年11月21日 20:29:18 +00:00Commented Nov 21, 2015 at 20:29
-
Add Field as a separate line of code and not a question that should be asked in conjunction with the one I have asked here is noted. I'll take the Tour and follow direction.nmichelg– nmichelg2015年11月22日 01:40:44 +00:00Commented Nov 22, 2015 at 1:40
2 Answers 2
Python is case sensitive.
arcpy.env.Workspace = "C:/Test2/GIS"
should be:
arcpy.env.workspace = "C:/Test2/GIS"
I just ran the code below as a successful test:
import arcpy
arcpy.env.workspace = "C:/Test2"
out_path = "C:/Test2/GIS"
out_name = "RhinoLines.shp"
geometry_type = "POLYLINE"
spatial_reference = arcpy.Describe("C:/Test2/GIS/RhinoTracks.shp").spatialReference
print spatial_reference.Name
if arcpy.Exists(out_name):
arcpy.Delete_management(out_name)
else:
print "Creating {0} in {1} with spatial reference {2} and {3} features".format(out_name,out_path,spatial_reference.Name,geometry_type)
arcpy.CreateFeatureclass_management(out_path, out_name, geometry_type, "#", "#", "#", spatial_reference)
The key difference was that you were providing the spatial reference object in the position where CreateFeatureclass_management() expected a template feature class. I used three "#"
to skip the optional parameters and place the spatial reference object where the tool expected it.
-
I just tried that and got the same result.nmichelg– nmichelg2015年11月21日 20:27:07 +00:00Commented Nov 21, 2015 at 20:27
-
In that case I suspect that how you are creating your spatial reference object may be astray but I am on my mobile phone so not in a position to test at the moment.2015年11月21日 20:36:24 +00:00Commented Nov 21, 2015 at 20:36
-
I am able to successfully print the name of the sr object.nmichelg– nmichelg2015年11月21日 21:25:50 +00:00Commented Nov 21, 2015 at 21:25
-
1The code does not check to see if the shapefile already exists and delete it prior to creating a new shapefile. If you have run this code more than once, the code should fail each time it tries to create the shapefile that already exists. Add if arcpy.Exists(out_name): followed by arcpy.Delete_management(out_name) prior to trying to create the shapefile to let you start over or else make the shapefile creation only occur when: if not arcpy.Exists(out_name): is true.Richard Fairhurst– Richard Fairhurst2015年11月21日 21:59:00 +00:00Commented Nov 21, 2015 at 21:59
-
Thankyou Richard I have implemented your instructions with an if else statement and curiously get the same result. I would like to post my new code here but am still learning how to access this forum so I don't want to post unformatted content.nmichelg– nmichelg2015年11月22日 01:10:04 +00:00Commented Nov 22, 2015 at 1:10
Welcome to the club. This structure saved me a lot of frustration while debugging scripts
import arcpy, traceback, os, sys
try:
# your code here
except:
message = "\n*** PYTHON ERRORS *** "; showPyMessage()
message = "Python Traceback Info: " + traceback.format_tb(sys.exc_info()[2])[0]; showPyMessage()
message = "Python Error Info: " + str(sys.exc_type)+ ": " + str(sys.exc_value) + "\n"; showPyMessage()
Also in geoprocessing options I strongly suggest set editor to something like this
C:\Python27\ArcGIS10.1\Lib\idlelib\idle.pyw
I don't understand why yours is pointing to site packages.