<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic coordinate conversion, for excel/csv files in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/coordinate-conversion-for-excel-csv-files/m-p/694535#M53852</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I have an excel file (or in fact converted to csv for easy use. Arcpy seems to be buggy about excel files...) with some lat/long coordinates that I would need to add corresponding projected coordinates. The way I can think of doing this is to use the MakeXYEvent to create an event layer, then reproject the layer and add the projected X and Y. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Implementation: &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;import os, arcpy, sys, traceback
from xlrd import open_workbook
import xlwt


arcpy.overwriteOutputs = True

projectionsPath = r"C:\Test\_UtilityScripts\Coordinate Systems_CopiedFromArcMap\Projected Coordinate Systems\State Plane\NAD 1983 (Meters)"
csvfile = r"C:\Test\6_USA_FIPs\excel\latlong_seperated\IL.csv"
lat_g = "lat_g"
long_g = "long_g"
outlayer = "xyEventlayer"
#savedlayer = r"C:\Test\6_USA_FIPs\python\temp\xyOutput.lyr"
#outputSHP = r"C:\Test\6_USA_FIPs\python\temp\xyOutput.shp"


if arcpy.Exists(outlayer):
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Delete_management(outlayer)

srGCS = os.path.join(r"C:\Test\_UtilityScripts\Coordinate Systems_CopiedFromArcMap\Geographic Coordinate Systems\North America", "NAD 1983.prj")
srPRJ = os.path.join(projectionsPath, "NAD 1983 StatePlane Illinois East FIPS 1201 (Meters).prj")
arcpy.MakeXYEventLayer_management(csvfile, lat_g, long_g, outlayer, srGCS)

rows = arcpy.SearchCursor(outlayer, "", srPRJ)
row = rows.next()
feat = row.Shape
pnt = feat.getPart()
print pnt.X&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I am using SearchCursor above with on the fly projection for coordinate conversion. But I am always getting coordinates of (nan, nan)??? I've tested the syntax with other shapefiles, and it should work... Anything special with this xyevent layer?&amp;nbsp; Please help, thanks people!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 30 Apr 2014 17:40:58 GMT</pubDate>
    <dc:creator>JianLiu</dc:creator>
    <dc:date>2014-04-30T17:40:58Z</dc:date>
    <item>
      <title>coordinate conversion, for excel/csv files</title>
      <link>https://community.esri.com/t5/python-questions/coordinate-conversion-for-excel-csv-files/m-p/694535#M53852</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I have an excel file (or in fact converted to csv for easy use. Arcpy seems to be buggy about excel files...) with some lat/long coordinates that I would need to add corresponding projected coordinates. The way I can think of doing this is to use the MakeXYEvent to create an event layer, then reproject the layer and add the projected X and Y. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Implementation: &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;import os, arcpy, sys, traceback
from xlrd import open_workbook
import xlwt


arcpy.overwriteOutputs = True

projectionsPath = r"C:\Test\_UtilityScripts\Coordinate Systems_CopiedFromArcMap\Projected Coordinate Systems\State Plane\NAD 1983 (Meters)"
csvfile = r"C:\Test\6_USA_FIPs\excel\latlong_seperated\IL.csv"
lat_g = "lat_g"
long_g = "long_g"
outlayer = "xyEventlayer"
#savedlayer = r"C:\Test\6_USA_FIPs\python\temp\xyOutput.lyr"
#outputSHP = r"C:\Test\6_USA_FIPs\python\temp\xyOutput.shp"


if arcpy.Exists(outlayer):
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Delete_management(outlayer)

srGCS = os.path.join(r"C:\Test\_UtilityScripts\Coordinate Systems_CopiedFromArcMap\Geographic Coordinate Systems\North America", "NAD 1983.prj")
srPRJ = os.path.join(projectionsPath, "NAD 1983 StatePlane Illinois East FIPS 1201 (Meters).prj")
arcpy.MakeXYEventLayer_management(csvfile, lat_g, long_g, outlayer, srGCS)

