>>> #////// 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. >>>
rec,x,y 1,-74.0,35.0 2,-74.1,35.1
# 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()
(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?
>>> # 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)