mandag den 21. juli 2008

Fra lat/lon til x/y og vice versa

Hvis du står og skal konvertere dine koordinater fra koordinater med en given projektion til længde/breddegrad (og omvendt) så er koden til dette her.

If you need to convert from projected coordinates to geographic coordinates (and vice versa) here is the code.

/Sik

Source: http://adityaiiii.blogspot.com/2008/07/convert-co-ordinates-between-geographic.html

Projected to Geographic co-ordinate

systemIPoint iPoint = new ESRI.ArcGIS.Geometry.Point();
iPoint.X = x;
iPoint.Y = y;
ISpatialReferenceFactory pSpatRefFact = new SpatialReferenceEnvironmentClass();
ISpatialReference pGeoCoordSystem = pSpatRefFact.CreateGeographicCoordinateSystem(4269); // Nad83


For more esriSRGeoCSType Constants see
http://edndoc.esri.com/arcobjects/9.1/ComponentHelp/esriGeometry/esriSRGeoCSType.htmIGeometry2

pGeometry = iPoint as Igeometry2;
pGeometry.SpatialReference = pSpatRefFact.CreateProjectedCoordinateSystem(2278);

For more esriSRProjCSType Constants see
http://edndoc.esri.com/arcobjects/9.1/ComponentHelp/esriGeometry

esriSRProjCSType.htmpGeometry.ProjectEx(pGeoCoordSystem,esriTransformDirection.esriTransformReverse, null, false, 0, 0);
IPoint GCSPoint = pGeometry as IPoint;
double lat = GCSPoint.Y;
double lng = GCSPoint.X;

Geographic to Projected co-ordinate system

ESRI.ArcGIS.Geometry.Point point1 = new ESRI.ArcGIS.Geometry.Point();
point1.X = x;
point1.Y = y;
ISpatialReferenceFactory pSpatRefFact = new SpatialReferenceEnvironmentClass();
ISpatialReference pProjCoordSystem = pSpatRefFact.CreateProjectedCoordinateSystem(53004); // Mercator

For more esriSRProjCSType Constants see http://edndoc.esri.com/arcobjects/9.1/ComponentHelp/esriGeometry/esriSRProjCSType.htm

IGeometry2 pGeometry = (IGeometry2)point1;
pGeometry.SpatialReference = pSpatRefFact.CreateGeographicCoordinateSystem(4326); // WGS_1984

For more esriSRGeoCSType Constants see
http://edndoc.esri.com/arcobjects/9.1/ComponentHelp/esriGeometry

esriSRGeoCSType.htmpGeometry.ProjectEx(pProjCoordSystem,esriTransformDirection.esriTransformReverse, null, false, 0, 0);
ESRI.ArcGIS.Geometry.Point PCSPoint = pGeo as ESRI.ArcGIS.Geometry.Point;

Ingen kommentarer:

Send en kommentar