public void epsg3140_to_wgs()
        {
            var crs = EpsgMicroDatabase.Default.GetCrs(3140);
            var wgs = EpsgMicroDatabase.Default.GetCrs(4326);

            // source for coordinates is http://epsg.io/3140/map
            var ptWgs = new GeographicCoordinate(-17.785, 177.97);
            var pt3140 = new Point2(530138.52663372, 821498.68898981); // units in links

            var gen = new EpsgCrsCoordinateOperationPathGenerator();
            var paths = gen.Generate(wgs, crs);
            var compiler = new StaticCoordinateOperationCompiler();
            var txs = paths
                .Select(p => compiler.Compile(p))
                .Where(p => p != null);

            var forward = txs.Single();
            var actualForward = (Point2)forward.TransformValue(ptWgs);
            Assert.AreEqual(pt3140.X, actualForward.X, 30);
            Assert.AreEqual(pt3140.Y, actualForward.Y, 30);

            var reverse = forward.GetInverse();
            var actualReverse = (GeographicCoordinate)reverse.TransformValue(pt3140);
            Assert.AreEqual(ptWgs.Longitude, actualReverse.Longitude, 0.01);
            Assert.AreEqual(ptWgs.Latitude, actualReverse.Latitude, 0.01);
        }
        public void Test(string sourceResourceName, string targetResourceName, double projectDelta, double unprojectDelta)
        {
            var sourceData = GoldData.GetReadyReader(sourceResourceName);
            var targetData = GoldData.GetReadyReader(targetResourceName);
            var sourceCrs = GoldData.GetCrs(sourceData);
            var targetCrs = GoldData.GetCrs(targetData);

            Assert.IsNotNull(sourceCrs);
            Assert.IsNotNull(targetCrs);

            var operationGenerator = new HelmertCrsCoordinateOperationPathGenerator();
            var operationPath = operationGenerator.Generate(sourceCrs, targetCrs).First();
            Assert.IsNotNull(operationPath);
            var compiler = new StaticCoordinateOperationCompiler();
            var forward = new CompiledConcatenatedTransformation<GeographicCoordinate, Point2>(compiler.Compile(operationPath) as IEnumerable<ITransformation>);
            var inverse = forward.GetInverse();

            while (sourceData.Read() && targetData.Read()) {
                var geogreaphicExpected = sourceData.CurrentLatLon();
                var projectedExpected = targetData.CurrentPoint2D();

                var projected = forward.TransformValue(geogreaphicExpected);
                Assert.AreEqual(projectedExpected.X, projected.X, projectDelta);
                Assert.AreEqual(projectedExpected.Y, projected.Y, projectDelta);

                var geographic = inverse.TransformValue(projectedExpected);
                Assert.AreEqual(geogreaphicExpected.Latitude, geographic.Latitude, unprojectDelta);
                Assert.AreEqual(geogreaphicExpected.Longitude, geographic.Longitude, unprojectDelta);
            }
        }
        public void epsg3079_to_epsg3575()
        {
            var from = EpsgMicroDatabase.Default.GetCrs(3079);
            var to = EpsgMicroDatabase.Default.GetCrs(3575);
            var wgs = EpsgMicroDatabase.Default.GetCrs(4326);

            var somePlaceInMichigan = new GeographicCoordinate(40.4, -91.8);
            var expected3079 = new Point2(6992.885640195105, -644.956855237484);
            var expected3575 = new Point2(-5244224.354585549, 1095575.5476152631);

            var gen = new EpsgCrsCoordinateOperationPathGenerator();
            var fromPath = gen.Generate(from, wgs).First();
            var toPath = gen.Generate(to, wgs).First();

            var compiler = new StaticCoordinateOperationCompiler();

            var fromTx = compiler.Compile(fromPath);
            var toTx = compiler.Compile(toPath);

            var a = (GeographicCoordinate)fromTx.TransformValue(expected3079);
            Assert.AreEqual(somePlaceInMichigan.Latitude, a.Latitude, 0.01);
            Assert.AreEqual(somePlaceInMichigan.Longitude, a.Longitude, 0.01);
            var b = (Point2)fromTx.GetInverse().TransformValue(somePlaceInMichigan);
            Assert.AreEqual(expected3079.X, b.X, 0.01);
            Assert.AreEqual(expected3079.Y, b.Y, 0.01);
            var d = (Point2)toTx.GetInverse().TransformValue(somePlaceInMichigan);
            Assert.AreEqual(expected3575.X, d.X, 0.01);
            Assert.AreEqual(expected3575.Y, d.Y, 0.01);
            var c = (GeographicCoordinate)toTx.TransformValue(expected3575);
            Assert.AreEqual(somePlaceInMichigan.Latitude, c.Latitude, 0.01);
            Assert.AreEqual(somePlaceInMichigan.Longitude, c.Longitude, 0.01);

            var allTotalPaths = gen.Generate(from, to).ToList();
            var totalPath = allTotalPaths.First();
            var totalTx = compiler.Compile(totalPath);
            var actual3575 = (Point2)totalTx.TransformValue(expected3079);

            Assert.AreEqual(expected3575.X, actual3575.X, 1);
            Assert.AreEqual(expected3575.Y, actual3575.Y, 1);
        }
