Select to view content in your preferred language

How do you project a single pointGeometry object with arcpy?

4798
10
05-01-2017 11:55 PM
JohnMcoy
Occasional Contributor

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)‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
Tags (2)
0 Kudos
10 Replies
RandyBurton
MVP Alum

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)
    )