question about coordinates

760
3
04-28-2017 05:38 AM
JohnMcoy
New Contributor III

Hi everyone!

I need your help again... So I have this code now, which takes all columns from database I need. The X and the Y coordinates fields isn`t at the database. How do I add two new columns only in exporting CSV file ? 

Thanks for any help!  

import arcpy 
import csv

fc = r"#"
out_csv = r"#.csv"

fields = ["#", "#"]

def tableToCSV(fc, out_csv):
    fld_list = arcpy.ListFields(fc)
    fld_names = [fld.name for fld in fld_list]
    with open(out_csv, 'wb') as csv_file:
        writer = csv.writer(csv_file, delimiter=';', lineterminator='\n')
        writer.writerow(fields)
        with arcpy.da.SearchCursor(fc, fields) as cursor:
            for row in cursor:
                writer.writerow(row)
        print out_csv  
    csv_file.close()



tableToCSV(fc, out_csv)
Tags (2)
0 Kudos
3 Replies
NeilAyres
MVP Alum

Sorry, a bit confused here.

Are you asking how to get the XY coordinates out of the fc and into the text file? This is a point feature I take it.

DanPatterson_Retired
MVP Emeritus

just call the builtin rather than doing it the hard way

http://desktop.arcgis.com/en/arcmap/latest/tools/data-management-toolbox/add-xy-coordinates.htm

you can zip that into your script prior to csv'ing it ... check the code snippet at the bottom

0 Kudos
RandyBurton
MVP Alum

To add  Point_X and Point_Y attribute fields to a feature to export to csv or excel table, I like to use:

spatial_reference = arcpy.SpatialReference("WGS 1984")

# AddGeometryAttributes_management (Input_Features, Geometry_Properties, {Length_Unit}, {Area_Unit}, {Coordinate_System})
arcpy.AddGeometryAttributes_management(featurename,"POINT_X_Y_Z_M","#","#",spatial_reference)‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