mapbox / vector-tile-cs

Parses vector tiles with C#.
BSD 3-Clause "New" or "Revised" License
57 stars 37 forks source link

vector-tile-cs

C# library for decoding Mapbox Vector Tiles @ v2.x (vector tile specification).

Decoding tiles created according to Mapbox Vector Tile Specification @ v1.x is not supported!

Available as nuget package: nuget.org

Vector tile parsers in other languages:

Build status

master branch:

Depends

Native C# implementation - no dependencies. NUnit and NUnit3TestAdapter for running tests - will get restored automatically when building the solution.

Example

byte[] data = //raw unzipped vectortile
VectorTile vt = new VectorTile(data);
//get available layer names
foreach(var lyrName in vt.LayerNames()) {
    //get layer by name
    VectorTileLayer lyr = vt.GetLayer(lyrName);
    //iterate through all features
    for(int i = 0; i < lyr.FeatureCount(); i++) {
        Debug.WriteLine("{0} lyr:{1} feat:{2}", fileName, lyr.Name, i);
        //get the feature
        VectorTileFeature feat = lyr.GetFeature(i);
        //get feature properties
        var properties = feat.GetProperties();
        foreach(var prop in properties) {
            Debug.WriteLine("key:{0} value:{1}", prop.Key, prop.Value);
        }
        //or get property value if you already know the key
        //object value = feat.GetValue(prop.Key);
        //iterate through all geometry parts
        //requesting coordinates as ints
        foreach(var part in feat.Geometry<int>()) {
            //iterate through coordinates of the part
            foreach(var geom in part) {
                Debug.WriteLine("geom.X:{0} geom.Y:{1}", geom.X, geom.Y);
            }
        }
    }
}