Ejemplo n.º 4
0
        private ConversionResult ExecuteStaticEpsg(TestCase testCase)
        {
            var result = new ConversionResult();
            try {

                var paths = _epsgPathGenerator.Generate(testCase.Source, testCase.Target)
                    .OrderBy(x => GetEpsgAccuracy(x) ?? 999);

                var compiler = new StaticCoordinateOperationCompiler();
                var compiledPaths = paths
                    .Select(x => compiler.Compile(x));

                var bestCompiledPath = compiledPaths.FirstOrDefault(x => x != null);

                var transformation = bestCompiledPath;
                if (transformation == null)
                    throw new InvalidOperationException("No compiled transformation");

                result.ResultData = transformation.TransformValues(testCase.InputCoordinates).ToArray();
            }
            catch (Exception ex) {
                result.Exception = ex;
            }

            return result;
        }
        public void epsg3857_to_wgs()
        {
            var crs = EpsgMicroDatabase.Default.GetCrs(3857);
            var wgs = EpsgMicroDatabase.Default.GetCrs(4326);

            var ptWgs = new GeographicCoordinate(45,10);
            var pt3857 = new Point2(1113194, 5621521);

            var gen = new EpsgCrsCoordinateOperationPathGenerator();
            var paths = gen.Generate(wgs, crs);
            var compiler = new StaticCoordinateOperationCompiler();
            var txs = paths.Select(p => compiler.Compile(p)).Where(p => p != null);

            var forward = txs.Single();

            var actualZeroMeters = (Point2)forward.TransformValue(GeographicCoordinate.Zero);
            Assert.AreEqual(0, actualZeroMeters.X);
            Assert.AreEqual(0, actualZeroMeters.Y, 0.0000001);

            var actualForward = (Point2)forward.TransformValue(ptWgs);
            Assert.AreEqual(pt3857.X, actualForward.X, 30);
            Assert.AreEqual(pt3857.Y, actualForward.Y, 30);

            var reverse = forward.GetInverse();

            var actualZeroDegrees = (GeographicCoordinate)reverse.TransformValue(Point2.Zero);
            Assert.AreEqual(0, actualZeroDegrees.Longitude);
            Assert.AreEqual(0, actualZeroDegrees.Latitude);

            var actualReverse = (GeographicCoordinate)reverse.TransformValue(pt3857);
            Assert.AreEqual(ptWgs.Longitude, actualReverse.Longitude, 0.0001);
            Assert.AreEqual(ptWgs.Latitude, actualReverse.Latitude, 0.0001);
        }
        public void epsg4326_to_5072()
        {
            var wgs = EpsgMicroDatabase.Default.GetCrs(4326);
            var nad83Albers = EpsgMicroDatabase.Default.GetCrs(5072);
            var gen = new EpsgCrsCoordinateOperationPathGenerator();
            var paths = gen.Generate(wgs, nad83Albers);
            var compiler = new StaticCoordinateOperationCompiler();
            var txs = paths.Select(p => compiler.Compile(p)).Where(p => p != null);
            var forward = txs.Single();

            var transformed = (Point2)forward.TransformValue(new GeographicCoordinate(39, -105));
            Assert.AreEqual(-771063, transformed.X, 1);
            Assert.AreEqual(1811448, transformed.Y, 1);
        }
