-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathGeometryExtensions.cs
219 lines (201 loc) · 7.39 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
using CoreGraphics;
using CoreLocation;
using Foundation;
using MapboxCommon;
using MapboxMapsObjC;
namespace MapboxMaui;
public static class GeometryExtensions
{
internal static MapTappedPosition ToMapTappedPosition(this CLLocationCoordinate2D coords, CGPoint screenCoordinate)
{
return new MapTappedPosition
{
ScreenPosition = new ScreenPosition(
screenCoordinate.X,
screenCoordinate.Y
),
Point = new GeoJSON.Text.Geometry.Point(
new Position(
coords.Latitude,
coords.Longitude
)
),
};
}
internal static MBXGeometry ToNative(this IGeometryObject xobj)
{
switch (xobj)
{
case GeoJSON.Text.Geometry.Point point:
return GeometryHelper.CreatePoint(
point.Coordinates.ToNSValue());
case LineString line:
return GeometryHelper.CreateLine(
line.Coordinates
.Select(x => x.ToNSValue())
.ToArray());
case Polygon polygon:
return GeometryHelper.CreatePolygon(
NSArray.FromNSObjects(
polygon.Coordinates
.Select(
x => NSArray.FromNSObjects(
x.Coordinates
.Select(
y => y.ToNSValue())
.ToArray()))
.ToArray()));
case MultiPoint multiPoint:
return GeometryHelper.CreateMultiPoint(
multiPoint.Coordinates
.Select(x => x.Coordinates.ToNSValue())
.ToArray());
case MultiLineString multiLineString:
return GeometryHelper.CreateMultiLine(
NSArray.FromNSObjects(
multiLineString.Coordinates
.Select(
x => NSArray.FromNSObjects(
x.Coordinates
.Select(
y => y.ToNSValue())
.ToArray()))
.ToArray()));
case MultiPolygon multiPolygon:
return GeometryHelper.CreateMultiPolygon(
NSArray.FromNSObjects(
multiPolygon.Coordinates
.Select(
x => NSArray.FromNSObjects(
x.Coordinates
.Select(
y => NSArray.FromNSObjects(
y.Coordinates
.Select(
z => z.ToNSValue())
.ToArray()))
.ToArray()))
.ToArray()));
}
return null;
}
internal static CGPoint ToCGPoint(this IPosition xobj)
{
return new CGPoint(
xobj.Latitude,
xobj.Longitude
);
}
internal static NSValue ToNSValue(this IPosition xobj)
{
return NSValue.FromCGPoint(
xobj.ToCGPoint()
);
}
internal static IPosition ToPosition(this NSValue src)
=> new Position(
src.CoordinateValue.Latitude,
src.CoordinateValue.Longitude
);
internal static IGeometryObject ToX(this MBXGeometry src)
{
switch (src.GeometryType)
{
case MBXGeometryType.Point:
var pointPosition = src
.ExtractLocations()
.ToPosition();
return new GeoJSON.Text.Geometry.Point(
pointPosition
);
case MBXGeometryType.Line:
var linePoints = src
.ExtractLocationsArray()
.Select(y => y.ToPosition());
return new LineString(
linePoints
);
case MBXGeometryType.Polygon:
var polygonPoints = src
.ExtractLocations2DArray()
.Cast<NSArray>()
.Select(
z => z
.Cast<NSValue>()
.Select(
y => new[] {
y.CoordinateValue.Longitude,
y.CoordinateValue.Latitude
})
);
return new Polygon(
polygonPoints
);
case MBXGeometryType.MultiPoint:
var multiPoints = src
.ExtractLocationsArray()
.Select(
y => new[] {
y.CoordinateValue.Longitude,
y.CoordinateValue.Latitude
});
return new MultiPoint(
multiPoints
);
case MBXGeometryType.MultiLine:
var multiLinePoints = src
.ExtractLocations2DArray()
.Cast<NSArray>()
.Select(
z => z
.Cast<NSValue>()
.Select(
y => new[] {
y.CoordinateValue.Longitude,
y.CoordinateValue.Latitude
})
);
return new MultiLineString(
multiLinePoints
);
case MBXGeometryType.MultiPolygon:
var multiPolygonPoints = src
.ExtractLocations3DArray()
.Cast<NSArray>()
.Select(
x => x
.Cast<NSArray>()
.Select(
z => z
.Cast<NSValue>()
.Select(
y => new[] {
y.CoordinateValue.Longitude,
y.CoordinateValue.Latitude
})
)
);
return new MultiPolygon(
multiPolygonPoints
);
case MBXGeometryType.GeometryCollection:
var geometries = src
.ExtractGeometriesArray()
.Select(x => x.ToX());
return new GeometryCollection(geometries);
}
throw new NotSupportedException("Invalid geometry type");
}
internal static Feature ToX(this MBXFeature src)
{
var geometry = src.Geometry.ToX();
var kvPairs = src.Properties
.Keys
.Select(x => new KeyValuePair<string, object>(
x.ToString(),
src.Properties[x])
);
var properties = new Dictionary<string, object>(kvPairs);
return new Feature(geometry, properties, src.Identifier?.ToString());
}
}