namespace Mapbox.Unity.Map
{
using System;
using System.Collections.Generic;
using Mapbox.Platform.TilesetTileJSON;
using UnityEngine;
[Serializable]
public class TileJsonData
{
public readonly string commonLayersKey = "(layer found in more than one data source)";
public readonly string optionalPropertiesString = "(may not appear across all locations)";
///
/// This boolean is to check if tile JSON data has loaded after the data source has changed
///
public bool tileJSONLoaded = false;
///
/// Layer Display Names seen in the editor
///
public List LayerDisplayNames = new List();
///
/// Property Display Names seen in the editor
///
public Dictionary> PropertyDisplayNames = new Dictionary>();
///
/// The description of the property in a layer
///
public Dictionary> LayerPropertyDescriptionDictionary = new Dictionary>();
///
/// List of data sources (tileset ids) linked to a layer name
///
public Dictionary> LayerSourcesDictionary = new Dictionary>();
///
/// Dictionary containting the list of layers in a source
///
public Dictionary> SourceLayersDictionary = new Dictionary>();
public void ClearData()
{
tileJSONLoaded = false;
LayerPropertyDescriptionDictionary.Clear();
LayerSourcesDictionary.Clear();
SourceLayersDictionary.Clear();
LayerDisplayNames.Clear();
PropertyDisplayNames.Clear();
}
public void ProcessTileJSONData(TileJSONResponse tjr)
{
tileJSONLoaded = true;
List layerPropertiesList = new List();
if (tjr == null || tjr.VectorLayers == null || tjr.VectorLayers.Length == 0)
{
return;
}
ClearData();
var propertyName = "";
var propertyDescription = "";
var layerSource = "";
foreach (var layer in tjr.VectorLayers)
{
//load layer names
var layerName = layer.Id;
layerPropertiesList = new List();
layerSource = layer.Source;
//loading layer sources
if (LayerSourcesDictionary.ContainsKey(layerName))
{
LayerSourcesDictionary[layerName].Add(layerSource);
}
else
{
LayerSourcesDictionary.Add(layerName, new List() { layerSource });
}
//loading layers to a data source
if (SourceLayersDictionary.ContainsKey(layerSource))
{
List sourceList = new List();
LayerSourcesDictionary.TryGetValue(layerName, out sourceList);
if (sourceList.Count > 1 && sourceList.Contains(layerSource)) // the current layerName has more than one source
{
if (SourceLayersDictionary.ContainsKey(commonLayersKey))
{
SourceLayersDictionary[commonLayersKey].Add(layerName);
}
else
{
SourceLayersDictionary.Add(commonLayersKey, new List() { layerName });
}
if (LayerDisplayNames.Contains(layerName))
{
LayerDisplayNames.Remove(layerName);
}
LayerDisplayNames.Add(layerName);
}
else
{
SourceLayersDictionary[layerSource].Add(layerName);
LayerDisplayNames.Add(layerName);
}
}
else
{
SourceLayersDictionary.Add(layerSource, new List() { layerName });
LayerDisplayNames.Add(layerName);
}
//Load properties
foreach (var property in layer.Fields)
{
propertyName = property.Key;
propertyDescription = property.Value;
layerPropertiesList.Add(propertyName);
//adding property descriptions
if (LayerPropertyDescriptionDictionary.ContainsKey(layerName))
{
if (!LayerPropertyDescriptionDictionary[layerName].ContainsKey(propertyName))
{
LayerPropertyDescriptionDictionary[layerName].Add(propertyName, propertyDescription);
}
}
else
{
LayerPropertyDescriptionDictionary.Add(layerName, new Dictionary() { { propertyName, propertyDescription } });
}
//Loading display names for properties
if (PropertyDisplayNames.ContainsKey(layerName))
{
if (!PropertyDisplayNames[layerName].Contains(propertyName))
{
PropertyDisplayNames[layerName].Add(propertyName);
//logic to add the list of masked properties from all sources that are not #1
if (LayerSourcesDictionary[layerName].Count > 1 && !string.IsNullOrEmpty(tjr.Source))
{
var firstSource = tjr.Source.Split(new string[] { "," }, System.StringSplitOptions.None)[0].Trim();
if (layerSource != firstSource)
{
if (PropertyDisplayNames[layerName].Contains(propertyName))
{
PropertyDisplayNames[layerName].Remove(propertyName);
}
PropertyDisplayNames[layerName].Add(propertyName);
}
}
}
}
else
{
PropertyDisplayNames.Add(layerName, new List { propertyName });
}
}
if (PropertyDisplayNames.ContainsKey(layerName) && PropertyDisplayNames[layerName].Count > 1)
{
PropertyDisplayNames[layerName].Sort();
}
}
if (LayerDisplayNames.Count > 1)
{
LayerDisplayNames.Sort();
}
}
}
}