private static AttributeKeyValue GetAttr(string key, Tile.Value value) { AttributeKeyValue res = null; if (value.HasBoolValue) { res = new AttributeKeyValue(key, value.BoolValue); } else if (value.HasDoubleValue) { res = new AttributeKeyValue(key, value.DoubleValue); } else if (value.HasFloatValue) { res = new AttributeKeyValue(key, value.FloatValue); } else if (value.HasIntValue) { res = new AttributeKeyValue(key, value.IntValue); } else if (value.HasStringValue) { res = new AttributeKeyValue(key, value.StringValue); } else if (value.HasSIntValue) { res = new AttributeKeyValue(key, value.SintValue, AttributeType.SIntValue); } else if (value.HasUIntValue) { res = new AttributeKeyValue(key, value.UintValue); } return(res); }
public static Tile.Value ToTileValue(AttributeKeyValue value) { Tile.Value tileValue = new Tile.Value(); if (value.AttributeType == AttributeType.StringValue) { tileValue.StringValue = value.Value; } else if (value.AttributeType == AttributeType.BoolValue) { tileValue.BoolValue = value.Value; } else if (value.AttributeType == AttributeType.DoubleValue) { tileValue.DoubleValue = value.Value; } else if (value.AttributeType == AttributeType.FloatValue) { tileValue.FloatValue = value.Value; } else if (value.AttributeType == AttributeType.IntValue) { tileValue.IntValue = value.Value; } else if (value.AttributeType == AttributeType.UIntValue) { tileValue.UintValue = value.Value; } else if (value.AttributeType == AttributeType.SIntValue) { tileValue.SintValue = value.Value; } else { throw new System.Exception(string.Format("Could not determine tileValue. valye type is {0}", value.GetType())); } return(tileValue); }
/// <summary> /// Encodes a Mapbox .mvt tile /// </summary> /// <param name="layers">List of VectorTileLayers to encode. A Tile should contain at least one layer</param> /// <param name="stream">output .mvt tile stream</param> public static void Encode(List <VectorTileLayer> layers, Stream stream) { Tile tile = new Tile(); foreach (var vectorTileLayer in layers) { Tile.Layer tileLayer = new Tile.Layer(); tile.Layers.Add(tileLayer); tileLayer.Name = vectorTileLayer.Name; tileLayer.Version = vectorTileLayer.Version; tileLayer.Extent = vectorTileLayer.Extent; //index the key value attributes List <string> keys = new List <string> (); List <AttributeKeyValue> values = new List <AttributeKeyValue>(); Dictionary <string, int> keysIndex = new Dictionary <string, int>(); Dictionary <dynamic, int> valuesIndex = new Dictionary <dynamic, int>(); foreach (var feature in vectorTileLayer.VectorTileFeatures) { foreach (var keyValue in feature.Attributes) { if (!keysIndex.ContainsKey(keyValue.Key)) { keysIndex.Add(keyValue.Key, keys.Count); keys.Add(keyValue.Key); } if (!valuesIndex.ContainsKey(keyValue.Value)) { valuesIndex.Add(keyValue.Value, values.Count); values.Add(keyValue); } } } for (int n = 0; n < vectorTileLayer.VectorTileFeatures.Count; ++n) { var feature = vectorTileLayer.VectorTileFeatures[n]; Tile.Feature tileFeature = new Tile.Feature(); tileLayer.Features.Add(tileFeature); ulong id; if (!ulong.TryParse(feature.Id, System.Globalization.NumberStyles.Integer, System.Globalization.CultureInfo.InvariantCulture, out id)) { id = (ulong)(n + 1); } tileFeature.Id = id; tileFeature.Type = feature.GeometryType; GeometryParser.EncodeGeometry(feature.Geometry, feature.GeometryType, tileFeature.Geometry); foreach (var keyValue in feature.Attributes) { tileFeature.Tags.Add((uint)keysIndex[keyValue.Key]); tileFeature.Tags.Add((uint)valuesIndex[keyValue.Value]); } } tileLayer.Keys.AddRange(keys); foreach (var value in values) { tileLayer.Values.Add(AttributeKeyValue.ToTileValue(value)); } } Serializer.Serialize <Tile>(stream, tile); }