-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathGeometryExtensions.cs
162 lines (149 loc) · 5.9 KB
/
GeometryExtensions.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
using Com.Mapbox.Maps;
using GeoJSON.Text.Geometry;
using Point = Microsoft.Maui.Graphics.Point;
namespace MapboxMaui;
public static class GeometryExtensions
{
internal static Com.Mapbox.Geojson.Point ToGeoPoint(this GeoJSON.Text.Geometry.IPosition xvalue)
{
return Com.Mapbox.Geojson.Point.FromLngLat(
xvalue.Longitude,
xvalue.Latitude);
}
internal static MapTappedPosition ToMapTappedPosition(this Com.Mapbox.Geojson.Point point, ScreenCoordinate screenCoordinate)
{
return new MapTappedPosition
{
ScreenPosition = new ScreenPosition(
screenCoordinate.GetX().PixelToPoint(),
screenCoordinate.GetY().PixelToPoint()),
Point = new GeoJSON.Text.Geometry.Point(
new Position(
screenCoordinate.GetX().PixelToPoint(),
screenCoordinate.GetY().PixelToPoint(),
point.HasAltitude ? point.Altitude() : null)
)
};
}
internal static Com.Mapbox.Geojson.IGeometry ToNative(this GeoJSON.Text.Geometry.IGeometryObject xvalue)
{
return xvalue switch
{
GeoJSON.Text.Geometry.Point point => Com.Mapbox.Geojson.Point.FromLngLat(
point.Coordinates.Longitude,
point.Coordinates.Latitude),
LineString line => Com.Mapbox.Geojson.LineString.FromLngLats(
Com.Mapbox.Geojson.MultiPoint.FromLngLats(
line.Coordinates.Select(ToGeoPoint).ToList()
)
),
Polygon polygon => Com.Mapbox.Geojson.Polygon.FromLngLats(
polygon.Coordinates
.Select(
x => x.Coordinates.Select(ToGeoPoint).ToList()
as IList<Com.Mapbox.Geojson.Point>)
.ToList()
),
MultiPoint multiPoint => Com.Mapbox.Geojson.MultiPoint.FromLngLats(
multiPoint.Coordinates
.Select(x => x.Coordinates.ToGeoPoint())
.ToList()
),
MultiLineString multiLineString => Com.Mapbox.Geojson.Polygon.FromLngLats(
multiLineString.Coordinates
.Select(
x => x.Coordinates.Select(ToGeoPoint).ToList()
as IList<Com.Mapbox.Geojson.Point>)
.ToList()
),
MultiPolygon multiPolygon => Com.Mapbox.Geojson.Polygon.FromLngLats(
multiPolygon.Coordinates
.Select(
x => x.Coordinates
.Select(
y => y.Coordinates.Select(ToGeoPoint))
.ToList()
as IList<Com.Mapbox.Geojson.Point>)
.ToList()
),
_ => null,
};
}
internal static Com.Mapbox.Geojson.Point ToNative(this Point xvalue)
{
return Com.Mapbox.Geojson.Point.FromLngLat(xvalue.Y, xvalue.X);
}
internal static GeoJSON.Text.Feature.Feature ToX(this Com.Mapbox.Geojson.Feature src)
=> new GeoJSON.Text.Feature.Feature(
src.Geometry().ToX(),
null, // TODO Convert to C# obj: src.Properties(),
src.Id());
internal static IGeometryObject ToX(this Com.Mapbox.Geojson.IGeometry src)
{
switch(src)
{
case Com.Mapbox.Geojson.Point point:
return new GeoJSON.Text.Geometry.Point(
new Position(
point.Latitude(), point.Longitude(),
point.HasAltitude ? point.Altitude() : null
)
);
case Com.Mapbox.Geojson.LineString lineString:
return new LineString(
lineString
.Coordinates()
.Select(x => new [] { x.Longitude(), x.Latitude() })
.ToList()
);
case Com.Mapbox.Geojson.Polygon polygon:
return new Polygon(
polygon
.Coordinates()
.Select(
y => y.Select(
x => new[] { x.Longitude(), x.Latitude() }
))
.ToList()
);
case Com.Mapbox.Geojson.MultiPoint multiPoint:
return new MultiPoint(
multiPoint
.Coordinates()
.Select(x => new[] { x.Longitude(), x.Latitude() })
.ToList()
);
case Com.Mapbox.Geojson.MultiLineString multiLineString:
return new MultiLineString(
multiLineString
.Coordinates()
.Select(
y => y.Select(
x => new[] { x.Longitude(), x.Latitude() }
))
.ToList()
);
case Com.Mapbox.Geojson.MultiPolygon multiPolygon:
return new MultiPolygon(
multiPolygon
.Coordinates()
.Select(
z => z.Select(
y => y.Select(
x => new[] { x.Longitude(), x.Latitude() }
)
)
)
.ToList()
);
case Com.Mapbox.Geojson.GeometryCollection geometryCollection:
return new GeometryCollection(
geometryCollection
.Geometries()
.Select(x => x.ToX())
.ToList()
);
}
throw new NotSupportedException("Invalid geometry type");
}
}