private static byte[] CreateMapSynchronized(IPRecord record, int zoomLevel, int desiredWidth, int desiredHeight, string mapFile)
        {
            byte[] sourceMapCompressed = File.ReadAllBytes("maps/" + mapFile);
            byte[] rgb = JpegCodec.Decode(sourceMapCompressed, out int w, out int h);

            RGBImage image = new RGBImage(rgb, w, h, rgb.Length / h);

            // Mark the image at the location of the IPRecord
            TileSystem.LatLongToPixelXY(record.latitude, record.longitude, zoomLevel, out int pixelX, out int pixelY);
            Color color = Color.Red;

            image.SetPixel(pixelX, pixelY, color);
            for (int i = 1; i < 8; i++)
            {
                image.SetPixel(pixelX + i, pixelY, color);
                image.SetPixel(pixelX - i, pixelY, color);
                image.SetPixel(pixelX, pixelY + i, color);
                image.SetPixel(pixelX, pixelY - i, color);
            }

            // Crop to desired size
            image = image.SubImage(pixelX - (desiredWidth / 2), pixelY - (desiredHeight / 2), desiredWidth, desiredHeight);

            byte[] jpegData = JpegCodec.Encode(image.rgb, image.width, image.height, 90);
            return(jpegData);
        }
        private List <Tile> MakeTileMatrix(int levelOfDetail, List <List <AltitudeResponse> > altitudes)
        {
            List <Tile> tileMatrix = new List <Tile>();

            foreach (var line in altitudes)
            {
                foreach (var point in line)
                {
                    TileSystem.LatLongToPixelXY(point.Location.Lat, point.Location.Lng, levelOfDetail, out var pixelX, out var pixelY);
                    TileSystem.PixelXYToTileXY(pixelX, pixelY, out var tileX, out var tileY);
                    TileSystem.MapPixelXYToTileInnerPixelXY(pixelX, pixelY, out var tileInnerX, out var tileInnerY);

                    MapPoint mapPoint = new MapPoint(tileInnerX, tileInnerY, point.Elevation);

                    if (mapPoint.Z > 0)
                    {
                        Tile tile = tileMatrix.FirstOrDefault(t => t.TileX == tileX && t.TileY == tileY);
                        if (tile == null)
                        {
                            tile = new Tile(levelOfDetail, tileX, tileY);
                            tileMatrix.Add(tile);
                        }
                        tile.Points.Add(mapPoint);
                    }
                }
            }
            return(tileMatrix);
        }
Beispiel #3
0
    public static Vector2Int GetVector2IntPixelCoords(int levelofDetail, double lat, double lng)
    {
        TileSystem.LatLongToPixelXY(lat, lng, levelofDetail, out var pixx, out var pixy);
        var rv = new Vector2Int(pixx, pixy);

        return(rv);
    }
Beispiel #4
0
    public static QkTile GetQktileFromLatLng(LatLng ll, int lod)
    {
        TileSystem.LatLongToPixelXY(ll.lat, ll.lng, lod, out var pixx, out var pixy);
        var vi = new Vector2Int(pixx, pixy);
        var rv = GetQktileFromPix(vi, lod);

        return(rv);
    }
