ArcGIS 10.0:Python Window-Try to Create a Feature of 12 Points from Txt-Err

725
4
10-01-2012 04:39 AM
ScottChang
New Contributor II
Hi all,
In a text file, I stored the ID, X-coordinate and Y-coordinate of 12 points - see the attached txt file.  I used the Python Window of my ArcGIS 10.0 to execute the following Python script:
>>> #////// newWritingGeometries12Points.py ////////////
... # Create a new "Point" feature class using a text file of coordinates.
... #   Each coordinate entry is semicolon delimited in the format of ID;X;Y
... # Import ArcPy and other required modules
... #
... import arcpy
... from arcpy import env
... import fileinput
... import string
... import os
... env.overwriteOutput = True
... # Get the coordinate ASCII file
... #
... #infile = arcpy.GetParameterAsText(0)
... infile = r"C:\TEMP\WritingGeometries\WritingGeometries_12Points.txt"
... # Get the output feature class
... # fcname = arcpy.GetParameterAsText(1)
... fcname = r"C:\TEMP\BS_Test.gdb\Points12_fc"
... # Get the template feature class
... #
... # template = arcpy.GetParameterAsText(2)
... template = r"C:\TEMP\BS_Test.gdb\bsInFeatureClass"
... try:
...    # Create the output feature class
...    #
...    arcpy.CreateFeatureclass_management(os.path.dirname(fcname),
...                                        os.path.basename(fcname), 
...                                        "Point", template)
...    # Open an insert cursor for the new feature class
...    #
...    cur = arcpy.InsertCursor(fcname)
...    # Create an array and point object needed to create features
...    #
...    pointArray = arcpy.Array()
...    pnt = arcpy.Point()
...    # Initialize a variable for keeping track of a feature's ID.
...    #
...    ID = -1 
...    for line in fileinput.input(infile): # Open the input file
...       # set the point's ID, X and Y properties
...       #
...       pnt.ID, pnt.X, pnt.Y = string.split(line,";")
...       print pnt.ID, pnt.X, pnt.Y
...       if ID == -1:
...          ID = pnt.ID
...       # Add the point to the feature's array of points
...       #   If the ID has changed, create a new feature
...       #
...       if ID != pnt.ID:
...          # Create a new row or feature, in the feature class
...          #
...          feat = cur.newRow()
...          # Set the geometry of the new feature to the array of points
...          #
...          feat.shape = pointArray
...          # Insert the feature
...          #
...          cur.insertRow(feat)
...          pointArray.removeAll()
...       pointArray.add(pnt)
...       ID = pnt.ID
...    # Add the last feature
...    #
...    feat = cur.newRow()
...    feat.shape = pointArray
...    cur.insertRow(feat)
...       
...    pointArray.removeAll()
...    fileinput.close()
...    del cur
...    arcpy.SetParameterAsText (0, fcname) # newly added 18Sept2012, per BS.
... except Exception as e:
...    print e.message
... 
1   -61845879.0968   45047635.4861

2   -3976119.96791   46073695.0451

ERROR 999999: Error executing function.

>>> 

I have no ideas why I got: ERROR 999999: Error executing function.  Please kindly help and tell me where I made mistakes and how to resolve this error.

Thanks,
Scott Chang
Tags (2)
0 Kudos
4 Replies
curtvprice
MVP Esteemed Contributor
We could dive in deeper on writing geometries, but if all you are working with is points, it's a LOT easier to create a csv file with field headings. Name it "myPoints.csv" and ArcGIS will see it as a table. You can feed that table directly to the MakeXYEventLayer tool. If you need a dataset, just follow that with Copy Features.

Here's an example file "test.csv":

rec,x,y
1,-74.0,35.0
2,-74.1,35.1


See the tool help for Make XY Event Layer for an example. Hope this helps!
0 Kudos
ScottChang
New Contributor II
Hi Curtis,  Thanks for your valuable response.

I want to do the "Make XY Event Layer" as you said.  I started to make a csv file for my 12 points (5 Columns: ID, Lot_ID, X_coord, Y_coord, Z_coord) and modified the MakeXYLayer.py to get the input data (ID, Lot_ID, X_coord, Y_coord, and Z), spRef, etc. into the table, geodatabase and mxd Map of ArcGIS 10.0 - please see my draft csv table (sc12points.csv) and scMakelayer.txt attached in this post.
# scMakeXYlayer.py   for 12 points
# Description: Creates an XY layer and exports it to a layer file
# Author: ESRI - modified by Scott Chang (Date:  04 Oct 2012)

# import system modules 
import arcpy
from arcpy import env

# Set environment settings
env.workspace = "C:/TEMP/WritingGeometries/sc12points"
 
try:
    # Set the local variables
    in_Table = "firestations.csv"
    x_coords = "POINT_X"
    y_coords = "POINT_Y"
    z_coords = "POINT_Z"
    out_Layer = "firestations_layer"
    saved_Layer = r"c:\output\firestations.lyr"
 
    # Set the spatial reference
    spRef = r"Coordinate Systems\Projected Coordinate Systems\Utm\Nad 1983\NAD 1983 UTM Zone 11N.prj"
 
    # Make the XY event layer...
    arcpy.MakeXYEventLayer_management(in_Table, x_coords, y_coords, out_Layer, spRef, z_coords)
 
    # Print the total rows
    print arcpy.GetCount_management(out_Layer)
 
    # Save to a layer file
    arcpy.SaveToLayerFile_management(out_Layer, saved_Layer)
 
