How to get Lat Lon?

739
4
07-28-2011 10:11 AM
GregRieck
Occasional Contributor III
Hello,

I'm looking for a way to obtain the Lat Lon value as new feature classes are created no matter what the current map projection is. I have feature classes that contain attribute fields for storing the Lat Lon values as a string. I need to populate theses fields as features are created.

Is there a function available in the object model that allows me to do this?

Greg
0 Kudos
4 Replies
NeilClemmons
Regular Contributor III
If the geometry has a projected coordinate system then you should be able to just use the geographic coordinate system it's based on and project it.  The following is a code snippet from one of our apps.

                ' Project the UTM to Lat/Long.
                Dim projCoordinateSystem As IProjectedCoordinateSystem = DirectCast(point.SpatialReference, IProjectedCoordinateSystem)
                point.Project(projCoordinateSystem.GeographicCoordinateSystem)

                ' Update the DD textboxes.
                DdLatitudeTextbox.Text = Format(point.Y, "0.000000")
                DdLongitudeTextbox.Text = Format(point.X, "0.000000")
0 Kudos
GregRieck
Occasional Contributor III
If the geometry has a projected coordinate system then you should be able to just use the geographic coordinate system it's based on and project it.  The following is a code snippet from one of our apps.

                ' Project the UTM to Lat/Long.
                Dim projCoordinateSystem As IProjectedCoordinateSystem = DirectCast(point.SpatialReference, IProjectedCoordinateSystem)
                point.Project(projCoordinateSystem.GeographicCoordinateSystem)

                ' Update the DD textboxes.
                DdLatitudeTextbox.Text = Format(point.Y, "0.000000")
                DdLongitudeTextbox.Text = Format(point.X, "0.000000")


Thanks Neil that's what I was looking for. Do you have a way to handle polygons by returning the lat lon of the centroid of the polygon?

Greg
0 Kudos
NeilClemmons
Regular Contributor III
Project the polygon just like this code projects the point.  Then QI from IPolygon to IArea and use the Centroid property.
0 Kudos
GregRieck
Occasional Contributor III
Thanks Neil, I knew that, just spaced it. I added a trap to make sure it's a Projected Coordinate System

    
      IPolygon pg = MyFeature.Shape as IPolygon;
      if (pg.SpatialReference is IProjectedCoordinateSystem)
      {
        projCoordinateSystem = pg.SpatialReference as IProjectedCoordinateSystem;
        pg.Project(projCoordinateSystem.GeographicCoordinateSystem);
        // Update the DD textboxes.
        IArea area = pg as IArea;
        obj.set_Value(gpslatitude, string.Format("{0:0.######}", area.Centroid.Y));
        obj.set_Value(gpslongitude, string.Format("{0:0.######}", area.Centroid.X));
      }
0 Kudos