bertt / mapbox-vector-tile-cs

A .NET library for decoding a Mapbox vector tile
MIT License
74 stars 16 forks source link

C#: How to encode vector tiles to be used in a mapbox endpoint #20

Closed Rumpelstinsk closed 4 years ago

Rumpelstinsk commented 4 years ago

I'm using react-map in a project. This map, adds a tile layer:

layers.push({
            'id': "map-layer-2",
            'type': 'line',
            'source': {
                'type': 'vector',
                'tiles': [
                    "http://localhost:52358/api/Planet/Test/7?TileRow={y}&TileCol={x}&Zoom={z}"
                ],
                'minzoom': 12
            },
            'source-layer': 'planet_osm_line',
            'layout': {
                'line-cap': 'round',
                'line-join': 'round'
            },
            'paint': {
                'line-opacity': 0.6,
                'line-color': 'rgb(14,144,236)',
                'line-width': 3
            }
        });

As you can see this layer queries an endpoint which provides a tile-layer. This endpoint is made in C#, and simple returns a Stream which reads a valid pbf file. However I don't want to provide the base pbf, I need to work a little on it first.

So I'm using your library to decode the pbf, and work with it, before returning the result to client. But, i don't know how to encode again the object, there is no to-pbf-byte method.

public async Task<Stream> Data(long tileRow, long tileCol, int zoom, ClientSettings clientSettings){
     //...some extra code here
     Stream data = //don't bother how this gets initialized. It's a correct pbf

      //At this point, if a simply do: "return data" everything works as expected. Mapbox show the plain data

     //Deserialize data to work with it
     var layerInfo = VectorTileParser.Parse(data);  //<---At this point layerInfo has all the info of the pbf. No problem here

     //For test purpose there is no more code.
     //At this point I need to create a valid pbf again
     return layerInfo.???;
}

I tried to serialice vector tile using BinaryFormatter, but it seems VectorTileLayer it's not marked as serializable, because it throws an exception.

using (var stream = new MemoryStream())
        {
            var formatter = new BinaryFormatter();
            formatter.Serialize(stream, layerInfo); //<-- exception here
            stream.Flush();
            return stream;
        }
bertt commented 4 years ago

Hi, interesting scenario you have. What kind of work on the original pbf do you perform? At the moment this library only does the decoding not encoding.

Rumpelstinsk commented 4 years ago

Hi @bertt thanks for the answer.

Acording to the info in pbf, I need to calculate some extra points and lines, acording to some math rules and user settings criteria. But that was out of the question.

If the library cannot encode right now, i will search for a workaround :)