I am running ArcMap 10.2 on Windows 7.
I have a point feature class that has a designated field for X and Y coordinate values. I am looking to automate the generation of these values within a model that I already use for multiple processes. I have tried many different attempts with Python code within the Calculate Field tool in Model Builder with no success.
Here are the options that I have tried thus far: (the formatting is awkward and I couldn't figure out how to fix it. The code is there, though.
1 - !shape.extent.XMax! (I think this is the wrong direction since our target layer is of point type)
2 - '=========================
'point_Get_X.cal
'Author: Ianko Tchoukanski
'=========================
On Error Resume Next
Dim pMxDoc As IMxDocument
Dim pMap As IMap
Dim pPoint As IPoint
Dim dX As Double
Dim bSrefFromMap As Boolean
'=======================
'Adjust the parameter below
'True ==> the coordinates will be calculated in the projection of the Map
'False ==> the coordinates will be calculated in the projection of the data
bSrefFromMap = True
'=======================
If (Not IsNull([Shape])) Then
Set pPoint = [Shape]
If (bSrefFromMap) Then
Set pMxDoc = ThisDocument
Set pMap = pMxDoc.FocusMap
pPoint.Project pMap.SpatialReference
End If
dX = pPoint.X
else
dX = 0
End If
3 - !shape.firstpoint.X!
4 – Dim dblX As Double
Dim pPoint As IPoint
Set pPoint = [Shape]
dblX = pPoint.X
Solved! Go to Solution.
The calculation only works with Python if you are using Desktop 10.0 or later. For a Point Feature Class the calculation should be:
!Shape.Centroid.X!
or
!Shape.Centroid.Y!
With Attribute Assistant you can have your coordinate fields filled in immediately upon point creation and updated upon any geometry update using the X_COORDINATE and Y_COORDINATE methods. You can get Attribute Assistant included with the download of the Address Data Management templates under the button that says "OPEN".
In the DynamicValue table you would enter the following:
Table Name field: name of the feature class to store the coordinates
Field Name field: the name of the field where you want to store your coordinate value
Method field: X_COORDINATE or Y_COORDINATE
Value Info field: Null
On Create field: True
On Change (Attribute) field: False
On Change (Geometry) field: True
Manual Only field: False
Rule Weight field: 1
The DynamicValue table has to be in a Dataset that is named identically to the one contained in the sample geodatabase when you copy it into the geodatabase where your data resides (Attribute Assistant only works with geodatabases). The DynamicValue table also has to be added to the map where you are editing and you would need to make sure the Attribute Assistant toolbar shows that the extension is active.
The calculation only works with Python if you are using Desktop 10.0 or later. For a Point Feature Class the calculation should be:
!Shape.Centroid.X!
or
!Shape.Centroid.Y!
With Attribute Assistant you can have your coordinate fields filled in immediately upon point creation and updated upon any geometry update using the X_COORDINATE and Y_COORDINATE methods. You can get Attribute Assistant included with the download of the Address Data Management templates under the button that says "OPEN".
In the DynamicValue table you would enter the following:
Table Name field: name of the feature class to store the coordinates
Field Name field: the name of the field where you want to store your coordinate value
Method field: X_COORDINATE or Y_COORDINATE
Value Info field: Null
On Create field: True
On Change (Attribute) field: False
On Change (Geometry) field: True
Manual Only field: False
Rule Weight field: 1
The DynamicValue table has to be in a Dataset that is named identically to the one contained in the sample geodatabase when you copy it into the geodatabase where your data resides (Attribute Assistant only works with geodatabases). The DynamicValue table also has to be added to the map where you are editing and you would need to make sure the Attribute Assistant toolbar shows that the extension is active.
Thanks for the response, Richard. This seems to be exactly what I need.
Another question going forward from this point, though... How do I get this X and Y value into say decimal degrees, or possibly even degrees, minutes, seconds?
The value that is returned for this looks like this:
Lat: -10882136.0839
Long: 3537254.8606
I tried to enter this into Google Earth just to verify that they lead to the same spot, but it doesn't even recognize the values. Does this have something to do with the coordinate system that is used? The above values were returned on a feature class in Web Mercator (Auxiliary Sphere).
*EDIT* I tested on a dummy point feature class in NAD 83 and got values that look like this:
Lat: -115.531531
Long: 38.591616
However, these aren't recognized by Google Earth either.
If you can provide any clarification as to what these values actually are, and how to get them into a more recognizable format, I'd really appreciate it.
Thanks!
Latitude is the Y Coordinate and Longitude is the X Coordinate. So the coordinates for NAD 83 do go to a spot in Nevada about 3000 feet south of R R Valley Rd to the Southeast of Highway 6 if you enter them as:
38.591616 -115.531531
I presume that is the correct spot. Coordinates will only be returned by Attribute Assistant in the native coordinate system of the Feature Class and if it is not in WGS 1984 the coordinates returned will not work correctly with Google Earth. Attribute Assistant has no options to do on the fly projections from one spatial reference to another.
This could mean that you have to maintain the point in two different feature classes if the coordinate system of all your base data is in Web Mercator (Auxiliary Sphere) and you also want WGS 1984 coordinates or you would need to do a separate geoprocessing step to do a temporary projection and coordinate calculation into a WGS 1984 projection, and either way then join and calculate the WGS 1984 coordinates into the Web Mercator feature class. Geoprocessing would normally not be live and copy pasting from one projection to another in an edit session is not recommended. I do the geoprocessing approach and use a python cursor and dictionary routine to do the transfer rather than a join and calculate for much better speed.
Edit: Corrected myself about the projection Google uses, which is WGS 1984, not NAD 83.
I see. For some reason I was mistakenly thinking that X was Latitude and Y was Longitude. Yes, they are correct when switched. I think I was still confused on the other stuff that I didn't even think to reverse them...(dummy ).
I don't think I'll use the attribute assistant because we don't actually create point data, we load it in from shapefiles. We create line and other data after that, but no points are created. Definitely good to know for future reference, though!
Thanks a ton for your assistance and thorough responses. I appreciate your time.
For lines you can get Lat and Long values for the start, end or centroid points on the line. The calculations are:
!Shape.FirstPoint.X! and !Shape.FirstPoint.Y!
!Shape.LastPoint.X! and !Shape.LastPoint.Y!
!Shape.Centroid.X! and !Shape.Centroid.Y!
The Attribute Assistant can extract each of these coordinates if you change the
Value Info field to S for the start of the line, E for the end of the line, or Null for the centroid. I use the S and E options for all of my road centerlines and then concatenate them with the length to form an attribute join field that represents the unique geometry of each line. I can detect changes this way. Attribute Assistant can also fill in one or more date fields automatically every time the geometry is created or altered or the attributes are altered. So you absolutely should invest the time in using it for all of your geometry or attribute alteration monitoring routines.