rows = arcpy.SearchCursor(outlayer, "", srPRJ)
row = rows.next()
feat = row.Shape
pnt = feat.getPart()
print pnt.X&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I am using SearchCursor above with on the fly projection for coordinate conversion. But I am always getting coordinates of (nan, nan)??? I've tested the syntax with other shapefiles, and it should work... Anything special with this xyevent layer?&amp;nbsp; Please help, thanks people!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 30 Apr 2014 17:40:58 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/coordinate-conversion-for-excel-csv-files/m-p/694535#M53852</guid>
      <dc:creator>JianLiu</dc:creator>
      <dc:date>2014-04-30T17:40:58Z</dc:date>
    </item>
    <item>
      <title>Re: coordinate conversion, for excel/csv files</title>
      <link>https://community.esri.com/t5/python-questions/coordinate-conversion-for-excel-csv-files/m-p/694536#M53853</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I'm a little unclear on what you're trying to do once you get the file into State Plane, but the process to getting it there could go something like this.&amp;nbsp; Note that here, I'm explicitly converting from WGS84 to State Plane:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;arcpy.overwriteOutputs = True

csvfile = r"C:\Users\meiben\Desktop\test\fires.csv"
lat_g = "lat_g"
long_g = "long_g"
outlayer = "xyEventlayer"

arcpy.MakeXYEventLayer_management(csvfile, long_g, lat_g, outlayer,arcpy.SpatialReference(4326))
arcpy.CopyFeatures_management(outlayer,"templayer")
arcpy.Delete_management(outlayer)

arcpy.Project_management("templayer","coord_conv",arcpy.SpatialReference(26771))
arcpy.Delete_management("templayer")&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;A couple changes that I made here was using the Spatial Reference method instead of pointing to a file on the system.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;outlayer,arcpy.SpatialReference(4326) = GCS WGS 84&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;arcpy.SpatialReference(26771) = PCS NAD_1927_StatePlane_Illinois_East_FIPS_1201&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Check out the following URL for a list of all the codes:&lt;/SPAN&gt;&lt;BR /&gt;&lt;A href="http://resources.arcgis.com/en/help/main/10.1/018z/pdf/projected_coordinate_systems.pdf" rel="nofollow noopener noreferrer" target="_blank"&gt;http://resources.arcgis.com/en/help/main/10.1/018z/pdf/projected_coordinate_systems.pdf&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;At the end of this script, you should have a feature layer to do whatever you like with.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Hope this helps!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Matt&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 05:13:41 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/coordinate-conversion-for-excel-csv-files/m-p/694536#M53853</guid>
      <dc:creator>MattEiben</dc:creator>
      <dc:date>2021-12-12T05:13:41Z</dc:date>
    </item>
    <item>
      <title>Re: coordinate conversion, for excel/csv files</title>
      <link>https://community.esri.com/t5/python-questions/coordinate-conversion-for-excel-csv-files/m-p/694537#M53854</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;I'm a little unclear on what you're trying to do once you get the file into State Plane, but the process to getting it there could go something like this.&amp;nbsp; Note that here, I'm explicitly converting from WGS84 to State Plane:&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;arcpy.overwriteOutputs = True

csvfile = r"C:\Users\meiben\Desktop\test\fires.csv"
lat_g = "lat_g"
long_g = "long_g"
outlayer = "xyEventlayer"

arcpy.MakeXYEventLayer_management(csvfile, long_g, lat_g, outlayer,arcpy.SpatialReference(4326))
arcpy.CopyFeatures_management(outlayer,"templayer")
arcpy.Delete_management(outlayer)