Ejemplo n.º 7
0
        public HttpResponseMessage Draw(double? minLon, double? maxLon, double? minLat, double? maxLat, int? maxImgWidth, int? maxImgHeight)
        {
            var shapefilePath = HostingEnvironment.MapPath("~/App_Data/builtupp_usa/builtupp_usa.shp");

            ITransformation<GeographicCoordinate, Point2> transformation = null;
            var targetCrs = EpsgMicroDatabase.Default.GetCrs(3005);
            LongitudeDegreeRange dataLongitudeRange;
            Range dataLatitudeRange;
            using (var shapeFile = Shapefile.Open(shapefilePath)) {
                dataLongitudeRange = new LongitudeDegreeRange(shapeFile.Extent.MinX, shapeFile.Extent.MaxX);
                dataLatitudeRange = new Range(shapeFile.Extent.MinY, shapeFile.Extent.MaxY);

                var dataCrs = EpsgMicroDatabase.Default.GetCrs(4326);
                var pathGenerator = new EpsgCrsCoordinateOperationPathGenerator();
                var paths = pathGenerator.Generate(dataCrs, targetCrs);
                var compiler = new StaticCoordinateOperationCompiler();
                var firstTransfom = paths.Select(p => {
                    return compiler.Compile(p);
                }).First(x => x != null);

                transformation = firstTransfom as ITransformation<GeographicCoordinate, Point2>;
                if (transformation == null && firstTransfom is IEnumerable<ITransformation>)
                    transformation = new CompiledConcatenatedTransformation<GeographicCoordinate, Point2>((IEnumerable<ITransformation>)firstTransfom);

            }

            var geoMbrMin = new GeographicCoordinate(minLat ?? dataLatitudeRange.Low, minLon ?? dataLongitudeRange.Start);
            var geoMbrMax = new GeographicCoordinate(maxLat ?? dataLatitudeRange.High, maxLon ?? dataLongitudeRange.End);
            var geoMbrTL = new GeographicCoordinate(geoMbrMax.Latitude, geoMbrMin.Longitude);
            var geoMbrTR = new GeographicCoordinate(geoMbrMin.Latitude, geoMbrMax.Longitude);

            var projectedMbrPoints = new[] {
                    geoMbrMin,
                    geoMbrMax,
                    geoMbrTL,
                    geoMbrTR,
                    new GeographicCoordinate(geoMbrMin.Latitude, Math.Abs(geoMbrMin.Longitude + geoMbrMax.Longitude) / 2.0)
                }
                .Select(transformation.TransformValue)
                .ToArray();

            var projectedExtent = new Mbr(
                new Point2(projectedMbrPoints.Min(x => x.X), projectedMbrPoints.Min(x => x.Y)),
                new Point2(projectedMbrPoints.Max(x => x.X), projectedMbrPoints.Max(x => x.Y))
            );

            var geogMapOrigin = new GeographicCoordinate(dataLatitudeRange.Mid, dataLongitudeRange.Mid);
            var mapOrigin = transformation.TransformValue(geogMapOrigin);

            var mapOffset = new Vector2(0/*-(mapOrigin.X - projectedExtent.X.Mid)*/, projectedExtent.Height / 2.0);

            var imageSizeLimits = new Vector2(maxImgWidth ?? 300, maxImgHeight ?? 300);
            if (imageSizeLimits.X > 4096 || imageSizeLimits.Y > 4096)
                throw new ArgumentException("Image size too large");

            var dataRatio = new Vector2(projectedExtent.Width / imageSizeLimits.X, projectedExtent.Height / imageSizeLimits.Y);
            var lowCorner = projectedExtent.Min;
            Vector2 desiredImageSize;
            double imageScaleFactor;
            if (dataRatio.Y < dataRatio.X) {
                imageScaleFactor = imageSizeLimits.X / projectedExtent.Width;
                desiredImageSize = new Vector2(imageSizeLimits.X, (int)(projectedExtent.Height * imageScaleFactor));
            }
            else {
                imageScaleFactor = imageSizeLimits.Y / projectedExtent.Height;
                desiredImageSize = new Vector2((int)(projectedExtent.Width * imageScaleFactor), imageSizeLimits.Y);
            }

            using (var image = new System.Drawing.Bitmap((int)desiredImageSize.X, (int)desiredImageSize.Y))
            using (var graphics = Graphics.FromImage(image)) {
                graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                var shapeSource = new PointShapefileShapeSource(shapefilePath);
                var shapeReader = new ShapeReader(shapeSource);
                using (var shapeEnumerator = shapeReader.GetEnumerator()) {
                    var sourceCoordinates = ReadPagesToGeographicCoordinate(shapeEnumerator).SelectMany(x => x);
                    var pointColor = Color.Black;
                    var circleFillBrush = new SolidBrush(Color.FromArgb(64,16,64,128));
                    var featureRadius = 3.0;
                    var featureDiameter = featureRadius * 2;
                    var featureDiameterFloat = (float)featureDiameter;
                    var topLeftOffset = new Vector2(-featureRadius,-featureRadius);

                    foreach (var transformedPoint in transformation.TransformValues(sourceCoordinates)) {
                        var offsetPoint = transformedPoint.Difference(lowCorner).Add(mapOffset);

                        var scaledPoint = offsetPoint.GetScaled(imageScaleFactor);
                        var screenPoint = new Point2(scaledPoint.X, image.Height - scaledPoint.Y);
                        var drawTopLeft = screenPoint.Add(topLeftOffset);

                        graphics.FillEllipse(circleFillBrush, (float)drawTopLeft.X, (float)drawTopLeft.Y, featureDiameterFloat, featureDiameterFloat);
                    }
                }
                var result = new HttpResponseMessage(HttpStatusCode.OK);
                byte[] imageBytes;
                using (var memoryStream = new MemoryStream()) {
                    image.Save(memoryStream, ImageFormat.Png);
                    imageBytes = memoryStream.ToArray();
                }
                result.Content = new ByteArrayContent(imageBytes);
                result.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("image/png");
                return result;
            }
        }
