Skip to content
Snippets Groups Projects
Commit 79efe7ea authored by Knuiman, Bart's avatar Knuiman, Bart
Browse files

Added projection methods to go from WebMercator to RD and GPS.

parent b18bf241
Branches master
No related tags found
Loading
......@@ -7,7 +7,7 @@ namespace Wander
{
public static class Projections
{
public enum Projection
public enum Common
{
NotSet,
WGS84,
......@@ -15,16 +15,16 @@ namespace Wander
WebMercator
}
static CoordinateSystem FromEnumProjection( CoordinateSystemFactory f, Projection pr )
static CoordinateSystem FromEnumProjection( CoordinateSystemFactory f, Common pr )
{
Debug.Assert( pr != Projection.NotSet, "Invalid enum to for a projection." );
Debug.Assert( pr != Common.NotSet, "Invalid enum to for a projection." );
switch (pr)
{
case Projection.WGS84:
case Common.WGS84:
return GeographicCoordinateSystem.WGS84;
case Projection.RD:
case Common.RD:
return f.CreateFromWkt( "PROJCS[\"Amersfoort / RD New\",GEOGCS[\"Amersfoort\",DATUM[\"Amersfoort\",SPHEROID[\"Bessel 1841\",6377397.155,299.1528128,AUTHORITY[\"EPSG\",\"7004\"]],TOWGS84[565.417,50.3319,465.552,-0.398957,0.343988,-1.8774,4.0725],AUTHORITY[\"EPSG\",\"6289\"]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],UNIT[\"degree\",0.0174532925199433,AUTHORITY[\"EPSG\",\"9122\"]],AUTHORITY[\"EPSG\",\"4289\"]],PROJECTION[\"Oblique_Stereographic\"],PARAMETER[\"latitude_of_origin\",52.15616055555555],PARAMETER[\"central_meridian\",5.38763888888889],PARAMETER[\"scale_factor\",0.9999079],PARAMETER[\"false_easting\",155000],PARAMETER[\"false_northing\",463000],UNIT[\"metre\",1,AUTHORITY[\"EPSG\",\"9001\"]],AXIS[\"X\",EAST],AXIS[\"Y\",NORTH],AUTHORITY[\"EPSG\",\"28992\"]]" );
case Projection.WebMercator:
case Common.WebMercator:
return ProjectedCoordinateSystem.WebMercator;
}
return ProjectedCoordinateSystem.WebMercator;
......@@ -36,7 +36,7 @@ namespace Wander
}
// Usage: ICoordinateTransformation.MathTransform.Transform( ref x, ref y )
public static ICoordinateTransformation CreateProjection( string fromWkt, Projection to )
public static ICoordinateTransformation CreateProjection( string fromWkt, Common to )
{
var f = new CoordinateSystemFactory();
var fromCoordinates = FromCustomWKT( f, fromWkt );
......@@ -45,7 +45,7 @@ namespace Wander
return factory.CreateFromCoordinateSystems( fromCoordinates, toCoordinates );
}
public static ICoordinateTransformation CreateProjection( Projection from, string toWkt )
public static ICoordinateTransformation CreateProjection( Common from, string toWkt )
{
var f = new CoordinateSystemFactory();
var fromCoordinates = FromEnumProjection( f, from );
......@@ -55,9 +55,9 @@ namespace Wander
}
// Usage: ICoordinateTransformation.MathTransform.Transform( ref x, ref y )
public static ICoordinateTransformation CreateProjection( Projection from, Projection to )
public static ICoordinateTransformation CreateProjection( Common from, Common to )
{
if (from == Projection.NotSet || to == Projection.NotSet)
if (from == Common.NotSet || to == Common.NotSet)
return null;
var f = new CoordinateSystemFactory();
var fromCoordinates = FromEnumProjection( f, from );
......@@ -66,6 +66,36 @@ namespace Wander
return factory.CreateFromCoordinateSystems( fromCoordinates, toCoordinates );
}
public static ICoordinateTransformation GPS2Web()
{
return CreateProjection( Common.WGS84, Common.WebMercator );
}
public static ICoordinateTransformation Web2GPS()
{
return CreateProjection( Common.WebMercator, Common.WGS84 );
}
public static ICoordinateTransformation GPS2RD()
{
return CreateProjection( Common.WGS84, Common.RD );
}
public static ICoordinateTransformation RD2GPS()
{
return CreateProjection( Common.RD, Common.WGS84 );
}
public static ICoordinateTransformation RD2Web()
{
return CreateProjection( Common.RD, Common.WebMercator );
}
public static ICoordinateTransformation Web2RD()
{
return CreateProjection( Common.WebMercator, Common.RD );
}
// Usage: ICoordinateTransformation.MathTransform.Transform( ref x, ref y )
// Use this site to lookup WKT's https://epsg.io/4326
public static ICoordinateTransformation CreateProjection( string fromWKT, string toWKT )
......@@ -78,17 +108,25 @@ namespace Wander
}
[MethodImpl( MethodImplOptions.AggressiveInlining )]
public static void Transform( ICoordinateTransformation transform, ref Vector3 v )
public static void Transform( this CoordinateTransformation transform, ref Vector3 v )
{
var res = transform.MathTransform.Transform( v.x, v.z );
v.x = (float)res.x;
v.z = (float)res.y;
}
// NOTE: For GPS, use Input: Lon(x)/lat(y). Output: also Lon(x)/lat(y).
[MethodImpl( MethodImplOptions.AggressiveInlining )]
public static (float x, float y) Transform( ICoordinateTransformation transform, float x, float y )
public static (double x, double y) Transform( this ICoordinateTransformation transform, double x, double y )
{
return ((float, float))transform.MathTransform.Transform( x, y );
return transform.MathTransform.Transform( x, y );
}
// NOTE: For GPS, use Input: Lon(x)/lat(y). Output: also Lon(x)/lat(y).
[MethodImpl( MethodImplOptions.AggressiveInlining )]
public static (float x, float y) TransformF( this ICoordinateTransformation transform, double x, double y )
{
return ((float, float))transform.MathTransform.Transform( x, y );
}
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment