ArcGIS10.0-PythonWindow:Make XY Event Layer-How to set Workspace Environment Setting?

3235
10
10-10-2012 09:45 AM
ScottChang
New Contributor II
Hi all,
I want to use the tutorials "Make XY Event Layer" example (stand-alone Python script) and Using environment settings in Python of ArcGIS 10.0 to create a 12-Point feature and its msd Map for my ArcGIS project.  The following is the stand-alone Python script of  "Make XY Event Layer" from the HELP of ArcGIS 10.0:
# MakeXYLayer.py
# Description: Creates an XY layer and exports it to a layer file

# import system modules 
import arcpy
from arcpy import env

# Set environment settings
env.workspace = "C:/data"
 
try:
    # Set the local variables
    in_Table = "firestations.dbf"
    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 made a csv file (ID, Lot_ID, x-coords, y-coords, z-coords) for my 12 points (saved as sc12points.xls and sc12points.csv) in my C:/TEMP/WritingGeometries/ folder. Also I have a geodatabase "BS_Test" in my C:/TEMP folder.  In  the Using environment settings in Python of ArcGIS 10.0, I saw env.workspace = "c:/St_Johns/data.gdb".  I know that I have to do/process the sc12points.csv file in the C:/TEMP/WritingGeometries to my C:/TEMP/BS_Test geodatabase - see the attached file.  But, (1) I have no ideas how to complete this process. (2) I have 2 more questions to ask: (2a) is in_Table = "sc12points.csv" right to use in my case? (2b) are x_coords = "POINT_X" and y_coords = "POINT_Y" right to use in my case? 

Please kindly help and advise.

Thanks,
Scott Chang
Tags (2)
0 Kudos
10 Replies
ChristopherThompson
Occasional Contributor III
first thing you need to do is call the env module -you do that by first importing arcpy then importing the env module from arcpy like this:

import arcpy
from arcpy import env

then you set the workspace by using the env.workspace function like this;

env.workspace = 'your file path'

In your case it would look like the following:

import arcpy
from arcpy import env
env.workspace = r'C:TEMP\BS_Test.gdb'


you will have to refer to the .csv file by using its full pathname, typically its convenient to set a variable equal to this to avoid having to type out long pathnames frequently. That would look like:

my_csv = r'C:\TEMP\WritingGeometries\sc12points.csv'

As far as your other two questions:
2a) yes, sc12points.csv is your in_table though given above you could change in_table to my_csv.
Both of these approaches end up with the same result:
my_csv = r'C:\TEMP\WritingGeometries\sc12points.csv'
arcpy.MakeXYEventLayer_management(my_csv, x_coords, y_coords, out_Layer, spRef, z_coords)

is the same as:

arcpy.MakeXYEventLayer_management(r'C:\TEMP\WritingGeometries\sc12points.csv'
, x_coords, y_coords, out_Layer, spRef, z_coords)


where the first method wins is when you are making many references to that table - its basically short cut notation to refer to it.

2b) in the example the names x_coords and y_coords are references to the names of the fields in the table which are "Point_X" and "Point_Y". In your case it looks like your fields are named x-coord and y-coord so that bit of code would look like this instead:
x_coords = "x-coord"
y_coords = "y-coord" 


That said, the hyphens in your field names (x-coords) MAY give you fits as its generally recommended not to unclude special characters or spaces in a field name. So you may want to stick with the example and use an underscore instead.
0 Kudos
ScottChang
New Contributor II
Hi Christopher,  Thanks for your valuable response.

