Mapsui
Mapsui copied to clipboard
GeoJSON support
It would be very useful if Mapsui had intrinsic support for the GeoJSON format.
To implement this, one could simply set up a GeoJsonProvider
(implementing the IProvider
) interface, much like the ShapeFile
or WmsProvider
. This would read & parse a GeoJSON file (could be a local file or remote URL) and spit out the corresponding list of features, which could then be rendered via some layer.
Any comments on this idea or how to implement it? I am slightly surprised that no one has started this kind of thing yet :D
I'll note that there seem to be two geojson files in the repository:
- https://github.com/Mapsui/Mapsui/blob/master/Samples/Mapsui.Samples.Common/EmbeddedResources/cities.geojson
- https://github.com/Mapsui/Mapsui/blob/master/Samples/Mapsui.Samples.Common/EmbeddedResources/countries-hires.json
... but apparently they're not being used anywhere.
Fun fact on the side: Opening the first link actually visualizes the city pins on a (mapbox) world map, so apparently GitHub itself has GeoJSON support ;) With the second link this does not work though, probably because of the differing file extension.
Isn't it possible to use the NTS extension NetTopologySuite.IO.GeoJSON to read all the data and then show on a map?
Isn't it possible to use the NTS extension NetTopologySuite.IO.GeoJSON to read all the data and then show on a map?
That looks like a good option.
Isn't it possible to use the NTS extension NetTopologySuite.IO.GeoJSON to read all the data and then show on a map?
Yes, good point. That would already cover a big part of my proposed GeoJsonProvider
. Then one probably only needs a thin wrapper that translates from NetTopologySuite.Features.IFeature
to Mapsui.Nts.GeometryFeature
or so.
ah yes. I have not looked into NetTopologySuite.Features.IFeature at all so far. I assume there are other NTS data sources that use it as well. The namespace NetTopologySuite.Features is not included anywhere in Mapsui. Perhaps it is possible to resolve this with a ToMapsui extension method.
I am curious about supporting locating a geojson file. I have one sitting around and I am currently having to use the geojson.net nuget to read in the FeatureCollection and then convert it to mapsui geometry, but they are basically all the same names/object types, so would be nice to just throw in a json file and then create the layers.
I actually ended up using the NetTopologySuite.IO.GeoJSON
nuget to read a FeatureCollection from the geojson string:
NetTopologySuite.Features.FeatureCollection fc;
var serializer = GeoJsonSerializer.Create();
using (var stringReader = new StringReader(geoJson))
using (var jsonReader = new JsonTextReader(stringReader))
{
fc = serializer.Deserialize<NetTopologySuite.Features.FeatureCollection>(jsonReader);
}
Then I convert this to a list of GeometryFeatures manually (with a custom style) and display those via a MemoryLayer.
@jamesmontemagno, I assume this is basically the same thing that you do. And yes, I agree it would be nice to have some sort of convenience wrapper in Mapsui for this kind of functionality (maybe via a GeoJsonProvider
as propsed above)?
Yup that is what i did basically. I guess that one is built in already as a dependency so maybe i will try that out.
GeoJSON support was added by @inforithmics in Mapsui.Extension. See the online sample by selecting 'GeoJson' under 'Data Formats'.