ArcPy Coordinate System – Applying Spatial Reference in XY Table to Point with ArcPy and ArcGIS Pro

arcgis-proarcpycoordinate system

I have a series of CSV files that I need to convert to point feature classes. I have the following code :

# Convert CSV file to raster
import arcpy
workspace = "Y://DEM250m//CSV_Points"
arcpy.env.workspace = workspace # Set environment settings
mycsvs = arcpy.ListFiles("*1980-2010.csv") # Access all historical data

sr = arcpy.SpatialReference()

# Convert CSV to point featureclass
out_table = "Y://DEM250m//CSV_Points//Historical"
for i in mycsvs:
    arcpy.management.XYTableToPoint(
        workspace + "//" + i,
        out_table + "//" + i, 
        'Latitude',
        'Longitude',
        'Elevation',
        arcpy.SpatialReference(4326)
        )

I have tried many variations of the following:

arcpy.SpatialReference(4326)
arcpy.SpatialReference(r'c:/DEM/dem.prj')
arcpy.SpatialReference('WGS 1984')

It never seems to want to project my csv data correctly (it always seems to show up in the middle of the ocean around the equator). I have checked what a lot of other people have posted about here before and tried those solutions, but they never seem to work for me.

What am I doing wrong?

I have added an image of what the relevant CSV columns look like.

enter image description here

Best Answer

You have an error in your code where you are supplying your coordinates in the sequence of Latitude (Y) then Longitude (X). Look at the tool's help page and you we see that the tool requires Longitude (X) then Latitude (Y).

My test data was a folder of just one csv so you'll need to adjust your code accordingly, but this worked for me:

import arcpy
arcpy.env.overwriteOutput = True
workspace = r"C:\Scratch"
arcpy.env.workspace = workspace # Set environment settings    
mycsvs = arcpy.ListFiles("*.csv") # Access all historical data    
sr = arcpy.SpatialReference(4326)

# Convert CSV to point featureclass
out_table = r"C:\Scratch"
for i in mycsvs:
    arcpy.management.XYTableToPoint(workspace + "//" + i,out_table + "//test2.shp",'Longitude','Latitude','Elevation',sr)
Related Question