Polygon with duplicated Coordinate

Topics: Algorithms, SharpMap v0.9 / v1.x
Mar 20, 2013 at 1:49 PM
Good morning,
Using nearly the latest trunk, it appears that SharpMap.Forms.MapBox.Tools.DrawPolygon
returns an NTS polygon with the Coordinate.Count – 2 being a duplicate of Coordinate.Count – 3. This appears to be consistent and thusly…Is this expected? If so will this behavior remain so? Again it is your design and I can cope with whatever as long as things remain consistent.

private void mapBox1_GeometryDefined(IGeometry geometry)
{
        if (geometry.IsValid == false)
        {
            AppendMessage("Error: Invalid geometry");
            return;
        }
        if (geometry.OgcGeometryType == OgcGeometryType.Polygon)
        {
            Polygon p = geometry as Polygon;
            GeoPoint[] cos = p.Coordinates;
/*
    [0] {(889164.122290909, 532141.273709091, NaN)} GeoAPI.Geometries.Coordinate
    [1] {(889789.122290909, 533760.591890909, NaN)} GeoAPI.Geometries.Coordinate
    [2] {(889902.758654545, 531601.500981818, NaN)} GeoAPI.Geometries.Coordinate
    [3] {(889902.758654545, 531601.500981818, NaN)} GeoAPI.Geometries.Coordinate
    [4] {(889164.122290909, 532141.273709091, NaN)} GeoAPI.Geometries.Coordinate
*/
        }
}
r,
dennis
Coordinator
Mar 20, 2013 at 2:31 PM
This is not by design. Could you provide a fix
Thanks
Mar 20, 2013 at 4:00 PM
FObermaier,

Here is a short term workaround for user code:
        if (geometry.OgcGeometryType == OgcGeometryType.Polygon)
        {
            Polygon p = geometry as Polygon;
            ILineString il = p.ExteriorRing;
            GeoPoint[] cor = il.Coordinates.Take(il.Coordinates.Count() - 1).ToArray();
            cor[cor.Length - 1] = il.Coordinates.Last();
            geometry = NTSGeometryFactory.CreatePolygon(cor);
        }
        else if (geometry.OgcGeometryType == OgcGeometryType.LineString)
        {
            LineString l = geometry as LineString;
            GeoPoint[] cos = l.Coordinates.Take(l.Coordinates.Count() - 1).ToArray();
            geometry = NTSGeometryFactory.CreateLineString(cos);
        }
I did just briefly take a look at MapBox.cs -> OnMouseDoubleClick(MouseEventArgs e)
1) It should address the evil user that just double-clicks which results in a null _pointArray
2) Polygon requires more than 3 points but in regard _pointArray 4 points.
3) LineString requires 2 or more points but in regard _pointArray 3 points.
The fix after that is just removing the extraneous point at the end of _pointArray.
Yes, I will work it up this evening.
The only question I have is it reasonable for GeometryDefined to return null when the user has behaved irrationally?
r,
dennis
Mar 20, 2013 at 6:17 PM
FObermaier,
Here you have it ... I would not mess around with _pointArray before this. All that seems to work just fine.
r,
dennis
   protected override void OnMouseDoubleClick(MouseEventArgs e)
    {
        base.OnMouseDoubleClick(e);

        if (_activeTool == Tools.DrawPolygon)
        {
            if (GeometryDefined != null)
            {
                if (_pointArray != null && _pointArray.Count >= 4)
                {
                    _pointArray.RemoveAt(_pointArray.Count - 1);
                    var cl = new NetTopologySuite.Geometries.CoordinateList(_pointArray);
                    cl.CloseRing();
                    GeometryDefined(Map.Factory.CreatePolygon(Map.Factory.CreateLinearRing(cl.ToCoordinateArray()), null));
                } else
                    GeometryDefined(null);
            }
            ActiveTool = Tools.None;
        }
        else if (_activeTool == Tools.DrawLine)
        {
            if (GeometryDefined != null)
            {
                if (_pointArray != null && _pointArray.Count >= 3)
                {
                    _pointArray.RemoveAt(_pointArray.Count - 1);
                    GeometryDefined(Map.Factory.CreateLineString(_pointArray.ToArray()));
                }
                else
                {
                    GeometryDefined(null);
                }
            }
            ActiveTool = Tools.None;
        }
    }
Coordinator
Mar 21, 2013 at 9:10 PM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.
Coordinator
Mar 21, 2013 at 9:14 PM
Slighly modified patch applied in trunk as of rev. 102208
Coordinator
Mar 21, 2013 at 9:50 PM
Found a better way to resolve this with reuse of NTS functiosn that's now checked in.