Beispiel #5
0
    public static Vector2 GetMeterCoords(int levelofDetail, double lat, double lng)
    {
        TileSystem.LatLongToPixelXY(lat, lng, levelofDetail, out var pixx, out var pixy);
        var fak = (float)TileSystem.GroundResolution(lat, levelofDetail);
        var rv  = new Vector2(pixx * fak, pixy * fak);

        return(rv);
    }
        /// <summary>
        /// Simple fast grid cluster algorithm with no object location shift
        /// Find the first object in each grid cell and make it the cluster
        /// Contains all the other objects in that cell
        /// </summary>
        /// <param name="clusterableMapLayerObjects">items to cluster</param>
        /// <param name="worker">thread to check in cancelled</param>
        /// <param name="zoomLevel">zoom level of map</param>
        /// <param name="bounds">The bounds of the map control</param>
        /// <returns>Was the action cancelled?</returns>
        private bool GridCluster(IEnumerable <IClusterable> clusterableMapLayerObjects, BackgroundWorker worker, int zoomLevel, LocationRect bounds)
        {
            //get width and height from bounds
            int x1;
            int x2;
            int y1;
            int y2;

            TileSystem.LatLongToPixelXY(bounds.North, bounds.West, zoomLevel, out x1, out y1);
            TileSystem.LatLongToPixelXY(bounds.South, bounds.East, zoomLevel, out x2, out y2);

            double width = Math.Abs(x2 - x1);
            // Break the map up into a grid
            var numXCells = (int)Math.Ceiling(width / clusterwidth);
            // Create an array to store the clusters
            var clusters = new Dictionary <int, IClusterable>();

            foreach (var clusterableMapObject in clusterableMapLayerObjects)
            {
                if (clusterableMapObject.Initalized)
                {
                    //what cluster cell does this belong to?
                    var x     = (int)Math.Floor((clusterableMapObject.ProjectedPoints[zoomLevel].X - x1) / clusterwidth);
                    var y     = (int)Math.Floor((clusterableMapObject.ProjectedPoints[zoomLevel].Y - y1) / clusterheight);
                    var index = x + y * numXCells;
                    //if this is the first it is the cluster
                    if (!clusters.ContainsKey(index))
                    {
                        clusters.Add(index, clusterableMapObject);
                        clusterableMapObject.IsCluster = true;
                    }
                    else
                    {
                        //else add to existing cluster
                        clusters[index].ClusteredElements.Add(clusterableMapObject);
                        clusterableMapObject.IsClustered = true;
                    }
                }
                else
                {
                    //listen for event when it is ready
                    clusterableMapObject.ProjectionComplete += (o, e) =>
                    {
                        isDirty = true;
                        startMapMovementWorker();
                    };
                }

                //exit loop if thread cancelled
                if (worker != null && worker.CancellationPending)
                {
                    return(false);
                }
            }

            return(true);
        }
Beispiel #7
0
        public IEnumerable <Hexagon> ProcessHexagonsFromGeoData(IEnumerable <GeoData> geoData, LayersLoaderTarget[] targets, BoundingBox boundingBox)
        {
            foreach (var geo in geoData)
            {
                bool intersectsBoundingBox = false;

                foreach (PointXY[] coordinates in geo.Points)
                {
                    foreach (PointXY point in coordinates)
                    {
                        //Check if point is outside the specified bounds
                        if (boundingBox == null || point.X > boundingBox.West && point.X < boundingBox.West && point.Y > boundingBox.South && point.Y < boundingBox.North)
                        {
                            intersectsBoundingBox = true;
                        }

                        (point.X, point.Y) = TileSystem.LatLongToPixelXY(point.Y, point.X, hexagonDefinition.ReferenceZoom);
                    }
                }

                if (intersectsBoundingBox)
                {
                    switch (geo.DataType)
                    {
                    case DataType.Area:
                    {
                        foreach (Hexagon hexagon in ProcessArea(geo, targets))
                        {
                            yield return(hexagon);
                        }

                        break;
                    }

                    case DataType.Path:
                    {
                        foreach (Hexagon hexagon in ProcessPath(geo, targets))
                        {
                            yield return(hexagon);
                        }

                        break;
                    }

                    case DataType.Pixel:
                    {
                        foreach (Hexagon hexagon in ProcessPixel(geo, targets))
                        {
                            yield return(hexagon);
                        }

                        break;
                    }
                    }
                }
            }
        }
Beispiel #8
0
        public void LatLonToPixelXYTest(double lon, double lat, int pixelX, int pixelY, int zoom)
        {
            PointXY pixel = new PointXY();

            (pixel.X, pixel.Y) = TileSystem.LatLongToPixelXY(lat, lon, zoom);

            Assert.Equal(pixelX, pixel.X);
            Assert.Equal(pixelY, pixel.Y);
        }
