Skip to content

Commit

Permalink
refactor(geobase): simplify positions as iterable coords #136
Browse files Browse the repository at this point in the history
  • Loading branch information
navispatial committed Jul 29, 2022
1 parent 712dd17 commit e3861ce
Show file tree
Hide file tree
Showing 16 changed files with 244 additions and 441 deletions.
12 changes: 12 additions & 0 deletions dart/geobase/lib/src/coordinates/base/box.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,18 @@ typedef CreateBox<T extends Box> = T Function({
/// xyz | west, south, minElev, east, north, maxElev
/// xym | west, south, minM, east, north, maxM
/// xyzm | west, south, minElev, minM, east, north, maxElev, maxM
///
/// Sub classes containing coordinate values mentioned above, should implement
/// equality and hashCode methods as:
///
/// ```dart
/// @override
/// bool operator ==(Object other) =>
/// other is Box && Box.testEquals(this, other);
///
/// @override
/// int get hashCode => Box.hash(this);
/// ```
abstract class Box extends Positionable {
/// Default `const` constructor to allow extending this abstract class.
const Box();
Expand Down
12 changes: 12 additions & 0 deletions dart/geobase/lib/src/coordinates/base/position.dart
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,18 @@ typedef TransformPosition = T Function<T extends Position>(T source);
/// 1 | y | lat
/// 2 | z | elev
/// 3 | m | m
///
/// Sub classes containing coordinate values mentioned above, should implement
/// equality and hashCode methods as:
///
/// ```dart
/// @override
/// bool operator ==(Object other) =>
/// other is Position && Position.testEquals(this, other);
///
/// @override
/// int get hashCode => Position.hash(this);
/// ```
abstract class Position extends Positionable {
/// Default `const` constructor to allow extending this abstract class.
const Position();
Expand Down
2 changes: 1 addition & 1 deletion dart/geobase/lib/src/coordinates/geographic/geobox.dart
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ class GeoBox extends Box {

@override
bool operator ==(Object other) =>
other is GeoBox && Box.testEquals(this, other);
other is Box && Box.testEquals(this, other);

@override
int get hashCode => Box.hash(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ class Geographic extends Position {

@override
bool operator ==(Object other) =>
other is Geographic && Position.testEquals(this, other);
other is Position && Position.testEquals(this, other);

@override
int get hashCode => Position.hash(this);
Expand Down
2 changes: 1 addition & 1 deletion dart/geobase/lib/src/coordinates/projected/projbox.dart
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ class ProjBox extends Box {

@override
bool operator ==(Object other) =>
other is ProjBox && Box.testEquals(this, other);
other is Box && Box.testEquals(this, other);

@override
int get hashCode => Box.hash(this);
Expand Down
2 changes: 1 addition & 1 deletion dart/geobase/lib/src/coordinates/projected/projected.dart
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ class Projected extends Position {

@override
bool operator ==(Object other) =>
other is Projected && Position.testEquals(this, other);
other is Position && Position.testEquals(this, other);

@override
int get hashCode => Position.hash(this);
Expand Down
11 changes: 5 additions & 6 deletions dart/geobase/lib/src/vector_data/array/coordinates.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,15 @@ import '/src/utils/num.dart';

part 'box_coords.dart';
part 'coordinates_mixin.dart';
part 'geographic_coords.dart';
part 'position_array.dart';
part 'position_coords.dart';
part 'projected_coords.dart';

/// Geospatial coordinate values as an iterable collection.
/// Geospatial coordinate values as an iterable collection of double values.
///
/// There are two known sub classes; [PositionCoords] containing coordinate
/// values of a single position and [PositionArray] containing coordinate values
/// of 0 to N positions.
/// See also sub classes:
/// * [PositionArray]: coordinate values of 0 to N positions in a flat structure
/// * [PositionCoords]: coordinate values of a single position
/// * [BoxCoords]: coordinate values of a single bounding box
abstract class Coordinates extends Iterable<double> implements Positionable {}

typedef _CreateAt<T> = T Function(
Expand Down
122 changes: 0 additions & 122 deletions dart/geobase/lib/src/vector_data/array/geographic_coords.dart

This file was deleted.

40 changes: 26 additions & 14 deletions dart/geobase/lib/src/vector_data/array/position_array.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ part of 'coordinates.dart';
/// The collection implements `Iterable<double>` with coordinate values of
/// positions as a flat structure (each position containing 2, 3, or 4 values).
///
/// Position data is also accessible as projected coordinates via [projected]
/// and as geographic coordinates via [geographic] properties. You can use
/// [data] to map coordinate values to custom position types.
/// Position data is also accessible as position coordinates via [data]. You can
/// also use [dataTo] to map coordinate values to custom position types.
///
/// See [Position] for description about supported coordinate values.
class PositionArray with _CoordinatesMixin {
Expand Down Expand Up @@ -63,22 +62,16 @@ class PositionArray with _CoordinatesMixin {
type: type,
);

/// Access positions as projected coordinates.
/// Access coordinate values of positions.
///
/// See [Projected] for description about supported coordinate values.
PositionData<ProjectedCoords, double> get projected =>
_PositionArrayData<ProjectedCoords>(_data, _type, ProjectedCoords.view);

/// Access positions as geographic coordinates.
///
/// See [Geographic] for description about supported coordinate values.
PositionData<GeographicCoords, double> get geographic =>
_PositionArrayData<GeographicCoords>(_data, _type, GeographicCoords.view);
/// See [Position] for description about supported coordinate values.
PositionData<PositionCoords, double> get data =>
_PositionArrayData<PositionCoords>(_data, _type, PositionCoords.view);

/// Access positions as custom type of [T].
///
/// The [factory] is used to create instances of [T] as needed.
PositionData<T, double> data<T extends Position>(
PositionData<T, double> dataTo<T extends Position>(
CreatePosition<T> factory,
) =>
_PositionArrayData<T>(_data, _type, _adapt(factory));
Expand Down Expand Up @@ -167,4 +160,23 @@ class _PositionArrayData<E extends Position> with PositionData<E, double> {

@override
bool get isMeasured => type.isMeasured;

@override
Iterable<E> get all sync* {
final dim = coordinateDimension;
final len = data.length;
var start = 0;
var end = dim;
while (end <= len) {
yield _doCreateRange(
data,
to: factory,
type: type,
start: start,
end: end,
);
start += dim;
end += dim;
}
}
}
Loading

0 comments on commit e3861ce

Please sign in to comment.