After getting a lot of requests to get latitude and longitude coordinates for features, I created a generic script that creates two new fields in the existing feature feature class and and calcs the latitude and longitude of the feature's "true centroid." The only problem is that you would have to run this script any time a feature's geometry was changed. I did some searching for something that already existed and didn't find anything that was simple and generic enough, so please let me know if you can do it better.
import arcpy fc = r"C:\temp\working.gdb\temp_fc" # Define and create longitude/latitude fields ## Longitude is X, Latitude is Y fieldsNew = ["GCS_WGS_1984_X", "GCS_WGS_1984_Y"] fieldsExisting = {f.name: f.type for f in arcpy.ListFields(fc)} for fNew in fieldsNew: # Add field if it doens't already exist if fNew not in fieldsExisting.keys(): arcpy.AddField_management( fc, ## in_table fNew, ## field_name "DOUBLE", ## field_type ) # If data type of existing field is not a Double, throw Exception elif fieldsExisting[fNew] != "Double": raise Exception( "Existing {} field is type {}. Must be 'Double'".format( fNew, fieldsExisting[fNew] ) ) # Update fields with GCS_WGS_1984 lat/long print("Updating Field values") fieldsNew.append("SHAPE@") GCS_WGS_1984 = arcpy.SpatialReference(4326) with arcpy.da.UpdateCursor(fc, fieldsNew, "", GCS_WGS_1984) as u_cursor: for row in u_cursor: point = row[2].trueCentroid row[0] = point.X row[1] = point.Y u_cursor.updateRow(row)
I just tried to copy the data to in_memory workspace and do the AddGeometry there thinking it would be faster, but it looks like AddGeomerty won't work with in_memory data. I get an
ERROR 000732: Input Features: Dataset does not exist or is not supported
I don't see anything explicit in the documentation about this (unlike with Project, which clearly says in_memory is not a valid workspace).
see my comment above
I should clarify, My plan was to create a variable that has a path "in_memory"
MyInputFC = r"C:\MySDEConn.sde\MyInputFC" inputFC_inmem = os.path.join("in_memory", "MyInputFC_inmem")
Then I copy rows from the source to the in_memory workspace
arcpy.CopyRows_management(MyInputFC, inputFC_inmem)
Then I try to run AddGeometry on the data in memory. This is where it errors.
arcpy.AddGeometryAttributes_management( inputFC_inmem, ## Input_Features "POINT_X_Y_Z_M", ## Geometry_Properties "", ## Length_Unit "", ## Area_Unit GCS_WGS_1984 ## Coordinate_System )
Then I would copy the rows back out of in_memory workspace.
your syntax seems correct
>>> import os
>>> out = os.path.join("in_memory","myfile")
>>> out
'in_memory/myfile'
so I don't know why it can't find it
It takes a good 20 seconds for the copy rows to complete, so I'm sure it's actually creating the in_memory data. My guess is that AddGeometry just can't work its magic in an in_memory workspace.
I just realized I was using copy rows instead of copy features so it was trying to add geometry attributes on a table instead of a feature class. DOH!
Using in_memory workspace to calculate X/Y is about twice as fast (compared to in a file geodatabase) on a point feature class with just over 80,000 rows.
Here is what I started with
The source code is in FORTRAN, but It is easily decipherable. It took me a few days to translate it into Javascript and it works very fast (I put it in my mousemove event). No ESRI or GIS - It stays in the program.
http://www.ngs.noaa.gov/PC_PROD/pc_prod.shtml
Has a bunch of these conversion programs available for public consumption.
Brent Hoskisson