arcpy.Project_management("templayer","coord_conv",arcpy.SpatialReference(26771))
arcpy.Delete_management("templayer")&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;A couple changes that I made here was using the Spatial Reference method instead of pointing to a file on the system.&lt;BR /&gt;&lt;BR /&gt;outlayer,arcpy.SpatialReference(4326) = GCS WGS 84&lt;BR /&gt;arcpy.SpatialReference(26771) = PCS NAD_1927_StatePlane_Illinois_East_FIPS_1201&lt;BR /&gt;&lt;BR /&gt;Check out the following URL for a list of all the codes:&lt;BR /&gt;&lt;A href="http://resources.arcgis.com/en/help/main/10.1/018z/pdf/projected_coordinate_systems.pdf" rel="nofollow noopener noreferrer" target="_blank"&gt;http://resources.arcgis.com/en/help/main/10.1/018z/pdf/projected_coordinate_systems.pdf&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;At the end of this script, you should have a feature layer to do whatever you like with.&lt;BR /&gt;&lt;BR /&gt;Hope this helps!&lt;BR /&gt;&lt;BR /&gt;Matt&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks Matt for the reply! I wanted to use SearchCursor to do the projection on the fly (&lt;/SPAN&gt;&lt;A href="http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//000v00000039000000" rel="nofollow noopener noreferrer" target="_blank"&gt;http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//000v00000039000000&lt;/A&gt;&lt;SPAN&gt;) and the code after the reprojection is just to print out the new coordinates for testing purposes, and that's where I got (nan,nan). &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I wanted to use SearchCursor because it doesn't write anything to the harddrive, but the Project_management function would work too. I had just found out why my code failed -- it's not particularly due to the XYEventLayer, which I thought might give a problem to the on-the-fly projection. It is in fact due to bad coordinates in my file. The projection failed without giving an error. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;As to the spatial reference input parameter, I think both the actual object and the string path would work. But it was also a factor that I had to filter out to pin down the problem. -- Sometimes arcpy is very confusing as to when it takes a string path or an actual object as input. There is no clear documentation at times, and it takes a long time to figure things out. Painful. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks for the help, much appreciated!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 05:13:43 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/coordinate-conversion-for-excel-csv-files/m-p/694537#M53854</guid>
      <dc:creator>JianLiu</dc:creator>
      <dc:date>2021-12-12T05:13:43Z</dc:date>
    </item>
    <item>
      <title>Re: coordinate conversion, for excel/csv files</title>
      <link>https://community.esri.com/t5/python-questions/coordinate-conversion-for-excel-csv-files/m-p/694538#M53855</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Glad you figured out the problem!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I agree that it can be confusing sometimes as to what needs a string vs an object.&amp;nbsp; Though I've found that often they're interchangable in the arcpy.SpatialReference method.&amp;nbsp; For example, something like this would be totally valid:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;WGS84 = """GEOGCS["GCS_WGS_1984",
&amp;nbsp;&amp;nbsp;&amp;nbsp; DATUM["D_WGS_1984",
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; SPHEROID["WGS_1984",6378137.0,298.257223563]
&amp;nbsp;&amp;nbsp;&amp;nbsp; ],
&amp;nbsp;&amp;nbsp;&amp;nbsp; PRIMEM["Greenwich",0.0],
&amp;nbsp;&amp;nbsp;&amp;nbsp; UNIT["Degree",0.0174532925199433]
]"""

arcpy.DefineProjection_management("featureClass",WGS84)&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Here I literally opened up a .prj file and pasted the (formatted for readability) contents into my file.&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Also, I'm not sure which version of ArcMap you're running, but if you have 10.1+, you can use the new Data Access Cursors to get your coordinates, via the token shortcuts.&amp;nbsp; Something like this:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;coords = [row[0] for row in arcpy.da.SearchCursor("FeatureClass",["SHAPE@XY"])]
&amp;gt;&amp;gt;&amp;gt; coords
&amp;gt;&amp;gt;&amp;gt; [(-117.54547981299241, 34.17563985328801)]&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Though note that this method will only get the centroid, so if this was a polygon, you'd only get one point returned.&amp;nbsp; If you wanted to get all vertices from the geometry:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;geom = [row[0] for row in arcpy.da.SearchCursor("FeatureClass",["SHAPE@"])][0]

partCount = geom.partCount

for n in range(partCount):
&amp;nbsp;&amp;nbsp;&amp;nbsp; part = geom.getPart(n)
&amp;nbsp;&amp;nbsp;&amp;nbsp; for point in part:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print point.X, point.Y&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Of course, the old school SearchCursor function still works great!&amp;nbsp; The new data access cursor methods are just a bit faster with larger datasets.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Matt&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 05:13:46 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/coordinate-conversion-for-excel-csv-files/m-p/694538#M53855</guid>
      <dc:creator>MattEiben</dc:creator>
      <dc:date>2021-12-12T05:13:46Z</dc:date>
    </item>
  </channel>
</rss>

