Beispiel #1
0
 public void Test1()
 {
     var meters  = WebMercatorHandler.LatLonToMeters(50, 4);
     var pixels0 = WebMercatorHandler.MetersToPixels(meters, 0, 1024);
     var pixels1 = WebMercatorHandler.MetersToPixels(meters, 1, 1024);
     var pixels2 = WebMercatorHandler.MetersToPixels(meters, 2, 1024);
     var pixels3 = WebMercatorHandler.MetersToPixels(meters, 3, 1024);
 }
        public (double longitude, double latitude) TransformInverse(int x, int y)
        {
            var globalX = _left + x;
            var globalY = _top - y;

            var meters      = WebMercatorHandler.PixelsToMeters((globalX, globalY), _tile.Zoom, (int)_extent);
            var coordinates = WebMercatorHandler.MetersToLatLon(meters);

            return(coordinates);
        }
        /// <summary>
        /// Initializes this transformation utility
        /// </summary>
        /// <param name="tile">The tile's bounds</param>
        /// <param name="extent">The tile's extent in pixel. Tiles are always square.</param>
        public TileGeometryTransform(Tiles.Tile tile, uint extent) : this()
        {
            _tile   = tile;
            _extent = extent;

            var meters = WebMercatorHandler.LatLonToMeters(_tile.Top, _tile.Left);
            var pixels = WebMercatorHandler.MetersToPixels(meters, tile.Zoom, (int)extent);

            _top  = (long)pixels.y;
            _left = (long)pixels.x;
        }
        /// <summary>
        /// Checks to see if a geometries envelope is greater than 1 square pixel in size for a specified zoom leve.
        /// </summary>
        /// <param name="polygon">Polygon to test.</param>
        /// <param name="zoom">Zoom level </param>
        /// <returns></returns>
        private static bool IsGreaterThanOnePixelOfTile(Geometry polygon, int zoom)
        {
            (double x1, double y1) = WebMercatorHandler.MetersToPixels(WebMercatorHandler.LatLonToMeters(polygon.EnvelopeInternal.MinY, polygon.EnvelopeInternal.MinX), zoom, 512);
            (double x2, double y2) = WebMercatorHandler.MetersToPixels(WebMercatorHandler.LatLonToMeters(polygon.EnvelopeInternal.MaxY, polygon.EnvelopeInternal.MaxX), zoom, 512);

            var dx = Math.Abs(x2 - x1);
            var dy = Math.Abs(y2 - y1);

            //Both must be greater than 0, and atleast one of them needs to be larger than 1.
            return(dx > 0 && dy > 0 && (dx > 1 || dy > 1));
        }
        /// <summary>
        /// Transforms the coordinate at <paramref name="index"/> of <paramref name="sequence"/> to the tile coordinate system.
        /// The return value is the position relative to the local point at (<paramref name="currentX"/>, <paramref name="currentY"/>).
        /// </summary>
        /// <param name="sequence">The input sequence</param>
        /// <param name="index">The index of the coordinate to transform</param>
        /// <param name="currentX">The current horizontal component of the cursor location. This value is updated.</param>
        /// <param name="currentY">The current vertical component of the cursor location. This value is updated.</param>
        /// <returns>The position relative to the local point at (<paramref name="currentX"/>, <paramref name="currentY"/>).</returns>
        public (int x, int y) Transform(CoordinateSequence sequence, int index, ref int currentX, ref int currentY)
        {
            var lon = sequence.GetOrdinate(index, Ordinate.X);
            var lat = sequence.GetOrdinate(index, Ordinate.Y);

            var meters = WebMercatorHandler.LatLonToMeters(lat, lon);
            var pixels = WebMercatorHandler.MetersToPixels(meters, _tile.Zoom, (int)_extent);

            int localX = (int)(pixels.x - _left);
            int localY = (int)(_top - pixels.y);
            int dx     = localX - currentX;
            int dy     = localY - currentY;

            currentX = localX;
            currentY = localY;

            return(dx, dy);
        }