I am trying to get XY coordinate ouput in decimal degrees (NAD_1983_StatePlane_California_VI_FIPS_0406_Feet)
but I get the following (6307536.064266056, 1984263.0718122125).
From the following script section pertaining to spatial reference search cursor,does anything appear to be the reason why I get my current results ?
import csv,os,arcpy
os.chdir(r'S:\abc)
#Set path location of Excel (CSV) file
(r'S:\GIS\Projects\abc)
#Set Excel variables
ofile = open('Coordinates.csv','w')
writer = csv.writer(ofile)
fc = "ESC_MS4structures selection"
desc = arcpy.Describe(fc)
field = ["FACILITYID","SHAPE@XY"]
sr=desc.spatialReference
cursor = arcpy.da.SearchCursor(fc,field,None,sr)
for row in cursor:
print (u'{0}, {1}'.format(row[0], row[1]))
writer.writerows(cursor)
#Close Excel (CSV) file
ofile.close()
os.startfile (r'S:\abc\Coordinates.csv')
Your state plane CRS has a linear unit of foot, so the coordinates are reported in units of foot. If you want to report units of decimal degrees, you need to project to a CRS with that unit, like WGS (or perhaps NAD83 for your data). Note that I'm using the PointGeometry object (converted to Point object) returned using the SHAPE@ token, as opposed to the raw coordinates returned by SHAPE@XY.
>>> fc = 'points'
... sr = arcpy.Describe(fc).spatialReference
... with arcpy.da.SearchCursor(fc,'SHAPE@',spatial_reference=sr) as cursor:
... for row in cursor:
... print ('{0}, {1}'.format(row[0].centroid.X, row[0].centroid.Y))
... wgs = '4326'
... print ('{0}, {1}'.format(row[0].projectAs(wgs).centroid.X, row[0].projectAs(wgs).centroid.Y))
...
652440.868186, 6194884.158
-120.56334429, 55.8752926871
652438.441583, 6194881.05195
-120.563384785, 55.8752655714
652435.723788, 6194875.81049
-120.563431131, 55.8752193782
652439.541643, 6194871.86322
-120.563372388, 55.8751827363
Makes sense, I was so focused on the syntax that I didn't think about the obvious aspect of projected state plane coordinates are not in decimal degrees.
I will go ahead and use your example to implement this how I want it.
Thanks
This works as I get the desired format -117.089329372, 33.1175902179.
I am trying to include the field Facilityid as well.
I know the writerow doesn't work as is.
fc = "ESC_MS4structures selection"
field = ["FACILITYID"]
with arcpy.da.SearchCursor(fc,'SHAPE@',spatial_reference=sr) as cursor:
for row in cursor:
wgs = '4326'
print ('{0}, {1}'.format(row[0].projectAs(wgs).centroid.X, row[0].projectAs(wgs).centroid.Y))
## writer.writerows('{0}, {1}'.format(row(0).projectAs(wgs).centroid.X, row(0).projectAs(wgs).centroid.Y))
## writer.writerows(cursor)
#Close Excel (CSV) file
ofile.close()
The second parameter in the SearchCursor can either be a single field or a list of fields. In your case, you're calling the individual rows 'row', so row[0] is the first field, row[1] is the second field, and so on.
with arcpy.da.SearchCursor(fc,['SHAPE@','FACILITYID'],spatial_reference=sr) as cursor:
^ row[0] would hold the shape, row[1] holds facility ID.
Perfect for print out.
Almost there, now just write to csv file
FACILITYID 423 -117.079916464, 33.108904151
#Set Excel variables
headers = "FaciliyID","X","Y"
ofile = open('Coordinates.csv','w')
writer = csv.writer(ofile)
writer.writerow(["FACLITYID", "X", "Y"])
fc = "ESC_MS4structures selection"
##field = ["FACILITYID"]
with arcpy.da.SearchCursor(fc,['SHAPE@','FACILITYID'],spatial_reference=sr) as cursor:
for row in cursor:
wgs = '4326'
print "FACILITYID", row[1], ('{0}, {1}'.format(row[0].projectAs(wgs).centroid.X, row[0].projectAs(wgs).centroid.Y,row))
writer.writerow row([1],('{0}, {1}'.format(row[0].projectAs(wgs).centroid.X, row[0].projectAs(wgs).centroid.Y,row))
I was able to write to excel. I just created a variable from the results and passed it to the writer, it gets too confusing on modifying syntax to accommodate the writer.
with arcpy.da.SearchCursor(fc,['SHAPE@','FACILITYID'],spatial_reference=sr) as cursor:
for row in cursor:
wgs = '4326' # Coordinate System WGS 1984 WKID is '4326'
toolresults = row[1], ('{0}, {1}'.format(row[0].projectAs(wgs).centroid.Y, row[0].projectAs(wgs).centroid.X,row))
writer.writerow (toolresults)