forked from dronemindeu/poly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoly.dart
596 lines (552 loc) · 20.4 KB
/
poly.dart
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
/// Library for checking if given point is present in Polygon or not
library poly;
export 'dart:math' show Point;
import 'dart:math' show Point, asin, sqrt, pow, sin, cos, pi;
import 'package:csv/csv.dart';
import 'dart:convert' show utf8;
import 'package:collection/collection.dart' as check_list
show DeepCollectionEquality;
// Appends 2 or 3 Strings - DO not use - problem
// TO DO - open issue on dart-lang : casting doesn't work - Check with dartpad & for List<num>
// Done
// for `List<List<num>> c3 = listAppend(c2, [[7,8]].cast() );`
// Getting following exception
// Unhandled exception:
//type 'List<dynamic>' is not a subtype of type 'List<List<num>>'
//#0 main (file:///E:/dart/poly/example/conversion.dart:14:19)
//#1 _startIsolate.<anonymous closure> (dart:isolate/runtime/libisolate_patch.dart:300:19)
//#2 _RawReceivePortImpl._handleMessage (dart:isolate/runtime/libisolate_patch.dart:171:12)
List listAppend(List one, List two) {
return [...one, ...two];
}
//List<Point<num>> new_w = append(l,[Point(74,99)].cast<Point<num>>());
//print("\n\n ${new_w}");
//List<Point<num>> notInsidePoints = append(l,([Point(75,90)] as List<Point<num>>));
// notInsidePoints = lAppend(l, [Point(75, 99)].cast<Point<num>>(),[Point(45,90)].cast<Point<num>>());
//List lAppend(List one, List two, [List three]) {
// List _appendedList = (three?.isEmpty ?? false)
// ? [one, two].expand((x) => x).toList()
// : [one, two, three].expand((x) => x).toList();
// return _appendedList;
//}
//TO DO - Add YX to XY - done in list
// ignore: todo
//TODO - kml support
///* `NeedsAtLeastThreePoints` is thrown if `Polygon.points` contains less than 3 points
class NeedsAtLeastThreePoints implements Exception {
final int _number_of_point;
NeedsAtLeastThreePoints(this._number_of_point);
@override
String toString() =>
'Please provide three or more points. Current number of Points: $_number_of_point';
int currentPointsInPolygon() => _number_of_point;
}
//TO DO - Check length of dart identifier - Not found
// Is WrongSizeForPoint good enough or should it be length instead of size
// NotJustTwoElementsInList_WrongSizeForPoint
// NotJustTwoElementsForPoint
///* `WrongSizeForPoint` is thrown if `to_Point()` has more or less than 2 element. Point has only x and y.
class WrongSizeForPoint implements Exception {
final int _length_of_list;
WrongSizeForPoint(this._length_of_list);
@override
String toString() =>
'Wrong size, List must have 2 element. Current Size: $_length_of_list';
int inputListLength() => _length_of_list;
}
//TO DO - Casting ??
// can we check if List<dynamic> is List<num> ?? Replace List<num> with List
///* returns `true` if `String` contains Space
bool containsSpace(String inputString) {
return inputString.contains(' ');
}
/// `List<num>` to `Point`
Point toPoint(List<num> list_of_xy) {
var _length_of_list_xy = list_of_xy.length;
if (_length_of_list_xy == 2) {
return Point(list_of_xy[0], list_of_xy[1]);
} else {
throw WrongSizeForPoint(_length_of_list_xy);
}
// return Point(x,y);
}
/// `Point` to `List<num>`
List<num> pointToList(Point inPoint) {
var output = <num>[inPoint.x, inPoint.y];
return output;
}
// ignore: todo
//TODO - Add a check to see if it's List<List<num>> or just List if it's casted ?
/// `List<List (x,y)>` to `List<Point>`
List<Point<num>> toListOfPoint(List<List<num>> list_of_list) {
var _out_list_of_point = <Point<num>>[];
list_of_list.forEach((_element_in_list_of_list) {
_out_list_of_point.add(toPoint(_element_in_list_of_list));
});
return _out_list_of_point;
}
/// `List<Point>` to `List<List (x,y)>`
List<List<num>> pointsToList(List<Point<num>> inputListOfPoint) {
var _out = <List<num>>[];
for (var _i = 0; _i < inputListOfPoint.length; _i++) {
_out.add(pointToList(inputListOfPoint[_i]));
}
return _out;
}
/// `List<List (x,y)>` to `Polygon`
Polygon toPolyFromListOfList(List<List<num>> list_of_list) {
var _length_of_poly = list_of_list.length;
if (_length_of_poly < 3) {
throw NeedsAtLeastThreePoints(_length_of_poly);
}
var _list_of_point = <Point<num>>[];
list_of_list.forEach((_element_in_list_of_list) {
_list_of_point.add(toPoint(_element_in_list_of_list));
});
// debug-print
// print(_list_of_point);
return Polygon(_list_of_point);
}
/// Returns `List<num>` from a `List<dynamic>`
/// * Can be used with [toPoly] as it accepts `List<num>`
/// * Optional Parameters -
/// * `sizeTwo`
/// - Default value `true`
/// - When set `false`, `Output List` can have more than 2 elements
/// * `replaceWithZero`
/// - Default value `false`
/// - When set `true`, elements with type `String` or `bool` will be replaced with 0, rather than being removed
/// * `reverseIt`
/// - Default value `false`
/// - When set `true`, `List` will be reversed
List<num> toListNum(List _inputList,
{bool reverseIt = false,
bool replaceWithZero = false,
bool sizeTwo = true}) {
var _list = [..._inputList];
var lengthOfPoints = _list.length;
if (replaceWithZero) {
//print("${lengthOfPoints}");
if (sizeTwo) {
if (lengthOfPoints < 2) {
_list = [0, 0];
}
var x = _list[0];
var y = _list[1];
//print(x.runtimeType);
if (((x.runtimeType == String) || (x.runtimeType == bool)) &&
((y.runtimeType == String) || (y.runtimeType == bool))) {
_list = [0, 0];
} else if ((x.runtimeType == String) || (x.runtimeType == bool)) {
_list = [0, y];
} else if ((y.runtimeType == String) || (y.runtimeType == bool)) {
_list = [x, 0];
}
} else {
for (var _i = 0; _i < lengthOfPoints; _i++) {
var element = _list[_i];
if ((element.runtimeType == String) || (element.runtimeType == bool)) {
_list[_i] = 0;
}
}
}
} else {
_list.removeWhere(
(_i) => (((_i.runtimeType == String) || (_i.runtimeType == bool))));
}
lengthOfPoints = sizeTwo ? 2 : _list.length;
//print("${lengthOfPoints}");
var numList = <num>[];
if (reverseIt) {
for (var _i = lengthOfPoints - 1; _i >= 0; _i--) {
var x = _list[_i];
//print(x.runtimeType);
numList.add(x);
}
} else {
for (var _i = 0; _i < lengthOfPoints; _i++) {
var x = _list[_i];
//print(x.runtimeType);
numList.add(x);
}
}
return numList;
}
/// Returns `List<List<num>>` from a `List<List<dynamic>>`
/// * Can be used with functions like [areAllPointsInsidePolygon_List] , [getList_IsListOfListInside] , [toPolyFromListOfList] , [toListOfPoint] which accepts `List<List<num>>`
/// * Optional Parameters -
/// * `replaceWithZero`
/// - Default value `false`
/// - When set `true`, elements with type `String` or `bool` will be replaced with 0, rather than being removed
/// * `swapXAndY`
/// - Default value `false`
/// - When set `true`, `xi` will be swapped with `yi`
/// - i.e. `[ [x1,y1], [x2,y2], ...]` -> `[ [y1,x1], [y2,x2], ...]`
List<List<num?>> toListListNum(List _inputListOfList,
{bool swapXAndY = false, bool replaceWithZero = false}) {
var _listOfList = [..._inputListOfList];
if (_listOfList[0][0].runtimeType == String) {
swapXAndY = swapXAndY
? swapXAndY
: ((_listOfList[0][0] == 'longitude' || _listOfList[0][0] == 'y')
? true
: false);
//_listOfList.removeAt(0);
}
var lengthOfPoints = _listOfList.length;
//print("Before: ${lengthOfPoints}");
if (replaceWithZero) {
//print("${lengthOfPoints}");
for (var _i = 0; _i < lengthOfPoints; _i++) {
var x = _listOfList[_i][0];
var y = _listOfList[_i][1];
//print(x.runtimeType);
if (((x.runtimeType == String) || (x.runtimeType == bool)) &&
((y.runtimeType == String) || (y.runtimeType == bool))) {
_listOfList[_i] = [0, 0];
} else if ((x.runtimeType == String) || (x.runtimeType == bool)) {
_listOfList[_i] = [0, y];
} else if ((y.runtimeType == String) || (y.runtimeType == bool)) {
_listOfList[_i] = [x, 0];
}
// print("${lengthOfPoints}");
}
} else {
_listOfList.removeWhere((_i) =>
(((_i[0].runtimeType == String) || (_i[0].runtimeType == bool)) ||
((_i[1].runtimeType == String) || (_i[1].runtimeType == bool))));
}
lengthOfPoints = _listOfList.length;
var numList = <List<num?>>[];
if (swapXAndY) {
for (var _i = 0; _i < lengthOfPoints; _i++) {
var x = _listOfList[_i][0];
var y = _listOfList[_i][1];
numList.add([y, x]);
//print(x.runtimeType);
// if ((x.runtimeType == String) || (y.runtimeType == String) ) {
// _listOfList.removeAt(_i);
// --lengthOfPoints;
// }
// else{
// numList.add([y, x]);
// }
}
} else {
//print("lengthOfPoints:${lengthOfPoints}");
for (var _i = 0; _i < lengthOfPoints; _i++) {
// print("_i:${_i}");
var y = _listOfList[_i][1];
var x = _listOfList[_i][0];
numList.add([x, y]);
//print(x.runtimeType);
// if ((x.runtimeType == String) || (y.runtimeType == String) ) {
// _listOfList.removeAt(_i);
// --lengthOfPoints;
// }
// else{
// numList.add([x, y]);
// }
}
}
return numList;
}
//import 'package:json_serializable/json_serializable.dart';
// ignore: todo
// TODO: Add json support ??
// ignore: todo
// TODO: Polygon Name ??
/// A class for representing two-dimensional Polygon defined with `List<Point<num>> points`.
class Polygon {
final List<Point<num>> points;
String? name;
// double version;
///Create a `Polygon` with vertices at `points`.
/// Pass a `List<Point<num>>`
Polygon(List<Point<num>> points) : points = points.toList(growable: false) {
var _number_of_point = this.points.length;
if (_number_of_point < 3) {
throw NeedsAtLeastThreePoints(_number_of_point);
// throw new ArgumentError("Please provide three or more points.");
}
// name = tname;
}
/// returns `List<List<num>>`
List<List<num>> listOfList() {
return pointsToList(points);
}
/// returns `true` if `(x,y)` is present inside `Polygon`
bool contains(num px, num py) {
num ax = 0;
num ay = 0;
var bx = points[points.length - 1].x - px;
var by = points[points.length - 1].y - py;
var depth = 0;
for (var i = 0; i < points.length; i++) {
ax = bx;
ay = by;
bx = points[i].x - px;
by = points[i].y - py;
if (ay < 0 && by < 0) continue; // both "up" or both "down"
if (ay > 0 && by > 0) continue; // both "up" or both "down"
if (ax < 0 && bx < 0) continue; // both points on left
num lx = ax - ay * (bx - ax) / (by - ay);
if (lx == 0) return true; // point on edge
if (lx > 0) depth++;
}
return (depth & 1) == 1;
}
/// Calculates distance between two points {lat1,lon1} and {lat2,lon2} in meter
/// source - https://edwilliams.org/avform.htm#Dist
// ignore: todo
/// TODO add examples & tests
double distanceInMeter(double lat1, double lon1, double lat2, double lon2) {
double d;
// d in rad
d = 2 *
asin(sqrt((pow((sin((lat1 - lat2) / 2)), 2)) +
cos(lat1) * cos(lat2) * (pow((sin((lon1 - lon2) / 2)), 2))));
// d in nm
d = d * 180 * 60 / pi;
// d in m
d = d * 1852;
return d;
}
/// Checks if `Point i` is inside Polygon with tolerance of T meter
// ignore: todo
/// TODO add examples & tests
// The following function is not completely optimized yet
bool isPointInsideT(Point i, double T) {
if (isPointInside(i)) {
return true;
} else {
var dis = 0.0;
var dd = 0.0;
dis = distanceInMeter(i.x as double, i.y as double, points[0].x as double,
points[0].y as double);
for (var j = 1; j < points.length; j++) {
dd = distanceInMeter(i.x as double, i.y as double,
points[j].x as double, points[j].y as double);
dis = dd < dis ? dd : dis;
}
return dis > T ? false : true;
}
}
/// Checks if 2 `Polygon` have same vertices i.e. `points`
bool? hasSamePoints(Polygon anotherPolygon) {
var same = (points.length == anotherPolygon.points.length);
if (!same) {
return false;
} else {
// int _length = points.length;
Function deepEq =
const check_list.DeepCollectionEquality.unordered().equals;
return deepEq(points, anotherPolygon.points);
}
}
/// returns `true` if `Point` is present inside `Polygon`
bool isPointInside(Point i) {
return contains(i.x, i.y);
}
/// returns `List<bool>` from `List<Point<num>>` based on if `Point<num>` is present or not
List<bool> getList_IsListOfPointInside(List<Point<num>> inputListOfPoint) {
var _out = <bool>[];
for (var _currentPoint in inputListOfPoint) {
_out.add(isPointInside(_currentPoint));
}
return _out;
}
/// returns `List<bool>` from `List<Point<num>>` based on if `Point<num>` is present or not
List<bool> getList_IsListOfListInside(List<List<num>> inputListOfList) {
var _out = <bool>[];
var _inputListOfPoint = toListOfPoint(inputListOfList);
_inputListOfPoint.forEach((_currentPoint) {
_out.add(isPointInside(_currentPoint));
});
return _out;
}
/// returns `true` if all `Point<num>` are inside `Polygon`
bool areAllPointsInsidePolygon_ListPoint(List<Point<num>> inputListOfPoint) {
return getList_IsListOfPointInside(inputListOfPoint).contains(false)
? false
: true;
}
/// returns `true` if all List<num> are inside `Polygon`
bool areAllPointsInsidePolygon_List(List<List<num>> inputListOfList) {
var inputListOfPoint = toListOfPoint(inputListOfList);
return getList_IsListOfPointInside(inputListOfPoint).contains(false)
? false
: true;
}
/// returns `List indexes` from `List<Point<num>>` based on which `Point<num>` are present inside
List<int> listIndexOfInsidePoint(List<Point<num>> inputListOfPoint) {
var _out = <int>[];
Point<num> _pointInList;
for (var index = 0; index < inputListOfPoint.length; index++) {
_pointInList = inputListOfPoint[index];
if (isPointInside(_pointInList)) {
_out.add(index);
}
}
return _out;
}
/// * Returns result of `ArePointsInside` as CSV String which can be later saved or displayed
/// * Output CSV String will by default contain a header row - `latitude,longitude,isInside`
/// * Optional Named parameter: `bool useXY`
/// * By passing, optional parameter: `useXY` as true, header will be `x,y` instead of `latitude,longitude`
/// * Default value of `useXY` is `false`
/// * Optional Named parameter: `String includeHeader`
/// * if optional parameter - `includeHeader` is passed as `false`, returning String will not contain header row
/// * Optional Named parameter: `String diffNameThanIsInside`
/// * Different name than Default name(`isInside`) will be used by passing optional parameter: `diffNameThanIsInside`
String IsInsideResultWithXY_ToCSVString(List<Point<num>> inputListOfPoint,
{bool includeHeader = true,
String? diffNameThanIsInside,
bool useXY = false}) {
var xYOut = <List>[];
if (includeHeader) {
var headerX = useXY ? 'x' : 'latitude';
var headerY = useXY ? 'y' : 'longitude';
// ignore: todo
// TODO - Remove three quotation marks """Example Name_with_space""" when '"x"' is used, instead of "x"
diffNameThanIsInside = containsSpace(diffNameThanIsInside!)
? '$diffNameThanIsInside'
: diffNameThanIsInside;
// print(diffNameThanIsInside);
String? headerIs =
(diffNameThanIsInside.isNotEmpty) ? diffNameThanIsInside : 'isInside';
var headerL = [headerX, headerY, headerIs];
xYOut.add(headerL);
}
for (var _currentPoint in inputListOfPoint) {
var c_out = isPointInside(_currentPoint);
// _out.add(c_out);
// x.add(_currentPoint.x);
// y.add(_currentPoint.y);
var Temp = [_currentPoint.x, _currentPoint.y, c_out];
xYOut.add(Temp);
}
var csv = const ListToCsvConverter().convert(xYOut);
return csv;
}
/// * Returns `Polygon` as CSV String which can be later saved or displayed
/// * Output CSV String will by default contain a header row - `latitude,longitude`
/// * Optional Named parameter: `bool useXY`
/// * By passing, optional parameter: `useXY` as true, header will be `x,y` instead of `latitude,longitude`
/// * Default value of `useXY` is `false`
/// * Optional Named parameter: `String includeHeader`
/// * if optional parameter - `includeHeader` is is passed as `false`, returning String will not contain header row
String toCSVString({bool includeHeader = true, bool useXY = false}) {
var _xY = <List>[];
if (includeHeader) {
var _headerX = useXY ? 'x' : 'latitude';
var _headerY = useXY ? 'y' : 'longitude';
var _headerL = [_headerX, _headerY];
_xY.add(_headerL);
}
for (var _currentPoint in points) {
var _currentXY = [_currentPoint.x, _currentPoint.y];
_xY.add(_currentXY);
}
var csv = const ListToCsvConverter().convert(_xY);
return csv;
}
} // class Polygon
/// * Returns `Future<List<List>>` based on `csvString`
/// * which then can be used - convert that list into `Polygon`
/// * Optional parameter: `bool noHeader`
/// * By passing optional parameter: `noHeader` as `true`, Resulting List will not contain header row
/// * Default value `false`
Future<List<List>?> csvToListOfList(var csvString,
{bool noHeader = false}) async {
final List<List>? listOfList = await csvString
.transform(utf8.decoder)
.transform(CsvToListConverter())
.toList();
if (noHeader) {
listOfList!.removeAt(0);
}
return listOfList;
}
/// * Returns `Future<Polygon>` based on `csvString`
/// * `csvString` may or may not contain header row
/// * This function checks if `latitude,longitude` or `x,y` are reversed
/// * By checking Header row label
/// * i.e. By checking 1st row 1st element is neither "longitude" or "y"
/// * If they are reversed, Returned `Polygon` will be `Polygon(latitude,longitude)`, instead of `Polygon(longitude,latitude)`
/// * This can be manually set by passing optional parameter: `isReversed` as `true`
/// * Optional parameter: `isReversed`
/// * `isReversed` has default value = `false`
Future<Polygon> csvToPoly(var csvString, {bool isReversed = false}) async {
List<List> _listOfList = await csvString
.transform(utf8.decoder)
.transform(CsvToListConverter())
.toList();
if (_listOfList[0][0].runtimeType == String) {
isReversed = isReversed
? isReversed
: ((_listOfList[0][0] == 'longitude' || _listOfList[0][0] == 'y')
? true
: false);
_listOfList.removeAt(0);
}
var numList = <List<num>>[];
var lengthOfPoints = _listOfList.length;
if (isReversed) {
for (var _i = 0; _i < lengthOfPoints; _i++) {
var x = _listOfList[_i][0];
var y = _listOfList[_i][1];
//print(x.runtimeType);
numList.add([y, x]);
}
} else {
for (var _i = 0; _i < lengthOfPoints; _i++) {
var y = _listOfList[_i][1];
var x = _listOfList[_i][0];
//print(x.runtimeType);
numList.add([x, y]);
}
}
var _out = toPolyFromListOfList(numList);
return _out;
}
//Future<Polygon> csvToPoly_casting_issue(var input, {bool isReversed = false}) async {
// List<List<num>> fields = await input
// .transform(utf8.decoder)
// .transform(new CsvToListConverter())
// .toList();
// if (fields[0][0].runtimeType == String) {
// fields.removeAt(0);
// }
// final List<List<num>> numList = fields.cast<List<num>>();
// List<List<num>> rever = [];
// for (int i = 0; i < numList.length; i++) {
// num x = numList[i][1].toDouble();
// num y = numList[i][0].toDouble();
// rever.add([x, y]);
// }
// Polygon _out = to_poly_from_list_of_list(numList);
// return _out;
//}
////WOrks - type double
//Future<void> csvToTemp(var input, {bool isReversed = false}) async {
// List<List> fields = await input
// .transform(utf8.decoder)
// .transform(new CsvToListConverter())
// .toList();
// if (fields[0][0].runtimeType == String) {
// fields.removeAt(0);
// }
//// final List<List<num>> numList = fields.cast<List<num>>();
//
// List<List<num>> rever = [];
// for (int i = 0; i <fields.length; i++) {
// var x = fields[i][1];
// var y = fields[i][0];
// print(x.runtimeType);
// rever.add([x, y]);
// }
//// return x;
//// Polygon _out = to_poly_from_list_of_list(rever);
//// return _out;
//}