Beispiel #9
0
    public void AddRowLngLat(LegLatLng ll, int lod, double pixToMeters, Vector2d org)
    {
        TileSystem.LatLongToPixelXY(ll.lat, ll.lng, lod, out var pixx, out var pixz);
        var x = pixx * pixToMeters - org.x;
        var z = pixz * pixToMeters - org.y;

        mapdata.Add(new LegMapCoordPoint {
            lng = (float)ll.lng, lat = (float)ll.lat, x = (float)x, z = (float)z
        });
    }
Beispiel #10
0
 public static RelativePixel GetRelativePixel(double lat, double lon, int zoom)
 {
     TileSystem.LatLongToPixelXY(lat, lon, zoom, out int pixelX, out int pixelY);
     TileSystem.PixelXYToTileXY(pixelX, pixelY, out int tileX, out int tileY);
     return(new RelativePixel()
     {
         pixel = new Point(pixelX - (256 * tileX), pixelY - (256 * tileY)),
         tile = new Point(tileX, tileY)
     });
 }
        public void LatLongToPixelXY_ShouldReturnCorrectPixelXYCoordinates()
        {
            double latitude      = 14.5995;
            double longitude     = 120.9842;
            int    levelOfDetail = 12;

            (int pileX, int pileY)expected = (876680, 481296);

            (int pileX, int pileY)actual = TileSystem.LatLongToPixelXY(latitude, longitude, levelOfDetail);

            Assert.Equal(expected, actual);
        }
Beispiel #12
0
    public Quilt(double latitude1, double longitude1, double latitude2, double longitude2, int maxLevelOfDetail = 23)
    {
        TileSystem.LatLongToPixelXY(latitude1, longitude1, maxLevelOfDetail, out int pixelX1, out int pixelY1);
        TileSystem.LatLongToPixelXY(latitude2, longitude2, maxLevelOfDetail, out int pixelX2, out int pixelY2);

        quilt    = new Bitmap(Math.Abs(pixelX1 - pixelX2), Math.Abs(pixelY1 - pixelY2));
        graphics = Graphics.FromImage(quilt);

        originX = Math.Min(pixelX1, pixelX2);
        originY = Math.Min(pixelY1, pixelY2);

        this.maxLevelOfDetail = maxLevelOfDetail;
    }
        private void projectionWorkerDoWork(object sender, DoWorkEventArgs args)
        {
            var worker = sender as BackgroundWorker;

            for (int i = 21; i > 0; i--)
            {
                if (worker != null && worker.CancellationPending)
                {
                    args.Cancel = true;
                    break;
                }
                int pixelX;
                int pixelY;
                TileSystem.LatLongToPixelXY(Location.Latitude, Location.Longitude, i, out pixelX, out pixelY);
                ProjectedPoints.Add(i, new Point(pixelX, pixelY));
            }

            Initalized = true;
        }
Beispiel #14
0
        /// <summary>
        /// This will list all the affected assets affected by lightning strikes.
        /// </summary>
        /// <returns>A collection of Asset objects.</returns>
        public List <Asset> ListAffectedAssets()
        {
            // This will get the data of lightning strikes.
            var strikes = _dataContext.Strikes
                          .Where(x => x.FlashType == FlashType.CloudToGround);

            // This will initialize the quad key collection.
            var quadKeys = new List <string>();

            // This will iterate through the collection of lightning strikes.
            foreach (var item in strikes)
            {
                // This will initialize the required values
                int pixelX, pixelY, tileX, tileY;

                // This will convert the inital longitude and latitude value to pixel x and y values.
                TileSystem.LatLongToPixelXY(item.Latitude, item.Longitude, 12, out pixelX, out pixelY);

                // This will convert the pixel x and y values to tile x and y values.
                TileSystem.PixelXYToTileXY(pixelX, pixelY, out tileX, out tileY);

                // This will convert the tile x and y values to a usable quad key.
                var quadKey = TileSystem.TileXYToQuadKey(tileX, tileY, 12);

                // This will determine if the resulting quad key already exists in the collection.
                if (!quadKeys.Contains(quadKey))
                {
                    // This will add the quad key to the quad key collection.
                    quadKeys.Add(quadKey);
                }
            }
            // This will extract the affected assets by their quad key.
            var affectedAssets = _dataContext.Assets.Where(x => quadKeys.Contains(x.QuadKey));

            return(affectedAssets.ToList());
        }
        internal static void Process(HexagonDataToMapArgs opts)
        {
            Stopwatch stopwatch = Stopwatch.StartNew();

            HexagonDefinition hexagonDefinition = opts.GetHexagonDefinition();

            //1.1. Get Bounding box coordinates

            BoundingBox bb = null;

            if (!string.IsNullOrEmpty(opts.Tile))
            {
                string[] tileSegments = opts.Tile.Split(":");

                if (tileSegments.Length != 3)
                {
                    throw new NotSupportedException($"A tile format must be supplied as z:x:y");
                }

                int zoom  = int.Parse(tileSegments[0]);
                int tileX = int.Parse(tileSegments[1]);
                int tileY = int.Parse(tileSegments[2]);

                var(pixelXLeft, pixelYTop) = TileSystem.TileXYToPixelXY(tileX, tileY);
                var(n, w) = TileSystem.PixelXYToLatLong(pixelXLeft, pixelYTop, opts.HexagonReferenceZoom);

                var(pixelXRight, pixelYBottom) = TileSystem.TileXYToPixelXY(tileX + 1, tileY + 1);
                var(s, e) = TileSystem.PixelXYToLatLong(pixelXRight, pixelYBottom, opts.HexagonReferenceZoom);

                bb = new BoundingBox
                {
                    East  = e,
                    West  = w,
                    North = n,
                    South = s
                };
            }
            else if (!string.IsNullOrEmpty(opts.East) && !string.IsNullOrEmpty(opts.West) &&
                     !string.IsNullOrEmpty(opts.North) && !string.IsNullOrEmpty(opts.South))
            {
                bb = new BoundingBox
                {
                    East  = double.Parse(opts.East),
                    West  = double.Parse(opts.West),
                    North = double.Parse(opts.North),
                    South = double.Parse(opts.South)
                };
            }

            List <object> hexagons;

            var hexagonDataReader = new SQLiteHexagonReader(opts.Input);

            if (bb == null)
            {
                hexagons = hexagonDataReader.Read(opts.DataProperties.ToArray()).ToList();
            }
            else
            {
                var(left, top)     = TileSystem.LatLongToPixelXY(bb.North, bb.West, opts.HexagonReferenceZoom);
                var(right, bottom) = TileSystem.LatLongToPixelXY(bb.South, bb.East, opts.HexagonReferenceZoom);
                hexagons           = hexagonDataReader.Read(left, right, top, bottom, opts.DataProperties.ToArray()).ToList();
            }

            using (StreamWriter outfile = new StreamWriter(opts.Output))
            {
                GeojsonExporter exporter = new GeojsonExporter();

                var result = exporter.GenerateMap(hexagons, hexagonDefinition, opts.HexagonReferenceZoom);
                outfile.Write(result);
            }


            stopwatch.Stop();

            Console.WriteLine($"Process took {stopwatch.ElapsedMilliseconds} ms");
        }
Beispiel #16
0
 public static Point LatLonToTileCoordinate(double latitude, double longitude, int zoomFactor)
 {
     TileSystem.LatLongToPixelXY(latitude, longitude, zoomFactor, out int pixelX, out int pixelY);
     TileSystem.PixelXYToTileXY(pixelX, pixelY, out int tileX, out int tileY);
     return(new Point(tileX, tileY));
 }