Save graphics overlays?

1123
4
06-28-2019 01:11 PM
JonShuler
New Contributor

We would like to save the graphics overlays on a map and allow a user to add them back to a map at a later time.  I did not see any functionality in the SDK for persisting the overlays.  Without writing a lot of custom code, is it possible to save the overlays or even the complete map with the overlays?

0 Kudos
4 Replies
PreetiMaske
Esri Contributor

Graphics overlays allow graphics with multiple geometries and have no schema and hence can't be stored/persisted. 

I am not completely sure of your workflow but seems like what you are trying to achieve might be possible by FeatureCollection.

JonShuler
New Contributor

We are using the API to sketch shapes (polygon, point, etc) on the map.  I found that the the Graphics object has a ToJson() for the geometry and symbol data so looking into using that.  I will also look into FeatureCollection.

0 Kudos
JoeHershman
MVP Regular Contributor

I think it would be pretty straight forward to just build out your own json array for each element using the .ToJson methods.  Something like...

// Save to file
JArray array = new JArray();
foreach (var graphic in overlay.Graphics)
{
	JProperty geometry = new JProperty("geometry", graphic.Geometry.ToJson());
	JProperty symbol = new JProperty("symbol", graphic.Symbol.ToJson());
	JObject jObject = new JObject(geometry, symbol);

	array.Add(jObject);
}

StreamWriter sw = new StreamWriter("D:\\outfile.json");
JsonWriter writer = new JsonTextWriter(sw);
array.WriteTo(writer);
writer.Close();


//read back in
StreamReader reader = new StreamReader("D:\\outfile.json");
string json = reader.ReadToEnd();
JArray array = JArray.Parse(json);


foreach (var d in array.OfType<JObject>())
{
	string geo = d["geometry"].ToString();
	string sym = d["symbol"].ToString();

	var graph = new Graphic(Geometry.FromJson(geo), Symbol.FromJson(sym));

	overlay.Graphics.Add(graph);
}
Thanks,
-Joe
0 Kudos
JoeHershman
MVP Regular Contributor

You could make it really clean and roll your own ToJson

public static class OverlayExtension
{
	public static string ToJson(this GraphicsOverlay overlay)
	{
		JArray array = new JArray();
		foreach (var graphic in overlay.Graphics)
		{
			JProperty geometry = new JProperty("geometry", graphic.Geometry.ToJson());
			JProperty symbol = new JProperty("symbol", graphic.Symbol.ToJson());
			JObject jObject = new JObject(geometry, symbol);

			array.Add(jObject);
		}

		return array.ToString();
	}

	public static void FromJson(this GraphicsOverlay overlay, string json)
	{
		JArray array = JArray.Parse(json);
		foreach (var jObject in array.OfType<JObject>())
		{
			string geometry = jObject["geometry"].ToString();
			string symbol = jObject["symbol"].ToString();

			var graphic = new Graphic(Geometry.FromJson(geometry), Symbol.FromJson(symbol));

			overlay.Graphics.Add(graphic);
		}
	}
}‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Then in code

string json = EditGraphicsOverlay.ToJson();
//Write to file

//read from file
EditGraphicsOverlay.FromJson(json);‍‍‍‍‍
Thanks,
-Joe
0 Kudos