Convert Single Geometry Type to Another?

Topics: SharpMap v2.0
Jun 25, 2010 at 4:53 PM
Is it possible to convert a geometry type to another? For instance, I'd like to convert a MultiLineString to a Polygon. Is this possible? If so, what's the best approach to doing so? The object I'm trying to convert visualizes as a polygon, but when brought into SharpMap, it's "IsClosed" property is false. Thanks! Jason
Coordinator
Jun 27, 2010 at 9:22 PM

Hello Freedomspeaks,

Sharpmap v2.0 relies on NetTopologySuite v2.11 for creation and validation of Geometries.

If your LineStrings are not closed, you could do the following:

var mls = geom as IMultiLineString;
var gf = geom.Factory;
var linearRings = new List<ILinearRing>;
foreach (var lineString in mls.Components)
{
    var seq = lineString.Coordinates.Clone() as ICoordinateSequnece;
    if (!seq.IsClosed) seq.CloseRing();
    linearRings.Add(gf.CreateLinearRing(seq));
}
return gf.CreatePolygon(linearRings);

This code has not seen a compiler, so there might be some errors, but -I think- it gives the broad picture.

Hth FObermaier

 

 

Jun 28, 2010 at 11:39 PM
Thanks for the reply. What .DLLs do I need to reference in my project to get this to build? I've tried the GeoAPI.dll and the NetTopologySuite.dll, but they don't seem to be working. Thanks again!
Coordinator
Jun 29, 2010 at 7:04 AM

You need to compile the sharpmap v2 solution yourself as there is no released version yet.

You can checkout the code via (tortoise)svn from http://sharpmapv2.googlecode.com/svn/trunk/

Hth FObermaier