Carol,
I have created a function to remove the duplicate line segments, and also change a little for David's code to make it easier.
functions:
private void RemoveDuplicateLineSegments(MultilineShape multilineShape)
{
MultilineShape result = new MultilineShape();
for (int i = multilineShape.Lines.Count - 1; i >= 0; i--)
{
LineShape line = multilineShape.Lines[i];
for (int j = multilineShape.Lines.Count - 1; j >= 0; j--)
{
if (j == i) { continue; }
if (line.IsTopologicallyEqual(multilineShape.Lines[j]))
{
multilineShape.Lines.RemoveAt(i);
break;
}
}
}
}
private MultilineShape ConvertPolygonToMultiLineShape(PolygonShape polygon)
{
MultilineShape newMultiLineShape = new MultilineShape();
for (int i = 0; i < polygon.OuterRing.Vertices.Count - 1; i++)
{
Vertex vertex1 = polygon.OuterRing.Vertices[i];
Vertex vertex2 = polygon.OuterRing.Vertices[i + 1];
newMultiLineShape.Lines.Add(new LineShape(new Vertex[] { vertex1, vertex2 }));
}
foreach (RingShape ring in polygon.InnerRings)
{
for (int i = 0; i < ring.Vertices.Count - 1; i++)
{
Vertex vertex1 = ring.Vertices[i];
Vertex vertex2 = ring.Vertices[i + 1];
newMultiLineShape.Lines.Add(new LineShape(new Vertex[] { vertex1, vertex2 }));
}
}
return newMultiLineShape;
}
private MultilineShape ConvertMultiPolygonToMultiLineShape(MultipolygonShape multiPolygon)
{
MultilineShape newMultiLineShape = new MultilineShape();
foreach (PolygonShape polygon in multiPolygon.Polygons)
{
for (int i = 0; i < polygon.OuterRing.Vertices.Count - 1; i++)
{
Vertex vertex1 = polygon.OuterRing.Vertices[i];
Vertex vertex2 = polygon.OuterRing.Vertices[i + 1];
newMultiLineShape.Lines.Add(new LineShape(new Vertex[] { vertex1, vertex2 }));
}
foreach (RingShape ring in polygon.InnerRings)
{
for (int i = 0; i < ring.Vertices.Count - 1; i++)
{
Vertex vertex1 = ring.Vertices[i];
Vertex vertex2 = ring.Vertices[i + 1];
newMultiLineShape.Lines.Add(new LineShape(new Vertex[] { vertex1, vertex2 }));
}
}
}
return newMultiLineShape;
}
sample test code:
// Code for ConvertMultiPolygonToMultiLineShape
PolygonShape polygon1 = new PolygonShape("POLYGON((10 60,40 70,30 85, 10 60))");
PolygonShape polygon2 = new PolygonShape("POLYGON((10 60,40 70,-30 -85, 10 60))");
MultipolygonShape polygons = new MultipolygonShape(new PolygonShape[] { polygon1, polygon2 });
MultilineShape multilineShape = ConvertMultiPolygonToMultiLineShape(polygons);
RemoveDuplicateLineSegments(multilineShape);
// Code for ConvertPolygonToMultiLineShape
PolygonShape polygon1 = new PolygonShape("POLYGON((10 60,40 70,30 85, 10 60))");
PolygonShape polygon2 = new PolygonShape("POLYGON((10 60,40 70,-30 -85, 10 60))");
MultilineShape line1 = ConvertPolygonToMultiLineShape(polygon1);
MultilineShape line2 = ConvertPolygonToMultiLineShape(polygon2);
MultilineShape multilineShape = new MultilineShape();
foreach (LineShape line in line1.Lines)
{
multilineShape.Lines.Add(line);
}
foreach (LineShape line in line2.Lines)
{
multilineShape.Lines.Add(line);
}
RemoveDuplicateLineSegments(multilineShape);
Please let me know if you have questions
Thanks
James