I followed your instructions to create a new Python script (chrisMakeXYlayer.py):
>>> # chrisMakeXYlayer.py   for 12 points
... # Description: Creates an XY layer and exports it to a layer file
... # Author: ESRI - modified by Scott Chang per Christopher Thompson (Date:  11 Oct 2012)
... # import system modules 
... import arcpy
... from arcpy import env
... # Set environment settings per Chris T.
... env.workspace = r'C:\TEMP\BS_Test.gdb'
...  
... try:
...     # Set the local variables
...     # in_Table = "firestations.csv" 
...     my_csv = r'C:\TEMP\WritingGeometries\sc12points.csv'
...  
...     x_coords = "x-coord"
...     y_coords = "y-coord"
...     z_coords = "z-coord"
...     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(my_csv, 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 C:\TEMP\WritingGeometries\sc12points.csv x-coord y-coord 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" z-coord
Start Time: Thu Oct 11 14:40:20 2012
Failed to execute. Parameters are not valid.
ERROR 000728: Field x-coord does not exist within table
ERROR 000728: Field y-coord does not exist within table
ERROR 000728: Field z-coord does not exist within table
Failed to execute (MakeXYEventLayer).
Failed at Thu Oct 11 14:40:20 2012 (Elapsed Time: 0.00 seconds)

I have no ideas why I got these errors.  Please kindly help and advise again.
Thanks,
Scott Chang
0 Kudos
ChristopherThompson
Occasional Contributor III
Failed to execute. Parameters are not valid.
ERROR 000728: Field x-coord does not exist within table
ERROR 000728: Field y-coord does not exist within table
ERROR 000728: Field z-coord does not exist within table


Take a look at your csv - did you rename those fields as i'd suggested to do away with the hyphens? The errors indicate that there isn't a field of the name (i.e. x-coord) in your csv file.

Make sure that the names you give on the right side of the statements below exactly match the names of the fields in your CSV.  It could also be that the hyphen is causing a problem as those are not usually valid characters in a field name.

x_coords = "x-coord"
y_coords = "y-coord"
z_coords = "z-coord"
0 Kudos
ScottChang
New Contributor II
Hi Christopher,

I just used X, Y and Z for X-coordinate, Y-coordinate and Z-coordinate repectively as the field names in my sc12points.csv file and I changed the following in chrisMakeXYlayer.py: x_coords = "X", y_coords = "Y" and z_coords = "Z" accordingly.  Then I exuted the revised chrisMakeXYlayer.py in the Python Window of my ArcGIS10.0:
>>> # chrisMakeXYlayer.py   for 12 points
... # Description: Creates an XY layer and exports it to a layer file
... # Author: ESRI - modified by Scott Chang per Christopher Thompson (Date:  12 Oct 2012)
... # import system modules 
... import arcpy
... from arcpy import env
... # Set environment settings per Chris T.
... env.workspace = r'C:\TEMP\BS_Test.gdb'
...  
... try:
...     # Set the local variables
...     # in_Table = "firestations.csv" 
...     my_csv = r'C:\TEMP\WritingGeometries\sc12points.csv'
...  
...     x_coords = "X"
...     y_coords = "Y"
...     z_coords = "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(my_csv, 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 C:\TEMP\WritingGeometries\sc12points.csv X 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" Z
Start Time: Fri Oct 12 07:54:15 2012
Failed to execute. Parameters are not valid.
ERROR 000732: XY Table: Dataset C:\TEMP\WritingGeometries\sc12points.csv does not exist or is not supported
Failed to execute (MakeXYEventLayer).
Failed at Fri Oct 12 07:54:16 2012 (Elapsed Time: 1.00 seconds)

I don't know how to handle these new errors.  Please kindly help and advise again.
Thanks,
Scott Chang
P. S.  Can I use the .xls or .xlsx file instead of the .csv file as the XY-coordinates-data input file?
0 Kudos
ChristopherThompson
Occasional Contributor III
Can you attach a portion of your .csv file so we can see what it looks like?
0 Kudos
ScottChang
New Contributor II
Hi Christopher,

I tried to copy and paste the page of my sc12points.csv on this page of response thread. This ERIS Forum can't take it. Also, this ERIS Forum does not take the sc12points.csv file as an attachment either.

I tried to change my_csv to my_xls (and other places) in my scottchrisMakeXYlayer.py, and I executed it.  I got the exact same error as I executed the chrisMakeXYlayer,py - see the results below:
>>> # chrisMakeXYlayer.py   for 12 points
... # Description: Creates an XY layer and exports it to a layer file
... # Author: ESRI - modified by Scott Chang per Christopher Thompson (Date:  12 Oct 2012)
... # import system modules 
... import arcpy
... from arcpy import env
... # Set environment settings per Chris T.
... env.workspace = r'C:\TEMP\BS_Test.gdb'
...  
... try:
...     # Set the local variables
...     # in_Table = "firestations.csv" 
...     my_xls = r'C:\TEMP\WritingGeometries\sc12points.xls'
...  
...     x_coords = "X"
...     y_coords = "Y"
...     z_coords = "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(my_xls, 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 C:\TEMP\WritingGeometries\sc12points.xls X 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" Z
Start Time: Fri Oct 12 09:56:38 2012
Failed to execute. Parameters are not valid.
ERROR 000732: XY Table: Dataset C:\TEMP\WritingGeometries\sc12points.xls does not exist or is not supported
Failed to execute (MakeXYEventLayer).
Failed at Fri Oct 12 09:56:38 2012 (Elapsed Time: 0.00 seconds)
>>> 


Also I attached my sc12points.xls file in this response.

Please respond.

Thanks,
Scott Chang
0 Kudos
ChristopherThompson
Occasional Contributor III
i'll take a look at this and see.. since we are heading into the weekend i probably won't get back to you until Monday.
0 Kudos
ChristopherThompson
Occasional Contributor III
Well, I'm not sure what is going on with the last couple of attempts - the error suggests that the script couldn't find the file (either the .csv or .xls) so you'll want to make sure you have the path and file name correct.  As for the other errors you encountered it I think its because you are including a reference to a Z.  In your table you have a column for Z coordinates but the data values are Null.  When I try the makexy tool like this it works just fine:
tb = r'C:\Documents and Settings\cthompson\My Documents\pythonstuff\testdata\test_data.csv'
xc = 'X'
yc = 'Y'
spRef = r"Coordinate Systems\Projected Coordinate Systems\Utm\Nad 1983\NAD 1983 UTM Zone 11N.prj"
xytab = arcpy.MakeXYEventLayer_management(tb,xc,yc,'csv_tab',spRef)


but when i try it while referencing the Z column in that table it bombs out:
tb = r'C:\Documents and Settings\cthompson\My Documents\pythonstuff\testdata\test_data.csv'
xc = 'X'
yc = 'Y'
zc = 'Z'
spRef = r"Coordinate Systems\Projected Coordinate Systems\Utm\Nad 1983\NAD 1983 UTM Zone 11N.prj"
xytab = arcpy.MakeXYEventLayer_management(tb,xc,yc,'csv_tab',spRef,zc)


So, try your code without the Z - you don't have to account for optional arguments in the tool.  See if that helps.  And again, make sure everything is where it should be and is named as you reference it in the script.
0 Kudos
ScottChang
New Contributor II
Hi Christopher, Thanks you for your valuable response.

1) Using sc12points.csv and chrisMakeXYlayer.py in ArcGIS 10.0 Python Window: I changed the my_csv to tb, x_coords to xc, y_coords to yc, and deleted z_coords in chrisMakeXYlayer.py and Z in sc12points.csv. Then I executed the chrisMakeXYlayer.py in the Python Window of my ArcGIS 10.0.  It worked nicely - see the attached file for details.

