Skip to content

Commit 31cb843

Browse files
committed
docs(geobase): Position and PositionSeries manipulation samples
1 parent eea8e78 commit 31cb843

File tree

2 files changed

+90
-1
lines changed

2 files changed

+90
-1
lines changed

dart/geobase/example/geobase_example.dart

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ void main() {
2121
// coordinates
2222
_positionData();
2323
_positionSeries();
24+
_positionManipulation();
25+
_positionSeriesManipulation();
2426
_geographicCoordinates();
2527
_geographicCoordinatesDMS();
2628
_projectedCoordinates();
@@ -355,6 +357,93 @@ void _positionSeries() {
355357
);
356358
}
357359

360+
void _positionManipulation() {
361+
// a position containing x, y and z
362+
final pos = [708221.0, 5707225.0, 45.0].xyz;
363+
364+
// multiplication operator - prints "708.221,5707.225,0.045" (values in km)
365+
// (the operand is a factor value applied to all coordinate values)
366+
print(pos * 0.001);
367+
368+
// negate operator - prints "-708221.0,-5707225.0,-45.0"
369+
print(-pos);
370+
371+
// following operators expect an operand to be another position object
372+
373+
// add operator - prints "708231.0,5707245.0,50.0"
374+
print(pos + [10.0, 20.0, 5.0].xyz);
375+
376+
// subtraction operator - prints "708211.0,5707205.0,40.0"
377+
print(pos - [10.0, 20.0, 5.0].xyz);
378+
379+
// division operator - prints "708.221,5707.225,45.0" (x and y values in km)
380+
print(pos / [1000.0, 1000.0, 1.0].xyz);
381+
382+
// modulo operator - prints "221.0,225.0,45.0"
383+
print(pos % [1000.0, 1000.0, 1000.0].xyz);
384+
385+
// there is support also for basic calculations in cartesian coordinates
386+
387+
// other point 1000.0 meters to the direction of 45° (north-east)
388+
final other = pos.destinationPoint2D(distance: 1000.0, bearing: 45.0);
389+
390+
// distance between points - prints "1000.0"
391+
print(pos.distanceTo2D(other).toStringAsFixed(1));
392+
393+
// bearing from point to another - prints "45.0"
394+
print(pos.bearingTo2D(other).toStringAsFixed(1));
395+
396+
// midpoint between two points - prints "708574.6,5707578.6"
397+
print(pos.midPointTo(other).toText(decimals: 1));
398+
399+
// intermediate point between two point (fraction range: 0.0 to 1.0)
400+
// prints "708397.8,5707401.8"
401+
print(pos.intermediatePointTo(other, fraction: 0.25).toText(decimals: 1));
402+
}
403+
404+
void _positionSeriesManipulation() {
405+
// a closed linear ring with positions in the counterclockwise (CCW) order
406+
final polygon = [
407+
[1.0, 6.0].xy,
408+
[3.0, 1.0].xy,
409+
[7.0, 2.0].xy,
410+
[4.0, 4.0].xy,
411+
[8.0, 5.0].xy,
412+
[1.0, 6.0].xy,
413+
].series();
414+
415+
// the area of a polygon formed by the linear ring - prints "16.5"
416+
print(polygon.signedArea2D());
417+
418+
// the perimeter of a polygon - prints "24.3"
419+
print(polygon.length2D().toStringAsFixed(1));
420+
421+
// the centroid position of a polygon - prints "3.9,3.7"
422+
print(polygon.centroid2D()!.toText(decimals: 1));
423+
424+
// a closed linear ring with positions in the clockwise (CW) order
425+
final reversed = polygon.reversed();
426+
427+
// a line string omitting the last position of `reversed`
428+
final line = reversed.range(0, reversed.positionCount - 1);
429+
430+
// the length of a line string - prints "18.9"
431+
print(line.length2D().toStringAsFixed(1));
432+
433+
// the line string modified by replacing positions at indexes 1 ja 2
434+
final lineModified = line.rangeReplaced(1, 3, [
435+
[3.5, 1.5].xy,
436+
[7.5, 2.5].xy,
437+
]);
438+
439+
// coordinate values of a line string multiplied by 100.0
440+
final lineModified2 = lineModified * 100.0;
441+
442+
// get position count and a position by index - prints "5" and "350.0,150.0"
443+
print(lineModified2.positionCount);
444+
print(lineModified2[1]);
445+
}
446+
358447
void _geographicCoordinates() {
359448
// A geographic position with longitude and latitude.
360449
Geographic(lon: -0.0014, lat: 51.4778);

dart/geobase/lib/src/coordinates/base/position.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -664,7 +664,7 @@ abstract class Position extends ValuePositionable {
664664
/// Examples:
665665
///
666666
/// ```dart
667-
/// // This usage of modulo operator returns a position of `[15.0, 15.0].xy`
667+
/// // This usage of division operator returns a position of `[15.0, 15.0].xy`
668668
/// [150.0, 300.0].xy / [10.0, 20.0].xy;
669669
/// ```
670670
Position operator /(Position divisor) => cartesianPositionDivision(

0 commit comments

Comments
 (0)