Skip to content

Commit

Permalink
Compute simplification tolerance based of given projection. Now the u…
Browse files Browse the repository at this point in the history
…ser-provided tolerance will have similar meaning independent of the user-selected projection.
  • Loading branch information
ignatz committed Jan 17, 2024
1 parent 23eae12 commit 1990204
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
8 changes: 7 additions & 1 deletion lib/src/layer/polygon_layer/polygon_layer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ class PolygonLayer extends StatefulWidget {

class _PolygonLayerState extends State<PolygonLayer> {
List<_ProjectedPolygon>? _cachedProjectedPolygons;
double? _effectiveTolerance;
final _cachedSimplifiedPolygons = <int, List<_ProjectedPolygon>>{};

@override
Expand All @@ -82,6 +83,7 @@ class _PolygonLayerState extends State<PolygonLayer> {

_cachedSimplifiedPolygons.clear();
_cachedProjectedPolygons = null;
_effectiveTolerance = null;
}

@override
Expand All @@ -96,13 +98,17 @@ class _PolygonLayerState extends State<PolygonLayer> {
),
growable: false,
);
final simplificationTolerance = _effectiveTolerance ??=
getEffectiveSimplificationTolerance(
camera.crs.projection, widget.simplificationTolerance);

final zoom = camera.zoom.floor();

final simplified = widget.simplificationTolerance == 0
? projected
: _cachedSimplifiedPolygons[zoom] ??= _computeZoomLevelSimplification(
projected,
widget.simplificationTolerance,
simplificationTolerance,
zoom,
);

Expand Down
16 changes: 15 additions & 1 deletion lib/src/misc/simplify.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// implementation based on
// https://github.com/mourner/simplify-js/blob/master/simplify.js

import 'package:flutter_map/src/geo/crs.dart';
import 'package:latlong2/latlong.dart';

// Custom point due to math.Point<double> being slow. Math operations tend to
Expand Down Expand Up @@ -156,3 +156,17 @@ List<DoublePoint> simplifyPoints(

return nextPoints;
}

double getEffectiveSimplificationTolerance(
Projection projection,
double tolerance, {
LatLng point = const LatLng(45, 90),
}) {
if (tolerance <= 0) return 0;

final p0 = projection.project(point);
final p1 = projection
.project(LatLng(point.latitude + tolerance, point.longitude + tolerance));

return p0.distanceTo(p1);
}

0 comments on commit 1990204

Please sign in to comment.