ThinkGeo.com    |     Documentation    |     Premium Support

GridFeatureLayer Serializes cell values

Hi,

I loaded a rather large ascii grid to one of my mapping projects. I subsequently noticed that when I serialized the layer that the serialized layer contained the entire string of cell values for the grid in a tag called gridValuesString. This inflated my 1Mb save file to half a Gb…

This seems rather needless as the original file is referenced in the pathFilename and we have to open the featuresource and generate grid matrix anyway if we want to do anything with this info.

Is there any way I can prevent this tag from being written to the serialized output without harming anything?

Thanks,
Damian

Hi Damian,

You are using BinaryFormatter for serialization? Can you use other Serialization in your project, for example NewtonsoftJson? It only serializes public properties, and you can also customize it. Here is a sample you can exclude some properties in the serialization, see if it works for you.

  using Newtonsoft.Json;
  using Newtonsoft.Json.Serialization;

  public class ShouldSerializeContractResolver : DefaultContractResolver
  {
      protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
      {
          JsonProperty property = base.CreateProperty(member, memberSerialization);
  
          if (member.Name == "SomePropertyYouDontWantToSerialize")
          {
              property.ShouldSerialize = instance => false;
          }
          return property;
      }
  }

    var obj = new GridFeatureSource();
    obj.PathFilename = @"D:\Source\thinkgeosdk\ThinkGeo.UI\Mobile\XamarinForms.Debug\Data\GridFile\Mosquitos.grd";
    obj.Open();

    string json = JsonConvert.SerializeObject(obj, new JsonSerializerSettings
    {
        ContractResolver = new ShouldSerializeContractResolver(),
        Formatting = Formatting.Indented
    });

Thanks,
Ben

Hi Ben,

I should have clarified a couple things:

  1. I am on version 10.
  2. I am using GeoSerializer, not BinaryFormatter. BinaryFormatter did not work well with version 10. Several layers wouldn’t deserialize correctly. Is the Json option viable for version 10?

Regards,
Damian

Hi Damian,

If you are using GeoSerializer, here below is an example how you can customize the GeoSerilizer to exclude certain fields.

var obj = new GridFeatureSource();
obj.PathFilename = "D:\Data\Mosquitos.grd";
obj.Open();

GeoSerializer geoSerializer = new GeoSerializer();
geoSerializer.Formatter = new XmlGeoSerializationFormatter();
var a = geoSerializer.Serialize(obj);

internal class XmlGeoSerializationFormatter : GeoSerializationFormatter
{
    /// <summary>
    /// Creates an instance of GeoSerializationFormatter.
    /// </summary>
    public XmlGeoSerializationFormatter()
    { }

    /// <summary>
    /// Format a GeoObjectModel into XML, and save the XML into a stream.
    /// </summary>
    /// <param name="model">The GeoObjectModel to create XML for.</param>
    /// <param name="stream">The Stream to save XML into.</param>
    protected override void SaveCore(GeoObjectModel model, Stream stream)
    {
        if (model.RootNode.Name == "GridFeatureSource")
        {
            for (int i = model.RootNode.Children.Count - 1; i >= 0; i--)
            {
                if (model.RootNode.Children[i] is GeoObjectNode)
                {
                    var objectNode = (GeoObjectNode)model.RootNode.Children[i];
                    if (objectNode.Name == "gridValuesString")
                    {
                        model.RootNode.Children.RemoveAt(i);
                        break;
                    }
                }
            }
        }

        StreamWriter sw = new StreamWriter(stream, Encoding);
        using (XmlWriter xmlWriter = XmlWriter.Create(sw, new XmlWriterSettings { Indent = true }))
        {
            Save(model.RootNode, xmlWriter);
        }
    }
	
	......
}

The complete class XmlGeoSerializationFormatter is attached below.
XmlGeoSerializationFormatter.txt (5.2 KB)

You can also try the Newtonsoft Json one if you want. It only serialize the public properties, it might / might not fit your case better.

Thanks,
Ben