-
Notifications
You must be signed in to change notification settings - Fork 310
/
LineStringExtracter.cs
73 lines (67 loc) · 2.71 KB
/
LineStringExtracter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
using System.Collections.Generic;
using System.Collections.ObjectModel;
namespace NetTopologySuite.Geometries.Utilities
{
/// <summary>
/// Extracts all the <see cref="LineString"/> elements from a <see cref="Geometry"/>.
/// </summary>
/// <see cref="GeometryExtracter"/>
public class LineStringExtracter : IGeometryFilter
{
/// <summary>
/// Extracts the <see cref="LineString"/> elements from a single <see cref="Geometry"/>
/// and adds them to the<see cref="ICollection{LineString}"/>.
/// </summary>
/// <param name="geom">The geometry from which to extract</param>
/// <param name="lines">The list to add the extracted elements to</param>
/// <returns>The <paramref name="lines"/> list argument</returns>
public static TCollection GetLines<TCollection>(Geometry geom, TCollection lines)
where TCollection : ICollection<Geometry>
{
if (geom is LineString)
{
lines.Add(geom);
}
else if (geom is GeometryCollection)
{
geom.Apply(new LineStringExtracter(lines));
}
// skip non-LineString elemental geometries
return lines;
}
/// <summary>
/// Extracts the <see cref="LineString"/> elements from a single <see cref="Geometry"/>
/// and returns them in a <see cref="List{LineString}"/>.
/// </summary>
/// <param name="geom">The geometry from which to extract</param>
/// <returns>A list containing the linear elements</returns>
public static ReadOnlyCollection<Geometry> GetLines(Geometry geom)
{
var lines = GetLines(geom, new List<Geometry>());
return lines.AsReadOnly();
}
/// <summary>
/// Extracts the <see cref="LineString"/> elements from a single <see cref="Geometry"/>
/// and returns them as either a <see cref="LineString"/> or <see cref="MultiLineString"/>.
/// </summary>
/// <param name="geom">The geometry from which to extract</param>
/// <returns>A linear geometry</returns>
public static Geometry GetGeometry(Geometry geom)
{
var list = GetLines(geom);
return geom.Factory.BuildGeometry(list);
}
private readonly ICollection<Geometry> _comps;
/// <summary>
/// Constructs a filter with a list in which to store the elements found.
/// </summary>
public LineStringExtracter(ICollection<Geometry> comps)
{
_comps = comps;
}
public void Filter(Geometry geom)
{
if (geom is LineString) _comps.Add(geom);
}
}
}