2) Using sc12points.xls and scottchrisMakeXYlayer.py in ArcGIS 10.0 Python Window: Similarly, I changed the my_xls to tb, x_coords to xc, y_coords to yc, and deleted z_coords in scottchrisMakeXYlayer.py and Z in sc12points.xls. Then I executed the scottchrisMakeXYlayer.py in the Python Window of my ArcGIS 10.0.  It did not work - see the below:
>>> # chrisMakeXYlayer.py   for 12 points: Just X- and Y-coordinate (No Z-coordinate in the input table)  
... # Description: Creates an XY layer and exports it to a layer file
... # Author: ESRI - modified by Scott Chang per Christopher Thompson (Date:  115 Oct 2012)
... # import system modules 
... import arcpy
... from arcpy import env
... # Set environment settings per Chris T.
... env.workspace = r'C:\TEMP\BS_Test.gdb'
...  
... try:
...     # Set the local variables
...     # in_Table = "firestations.csv" 
...     tb = r'C:\TEMP\WritingGeometries\sc12points.xls'
...  
...     xc = "X"
...     yc = "Y"
...    
...     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...
...     xytab = arcpy.MakeXYEventLayer_management(tb, xc, yc, out_Layer, spRef)
...  
...     # 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 C:\TEMP\WritingGeometries\sc12points.xls X 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" #
Start Time: Mon Oct 15 13:59:51 2012
Failed to execute. Parameters are not valid.
ERROR 000732: XY Table: Dataset C:\TEMP\WritingGeometries\sc12points.xls does not exist or is not supported
Failed to execute (MakeXYEventLayer).
Failed at Mon Oct 15 13:59:51 2012 (Elapsed Time: 0.00 seconds)


Question #1:  Why 2) did not work for the .xls file? Please explain this for me.  In the regular Make XY Layer case, the .xls input file is OK.  
Question #2:  In the future, I need the Z_coordinate to be input in my real projects.  Please let me know how you can make the values of Z-coordinate read and processed in the MakeXYZlayer projects.

Many Thanks,
Scott Chang
0 Kudos