Lat(!Shape!) def Lat(shape): arcpy.env.cartographicCoordinateSystem = "Coordinate Systems\Geographic Coordinate\Systems\World\WGS 1984.prj" lat = shape.centroid.x return lat
import arcpy latLonRef = "Coordinate Systems\Geographic Coordinate Systems\World\WGS 1984.prj" featureClasses = arcpy.GetParameterAsText(0) featureClassesList = featureClasses.split(";") for featureClass in featureClassesList: arcpy.AddMessage("Calculating XY coordinates for: " + featureClass) arcpy.AddField_management(featureClass, "LAT", "DOUBLE") arcpy.AddField_management(featureClass, "LON", "DOUBLE") rows = arcpy.UpdateCursor(featureClass, "", latLonRef) for row in rows: feat = row.shape coord = feat.getPart() lon = coord.X lat = coord.Y row.LAT = lat row.LON = lon rows.updateRow(row) #arcpy.AddMessage(str(lat) + ", " + str(lon))
You sir are a gentlemen and a scholar. That's exactly what I was looking for.
If anyone is interested, here's my updated code for a script tool with a multivalue parameter.import arcpy latLonRef = "Coordinate Systems\Geographic Coordinate Systems\World\WGS 1984.prj" featureClasses = arcpy.GetParameterAsText(0) featureClassesList = featureClasses.split(";") for featureClass in featureClassesList: arcpy.AddMessage("Calculating XY coordinates for: " + featureClass) arcpy.AddField_management(featureClass, "LAT", "DOUBLE") arcpy.AddField_management(featureClass, "LON", "DOUBLE") rows = arcpy.UpdateCursor(featureClass, "", latLonRef) for row in rows: feat = row.shape coord = feat.getPart() lon = coord.X lat = coord.Y row.LAT = lat row.LON = lon rows.updateRow(row) #arcpy.AddMessage(str(lat) + ", " + str(lon))
This works for what I wanted except it locks up ArcMap 10.2.2 when I run it. When I check the attribute table the columns are created and populated. Any ideas why this would "freeze up" ArcMap and not continue the other request in the script?
Any suggestions would be appreciated!
Addendum: Okay, I wasn't patient enough. It takes almost 30 minutes for the lat long function to run. It will then complete the python script. Not sure why it takes so long. I am wondering if it has something to do with it being in a Sql GDB? After testing the lat long it looks like they are in the GDB projection (state plane) not WGS84, which is what I wanted. I am going to try Plan B and set up a staging file to add the fields and calculations in a shapefile then copy to the GDB.
#expression = "!shape.area@acres!" #gp.CalculateField_management(fc, fn, expression, "PYTHON_9.3")
# Import system modules import arcgisscripting, os gp = arcgisscripting.create(9.3) # Load required toolboxes... #gp.AddToolbox("C:/Program Files/ArcGIS/ArcToolbox/Toolboxes/Data Management Tools.tbx") gp.AddToolbox("C:/Program Files/ArcGIS/Desktop10.0/ArcToolbox/Toolboxes/Data Management Tools.tbx") #gp.workspace = r"G:\\63D RRC GIS Data\\63D_RSC_GDB" gp.workspace = r"C:\\temp\\python" #gp.workspace = r"G:\\63D RRC GIS Data" pathwrksp = gp.workspace # print pathwrksp PointRef = r"C:\\Program Files\\ArcGIS\\Desktop10.0\\Coordinate Systems\\Geographic Coordinate Systems\\World\\WGS 1984.prj" PolyRef = r"C:\\Program Files\\ArcGIS\\Desktop10.0\\Coordinate Systems\\Projected Coordinate Systems\\Continental\\North America\\USA Contiguous Albers Equal Area Conic USGS.prj" path2wrksps = gp.ListWorkspaces("OK002*") for path2wrksp in path2wrksps: #print "... " + path2wrksp gp.workspace = path2wrksp facil_id = str(path2wrksp[34:39]) instln_id = "06510" #print facil_id wrksps3 = gp.ListWorkspaces("*", "FileGDB") for wrksp3 in wrksps3: gp.workspace = wrksp3 print "...... " + gp.workspace datasets = gp.ListDatasets("cad*") for dataset in datasets: gp.workspace = wrksp3 + "\\" + dataset #print dataset FcList = gp.ListFeatureClasses("*","Point") for fc in FcList: gp.workspace = dataset + "\\" + fc if fc <> "soil_map_unit_area": if fc <> "wetland_area": if fc <> "flood_zone_area": print fc #if gp.exists("vehicle_parking_area"): rows = gp.UpdateCursor(fc, "", PointRef) for row in rows: allFields = gp.listfields(fc) print fc for field in allFields: fn = field.name if fn == "coord_x": feat = row.shape coord = feat.getPart() lon = coord.X row.coord_x = lon rows.updateRow(row) print fc, fn + " calculated to long" elif fn == "coord_y": feat = row.shape coord = feat.getPart() lat = coord.Y row.coord_y = lat rows.updateRow(row) print fc, fn + " calculated to lat" FcList = gp.ListFeatureClasses("*","Polygon") for fc in FcList: gp.workspace = dataset + "\\" + fc if fc <> "soil_map_unit_area": if fc <> "wetland_area": if fc <> "flood_zone_area": print fc rows = gp.UpdateCursor(fc, "", PolyRef) for row in rows: allFields = gp.listfields(fc) print fc for field in allFields: fn = field.name if fn == "area_size": feat = row.getValue("SHAPE") cent = feat.area rows.updateRow(row) #expression = "!shape.area@acres!" #gp.CalculateField_management(fc, fn, expression, "PYTHON_9.3") print fc, fn + " calculated to area"