Search code examples
c#jsongeojson

c# to geojson popup items bracketing []


I have a database of GPS coordinates and information about trucks and I’m trying to build a map with that information using GeoJson. I collect the information using MYSQL and I’m creating the GeoJson file in c#. I can create the correct GeoJson file to show the location, heading and a small popup. However; I want to expand that popup. I’ll be using 1 truck in the examples for simplification. I also changed the coordinates for this example.

My C# output;

{
  "type":"FeatureCollection",
  "features":[
    {
      "type":"Feature",
      "properties":{
        "popup":{
          "title":"Truck_001",
          "subtitle":"Laatst geupdate: 23.1500 uur geleden.",
          "imageUrl":"<left blank for stacksocial>",
          "items":{
            "type":"text",
            "label":"text",
            "value":"text items"
          }
        },
        "tooltip":"Laatst geupdate: 23.1500 uur geleden.",
        "marker":{
          "prefix":"fa",
          "icon":"fa-chevron-up",
          "markerColor":"#003da5",
          "iconColor":"white",
          "square":false,
          "circle":false,
          "pureIcon":false,
          "layer":"layer1",
          "size":"medium"
        }
      },
      "geometry":{
        "type":"Point",
        "coordinates":[
          1.981588,
          32.1926765
        ]
      }
    }
  ]
}

The truck will show on the map, but the items will not be shown due to missing the following brackets: ‘[]’

The desired output (see ‘[‘ and ‘]’ around the item):

{
  "type":"FeatureCollection",
  "features":[
    {
      "type":"Feature",
      "properties":{
        "popup":{
          "title":"Truck_001",
          "subtitle":"Laatst geupdate: 23.1500 uur geleden.",
          "imageUrl":"<left blank for stacksocial>",
          "items":[ /*HERE*/
            {
              "type":"text",
              "label":"text",
              "value":"text items"
            }
          ] /* HERE */
        },
        "tooltip":"Laatst geupdate: 23.1500 uur geleden.",
        "marker":{
          "prefix":"fa",
          "icon":"fa-chevron-up",
          "markerColor":"#003da5",
          "iconColor":"white",
          "square":false,
          "circle":false,
          "pureIcon":false,
          "layer":"layer1",
          "size":"medium"
        }
      },
      "geometry":{
        "type":"Point",
        "coordinates":[
          1.981588,
          32.1926765
        ]
      }
    }
  ]
}

My C# Code:

public class Marker
{
    public string prefix;
    public string icon;
    public string markerColor;
    public string iconColor;
    public bool square;
    public bool circle;
    public bool pureIcon;
    public string layer;
    public string size;
}

public class Popup
{
    public string title;
    public string subtitle;
    public string imageUrl;
    public Items items;
}

public class Items
{
    public string type;
    public string label;
    public string value;
}

public class Properties
{
    public Popup popup;
    public string tooltip;
    public Marker marker;
}

public class Geometry
{
    public string type;
    public double[] coordinates;
}

public class Feature
{
    public string type;
    public Properties properties;
    public Geometry geometry;
}

public class GeoJSON
{
    public string type;
    public List<Feature> features;
}

public class SinglePosition
{
    public string headingicon;
    public string ts;
    public string Lastupdate;
    public string ticksts;
    public string ticksnow;
    public double lat;
    public double lng;
    public int heading;
    public string devId;
    public int heading1;
}

public static object[] Evaluate(object[] input)
{
    string dbres = input[0].ToString();

    var allPositions = dbres.FromJSON<SinglePosition[]>();
    if (allPositions == null || allPositions.Length == 0)
        return null;

    var toMap = new GeoJSON() { type = "FeatureCollection", features = new List<Feature>() };

    foreach (var p in allPositions)
    {
        if ((p.heading > 315) || (p.heading <= 45))
        {
            p.headingicon = "fa-chevron-up";
        }
        else if ((p.heading > 45) && (p.heading <= 135))
        {
            p.headingicon = "fa-chevron-right";
        }
        else if ((p.heading > 135) && (p.heading <= 225))
        {
            p.headingicon = "fa-chevron-down";
        }
        else if ((p.heading > 225) && (p.heading <= 315))
        {
            p.headingicon = "fa-chevron-left";
        }
        else
        {
            p.headingicon = "fa-car";
        }
        toMap.features.Add(new Feature()
        {
            type = "Feature",
            properties = new Properties()
            {
                popup = new Popup()
                {
                    title = p.devId,
                    subtitle = "Laatst geupdate: " + p.Lastupdate + " uur geleden.",
                    imageUrl = "<URL>",

                    items = new Items()
                    //[
                    {
                        type = "text",
                        label = "text",
                        value = "text items"
                    }
                    //]
                },
                tooltip = "Laatst geupdate: " + p.Lastupdate + " uur geleden.",
                marker = new Marker()
                {
                    prefix = "fa", // glyphicon or fa
                    icon = p.headingicon,
                    markerColor = "#003da5",
                    iconColor = "white",  // #b60055
                    square = false,
                    circle = false,
                    pureIcon = false,
                    layer = "layer1",
                    size = "medium"
                }
            },
            geometry = new Geometry()
            {
                type = "Point",
                coordinates = new double[] { p.lng, p.lat }
            }
        });
    }

    return new object[] { toMap.ToJSON() };
}

I have read online that those ‘[]’ brackets are attributes, but I have no idea how to use them. How could I use them in my code so that the class items uses them?


Solution

  • These brackets ([]) indicate an array, both in your JSON and in C#. You could just make items of the type Items[], but that would limit you to a fixed size array, so i choose List<Item> for the Task.

    This should work:

    // ...
    
    popup = new Popup
    {
        title = p.devId,
        subtitle = "Laatst geupdate: " + p.Lastupdate + " uur geleden.",
        imageUrl = "<URL>",
    
        items = new List<Item>
        {
            new Item
            {
                type = "text",
                label = "text",
                value = "text items"
            }
        }
    }
    
    // ...
    
    public class Popup
    {
        public string title;
        public string subtitle;
        public string imageUrl;
        public List<Item> items;
    }
    
    public class Item
    {
        public string type;
        public string label;
        public string value;
    }
    
    // ...