ThinkGeo.com    |     Documentation    |     Premium Support

What format GeoJson does CreateFeatureFromGeoJson expect?

Hello and I hope you can help here.
Using Xamarin Forms and I am new to your products.

I have some data I want to load into Features. My classes have the Geometry type which is a GeoJson format. We are using the NuGet GeoJSON.net (https://github.com/GeoJSON-Net).

I assumed that I can create a feature from that GeoJson string using the Feature.CreateFeatureFromGeoJson. But I get an exception of:
System.InvalidCastException: Specified cast is not valid.
at ThinkGeo.Core.Feature.CreateFeatureFromGeoJson (System.String geoJson) [0x00006] in <88a93a0ef8fd40b6b3125d2895e99794>:0

My assumption is that method would take a GeoJSON geometry and create a feature but it appears it wants the string to be based from one of your Features converted to GeoJson.

My starting JSON is:
"{\"type\":\"Point\",\"coordinates\":[2.0,1.0,3.0]}"

Now I did create a feature based on a point and called its GetGeoJson() and I get back a format of:
"{\"geometry\":{\"coordinates\":[1,2],\"type\":\"Point\"},\"properties\":{},\"bbox\":[0.999995,1.999995,1.000005,2.000005],\"id\":\"b11b5b0f-d8c5-41cb-9bdd-9281486c114a\",\"type\":\"Feature\"}"

Quite a different string to the one I am wanting to use.

My questions:

  1. So it appears that your GeoJson get and create are for a specific format and not the GeoJson RFC standard?

  2. I assume to create features from my data feed I will need to create a Point from my GeoJson and then create a Feature based on that Point?

Or is there some other way to get my data into a set of features?

My data is a class with a Geometry using the NuGet GeoJSON.net.

I hope I have explained myself well enough for you to answer.

Regards
Chris …

Hey @Chris_Marassovich,

We do support the standard GeoJson RFC specification. We support Geometry (Point, Linestring, Polygon, etc.), Feature, and FeatureCollection types from their spec. We also include some extra members when converting Features to GeoJSON.

The GeoJSON that you provided was just a Geometry GeoJSON Object, not a Feature. As such, you need to use the CreateShapeFromGeoJson static method available on any of the BaseShape subclasses:

var pointShape = PointShape.CreateShapeFromGeoJson("{\"type\":\"Point\",\"coordinates\":[2.0,1.0,3.0]}");

pointShape.GetGeoJson();
>>>  "{\"coordinates\":[2,1],\"type\":\"Point\"}"

When you call GetGeoJson() on a Feature, the output will be in the form of a GeoJSON Feature Object that includes the optional “id” and “bbox” members in the RFC standard.

When you create that feature and you only have the Geometry portion of the GeoJSON object, then yes you will need to create the Shape first.

Feature feature = new Feature(PointShape.CreateShapeFromGeoJson("{\"type\":\"Point\",\"coordinates\":[2.0,1.0,3.0]}"), new Dictionary<string, string>() {{"hello", "world"}});

feature.GetGeoJson();
>>> "{\"geometry\":{\"coordinates\":[2,1],\"type\":\"Point\"},\"properties\":{\"hello\":\"world\"},\"bbox\":[1.999995,0.999995,2.000005,1.000005],\"id\":\"a87d1def-6c3c-478e-92b1-a3361b662e41\",\"type\":\"Feature\"}"

If you have access to the whole FeatureCollection Object, you can use the CreateFeatureFromGeoJson or CreateFeaturesFromGeoJson static methods on the Feature class:

// Using the example JSON from the RFC (https://datatracker.ietf.org/doc/html/rfc7946#section-1.5)
Collection<Feature> features = Feature.CreateFeaturesFromGeoJson("{\"type\":\"FeatureCollection\",\"features\":[{\"type\":\"Feature\",\"geometry\":{\"type\":\"Point\",\"coordinates\":[102,0.5]},\"properties\":{\"prop0\":\"value0\"}},{\"type\":\"Feature\",\"geometry\":{\"type\":\"LineString\",\"coordinates\":[[102,0],[103,1],[104,0],[105,1]]},\"properties\":{\"prop0\":\"value0\",\"prop1\":0}},{\"type\":\"Feature\",\"geometry\":{\"type\":\"Polygon\",\"coordinates\":[[[100,0],[101,0],[101,1],[100,1],[100,0]]]},\"properties\":{\"prop0\":\"value0\",\"prop1\":{\"this\":\"that\"}}}]}")
>>> {Count = 3}
    [0]: {POINT(102 0.5)}
    [1]: {LINESTRING(102 0,103 1,104 0,105 1)}
    [2]: {POLYGON((100 0,101 0,101 1,100 1,100 0))}

Thanks,
Kyle

Kyle,

Thanks for that prompt and very complete response.
I feel a little silly not looking further down to the BaseShape for the *FromGeoJson.methods.

Thank you.
Chris …

Hey @Chris_Marassovich,

You’re welcome. Don’t feel silly, everyone overlooks things at times. It’s inherit to our field :slight_smile:

Thanks,
Kyle

Hi, Kyle
Where can I find CreateFeaturesFromGeoJson function in Feature.
I have searched the documentation I did not find it neither.
Best Regards

Hey @NadMan,

CreateFeaturesFromGeoJson is a static method found in the Feature class which returns a collection of features from your JSON data. This is how you would use it:

Collection<Feature> features = Feature.CreateFeaturesFromGeoJson("{\"type\":\"FeatureCollection\",\"features\":[{\"type\":\"Feature\",\"geometry\":{\"type\":\"Point\",\"coordinates\":[102,0.5]},\"properties\":{\"prop0\":\"value0\"}},{\"type\":\"Feature\",\"geometry\":{\"type\":\"LineString\",\"coordinates\":[[102,0],[103,1],[104,0],[105,1]]},\"properties\":{\"prop0\":\"value0\",\"prop1\":0}},{\"type\":\"Feature\",\"geometry\":{\"type\":\"Polygon\",\"coordinates\":[[[100,0],[101,0],[101,1],[100,1],[100,0]]]},\"properties\":{\"prop0\":\"value0\",\"prop1\":{\"this\":\"that\"}}}]}")

>>> {Count = 3}
    [0]: {POINT(102 0.5)}
    [1]: {LINESTRING(102 0,103 1,104 0,105 1)}
    [2]: {POLYGON((100 0,101 0,101 1,100 1,100 0))}

Thanks,
Kyle

Kyle,

I am not finding CreateFeaturesFromGeoJson as a static method on Feature. I only see the method CreateFeatureFromGeoJson(…)

Thanks,
Joe

Hey @Joe_Tinguely,

The CreateFeaturesFromGeoJson is available in ThinkGeo v12 and above. For versions below that, you can still do that by parsing the GeoJSON using a JSON library and then looping over the features array to create the Feature objects.

Thanks,
Kyle