Skip to content

Commit

Permalink
refactor(geobase,geodata): use crs as CoordRefSys in spatial extends #…
Browse files Browse the repository at this point in the history
…196

BREAKING CHANGE: parameter name changed
  • Loading branch information
navispatial committed Aug 25, 2023
1 parent eca2432 commit cbc26a6
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 74 deletions.
1 change: 1 addition & 0 deletions dart/geobase/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ NOTE: Version 0.6.0 currently [under development](https://github.com/navibyte/ge
⚠️ Breaking changes:
* [Simplify Feature and FeatureCollection types #195](https://github.com/navibyte/geospatial/issues/195)
* [Remove deprecated types in geobase version 0.6.0 #194](https://github.com/navibyte/geospatial/issues/194)
* [Consistent crs and trs references in meta classes #196](https://github.com/navibyte/geospatial/issues/196)

## 0.5.1

Expand Down
4 changes: 2 additions & 2 deletions dart/geobase/example/geobase_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -892,14 +892,14 @@ void _temporalData() {
void _geospatialExtents() {
// An extent with spatial (WGS 84 longitude-latitude) and temporal parts.
GeoExtent.single(
coordRefSys: CoordRefSys.CRS84,
crs: CoordRefSys.CRS84,
bbox: GeoBox(west: -20.0, south: 50.0, east: 20.0, north: 60.0),
interval: Interval.parse('../2020-10-31'),
);

// An extent with multiple spatial bounds and temporal interval segments.
GeoExtent.multi(
coordRefSys: CoordRefSys.CRS84,
crs: CoordRefSys.CRS84,
boxes: [
GeoBox(west: -20.0, south: 50.0, east: 20.0, north: 60.0),
GeoBox(west: 40.0, south: 50.0, east: 60.0, north: 60.0),
Expand Down
20 changes: 6 additions & 14 deletions dart/geobase/lib/src/meta/extent/geo_extent.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,15 @@ class GeoExtent {

/// A geospatial extent of one [bbox] and optional [interval].
///
/// A coordinate reference system can be specified by [crs] or [coordRefSys],
/// and a temporal reference system by [trs].
///
/// See [SpatialExtent.single] for how [crs] or [coordRefSys] is handled.
/// A coordinate reference system can be specified by [crs], and a
/// temporal reference system by [trs].
GeoExtent.single({
required GeoBox bbox,
Interval? interval,
CoordRefSys? coordRefSys,
String? crs,
CoordRefSys crs = CoordRefSys.CRS84,
String trs = 'http://www.opengis.net/def/uom/ISO-8601/0/Gregorian',
}) : _spatial = SpatialExtent<GeoBox>.single(
bbox,
coordRefSys: coordRefSys,
crs: crs,
),
_temporal = interval != null
Expand All @@ -54,19 +50,15 @@ class GeoExtent {

/// A geospatial extent of [boxes] and optional [intervals].
///
/// A coordinate reference system can be specified by [crs] or [coordRefSys],
/// and a temporal reference system by [trs].
///
/// See [SpatialExtent.single] for how [crs] or [coordRefSys] is handled.
/// A coordinate reference system can be specified by [crs], and a
/// temporal reference system by [trs].
GeoExtent.multi({
required Iterable<GeoBox> boxes,
Iterable<Interval>? intervals,
CoordRefSys? coordRefSys,
String? crs,
CoordRefSys crs = CoordRefSys.CRS84,
String trs = 'http://www.opengis.net/def/uom/ISO-8601/0/Gregorian',
}) : _spatial = SpatialExtent<GeoBox>.multi(
boxes,
coordRefSys: coordRefSys,
crs: crs,
),
_temporal = intervals != null
Expand Down
69 changes: 22 additions & 47 deletions dart/geobase/lib/src/meta/extent/spatial_extent.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,50 +14,25 @@ import '/src/coordinates/crs/coord_ref_sys.dart';
class SpatialExtent<T extends Box> {
final T _first;
final Iterable<T>? _boxes;
final CoordRefSys _coordRefSys;
final CoordRefSys _crs;

/// A spatial extent of one [bbox] (coordinate reference system in
/// [coordRefSys]).
const SpatialExtent(
/// A spatial extent of one [bbox] with coordinate reference system specified
/// by [crs].
const SpatialExtent.single(
T bbox, {
CoordRefSys coordRefSys = CoordRefSys.CRS84,
CoordRefSys crs = CoordRefSys.CRS84,
}) : _first = bbox,
_boxes = null,
_coordRefSys = coordRefSys;
_crs = crs;

/// A spatial extent of one [bbox].
///
/// The coordinate reference system is resolved in this order:
/// 1. [coordRefSys] if it's non-null
/// 2. otherwise `CoordRefSys.normalized(crs)` if [crs] is non-null
/// 3. otherwise `CoordRefSys.CRS84`
SpatialExtent.single(
T bbox, {
CoordRefSys? coordRefSys,
String? crs,
}) : _first = bbox,
_boxes = null,
_coordRefSys = CoordRefSys.from(
coordRefSys: coordRefSys,
crs: crs,
);

/// A spatial extent of [boxes].
///
/// The coordinate reference system is resolved in this order:
/// 1. [coordRefSys] if it's non-null
/// 2. otherwise `CoordRefSys.normalized(crs)` if [crs] is non-null
/// 3. otherwise `CoordRefSys.CRS84`
/// A spatial extent of [boxes] with coordinate reference system specified
/// by [crs].
SpatialExtent.multi(
Iterable<T> boxes, {
CoordRefSys? coordRefSys,
String? crs,
CoordRefSys crs = CoordRefSys.CRS84,
}) : _boxes = _validate(boxes),
_first = boxes.first,
_coordRefSys = CoordRefSys.from(
coordRefSys: coordRefSys,
crs: crs,
);
_crs = crs;

static Iterable<T> _validate<T extends Box>(Iterable<T> boxes) {
if (boxes.isEmpty) {
Expand All @@ -73,18 +48,18 @@ class SpatialExtent<T extends Box> {
Iterable<T> get boxes => _boxes ?? [_first];

/// The coordinate reference system for bounding boxes of this extent.
CoordRefSys get coordRefSys => _coordRefSys;
CoordRefSys get crs => _crs;

/// Copy this spatial extent with optional [bbox] and/or [coordRefSys]
/// parameters changed.
SpatialExtent<T> copyWith({T? bbox, CoordRefSys? coordRefSys}) {
if (bbox != null) {
return SpatialExtent(bbox, coordRefSys: coordRefSys ?? _coordRefSys);
/// Copy this spatial extent with optional [boxes] and/or [crs] parameters
/// changed.
SpatialExtent<T> copyWith({Iterable<T>? boxes, CoordRefSys? crs}) {
if (boxes != null) {
return SpatialExtent.multi(boxes, crs: crs ?? _crs);
} else {
if (coordRefSys != null) {
if (crs != null) {
return _boxes != null
? SpatialExtent.multi(_boxes!, coordRefSys: coordRefSys)
: SpatialExtent(_first, coordRefSys: coordRefSys);
? SpatialExtent.multi(_boxes!, crs: crs)
: SpatialExtent.single(_first, crs: crs);
} else {
// ignore: avoid_returning_this
return this;
Expand All @@ -94,7 +69,7 @@ class SpatialExtent<T extends Box> {

@override
String toString() {
final buf = StringBuffer()..write(coordRefSys);
final buf = StringBuffer()..write(crs);
for (final item in boxes) {
buf
..write(',[')
Expand All @@ -106,7 +81,7 @@ class SpatialExtent<T extends Box> {

@override
bool operator ==(Object other) {
if (other is SpatialExtent<T> && coordRefSys == other.coordRefSys) {
if (other is SpatialExtent<T> && crs == other.crs) {
final items1 = boxes;
final items2 = other.boxes;
if (items1.length == items2.length) {
Expand All @@ -123,5 +98,5 @@ class SpatialExtent<T extends Box> {
}

@override
int get hashCode => Object.hash(coordRefSys, Object.hashAll(boxes));
int get hashCode => Object.hash(crs, Object.hashAll(boxes));
}
4 changes: 2 additions & 2 deletions dart/geobase/test/meta/extent_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ void main() {
expect(spatialSingle, spatialMulti1);
expect(spatialSingle, isNot(spatialMulti2));
expect(spatialMulti3, isNot(spatialMulti2));
expect(spatialSingle.coordRefSys, spatialMulti1.coordRefSys);
expect(spatialSingle.crs, spatialMulti1.crs);
expect(spatialSingle.first, spatialMulti1.first);
expect(spatialSingle.first, spatialMulti2.first);
expect(spatialSingle.first, isNot(spatialMulti3.first));
Expand Down Expand Up @@ -59,7 +59,7 @@ void main() {
GeoBox(west: -19.2, south: -4.5, east: 12.2, north: 24.5),
);
expect(
extent1.spatial.coordRefSys.toString(),
extent1.spatial.crs.toString(),
'http://www.opengis.net/def/crs/OGC/1.3/CRS84',
);
expect(extent1.temporal!.first, interval1);
Expand Down
1 change: 1 addition & 0 deletions dart/geodata/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ NOTE: Version 0.13.0 currently under development (0.13.0-dev.0).

🛠 Refactoring:
* [Simplify Feature and FeatureCollection types #195](https://github.com/navibyte/geospatial/issues/195)
* [Consistent crs and trs references in meta classes #196](https://github.com/navibyte/geospatial/issues/196)

## 0.12.1

Expand Down
2 changes: 1 addition & 1 deletion dart/geodata/example/geodata_cli_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ void _printCollection(OGCCollectionMeta meta) {

final extent = meta.extent;
if (extent != null) {
print(' extent crs: ${extent.spatial.coordRefSys}');
print(' extent crs: ${extent.spatial.crs}');
for (final bounds in extent.spatial.boxes) {
print(' spatial bbox min: ${bounds.min}');
print(' spatial bbox max: ${bounds.max}');
Expand Down
2 changes: 1 addition & 1 deletion dart/geodata/example/ogcapi_features_crs_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ Future<void> main(List<String> args) async {
final airportsMeta = await airports.meta();
final extent = airportsMeta.extent?.spatial;
if (extent != null) {
final crs = extent.coordRefSys;
final crs = extent.crs;
print('Spatial bbox list (crs: $crs):');
for (final box in extent.boxes) {
print(' $box');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -587,26 +587,25 @@ OGCCollectionMeta _collectionFromJson(
GeoExtent _extentFromJson(Map<String, dynamic> data) {
final spatial = data['spatial'];
final spatialIsMap = spatial is Map<String, dynamic>;
final crs = (spatialIsMap ? spatial['crs'] as String? : null) ??
'http://www.opengis.net/def/crs/OGC/1.3/CRS84';
final crs = (spatialIsMap ? spatial['crs'] as String? : null);

// try to parse bboxes
SpatialExtent<GeoBox> spatialExtent;
final bbox = spatialIsMap ? spatial['bbox'] as Iterable<dynamic>? : null;
if (bbox != null) {
// by standard: "bbox" is a list of bboxes
spatialExtent = SpatialExtent.multi(
bbox.map((e) => _bboxFromJson(e! as List<dynamic>)),
crs: crs,
);
final b = bbox.map((e) => _bboxFromJson(e! as List<dynamic>));
spatialExtent = crs != null
? SpatialExtent.multi(b, crs: CoordRefSys.normalized(crs))
: SpatialExtent.multi(b);
} else {
// not standard: assume "spatial" as one bbox
try {
spatialExtent =
SpatialExtent.single(_bboxFromJson(spatial! as List<dynamic>));
} catch (_) {
// fallback (world extent)
spatialExtent = const SpatialExtent(
spatialExtent = const SpatialExtent.single(
GeoBox(west: -180.0, south: -90.0, east: 180.0, north: 90.0),
);
}
Expand Down

0 comments on commit cbc26a6

Please sign in to comment.