Skip to content

Commit

Permalink
Respect device pixel ratio when (un)transforming pixels into projecte…
Browse files Browse the repository at this point in the history
…d space.
  • Loading branch information
ignatz committed Jan 24, 2024
1 parent 712ee36 commit 97e6c4d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
7 changes: 5 additions & 2 deletions lib/src/layer/polygon_layer/polygon_layer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,10 @@ class _PolygonLayerState extends State<PolygonLayer> {
? projected
: _cachedSimplifiedPolygons[camera.zoom.floor()] ??=
_computeZoomLevelSimplification(
camera: camera,
polygons: projected,
pixelTolerance: widget.simplificationTolerance,
camera: camera,
devicePixelRatio: MediaQuery.of(context).devicePixelRatio,
);

final culled = !widget.polygonCulling
Expand All @@ -122,14 +123,16 @@ class _PolygonLayerState extends State<PolygonLayer> {
}

static List<_ProjectedPolygon> _computeZoomLevelSimplification({
required MapCamera camera,
required List<_ProjectedPolygon> polygons,
required double pixelTolerance,
required MapCamera camera,
required double devicePixelRatio,
}) {
final tolerance = getEffectiveSimplificationTolerance(
crs: camera.crs,
zoom: camera.zoom.floor(),
pixelTolerance: pixelTolerance,
devicePixelRatio: devicePixelRatio,
);

return List<_ProjectedPolygon>.generate(
Expand Down
7 changes: 5 additions & 2 deletions lib/src/layer/polyline_layer/polyline_layer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,10 @@ class _PolylineLayerState<R extends Object> extends State<PolylineLayer<R>> {
? projected
: _cachedSimplifiedPolylines[camera.zoom.floor()] ??=
_computeZoomLevelSimplification(
camera: camera,
polylines: projected,
pixelTolerance: widget.simplificationTolerance,
camera: camera,
devicePixelRatio: MediaQuery.of(context).devicePixelRatio,
);

final culled = widget.cullingMargin == null
Expand Down Expand Up @@ -221,14 +222,16 @@ class _PolylineLayerState<R extends Object> extends State<PolylineLayer<R>> {
}

static List<_ProjectedPolyline> _computeZoomLevelSimplification({
required MapCamera camera,
required List<_ProjectedPolyline> polylines,
required double pixelTolerance,
required MapCamera camera,
required double devicePixelRatio,
}) {
final tolerance = getEffectiveSimplificationTolerance(
crs: camera.crs,
zoom: camera.zoom.floor(),
pixelTolerance: pixelTolerance,
devicePixelRatio: devicePixelRatio,
);

return List<_ProjectedPolyline>.generate(
Expand Down
10 changes: 6 additions & 4 deletions lib/src/misc/simplify.dart
Original file line number Diff line number Diff line change
Expand Up @@ -142,14 +142,16 @@ double getEffectiveSimplificationTolerance({
required Crs crs,
required int zoom,
required double pixelTolerance,
required double devicePixelRatio,
}) {
if (pixelTolerance <= 0) return 0;

final (x0, y0) = crs.untransform(0, 0, crs.scale(zoom.toDouble()));
final scale = crs.scale(zoom.toDouble());
final (x0, y0) = crs.untransform(0, 0, scale);
final (x1, y1) = crs.untransform(
pixelTolerance,
pixelTolerance,
crs.scale(zoom.toDouble()),
pixelTolerance * devicePixelRatio,
pixelTolerance * devicePixelRatio,
scale,
);

final dx = x1 - x0;
Expand Down

0 comments on commit 97e6c4d

Please sign in to comment.