I am creating a tool using Python to convert .csv files into points. However, the points were collected in EPSG: 2274 and not WGS84 (EPSG:4326), so in order to create the points I have to convert the lat/long values into WGS84 for them to plot correctly I believe. I have created a script that does this and it works, but the points are not placed correctly geographically. They end up all clustered and in Arkansas (highlighted points) instead of Tennessee (blue pins).
Is there something I need to change in the code to project these values into points?
import csv
import arcpy
import ogr, osr
#set input variables
spreadsheet = "C:\\Monument_Surveyors\\ControlPointInformation_Test_oldconfiguration.csv"
fc = r"C:\Monument_Surveyors\Monuments\Monuments_copying.gdb\Monuments_STAGE_editing_SPCS"
# Spatial Reference System
inputEPSG = 4326
outputEPSG = 2274
#open csv file
with open(spreadsheet, "r") as monument_sheet:
#create reader
csvReader = csv.reader(monument_sheet)
#call header
header = next(csvReader)
#grab index position by using field name
#not necessary if index is already known
latIndex = header.index("Latitude:")
longIndex = header.index("Longitude")
#create empty list for string coordinates
coords = []
#parse through rows and call data in each row index
for row in csvReader:
lat = float(row[latIndex])
long = float(row[longIndex])
coords.append([lat, long])
print("Coords: " + str(coords))
# Spatial Reference System
inputEPSG = 2274
outputEPSG = 4326
#convert string coordinates to float coordinates
coordsFloat = []
for coord in coords:
for i in coord:
coordsFloat.append(float(i))
xypoints = [coordsFloat[x:x+2] for x in range(0, len(coordsFloat),2)]
projCoords = []
projXcoords = []
projYcoords = []
latList = []
longList = []
i = 0
while i < len(xypoints):
#define lat and long index within each coordinate pair
pointY = xypoints[i][0]
pointX = -abs(xypoints[i][1])
# create a geometry from coordinates
point = ogr.Geometry(ogr.wkbPoint)
point.AddPoint(pointX, pointY)
# create coordinate transformation
inSpatialRef = osr.SpatialReference()
inSpatialRef.ImportFromEPSG(inputEPSG)
outSpatialRef = osr.SpatialReference()
outSpatialRef.ImportFromEPSG(outputEPSG)
coordTransform = osr.CoordinateTransformation(inSpatialRef, outSpatialRef)
# transform point
point.Transform(coordTransform)
#define lat and long
lat = point.GetY()
long = point.GetX()
latList.append(lat)
longList.append(long)
#print("long: " + str(long))
#print("GetX: " + str(point.GetX()))
#concatonate list element-wise
projCoords = [None]*(len(latList)+len(longList))
projCoords[::2] = latList
projCoords[1::2] = longList
#print("latList: " + str(latList))
#print("longList: " + str(longList))
i+=1
#group coordinates
projCoordsTuple = zip(projCoords[0::2], projCoords[1::2])
#convert tuple to a list within a list
projCoordsList = []
for row in projCoordsTuple:
latIndex = row[0]
longIndex = row[1]
projCoordsList.append([longIndex, latIndex])
print(projCoordsList)
try:
#create cursor
cur = arcpy.da.InsertCursor(fc, ("SHAPE@X", "SHAPE@Y"))
#iterate through rows to insert point
for i in projCoordsList:
cur.insertRow(i)
print ("feature created")
except:
print(arcpy.GetMessages())
print ("feature not created")
finally:
del cur
EDIT: I would like to update this specific script as a simple Append will not do the trick - I need to do some additional field calculations that do not exist in the .csv spreadsheet.
arcpy.da.InsertCursor
, build aarcpy.PointGeometry(arcpy.Point(X, Y), arcpy.SpatialReference(2274))
and use the"SHAPE@"
token in the InsertCursor, each PointGeometry will be reprojected automatically to match the FeatureClass which is in EPSG:4326.PointGeometry()
objects from your CSV then use thepoint.ProjectAs()
method: pro.arcgis.com/en/pro-app/2.8/arcpy/classes/pointgeometry.htm