3

I am trying to convert my X and Y fields from feet to lat and long. I can easily do this in Calculate Geometry from the attribute table, but I'm needing to use this in a model and Calculate Geometry is not available as a tool. I'm using the Calculate Field tool and trying to find a script that converts this to lat and long. I've tried the following script using the advanced code for VBA with no luck:

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

dblX

I'd like to avoid having to create a totally new shapefile or feature class. I just want to change the existing fields in this existing feature class.

Thanks!!

1
  • Are you converting the X and Y coordinates in order to reproject the data?
    – jvangeld
    Commented Sep 13, 2011 at 1:58

3 Answers 3

3

Here are some Calculate Field examples. If you scroll down about 3/4 of the way you'll see some examples for Geometry Unit conversion.

For scripting, you might consider adding fields as opposed to editing existing ones.

1

How about Calculate Field or Add XY Coordinates? I think in the latter, the data will already have to be in a geographic coordinate system (but it looks like it is).

1

Sorry for the late response but I just happened on this forum looking for help on another topic. I too needed a way to add Lat/Long fields to my records for reference from other non-spatial applications. I wrote an ArcPy script which adds and calculates Lat/Long fields for all visible feature layers in your map (so if you don't want fields added to a layer then turn it off!) You would need to make 2 revisions to this code in order to make it work for you: 1) This script creates a temporary layer, WGS_Output, which is created in WGS_1984 coordsys. You need to modify line 5 to point at your default workspace. 2) Line 12 performs a join between the original layer and the WGS_Output layer. I used the OBJNAME field which is a required/unique field generated by the Topcon Survey Controller we use. If you don't have a unique field like this you will need to add one to successfully perform the join.

import arcpy
import getpass

user = getpass.getuser()
mxd = arcpy.mapping.MapDocument("CURRENT")
WGS_Output = "C:\\Users\\"+user+"\\My Documents\\ArcGIS\\Default.gdb\\WGS_Output"

for lyr in arcpy.mapping.ListLayers(mxd):
    if(lyr.visible & lyr.isFeatureLayer):
        arcpy.AddField_management(lyr.name, "LONGITUDE", "DOUBLE", "", "", "", "", "NON_NULLABLE", "NON_REQUIRED", "")
        arcpy.AddField_management(lyr.name, "LATITUDE", "DOUBLE", "", "", "", "", "NON_NULLABLE", "NON_REQUIRED", "")
        arcpy.Project_management(lyr.name, WGS_Output, "GEOGCS['GCS_WGS_1984',DATUM['D_WGS_1984',SPHEROID['WGS_1984',6378137.0,298.257223563]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]]", "NAD_1983_To_WGS_1984_5", "")
        arcpy.AddXY_management(WGS_Output)
        arcpy.AddJoin_management(lyr.name, "OBJNAME", WGS_Output, "OBJNAME", "KEEP_COMMON")
        arcpy.CalculateField_management(lyr.name, lyr.name+".LONGITUDE", "[WGS_Output.POINT_X]", "VB", "")
        arcpy.CalculateField_management(lyr.name, lyr.name+".LATITUDE", "[WGS_Output.POINT_Y]", "VB", "")
        arcpy.RemoveJoin_management(lyr.name, "WGS_Output")

Good Luck, Mike

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.