Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static class GeoConverter
- {
- private static ICoordinateSystem _epsg3785;
- public static ICoordinateSystem epsg3785
- {
- get
- {
- if (_epsg3785 == null)
- {
- var cf = new CoordinateSystemFactory();
- _epsg3785 = cf.CreateFromWkt("PROJCS[\"WGS84 / Google Mercator\", " +
- " GEOGCS[\"WGS 84\", " +
- " DATUM[\"World Geodetic System 1984\", " +
- " SPHEROID[\"WGS 84\", 6378137.0, 298.257223563, AUTHORITY[\"EPSG\",\"7030\"]], " +
- " AUTHORITY[\"EPSG\",\"6326\"]], " +
- " PRIMEM[\"Greenwich\", 0.0, AUTHORITY[\"EPSG\",\"8901\"]], " +
- " UNIT[\"degree\", 0.017453292519943295], " +
- " AXIS[\"Longitude\", EAST], " +
- " AXIS[\"Latitude\", NORTH], " +
- " AUTHORITY[\"EPSG\",\"4326\"]], " +
- " PROJECTION[\"Mercator_1SP\"], " +
- " PARAMETER[\"semi_minor\", 6378137.0], " +
- " PARAMETER[\"latitude_of_origin\", 0.0], " +
- " PARAMETER[\"central_meridian\", 0.0], " +
- " PARAMETER[\"scale_factor\", 1.0], " +
- " PARAMETER[\"false_easting\", 0.0], " +
- " PARAMETER[\"false_northing\", 0.0], " +
- " UNIT[\"m\", 1.0], " +
- " AXIS[\"x\", EAST], " +
- " AXIS[\"y\", NORTH], " +
- " AUTHORITY[\"EPSG\",\"900913\"]]");
- }
- return _epsg3785;
- }
- }
- public static double[] formWGS84toEPSG3785(double[] coordinates)
- {
- var converter = new CoordinateTransformationFactory().CreateFromCoordinateSystems(GeographicCoordinateSystem.WGS84, epsg3785);
- return converter.MathTransform.Transform(coordinates);
- }
- public static double[] formEPSG3785toWGS84(double[] coordinates)
- {
- var converter = new CoordinateTransformationFactory().CreateFromCoordinateSystems(epsg3785, GeographicCoordinateSystem.WGS84);
- return converter.MathTransform.Transform(coordinates);
- }
- public static string ConvertWKT(string wkt, string convertType)
- {
- var reExp = new Regex(@"([0-9]+\.[0-9]+) ([0-9]+\.[0-9]+)");
- var matches = reExp.Matches(wkt);
- var points = new Dictionary<string, double[]>();
- foreach (var match in matches)
- {
- var coord = new double[0];
- var coord1 = double.Parse((match as Match).Groups[1].ToString(), CultureInfo.InvariantCulture);
- var coord2 = double.Parse((match as Match).Groups[2].ToString(), CultureInfo.InvariantCulture);
- if (convertType == "formEPSG3785toWGS84")
- {
- coord = formEPSG3785toWGS84(new double[] { coord1, coord2 });
- }
- if (convertType == "formWGS84toEPSG3785")
- {
- coord = formWGS84toEPSG3785(new double[] { coord1, coord2 });
- }
- var oldCoords = (match as Match).Groups[0].ToString();
- if (!points.ContainsKey(oldCoords))
- {
- points.Add(oldCoords, coord);
- }
- }
- foreach (var doublese in points)
- {
- wkt = wkt.Replace(doublese.Key, doublese.Value[0].ToString(CultureInfo.InvariantCulture) + " " + doublese.Value[0].ToString(CultureInfo.InvariantCulture));
- }
- return wkt;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement