Created
February 18, 2016 16:13
-
-
Save erycson/98fe8ab66d168e643621 to your computer and use it in GitHub Desktop.
Converting latitude and longitude to UTM using ProjNet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* LL to UTM Converter | |
* Copyright (C) 2016 Érycson Nóbrega <[email protected]> | |
* | |
* This program is free software: you can redistribute it and/or modify | |
* it under the terms of the GNU General Public License as published by | |
* the Free Software Foundation, either version 3 of the License, or | |
* (at your option) any later version. | |
* | |
* This program is distributed in the hope that it will be useful, | |
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
* GNU General Public License for more details. | |
* | |
* You should have received a copy of the GNU General Public License | |
* along with this program. If not, see <http://www.gnu.org/licenses/>. | |
*/ | |
using System; | |
using GeoAPI.CoordinateSystems; | |
using GeoAPI.CoordinateSystems.Transformations; | |
using ProjNet.CoordinateSystems; | |
using ProjNet.CoordinateSystems.Transformations; | |
namespace LLtoUTM | |
{ | |
class WGS84Coordinate { | |
public double Latitude; | |
public double Longitude; | |
} | |
class UTMCoordinate { | |
public double X; | |
public double Y; | |
public int Zone; | |
public bool ZoneIsNorth; | |
} | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
new Program(); | |
} | |
public Program() | |
{ | |
WGS84Coordinate ll = new WGS84Coordinate(); | |
ll.Latitude = -7.06965399; | |
ll.Longitude = -34.84076309; | |
UTMCoordinate _utm = LLtoUTM(ll); | |
Console.WriteLine("{0}E {1}N", _utm.Y, _utm.X); | |
WGS84Coordinate _ll = UTMtoLL(_utm); | |
Console.WriteLine("Lat: {0} Lon: {1}", _ll.Latitude, _ll.Longitude); | |
Console.ReadKey(); | |
} | |
int GetZone(double latitude, double longitude) | |
{ | |
if (latitude >= 56 && latitude < 64 && longitude >= 3 && longitude < 13) | |
return 32; | |
if (latitude >= 72 && latitude < 84) | |
{ | |
if (longitude >= 0 && longitude < 9) | |
return 31; | |
else if (longitude >= 9 && longitude < 21) | |
return 33; | |
if (longitude >= 21 && longitude < 33) | |
return 35; | |
if (longitude >= 33 && longitude < 42) | |
return 37; | |
} | |
return (int)Math.Ceiling((longitude + 180) / 6); | |
} | |
// http://romansk.blogspot.com.br/2013/09/converting-latitude-and-longitude-to.html | |
UTMCoordinate LLtoUTM(WGS84Coordinate pos) | |
{ | |
if (pos.Latitude < -80 || pos.Longitude > 84) | |
throw new Exception(); | |
int zone = GetZone(pos.Latitude, pos.Longitude); | |
CoordinateTransformationFactory ctfac = new CoordinateTransformationFactory(); | |
ICoordinateSystem wgs84 = ProjNet.CoordinateSystems.GeographicCoordinateSystem.WGS84; | |
ICoordinateSystem utm = ProjNet.CoordinateSystems.ProjectedCoordinateSystem.WGS84_UTM(zone, pos.Latitude > 0); | |
ICoordinateTransformation trans = ctfac.CreateFromCoordinateSystems(wgs84, utm); | |
double[] result = trans.MathTransform.Transform(new double[] { pos.Longitude, pos.Latitude }); | |
return new UTMCoordinate { | |
X = result[1], // N | |
Y = result[0], // E | |
Zone = zone, | |
ZoneIsNorth = pos.Latitude > 0 | |
}; | |
} | |
WGS84Coordinate UTMtoLL(UTMCoordinate pos) { | |
CoordinateTransformationFactory ctfac = new CoordinateTransformationFactory(); | |
ICoordinateSystem wgs84 = ProjNet.CoordinateSystems.GeographicCoordinateSystem.WGS84; | |
ICoordinateSystem utm = ProjNet.CoordinateSystems.ProjectedCoordinateSystem.WGS84_UTM(pos.Zone, pos.ZoneIsNorth); | |
ICoordinateTransformation trans = ctfac.CreateFromCoordinateSystems(utm, wgs84); | |
double[] result = trans.MathTransform.Transform(new double[] { pos.Y, pos.X }); | |
return new WGS84Coordinate | |
{ | |
Latitude = result[1], | |
Longitude = result[0] | |
}; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment