Exemplo n.º 1
0
        public IEnumerable <Hexagon> ProcessPath(GeoData geoData, LayersLoaderTarget[] targets = null)
        {
            foreach (var coordinates in geoData.Points)
            {
                for (var i = 0; i < coordinates.Length - 1; i++)
                {
                    var currentPoint = coordinates[i];
                    var nextPoint    = coordinates[i + 1];

                    //for each segment check intersection with the edges from the hexagons

                    var hexagonLocations = PathHelper.GetHexagonsIntersectedByLine(currentPoint, nextPoint, hexagonDefinition);

                    foreach (var hexagonLocation in hexagonLocations)
                    {
                        var hexagonPoints = HexagonUtils.GetPointsXYOfHexagon(hexagonLocation, hexagonDefinition);

                        for (var j = 0; j < hexagonPoints.Count - 1; j++)
                        {
                            var hexagonCurrentPoint = hexagonPoints[j];
                            var hexagonNextPoint    = hexagonPoints[j + 1];

                            if (PathHelper.GetLineIntersection(currentPoint, nextPoint, hexagonCurrentPoint,
                                                               hexagonNextPoint) != null)
                            {
                                yield return(CreateHexagon(geoData, targets, hexagonLocation, (geo, target) => (int)Math.Pow(2, j)));
                            }
                        }
                    }
                }
            }
        }
Exemplo n.º 2
0
        public static IEnumerable <HexagonLocationUV> GetHexagonsIntersectedByLine(PointXY startPoint, PointXY endPoint, HexagonDefinition hexagonDefinition)
        {
            var startHexagon = HexagonUtils.GetHexagonLocationUVForPointXY(startPoint, hexagonDefinition);
            var endHexagon   = HexagonUtils.GetHexagonLocationUVForPointXY(endPoint, hexagonDefinition);

            var minU = new[] { startHexagon.U, endHexagon.U }.Min() - 1;
            var maxU = new[] { startHexagon.U, endHexagon.U }.Max() + 1;

            var minV = new[] { startHexagon.V, endHexagon.V }.Min() - 1;
            var maxV = new[] { startHexagon.V, endHexagon.V }.Max() + 1;

            var matchingHexagons = new List <HexagonLocationUV>();

            for (var u = minU; u <= maxU; u++)
            {
                for (var v = minV; v <= maxV; v++)
                {
                    var points = HexagonUtils.GetPointsXYOfHexagon(new HexagonLocationUV(u, v), hexagonDefinition);

                    for (var k = 0; k < points.Count() - 1; k++)
                    {
                        var p0 = points[k];
                        var p1 = points[k + 1];

                        if (GetLineIntersection(p0, p1, startPoint, endPoint) != null)
                        {
                            matchingHexagons.Add(new HexagonLocationUV(u, v));
                            break;
                        }
                    }
                }
            }

            return(matchingHexagons);
        }
Exemplo n.º 3
0
        public string GenerateMap(List <object> hexagons, HexagonDefinition hexagonDefinition, int hexagonReferenceZoom)
        {
            GeoJsonWriter writer = new GeoJsonWriter();

            FeatureCollection hexagonCollection = new FeatureCollection();

            foreach (IDictionary <string, object> hexagon in hexagons)
            {
                HexagonLocationUV locationUV = new HexagonLocationUV(Convert.ToInt32(hexagon["U"]), Convert.ToInt32(hexagon["V"]));

                IList <PointXY> points = HexagonUtils.GetPointsXYOfHexagon(locationUV, hexagonDefinition);

                LinearRing ring = new LinearRing(points.Select(pixelCoordinate =>
                {
                    var(latitude, longitude) = TileSystem.PixelXYToLatLong((int)pixelCoordinate.X, (int)pixelCoordinate.Y, hexagonReferenceZoom);

                    return(new Coordinate(longitude, latitude));
                }).ToArray());


                Polygon hexagonPolygon = new Polygon(ring);

                AttributesTable attributes = new AttributesTable(
                    new List <KeyValuePair <string, object> >
                {
                    new KeyValuePair <string, object>("U", locationUV.U),
                    new KeyValuePair <string, object>("V", locationUV.V),
                });


                foreach (var key in hexagon.Keys)
                {
                    switch (key)
                    {
                    case "U":
                    case "V":
                        break;

                    default:
                        object value = hexagon[key];
                        attributes.Add(key, value);
                        break;
                    }
                }

                Feature hexagonFeature = new Feature(hexagonPolygon, attributes);
                hexagonCollection.Add(hexagonFeature);
            }

            //4. Export Geojson just for the hexagons in this particular tile

            string result = writer.Write(hexagonCollection);

            return(result);
        }
Exemplo n.º 4
0
        public void GetPointsXYOfHexagon()
        {
            IList <PointXY> points = HexagonUtils.GetPointsXYOfHexagon(new HexagonLocationUV(0, 0), _hexagonDefinition);

            Assert.Equal(-100, points[0].X);
            Assert.Equal(0, points[0].Y);

            Assert.Equal(-50, points[1].X);
            Assert.Equal(-86.6, Math.Round(points[1].Y, 2));

            Assert.Equal(50, points[2].X);
            Assert.Equal(-86.6, Math.Round(points[2].Y, 2));

            Assert.Equal(100, points[3].X);
            Assert.Equal(0, points[3].Y);

            Assert.Equal(50, points[4].X);
            Assert.Equal(86.6, Math.Round(points[4].Y, 2));

            Assert.Equal(-50, points[5].X);
            Assert.Equal(86.6, Math.Round(points[5].Y, 2));
        }