Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NettopologySuite and Dotspatial WKTReader

So I have a bunch of WKT representing a multi-polygon. I exclusively use DotSpatial to manipulate geometries. Specifically I'm trying to get a DotSpatial.Topology.IGeometry out of my WKT.

Unfortunately the DotSpatial guys recently moved a lot of stuff into NetTopologySuit and that is where their WKTreader lives.

But the reader passes back a GeoAPI.Geometries.IGeometry, which you can't just cast to a DotSpatial.Topology.IGeometry because they're not the same class. I'm actually gettting a little aggravated having to transform what is essentially the same class across three different API (DotSpatial, GeoAPI and NetTopologySuite) that all have similarly named similar classes.

All I want to do is pull ina DotSpatial.Topology.IGeometry from well known text. Specifically it's a MultiPolygon that I'm working with but whatever.

I really don't want to have some great bit conditional or select statement to figure out what to do with the GeoAPI IGeometry class so i can use it. I already have a big converter class for convering various geometry classes to dotSpatial and I'd rather get rid of it entirely. Less code is better code.

Can anyone assist with transforming a GeoAPI.Geometries.IGeometry into a DotSpatial.Geometries.IGeometry? I can't just cast it as I get the following error: Unable to cast object of type 'NetTopologySuite.Geometries.MultiPolygon' to type 'DotSpatial.Topology.IGeometry'.

Any ideas?

Thanks in advance, E

like image 801
Ed Kramer Avatar asked Jun 08 '18 20:06

Ed Kramer


1 Answers

Sorry for the confusion. There was a transition away from keeping the IGeometry interface in DotSpatial and pushing it down to GeoAPI. This allows a common interface to be used to communicate class information between the two libraries without requiring a direct dependency, but lets NetTopologySuite focus on keeping in synch with the latest updates coming from Java Topology Suite and other topology improvements without the DotSpatial team having to replicate the source all the time in their own project. The trick is to dimension your variables as a GeoAPI IGeometry so that both libraries can use the object. DotSpatial doesn't reference NetTopologySuite directly I don't think. Also, this post may have been from a time when the transition was still in development, so hopefully this is straight forward now as long as you have the latest version. (I used commit cfb523 on May 22, 2018 available here: https://github.com/DotSpatial/DotSpatial

enter image description here

       using GeoAPI.Geometries;
       using NetTopologySuite.IO;
       using DotSpatial.Data;


       // From any WKT string
        String WKT = "MULTIPOLYGON (((40 40, 20 45, 45 30, 40 40)),((20 35, 10 30, 10 10, 30 5, 45 20, 20 35),(30 20, 20 15, 20 25, 30 20)))";
        // Create a Well Known Text Reader from NetTopologySuite
        WKTReader reader = new WKTReader();
        // NetTopologySuite passes back a GeoApi IGeometry.  This is a shared interface that can be used by both libraries.
        IGeometry geom = reader.Read(WKT);
        // Create a Feature (a DotSpatial object) using the GeoApi IGeometry from NetTopologySuite.
        Feature f = new Feature(geom);
like image 106
Ted Avatar answered Sep 18 '22 11:09

Ted