Skip to content

Commit

Permalink
feat(geobase): recalculate bounds in project if bounds was available #…
Browse files Browse the repository at this point in the history
  • Loading branch information
navispatial committed Aug 10, 2023
1 parent 294d452 commit 4d6c7d5
Show file tree
Hide file tree
Showing 14 changed files with 235 additions and 75 deletions.
10 changes: 7 additions & 3 deletions dart/geobase/lib/src/utils/bounds_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class BoundsBuilder {
Iterable<PositionArray>? arrays,
Iterable<Position>? positions,
required Coords type,
bool calculateChilds = false,
bool recalculateChilds = false,
}) {
if (item == null &&
(collection == null || collection.isEmpty) &&
Expand All @@ -50,15 +50,19 @@ class BoundsBuilder {
final builder = BoundsBuilder(type);

if (item != null) {
final bounds = calculateChilds ? item.calculateBounds() : item.bounds;
final bounds = recalculateChilds || item.bounds == null
? item.calculateBounds()
: item.bounds;
if (bounds != null) {
builder.addBounds(bounds);
}
}

if (collection != null) {
for (final elem in collection) {
final bounds = calculateChilds ? elem.calculateBounds() : elem.bounds;
final bounds = recalculateChilds || elem.bounds == null
? elem.calculateBounds()
: elem.bounds;
if (bounds != null) {
builder.addBounds(bounds);
}
Expand Down
7 changes: 3 additions & 4 deletions dart/geobase/lib/src/vector_data/model/bounded/bounded.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,8 @@ abstract class Bounded {
///
/// The returned subtype must be the same as the type of this.
///
/// Note that any available [bounds] object on this is not projected (that is
/// the bounds for a returned object is null).
/// If [bounds] object is available on this, it's recalculated after
/// projecting geometries. If [bounds] is null, then it's null after
/// projecting too.
Bounded project(Projection projection);

// NOTE: add an optional param to "project" to ask calcuting bounds after op
}
66 changes: 49 additions & 17 deletions dart/geobase/lib/src/vector_data/model/feature/feature.dart
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ class Feature<T extends Geometry> extends FeatureObject {
item: geometry, // the main geometry of Feature
collection: customGeometries?.values, // other geoms of CustomFeature
type: resolveCoordType(),
calculateChilds: true,
recalculateChilds: true,
);

@override
Expand All @@ -259,18 +259,31 @@ class Feature<T extends Geometry> extends FeatureObject {
? BoundsBuilder.calculateBounds(
item: geom,
type: resolveCoordTypeFrom(item: geom),
calculateChilds: false,
recalculateChilds: false,
)
: bounds,
);
}

@override
Feature<T> project(Projection projection) => Feature<T>(
id: _id,
geometry: _geometry?.project(projection) as T?,
properties: _properties,
);
Feature<T> project(Projection projection) {
final projectedGeom = _geometry?.project(projection) as T?;

return Feature<T>(
id: _id,
geometry: projectedGeom,
properties: _properties,

// bounds calculated from projected geometry if there was bounds before
bounds: bounds != null && projectedGeom != null
? BoundsBuilder.calculateBounds(
item: projectedGeom,
type: resolveCoordTypeFrom(item: projectedGeom),
recalculateChilds: false,
)
: null,
);
}

@override
void writeTo(FeatureContent writer) {
Expand Down Expand Up @@ -514,22 +527,41 @@ class _CustomFeature<T extends Geometry> extends Feature<T> {
item: geom,
collection: custGeom?.values,
),
calculateChilds: false,
recalculateChilds: false,
)
: bounds,
);
}

@override
Feature<T> project(Projection projection) => _CustomFeature<T>(
id: _id,
geometry: _geometry?.project(projection) as T?,
properties: _properties,
custom: _custom,
customGeometries: _customGeometries?.map<String, Geometry>(
(key, geom) => MapEntry(key, geom.project(projection)),
),
);
Feature<T> project(Projection projection) {
final projectedGeom = _geometry?.project(projection) as T?;
final projectedCustGeom = _customGeometries?.map<String, Geometry>(
(key, geom) => MapEntry(key, geom.project(projection)),
);

return _CustomFeature<T>(
id: _id,
geometry: projectedGeom,
properties: _properties,
custom: _custom,
customGeometries: projectedCustGeom,

// bounds calculated from projected geometries if there was bounds before
bounds:
bounds != null && (projectedGeom != null || projectedCustGeom != null)
? BoundsBuilder.calculateBounds(
item: projectedGeom,
collection: projectedCustGeom?.values,
type: resolveCoordTypeFrom(
item: projectedGeom,
collection: projectedCustGeom?.values,
),
recalculateChilds: false,
)
: null,
);
}

@override
void writeTo(FeatureContent writer) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ class FeatureCollection<E extends Feature> extends FeatureObject {
BoxCoords? calculateBounds() => BoundsBuilder.calculateBounds(
collection: features,
type: resolveCoordType(),
calculateChilds: true,
recalculateChilds: true,
);

@override
Expand All @@ -189,19 +189,32 @@ class FeatureCollection<E extends Feature> extends FeatureObject {
? BoundsBuilder.calculateBounds(
collection: collection,
type: resolveCoordTypeFrom(collection: collection),
calculateChilds: false,
recalculateChilds: false,
)
: bounds,
);
}

@override
FeatureCollection<E> project(Projection projection) => FeatureCollection<E>._(
_features
.map<E>((feature) => feature.project(projection) as E)
.toList(growable: false),
_custom,
);
FeatureCollection<E> project(Projection projection) {
final projected = _features
.map<E>((feature) => feature.project(projection) as E)
.toList(growable: false);

return FeatureCollection<E>._(
projected,
_custom,

// bounds calculated from projected collection if there was bounds before
bounds: bounds != null
? BoundsBuilder.calculateBounds(
collection: projected,
type: resolveCoordTypeFrom(collection: projected),
recalculateChilds: false,
)
: null,
);
}

@override
void writeTo(FeatureContent writer) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ abstract class FeatureObject extends Bounded {
/// Any custom data or properties (other than geometries) are not projected,
/// just copied (by references).
///
/// Note that any available [bounds] object on this is not projected (that is
/// the bounds for a returned feature object is null).
/// If [bounds] object is available on this, it's recalculated after
/// projecting geometries. If [bounds] is null, then it's null after
/// projecting too.
@override
FeatureObject project(Projection projection);

Expand Down
5 changes: 3 additions & 2 deletions dart/geobase/lib/src/vector_data/model/geometry/geometry.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@ abstract class Geometry extends Bounded {
///
/// The returned geometry sub type must be the same as the type of this.
///
/// Note that any available [bounds] object on this is not projected (that is
/// the bounds for a returned geometry is null).
/// If [bounds] object is available on this, it's recalculated after
/// projecting geometries. If [bounds] is null, then it's null after
/// projecting too.
@override
Geometry project(Projection projection);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ class GeometryCollection<E extends Geometry> extends Geometry {
BoxCoords? calculateBounds() => BoundsBuilder.calculateBounds(
collection: _geometries,
type: resolveCoordType(),
calculateChilds: true,
recalculateChilds: true,
);

@override
Expand All @@ -161,18 +161,31 @@ class GeometryCollection<E extends Geometry> extends Geometry {
? BoundsBuilder.calculateBounds(
collection: collection,
type: resolveCoordTypeFrom(collection: collection),
calculateChilds: false,
recalculateChilds: false,
)
: bounds,
);
}

@override
GeometryCollection<E> project(Projection projection) => GeometryCollection<E>(
_geometries
.map<E>((geometry) => geometry.project(projection) as E)
.toList(growable: false),
);
GeometryCollection<E> project(Projection projection) {
final projected = _geometries
.map<E>((geometry) => geometry.project(projection) as E)
.toList(growable: false);

return GeometryCollection<E>(
projected,

// bounds calculated from projected collection if there was bounds before
bounds: bounds != null
? BoundsBuilder.calculateBounds(
collection: projected,
type: resolveCoordTypeFrom(collection: projected),
recalculateChilds: false,
)
: null,
);
}

@override
void writeTo(GeometryContent writer, {String? name}) => isEmpty
Expand Down
17 changes: 15 additions & 2 deletions dart/geobase/lib/src/vector_data/model/geometry/linestring.dart
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,21 @@ class LineString extends SimpleGeometry {
}

@override
LineString project(Projection projection) =>
LineString(_chain.project(projection));
LineString project(Projection projection) {
final projected = _chain.project(projection);

return LineString(
projected,

// bounds calculated from projected chain if there was bounds before
bounds: bounds != null
? BoundsBuilder.calculateBounds(
array: projected,
type: coordType,
)
: null,
);
}

@override
void writeTo(SimpleGeometryContent writer, {String? name}) => isEmpty
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,12 +223,24 @@ class MultiLineString extends SimpleGeometry {
}

@override
MultiLineString project(Projection projection) => MultiLineString._(
_lineStrings
.map((chain) => chain.project(projection))
.toList(growable: false),
type: _type,
);
MultiLineString project(Projection projection) {
final projected = _lineStrings
.map((chain) => chain.project(projection))
.toList(growable: false);

return MultiLineString._(
projected,
type: _type,

// bounds calculated from projected geometry if there was bounds before
bounds: bounds != null
? BoundsBuilder.calculateBounds(
arrays: projected,
type: coordType,
)
: null,
);
}

@override
void writeTo(SimpleGeometryContent writer, {String? name}) => isEmpty
Expand Down
24 changes: 18 additions & 6 deletions dart/geobase/lib/src/vector_data/model/geometry/multi_point.dart
Original file line number Diff line number Diff line change
Expand Up @@ -199,12 +199,24 @@ class MultiPoint extends SimpleGeometry {
}

@override
MultiPoint project(Projection projection) => MultiPoint._(
_points
.map((pos) => projection.project(pos, to: PositionCoords.create))
.toList(growable: false),
type: _type,
);
MultiPoint project(Projection projection) {
final projected = _points
.map((pos) => projection.project(pos, to: PositionCoords.create))
.toList(growable: false);

return MultiPoint._(
projected,
type: _type,

// bounds calculated from projected geometry if there was bounds before
bounds: bounds != null
? BoundsBuilder.calculateBounds(
positions: projected,
type: coordType,
)
: null,
);
}

@override
void writeTo(SimpleGeometryContent writer, {String? name}) => isEmpty
Expand Down
Loading

0 comments on commit 4d6c7d5

Please sign in to comment.