From cbc26a65c96d245894b95f62147f5e2a3aaeb52e Mon Sep 17 00:00:00 2001 From: navibyte <88932567+navispatial@users.noreply.github.com> Date: Fri, 25 Aug 2023 23:20:54 +0300 Subject: [PATCH] refactor(geobase,geodata): use crs as CoordRefSys in spatial extends #196 BREAKING CHANGE: parameter name changed --- dart/geobase/CHANGELOG.md | 1 + dart/geobase/example/geobase_example.dart | 4 +- .../lib/src/meta/extent/geo_extent.dart | 20 ++---- .../lib/src/meta/extent/spatial_extent.dart | 69 ++++++------------- dart/geobase/test/meta/extent_test.dart | 4 +- dart/geodata/CHANGELOG.md | 1 + dart/geodata/example/geodata_cli_example.dart | 2 +- .../example/ogcapi_features_crs_example.dart | 2 +- .../service/client/ogc_feature_client.dart | 13 ++-- 9 files changed, 42 insertions(+), 74 deletions(-) diff --git a/dart/geobase/CHANGELOG.md b/dart/geobase/CHANGELOG.md index 65f430a6..01399ad6 100644 --- a/dart/geobase/CHANGELOG.md +++ b/dart/geobase/CHANGELOG.md @@ -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 diff --git a/dart/geobase/example/geobase_example.dart b/dart/geobase/example/geobase_example.dart index f3345f8d..d5d4badb 100644 --- a/dart/geobase/example/geobase_example.dart +++ b/dart/geobase/example/geobase_example.dart @@ -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), diff --git a/dart/geobase/lib/src/meta/extent/geo_extent.dart b/dart/geobase/lib/src/meta/extent/geo_extent.dart index 5acb926a..f185dd40 100644 --- a/dart/geobase/lib/src/meta/extent/geo_extent.dart +++ b/dart/geobase/lib/src/meta/extent/geo_extent.dart @@ -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.single( bbox, - coordRefSys: coordRefSys, crs: crs, ), _temporal = interval != null @@ -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 boxes, Iterable? intervals, - CoordRefSys? coordRefSys, - String? crs, + CoordRefSys crs = CoordRefSys.CRS84, String trs = 'http://www.opengis.net/def/uom/ISO-8601/0/Gregorian', }) : _spatial = SpatialExtent.multi( boxes, - coordRefSys: coordRefSys, crs: crs, ), _temporal = intervals != null diff --git a/dart/geobase/lib/src/meta/extent/spatial_extent.dart b/dart/geobase/lib/src/meta/extent/spatial_extent.dart index 42075b6e..5671e76d 100644 --- a/dart/geobase/lib/src/meta/extent/spatial_extent.dart +++ b/dart/geobase/lib/src/meta/extent/spatial_extent.dart @@ -14,50 +14,25 @@ import '/src/coordinates/crs/coord_ref_sys.dart'; class SpatialExtent { final T _first; final Iterable? _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 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 _validate(Iterable boxes) { if (boxes.isEmpty) { @@ -73,18 +48,18 @@ class SpatialExtent { Iterable 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 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 copyWith({Iterable? 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; @@ -94,7 +69,7 @@ class SpatialExtent { @override String toString() { - final buf = StringBuffer()..write(coordRefSys); + final buf = StringBuffer()..write(crs); for (final item in boxes) { buf ..write(',[') @@ -106,7 +81,7 @@ class SpatialExtent { @override bool operator ==(Object other) { - if (other is SpatialExtent && coordRefSys == other.coordRefSys) { + if (other is SpatialExtent && crs == other.crs) { final items1 = boxes; final items2 = other.boxes; if (items1.length == items2.length) { @@ -123,5 +98,5 @@ class SpatialExtent { } @override - int get hashCode => Object.hash(coordRefSys, Object.hashAll(boxes)); + int get hashCode => Object.hash(crs, Object.hashAll(boxes)); } diff --git a/dart/geobase/test/meta/extent_test.dart b/dart/geobase/test/meta/extent_test.dart index ffcb6f5b..72f85b73 100644 --- a/dart/geobase/test/meta/extent_test.dart +++ b/dart/geobase/test/meta/extent_test.dart @@ -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)); @@ -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); diff --git a/dart/geodata/CHANGELOG.md b/dart/geodata/CHANGELOG.md index fb855fd2..cb6ff2f7 100644 --- a/dart/geodata/CHANGELOG.md +++ b/dart/geodata/CHANGELOG.md @@ -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 diff --git a/dart/geodata/example/geodata_cli_example.dart b/dart/geodata/example/geodata_cli_example.dart index 8cc555f0..533d2c84 100644 --- a/dart/geodata/example/geodata_cli_example.dart +++ b/dart/geodata/example/geodata_cli_example.dart @@ -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}'); diff --git a/dart/geodata/example/ogcapi_features_crs_example.dart b/dart/geodata/example/ogcapi_features_crs_example.dart index de96c353..be38ae3e 100644 --- a/dart/geodata/example/ogcapi_features_crs_example.dart +++ b/dart/geodata/example/ogcapi_features_crs_example.dart @@ -61,7 +61,7 @@ Future main(List 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'); diff --git a/dart/geodata/lib/src/ogcapi_features/service/client/ogc_feature_client.dart b/dart/geodata/lib/src/ogcapi_features/service/client/ogc_feature_client.dart index 27d7df5a..5fa4b25d 100644 --- a/dart/geodata/lib/src/ogcapi_features/service/client/ogc_feature_client.dart +++ b/dart/geodata/lib/src/ogcapi_features/service/client/ogc_feature_client.dart @@ -587,18 +587,17 @@ OGCCollectionMeta _collectionFromJson( GeoExtent _extentFromJson(Map data) { final spatial = data['spatial']; final spatialIsMap = spatial is Map; - 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 spatialExtent; final bbox = spatialIsMap ? spatial['bbox'] as Iterable? : null; if (bbox != null) { // by standard: "bbox" is a list of bboxes - spatialExtent = SpatialExtent.multi( - bbox.map((e) => _bboxFromJson(e! as List)), - crs: crs, - ); + final b = bbox.map((e) => _bboxFromJson(e! as List)); + spatialExtent = crs != null + ? SpatialExtent.multi(b, crs: CoordRefSys.normalized(crs)) + : SpatialExtent.multi(b); } else { // not standard: assume "spatial" as one bbox try { @@ -606,7 +605,7 @@ GeoExtent _extentFromJson(Map data) { SpatialExtent.single(_bboxFromJson(spatial! as List)); } catch (_) { // fallback (world extent) - spatialExtent = const SpatialExtent( + spatialExtent = const SpatialExtent.single( GeoBox(west: -180.0, south: -90.0, east: 180.0, north: 90.0), ); }