field calculator

3414
5
11-05-2011 09:10 AM
by Anonymous User
Not applicable
Original User: luke.kaim

I need some help with this code. I need to be able to change the units of X and Y within model builder or python. Therefore, I can not use calculate geometry. How do I convert the X and Y locations into decimal degrees using the field calculator? The below code is not working. I want to find X and Y location of points and then be able to change the units of the points from meters to decimal degrees. I need to be able to automate this process. Please help. I know I can use the function "Add X,Y" but that does not allow one to change units. How do I change units after I have the two columns?

X-coordinate of a point

Dim dblX As Double
Dim pPoint As IPoint
Set pPoint = [Shape]
dblX = pPoint.X

Y-coordinate of a point

Dim dblY As Double
Dim pPoint As IPoint
Set pPoint = [Shape]
dblY = pPoint.Y

"Shape and length properties of the geometry field can be modified with unit types expressed with an @ sign".



Thank you,
Luke Kaim

Thank you
Luke Kaim (SIE)
Lucas.Kaim@maine.edu
(914)263-7866
0 Kudos
5 Replies
by Anonymous User
Not applicable
Original User: Hornbydd

Why don't you project your original data into lat/long then join that table back to your original data and pass over the XY coordinates using the calculate tool?
0 Kudos
JakeSkinner
Esri Esteemed Contributor
You can create a new field of type text and then run the following within the field calculator:

Pre-logic Script Code
def dd2DMS(dd):
    if dd > 0:
        deg = int(dd)
        min = int((dd-deg) * 60)
        sec = (((dd - deg) * 60) - min) * 60
        return str(deg) + " " + str(min) + "' " + "%.4f" % sec + '"'
    if dd < 0:
        deg = int(dd)
        min = int((dd-deg) * 60 * -1)
        sec = (((dd - deg) * 60 * -1) - min) * 60
        return str(deg) + " " + str(min) + "' " + "%.4f" % sec + '"'


[FIELDNAME] =
dd2DMS(!POINT_X!)
0 Kudos
by Anonymous User
Not applicable
Original User: luke.kaim

Thank you so much JSkinn3.

This is heading in the right direction. I am using utm though so my original field is in meters. I need to go from meters to decimal degrees. The code you wrote I believe goes from dd to degrees minutes seconds. What would the code look like from meters to dd? I also tried your code and I could not get it to work even when I used dd as my input.

Thank you,
Luke Kaim
0 Kudos
JakeSkinner
Esri Esteemed Contributor
Hi Lucas,

Sorry, I mis-read your previous post.  You can update from meters to DD using the 'arcpy.UpdateCursor' function.  With cursors, you have the ability to set a working coordinate system.  Ex:

import arcpy
from arcpy import env
env.workspace = r"C:\temp\python\test.gdb"

fc = "Cities_Meters"

prj = r"C:\Program Files (x86)\ArcGIS\Desktop10.0\Coordinate Systems\Geographic Coordinate Systems\World\WGS 1984.prj"
spatRef = arcpy.SpatialReference(prj)
env.geographicTransformations = "NAD_1983_To_WGS_1984_1"

rows = arcpy.UpdateCursor(fc, "", spatRef)
for row in rows:
    geom = row.shape
    row.POINT_X = geom.centroid.X
    row.POINT_Y = geom.centroid.Y
    rows.updateRow(row)

del row, rows    


Since I am switching datums from NAD_83 to WGS_84, I needed to apply a geographic transformation.  You will need to update 'row.POINT_X' and 'row.POINT_Y' with the fields you want to store the DD values.

Also, here is how you can do this using the field calculator:

def func():
  fc = "Cities_Meters"
  prj = r"C:\Program Files (x86)\ArcGIS\Desktop10.0\Coordinate Systems\Geographic Coordinate Systems\World\WGS 1984.prj"
  spatRef = arcpy.SpatialReference(prj)
  arcpy.env.geographicTransformations = "NAD_1983_To_WGS_1984_1"

  rows = arcpy.UpdateCursor(fc, "", spatRef)
  for row in rows:
    geom = row.shape
    return geom.centroid.X


func()


With the field calculator you will have to run it for each individual field, and change 'geom.centroid.X' to 'geom.centroid.Y' for the appropriate field.
0 Kudos
by Anonymous User
Not applicable
Original User: luke.kaim

Thank you so much JSkinn3. I totally owe you a beer or a two.

Luke
0 Kudos