Mapsui icon indicating copy to clipboard operation
Mapsui copied to clipboard

GeoJSON support

Open janusw opened this issue 2 years ago • 5 comments

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

janusw avatar Jun 10 '22 12:06 janusw

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.

janusw avatar Jun 10 '22 16:06 janusw

Isn't it possible to use the NTS extension NetTopologySuite.IO.GeoJSON to read all the data and then show on a map?

charlenni avatar Jun 11 '22 07:06 charlenni

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.

pauldendulk avatar Jun 12 '22 07:06 pauldendulk

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.

janusw avatar Jun 12 '22 10:06 janusw

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.

pauldendulk avatar Jun 20 '22 09:06 pauldendulk

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.

jamesmontemagno avatar Dec 15 '22 04:12 jamesmontemagno

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)?

janusw avatar Dec 15 '22 09:12 janusw

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.

jamesmontemagno avatar Dec 15 '22 16:12 jamesmontemagno

GeoJSON support was added by @inforithmics in Mapsui.Extension. See the online sample by selecting 'GeoJson' under 'Data Formats'.

pauldendulk avatar Jun 10 '23 08:06 pauldendulk