Hi everyone,
So I got a task, and I don`t know how to do it. In csv file, i have to create two new columns which shows WGS-84 coordinates. Maybe someone, have solution for this ? I can`t undertstand what is in 29 line... I tried to google it.. And I found the example... but without explanation.
Thanks for any help
import arcpy
import csv
fc = r"#"
out_csv = r"#"
fields = {
"OBJECTID": "OBJECTID",
"Global_ID": "Global_ID",
"Global_Copy": "Global_Copy",
"SHAPE@X": "POINT_X",
"SHAPE@Y": "POINT_Y",
"SHAPE@X": "LAT",
"SHAPE@Y": "LONG"
}
table_fields = []
csv_fields = []
for field in fields:
table_fields.append(field)
csv_fields.append(fields[field])
def tableToCSV(fc, out_csv):
with open(out_csv, 'wb') as csv_file:
writer = csv.writer(csv_file, delimiter=';', lineterminator='\n')
writer.writerow(csv_fields)
with arcpy.da.SearchCursor(fc, table_fields) as cursor:
for row in cursor:
arcpy.PointGeometry(arcpy.Point(X,Y),arcpy.SpatialReference(3346)).projectAs(arcpy.SpatialReference(4326))
point = Point(row[idxLAT],row[idxLONG])
#TODO: reproject
reppoint = point
row[idxLAT] = reppoint.x
row[idxLONG] = reppoint.y
writer.writerow(row)
print out_csv
#csv_file.close()
tableToCSV(fc, out_csv)
This provides some interesting results. By specifying a spatial reference in the search cursor, you can get latitude and longitude very easily. And it is easy to have tab delimited output.
# WGS 1984 : (4326) Lat/Lon
print "{}\t{}\t{}".format(
"OBJECTID",
"POINT_X",
"POINT_Y"
)
for row in arcpy.da.SearchCursor("MyPointLayer",["OID@","SHAPE@X","SHAPE@Y"],spatial_reference=arcpy.SpatialReference(4326)):
print "{}\t{}\t{}".format(
row[0], # OBJECTID
row[1], # POINT_X (LON)
row[2] # POINT_Y (LAT)
)