Ejemplo n.º 8
0
        private Helmert7Transformation ExtractHelmert7Transformation(EpsgCoordinateTransformInfo transform)
        {
            var method = transform.Method;
            Contract.Assume(method != null);

            var compiler = new StaticCoordinateOperationCompiler();
            var compileRequest = new CoordinateOperationCrsPathInfo(
                new[] { transform.SourceCrs, transform.TargetCrs },
                new[] { transform });
            var compileResult = compiler.Compile(compileRequest);
            if (compileResult == null)
                return null;
            var transformSteps = (compileResult as IEnumerable<ITransformation>) ?? ArrayUtil.CreateSingleElementArray(compileResult);
            var exposedSteps = transformSteps.Select(step => {
                if (step is GeocentricTransformationGeographicWrapper) {
                    return ((GeocentricTransformationGeographicWrapper)step).GeocentricCore;
                }
                return step;
            });

            foreach (var step in exposedSteps) {
                if (step is Helmert7Transformation) {
                    return step as Helmert7Transformation;
                }
                if (step is GeocentricTranslation) {
                    return new Helmert7Transformation(((GeocentricTranslation)step).Delta);
                }
                if (step is GeographicGeocentricTranslation) {
                    return new Helmert7Transformation(((GeographicGeocentricTranslation)step).Delta);
                }
            }

            return null;
        }