Hello,
I am trying to return a list of things as properties of a feature in geoJson…
I am close but not quite there…
I am creating a geoJson string that represents the list and putting it into the ColumnValues.
This string is then quoted in the final output which is not desired in this case. This makes the array get parsed as a single string
How can I attach the list to the feature so that it doesn’t get quoted in the final output?
(or do I need to extend Feature and override GetGeoJson() ?)
//pseudo code
InMemoryFeatureLayer layer = LoadLayerFromSomewhere();<load></load>
foreach(var feat in layer.InternalFeatures)
{
feat.columnValues.Add(“prop1”, “prop1value”); //simple text property
ThingsList things = GetThingsForFeature(feat);<get></get>
feat.columnValues.Add(“things”, things.toGeoJson()); //this isn’t correct
// example things.toGeoJson() result = “[{"thingName":"name","thingType":"type"},{thing2
<thing2>}, {etc}]”</thing2>
}
return “[” + string.Join(",", layer.InternalFeatures.Select(f => f.GetGeoJson())) + “]”;
//end pseudo code
when the final geojson is constructed the properties section of the results:
"properties":{"prop1":"prop1value","things":"[{"thingName":"name","thingType":"type"},
<thing2>{thing2
<thing2>}, {etc}</thing2>
]"}
the correct result would be:
"properties":{"prop1":"prop1value","things":[{"thingName":"name","thingType":"type"},
<thing2>{thing2
<thing2>}, {etc}</thing2>
]}
Thanks a lot in advance.
Andrew</thing2>
</thing2>
Feature ToGeoJson() returning array of data in properties
Hi Andrew,
I am afraid there is no a good way to avoid the quotes when calling GetGeoJson method except inherit the feature and override this method to custom the output. Please let us know if any other questions.
Thanks,
Troy
Andrew,
Maybe defining an extension method for Feature is another option:
public static class FeatureExtension
{
public static string GetFormatedGeoJson(this Feature feature)
{
string tempGeojson = feature.GetGeoJson();
return tempGeojson;
}
}
Hi Troy,
Thanks for the help.
I suspected that something like that would need to be used but wanted to check first…
For the record I used the extension method approach and it works perfectly.
final code:
public
static
string
GetMyGeoJson(
this
Feature feature)
{
var tempGeoJson = feature.GetGeoJson();
var pos = tempGeoJson.IndexOf(
"properties":{"
) + 13;
ThingsList things = GetThingsForFeature(feature);
tempGeoJson = tempGeoJson.Insert(pos,
""things":"
+ things.toGeoJson() +
","
);
return
tempGeoJson;
}
Thanks a lot,
Andrew
Hi Andrew,
I am glad to hear that works for you and thanks for your share.
Regards,
Don