Exemple #1
0
        public IActionResult GetMvt(int z, uint x, uint y)
        {
            if (z != 14)
            {
                return(NotFound());
            }

            var tile = TileStatic.ToLocalId(x, y, z);

            try
            {
                var box = TileStatic.Box(z, tile);

                var landusePolygons = LandusePolygons.GetLandusePolygons(box, z, Startup.TileSource.GetTile, t =>
                {
                    if (DefaultMergeFactorCalculator.Landuses.TryCalculateValue(t, out var type))
                    {
                        return(type);
                    }

                    return(null);
                }).Select(p => new Feature(p.polygon, new AttributesTable {
                    { "type", p.landuseType }
                }));

                var layer = new Layer {
                    Name = "landuse"
                };
                foreach (var loc in landusePolygons)
                {
                    layer.Features.Add(loc);
                }

                var vectorTile = new VectorTile
                {
                    TileId = new NetTopologySuite.IO.VectorTiles.Tiles.Tile((int)x, (int)y, z).Id
                };
                vectorTile.Layers.Add(layer);

                var memoryStream = new MemoryStream();
                vectorTile.Write(memoryStream);
                memoryStream.Seek(0, SeekOrigin.Begin);

                return(new FileStreamResult(memoryStream, "application/x-protobuf"));
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }
        public async Task <IActionResult> GetMvt(int z, uint x, uint y)
        {
            if (z < 13)
            {
                return(NotFound());
            }

            try
            {
                var polygons = TiledPolygonGraphBuilder.GetPolygonsForTile((x, y, z), Startup.CachePath, Startup.TileSource.GetTile,
                                                                           IsBarrier);

                var layer = new Layer {
                    Name = "urban-polygons"
                };
                await foreach (var loc in polygons)
                {
                    var max     = double.MinValue;
                    var maxType = string.Empty;

                    var names      = loc.Attributes.GetNames();
                    var values     = loc.Attributes.GetValues();
                    var attributes = new AttributesTable();
                    for (var a = 0; a < names.Length; a++)
                    {
                        attributes.Add(names[a], values[a]);

                        var name = names[a];
                        if (name.StartsWith("face"))
                        {
                            continue;
                        }

                        var o = values[a];
                        var d = o is IConvertible convertible?convertible.ToDouble(null) : 0d;

                        if (d > max)
                        {
                            max     = d;
                            maxType = name;
                        }
                    }

                    if (!string.IsNullOrWhiteSpace(maxType))
                    {
                        attributes.Add("type", maxType);
                    }

                    layer.Features.Add(new Feature(loc.Geometry, attributes));
                }

                var vectorTile = new VectorTile
                {
                    TileId = new NetTopologySuite.IO.VectorTiles.Tiles.Tile((int)x, (int)y, z).Id
                };
                vectorTile.Layers.Add(layer);

                var memoryStream = new MemoryStream();
                vectorTile.Write(memoryStream);
                memoryStream.Seek(0, SeekOrigin.Begin);

                return(new FileStreamResult(memoryStream, "application/x-protobuf"));
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }
Exemple #3
0
        protected void AssertRoundTrip(Geometry inputGeometry, Geometry expectedGeometry,
                                       string?name = null, IAttributesTable?properties = null, uint?id = null,
                                       double expectedNumFeatures = 1, IAttributesTable?expectedProperties = null)
        {
            if (inputGeometry == null)
            {
                inputGeometry = FeatureGeometry;
            }
            if (expectedGeometry == null)
            {
                expectedGeometry = inputGeometry;
            }

            if (string.IsNullOrWhiteSpace(name))
            {
                name = LayerName;
            }
            if (properties == null)
            {
                properties = FeatureProperties;
            }
            if (expectedProperties == null)
            {
                expectedProperties = properties;
            }

            var featureS = new Feature(inputGeometry, properties);

            if (id.HasValue)
            {
                featureS.Attributes[UID] = id;
            }

            var vtS = new VectorTile {
                TileId = 0
            };
            var lyrS = new Layer {
                Name = name
            };

            lyrS.Features.Add(featureS);
            vtS.Layers.Add(lyrS);

            VectorTile?vtD = null;

            using (var ms = new MemoryStream())
            {
                vtS.Write(ms, Extent);
                ms.Position = 0;
                vtD         = new MapboxTileReader(Factory).Read(ms, new VectorTiles.Tiles.Tile(0));
            }

            Assert.NotNull(vtD);
            Assert.False(vtD.IsEmpty);
            Assert.Equal(1, vtD.Layers.Count);
            Assert.Equal(expectedNumFeatures, vtD.Layers[0].Features.Count);
            var featureD = vtD.Layers[0].Features[0];

            // Perform geometry checks
            CheckGeometry(expectedGeometry, featureD.Geometry);

            // Perform attribute checks
            CheckAttributes(expectedProperties, featureD.Attributes);
        }
Exemple #4
0
        private void FeatureIdReadWriteTest(Features.Feature featureS, ulong expectedId, string?idAttributeName = null)
        {
            var vtS = new VectorTile {
                TileId = 0
            };
            var lyrS = new Layer {
                Name = "test"
            };

            lyrS.Features.Add(featureS);
            vtS.Layers.Add(lyrS);

            using (var ms = new MemoryStream())
            {
                if (string.IsNullOrEmpty(idAttributeName))
                {
                    //No ID property specified when writing.
                    vtS.Write(ms);
                }
                else
                {
                    vtS.Write(ms, 4096, idAttributeName);
                }
                ms.Position = 0;

                //Get the raw Mapbox Tile Feature Id value and compare to expected.
                var tile = ProtoBuf.Serializer.Deserialize <NetTopologySuite.IO.VectorTiles.Mapbox.Tile>(ms);
                var id   = tile.Layers[0].Features[0].Id;

                //Verify the id values match.
                Assert.Equal(expectedId, id);

                //Now test the Mapbox reader.

                if (string.IsNullOrEmpty(idAttributeName))
                {
                    idAttributeName = "id";
                }

                //Clear the keys and the feature's tags from the vector tiles. This will remove attribute values from the Mapbox feature.
                //This ensures we are testing the Mapbox Features ID value, and not features attributes.
                tile.Layers[0].Keys.Clear();
                tile.Layers[0].Features[0].Tags.Clear();

                using (var ms2 = new MemoryStream())
                {
                    //Serialize the modified tile.
                    ProtoBuf.Serializer.Serialize <NetTopologySuite.IO.VectorTiles.Mapbox.Tile>(ms2, tile);
                    ms2.Position = 0;

                    //Read the tile. Specify the ID attribute name we want the ID value to be stored.
                    //By default the reader won't capture the ID value unless specified what attribute to store it in.
                    //The reason for this is that all Mapbox features have a default ID of 0, and it may be undesirable to capture this. This also ensures backwords compatibility.
                    var vtD = new MapboxTileReader().Read(ms2, new VectorTiles.Tiles.Tile(0), idAttributeName);

                    //Get the first feature. The ID value should be captured in it's attributes.
                    var f = vtD.Layers[0].Features[0];

                    //Compare with the expected ID.
                    Assert.Equal(expectedId, f.Attributes[idAttributeName]);
                }
            }
        }