except:
    # If an error occurred print the message to the screen
    print arcpy.GetMessages()
  

I need your technical exerptise for resolving the following 3 things:
(1)  In your "Test.csv", you have 3 columns: rec, X, Y. In my sc12points.csv, I have 5 Columns: ID, Lot_ID, X_coord, Y_coord, Z_coord). How can I read these 5 columns in the "try:" section of the Python Script of my sc12points.txt (this will be changed to .py)?  My real problem is: how can I write the ArcPy-Python code statements to read the ID and Lot_ID in the "try:" section?
(2)  How can I specify the World 84 coordinates system in the spRef?
(3) I used my Excel 2007 to create my 12points.csv file. I just typed in the names of 5 columns in the first row and then typed in (i) 1, 2, 3, ...., 12 in ID, (ii) Lot_ID, copied the values of X_coord and Y_coord from my old text file to my new csv file, and (iii) leave all the indendations to the right.  Do I have to change (i) the types of ID and Lot_ID to integer, (ii) the types of X_coord, Y_coord and Z_coord to float, and (iii) indentation of all the input data to the left?

Please kindly help and advise.

Thanks,
Scott Chang
0 Kudos
curtvprice
MVP Esteemed Contributor
 
(1)  In your "Test.csv", you have 3 columns: rec, X, Y. In my sc12points.csv, I have 5 Columns: ID, Lot_ID, X_coord, Y_coord, Z_coord). How can I read these 5 columns in the "try:" section of the Python Script of my sc12points.txt (this will be changed to .py)?  My real problem is: how can I write the ArcPy-Python code statements to read the ID and Lot_ID in the "try:" section?

As long as they are the file, they will come in as columns in the table. You don't need to specify them in the Python code, just in the input file.

(2)  How can I specify the World 84 coordinates system in the spRef?

Specify the appropriate path for spRef. I think what you want is:

spRef = r"Coordinate Systems\Geographic Coordinate Systems\World\WGS 1984"


(3) I used my Excel 2007 to create my 12points.csv file. I just typed in the names of 5 columns in the first row and then typed in (i) 1, 2, 3, ...., 12 in ID, (ii) Lot_ID, copied the values of X_coord and Y_coord from my old text file to my new csv file, and (iii) leave all the indendations to the right.  Do I have to change (i) the types of ID and Lot_ID to integer, (ii) the types of X_coord, Y_coord and Z_coord to float, and (iii) indentation of all the input data to the left?


I don't think it matters that much, whitespace around column delimeters is usualy ignored,  and the field types are defined from the data as it is read.
0 Kudos
ScottChang
New Contributor II
Hi Curtis,
In the Python Window of my ArcGIS 10.0, I executed the following Python script:
>>> # scMakeXYlayer.py   for 12 points
... # Description: Creates an XY layer and exports it to a layer file
... # Author: ESRI - modified by Scott Chang (Date:  04 Oct 2012)
... # import system modules 
... import arcpy
... from arcpy import env
... # Set environment settings
... env.workspace = "C:/TEMP/WritingGeometries/sc12points"
...  
... try:
...     # Set the local variables
...     in_Table = "firestations.csv"
...     x_coords = "POINT_X"
...     y_coords = "POINT_Y"
...     z_coords = "POINT_Z"
...     out_Layer = "firestations_layer"
...     saved_Layer = r"c:\TEMP\firestations.lyr"
...  
...     # Set the spatial reference
...     spRef = r"Coordinate Systems\Projected Coordinate Systems\Utm\Nad 1983\NAD 1983 UTM Zone 11N.prj"
...  
...     # Make the XY event layer...
...     arcpy.MakeXYEventLayer_management(in_Table, x_coords, y_coords, out_Layer, spRef, z_coords)
...  
...     # Print the total rows
...     print arcpy.GetCount_management(out_Layer)
...  
...     # Save to a layer file
...     arcpy.SaveToLayerFile_management(out_Layer, saved_Layer)
...  
... except:
...     # If an error occurred print the message to the screen
...     print arcpy.GetMessages()
... 
Executing: MakeXYEventLayer firestations.csv POINT_X POINT_Y firestations_layer "PROJCS['NAD_1983_UTM_Zone_11N',GEOGCS['GCS_North_American_1983',DATUM['D_North_American_1983',SPHEROID['GRS_1980',6378137.0,298.257222101]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]],PROJECTION['Transverse_Mercator'],PARAMETER['False_Easting',500000.0],PARAMETER['False_Northing',0.0],PARAMETER['Central_Meridian',-117.0],PARAMETER['Scale_Factor',0.9996],PARAMETER['Latitude_Of_Origin',0.0],UNIT['Meter',1.0]];-5120900 -9998100 10000;-100000 10000;-100000 10000;0.001;0.001;0.001;IsHighPrecision" POINT_Z
Start Time: Tue Oct 09 14:47:53 2012
Failed to execute. Parameters are not valid.
ERROR 000732: XY Table: Dataset firestations.csv does not exist or is not supported
Failed to execute (MakeXYEventLayer).
Failed at Tue Oct 09 14:47:53 2012 (Elapsed Time: 0.00 seconds)


I have no ideas why I got "Failed to execute. Parameters are not valid.  ERROR 000732: XY Table....

Please kindly help and tell me how I can fix the problem.

Thanks,
Scott Chang

P. S.  This Forum does not take the csv file. So I used the txt file attached in this post for the input data.
0 Kudos