Skip to main content

Reference Library

Go Search
Reference Library
  
Reference Library > Wiki Pages > Polygon Simplification  

Polygon Simplification

Overview
 
It is sometimes useful to simplify polygons, that is, to impose a minimum diistance betwen vertices to reduce the vertex count.  This is useful when using Virtual Earth to display polygons, for example, because VE has a problem rendering very large polygons.
 
Example
 
First determine the distance to require.  The following snippet computes a distance equal to the width of four pixels on the map.
 
Polygon box = MapControlBridge1.MapRequest.RequestEnvelope.ToPolygon();
double minDistance = (box.Bounds.MagX / MapNavigator1.MapImage.Width.Value) * 4;
 
The call to simplify follows.
 
polyG.Simplify( minDistance );
 
The simplify routine removes points from rings and holes.  This can result in a ring or hole with too few vertices, or with a closing point removed.  The following code repairs such damage as far as possible. 
 
// clean up rings or holes invalidated by simplification
for ( int r = polyG.RingCount - 1; r >= 0; r-- )
{
    Ring ring = polyG[ r ];
    for ( int h = ring.Holes.Count - 1; h >= 0; h-- )
    {
        Hole hole = ring.Holes[ h ];
        if ( !hole.IsClosed() ) // returns false if closing point is missing or vertex count < 4
        {
            if ( hole.Count > 3
                || ( hole.Count == 3 // if exactly three, confirm there is not a closing point
                    && ( hole[ 0 ].X != hole[ 2 ].X || hole[ 0 ].Y != hole[ 2 ].Y ) )
                )
            {
                hole.Close();
            }
            else
            {
                ring.Holes.Remove( hole );
            }
        }
    }
    if (!ring.IsClosed())
    {
        if ( ring.Count > 3
                || ( ring.Count == 3
                    && ( ring[ 0 ].X != ring[ 2 ].X || ring[ 0 ].Y != ring[ 2 ].Y ) )
                )
        {
            ring.Close();
        }
        else
        {
            // polygon lacks a method to remove rings... copy valid rings to a new, empty polygon
            Polygon temp = new Polygon();
            for ( int n = 0; n < polyG.RingCount; n++ )
            {
                if ( n != r )
                {
                    temp.Add( polyG[ n ] );
                }
            }
            polyG = temp;
        }
    }
}
 
After this routine, test the validity of the polygon before attempting to use it.
 
if ( polyG.RingCount > 0 )

 

Last modified at 11/11/2008 6:12 PM  by WEB\mark