Re: Converting polygons and multi-polygons to GeoJSON

723 views
Skip to first unread message
Message has been deleted

xivk

unread,
Sep 1, 2016, 9:42:29 AM9/1/16
to NetTopologySuite
I usually use this:

private static string ToJson(FeatureCollection featureCollection)
{
var jsonSerializer = new NetTopologySuite.IO.GeoJsonSerializer();
var jsonStream = new StringWriter();
jsonSerializer.Serialize(jsonStream, featureCollection);
var json = jsonStream.ToInvariantString();
return json;
}

Perhaps you can reuse part of this...

On Thursday, September 1, 2016 at 2:35:21 PM UTC+2, Fenix Fyre wrote:
Can anyone provide me with some assistance I need to convert some polygons and multi-polygons stored in a database to GeoJSON so that I can display them on a Google Map?

I have created the code below which currently returns an empty JSON string:

 public ContentResult Boundaries()
        {
            ViewIt_V2Entities viewItV2 = new ViewIt_V2Entities();
            var geometryQuery = (from shapes in viewItV2.UTLA_Shapes
                select shapes.Geometry).ToList();
            GeometryConverter geometryConverter = new GeometryConverter();
            StringBuilder sb = new StringBuilder();
            StringWriter sw = new StringWriter(sb);
            JsonWriter jsonWriter = new JsonTextWriter(sw);
            JsonSerializer jsonSerializer = new GeoJsonSerializer();
            foreach (var geometry in geometryQuery)
            {
                geometryConverter.WriteJson(jsonWriter, geometry, jsonSerializer);
            }
            return Content(sb.ToString(), "application/json");
        }
How to do I go about converting the polygons and multi-polygons to GeoJSON using the NetTopologySuite?

Thanks,

Simon 

Fenix Fyre

unread,
Sep 1, 2016, 10:19:59 AM9/1/16
to NetTopologySuite
Thanks xivk for the prompt reply was wondering how you go about converting DbGeometry to a FeatureCollection have tried to use a foreach loop to iterate through the geometryQuery List and add each polygon to a Feature but am presented with the error cannot resolve constructor.

Any ideas?

Ben Abelshausen

unread,
Sep 1, 2016, 10:30:51 AM9/1/16
to nettopol...@googlegroups.com
No sorry, I guess creating a new feature collection and adding the geometries as features? Have never worked with DbGeometry or geometryQuery...

Met vriendelijke groeten,
Best regards,

Ben Abelshausen

--
You received this message because you are subscribed to the Google Groups "NetTopologySuite" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nettopologysuite+unsubscribe@googlegroups.com.
To post to this group, send email to nettopologysuite@googlegroups.com.
Visit this group at https://groups.google.com/group/nettopologysuite.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages