Skip to content

Commit 60d9846

Browse files
mootwJaffaKetchup
andauthored
feat!: migrate Point<double> to Offset internally (#1996)
* initial commit, started work, going well * more insane progress, still going well * compiler happy * deprecate notices, only really needed because of bounds and tilecoordinates * this definitely touches an externally accessible API: mapController.camera.pointToLatLng * dart format * remove stuff * more stuffffffffff * oops * fix tests * use double.negativeInfinity no Offset.negitiveInfinity exists * rename pointToLatLng to offsetToLatLng * remove unused duplicate function * remove todo * Update lib/src/map/camera/camera.dart Co-authored-by: Luka S <[email protected]> * format change * replace helper function * start migration guide * mark as internal * Update lib/src/layer/shared/layer_interactivity/internal_hit_detectable.dart Co-authored-by: Luka S <[email protected]> * rename * re-phrase things * rotateAroundPoint fix * convert doublepoint to offset * dart format * fix visible scope lol * Update flutter_map.dart * Update internal_hit_detectable.dart * fix straggling Point * convert to use size * migrate easy Bounds<double> to Rect * dart format . * migrate most methods that use Bounds * dart format . * Update CHANGELOG.md * more complete migration of Rect * dart format * convert Bounds to IntegerBounds and made internal most methods on integer bounds are not used * fix lints * fix test * fix bug introduced in commit b6d2d95 b6d2d95 * fix lint * rename method * fix some nits * Update lib/src/geo/crs.dart Co-authored-by: Luka S <[email protected]> * Update lib/src/geo/crs.dart Co-authored-by: Luka S <[email protected]> * Update lib/src/geo/crs.dart Co-authored-by: Luka S <[email protected]> * dart format * rename project method * dart format . * fix unused import --------- Co-authored-by: Luka S <[email protected]>
1 parent 4dc3bca commit 60d9846

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+675
-779
lines changed

CHANGELOG.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,20 @@ Please consider [donating](https://docs.fleaflet.dev/supporters#support-us) or [
44

55
This CHANGELOG does not include every commit and/or PR - it is a hand picked selection of the most important ones. For a full list of changes, please check the GitHub repository releases/tags.
66

7+
## [8.0.0] - 2024/07/02
8+
9+
Migration from `Point` class internally, but this also affects the external API.
10+
11+
Migration Guide:
12+
- any methods that previously required `Point<double>` now require `Offset`, `Size`, or `Rect` as return values and parameters
13+
- `pointToLatLng` -> `offsetToLatLng`
14+
- `PointExtension` and `OffsetToPointExtension` marked as internal
15+
- `MapController.rotateAroundPoint` now only accepts an Offset
16+
17+
18+
Contains the following user-affecting bug fixes:
19+
20+
721
## [7.0.2] - 2024/07/02
822

923
> Note that this version causes a technically breaking change by removing `PolygonLayer.useDynamicUpdate` & `PolylineLayer.useDynamicUpdate`, introduced in v7.0.1. However, the implementations for these was broken on introduction, and their intended purpose no longer exists. Therefore, these should not have been used in any capacity, and should not affect any projects.

benchmark/crs.dart

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import 'dart:async';
2-
import 'dart:math' as math;
2+
import 'dart:ui';
33

44
import 'package:flutter_map/src/geo/crs.dart';
55
import 'package:latlong2/latlong.dart';
@@ -49,14 +49,14 @@ Future<void> main() async {
4949
return x + y;
5050
}));
5151

52-
results.add(await timedRun('Concrete type: ${crs.code}.latLngToPoint()', () {
52+
results.add(await timedRun('Concrete type: ${crs.code}.latLngToOffset()', () {
5353
double x = 0;
5454
double y = 0;
5555
for (int i = 0; i < N; ++i) {
5656
final latlng = LatLng((i % 90).toDouble(), (i % 180).toDouble());
57-
final p = crs.latLngToPoint(latlng, 1);
58-
x += p.x;
59-
y += p.y;
57+
final p = crs.latLngToOffset(latlng, 1);
58+
x += p.dx;
59+
y += p.dy;
6060
}
6161
return x + y;
6262
}));
@@ -84,9 +84,9 @@ Future<void> main() async {
8484
double y = 0;
8585
for (int i = 0; i < N; ++i) {
8686
final latlng = LatLng((i % 90).toDouble(), (i % 180).toDouble());
87-
final point = crs.latLngToPoint(latlng, 1);
88-
x += point.x;
89-
y += point.y;
87+
final point = crs.latLngToOffset(latlng, 1);
88+
x += point.dx;
89+
y += point.dy;
9090
}
9191
return x + y;
9292
}));
@@ -95,7 +95,7 @@ Future<void> main() async {
9595
double x = 0;
9696
double y = 0;
9797
for (int i = 0; i < N; ++i) {
98-
final latlng = crs.pointToLatLng(math.Point<double>(x, y), 1);
98+
final latlng = crs.offsetToLatLng(Offset(x, y), 1);
9999
x += latlng.longitude;
100100
y += latlng.latitude;
101101
}

benchmark/point_in_polygon.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ Future<void> main() async {
4949
final circle = makeCircle(1000, 1, 0);
5050

5151
results.add(await timedRun('In circle', () {
52-
const point = math.Point(0, 0);
52+
const point = Offset.zero;
5353

5454
bool yesPlease = true;
5555
for (int i = 0; i < N; ++i) {
@@ -61,7 +61,7 @@ Future<void> main() async {
6161
}));
6262

6363
results.add(await timedRun('Not in circle', () {
64-
const point = math.Point(4, 4);
64+
const point = Offset(4, 4);
6565

6666
bool noSir = false;
6767
for (int i = 0; i < N; ++i) {

example/lib/pages/epsg3996_crs.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ class EPSG3996PageState extends State<EPSG3996Page> {
4141
128,
4242
];
4343

44-
final epsg3413Bounds = Bounds<double>(
45-
const Point<double>(-4511619, -4511336),
46-
const Point<double>(4510883, 4510996),
44+
final epsg3413Bounds = Rect.fromPoints(
45+
const Offset(-4511619, -4511336),
46+
const Offset(4510883, 4510996),
4747
);
4848

4949
maxZoom = resolutions.length - 1;

example/lib/pages/latlng_to_screen_point.dart

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import 'dart:math';
2-
31
import 'package:flutter/material.dart';
42
import 'package:flutter/scheduler.dart';
53
import 'package:flutter_map/flutter_map.dart';
@@ -23,7 +21,7 @@ class _LatLngToScreenPointPageState extends State<LatLngToScreenPointPage> {
2321
final mapController = MapController();
2422

2523
LatLng? tappedCoords;
26-
Point<double>? tappedPoint;
24+
Offset? tappedPoint;
2725

2826
@override
2927
void initState() {
@@ -52,8 +50,8 @@ class _LatLngToScreenPointPageState extends State<LatLngToScreenPointPage> {
5250
),
5351
onTap: (_, latLng) {
5452
final point = mapController.camera
55-
.latLngToScreenPoint(tappedCoords = latLng);
56-
setState(() => tappedPoint = Point(point.x, point.y));
53+
.latLngToScreenOffset(tappedCoords = latLng);
54+
setState(() => tappedPoint = Offset(point.dx, point.dy));
5755
},
5856
),
5957
children: [
@@ -77,8 +75,8 @@ class _LatLngToScreenPointPageState extends State<LatLngToScreenPointPage> {
7775
),
7876
if (tappedPoint != null)
7977
Positioned(
80-
left: tappedPoint!.x - 60 / 2,
81-
top: tappedPoint!.y - 60 / 2,
78+
left: tappedPoint!.dx - 60 / 2,
79+
top: tappedPoint!.dy - 60 / 2,
8280
child: const IgnorePointer(
8381
child: Icon(
8482
Icons.center_focus_strong_outlined,

example/lib/pages/screen_point_to_latlng.dart

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import 'dart:math';
2-
31
import 'package:flutter/material.dart';
42
import 'package:flutter_map/flutter_map.dart';
53
import 'package:flutter_map_example/misc/tile_providers.dart';
@@ -95,8 +93,9 @@ class PointToLatlngPage extends State<ScreenPointToLatLngPage> {
9593
);
9694
}
9795

98-
void updatePoint(BuildContext context) => setState(() => latLng =
99-
mapController.camera.pointToLatLng(Point(_getPointX(context), pointY)));
96+
void updatePoint(BuildContext context) =>
97+
setState(() => latLng = mapController.camera
98+
.screenOffsetToLatLng(Offset(_getPointX(context), pointY)));
10099

101100
double _getPointX(BuildContext context) =>
102101
MediaQuery.sizeOf(context).width / 2;

lib/flutter_map.dart

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,3 @@ export 'package:flutter_map/src/map/options/cursor_keyboard_rotation.dart';
5959
export 'package:flutter_map/src/map/options/interaction.dart';
6060
export 'package:flutter_map/src/map/options/options.dart';
6161
export 'package:flutter_map/src/map/widget.dart';
62-
export 'package:flutter_map/src/misc/bounds.dart';
63-
export 'package:flutter_map/src/misc/extensions.dart';

lib/src/geo/crs.dart

Lines changed: 43 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import 'dart:math' as math hide Point;
22
import 'dart:math' show Point;
3+
import 'dart:ui';
34

4-
import 'package:flutter_map/src/misc/bounds.dart';
5-
import 'package:flutter_map/src/misc/simplify.dart';
5+
import 'package:flutter_map/src/misc/extensions.dart';
66
import 'package:latlong2/latlong.dart';
77
import 'package:meta/meta.dart';
88
import 'package:proj4dart/proj4dart.dart' as proj4;
@@ -51,14 +51,14 @@ abstract class Crs {
5151
/// scaled map point.
5252
(double, double) latLngToXY(LatLng latlng, double scale);
5353

54-
/// Similar to [latLngToXY] but converts the XY coordinates to a [Point].
55-
Point<double> latLngToPoint(LatLng latlng, double zoom) {
54+
/// Similar to [latLngToXY] but converts the XY coordinates to an [Offset].
55+
Offset latLngToOffset(LatLng latlng, double zoom) {
5656
final (x, y) = latLngToXY(latlng, scale(zoom));
57-
return Point<double>(x, y);
57+
return Offset(x, y);
5858
}
5959

6060
/// Converts a map point to the sphere coordinate (at a certain zoom).
61-
LatLng pointToLatLng(Point point, double zoom);
61+
LatLng offsetToLatLng(Offset point, double zoom);
6262

6363
/// Zoom to Scale function.
6464
double scale(double zoom) => 256.0 * math.pow(2, zoom);
@@ -67,7 +67,7 @@ abstract class Crs {
6767
double zoom(double scale) => math.log(scale / 256) / math.ln2;
6868

6969
/// Rescales the bounds to a given zoom value.
70-
Bounds<double>? getProjectedBounds(double zoom);
70+
Rect? getProjectedBounds(double zoom);
7171

7272
/// Returns true if we want the world to be replicated, longitude-wise.
7373
bool get replicatesWorldLongitude => false;
@@ -108,26 +108,26 @@ abstract class CrsWithStaticTransformation extends Crs {
108108
}
109109

110110
@override
111-
LatLng pointToLatLng(Point point, double zoom) {
111+
LatLng offsetToLatLng(Offset point, double zoom) {
112112
final (x, y) = _transformation.untransform(
113-
point.x.toDouble(),
114-
point.y.toDouble(),
113+
point.dx,
114+
point.dy,
115115
scale(zoom),
116116
);
117117
return projection.unprojectXY(x, y);
118118
}
119119

120120
@override
121-
Bounds<double>? getProjectedBounds(double zoom) {
121+
Rect? getProjectedBounds(double zoom) {
122122
if (infinite) return null;
123123

124124
final b = projection.bounds!;
125125
final s = scale(zoom);
126-
final (minx, miny) = _transformation.transform(b.min.x, b.min.y, s);
127-
final (maxx, maxy) = _transformation.transform(b.max.x, b.max.y, s);
128-
return Bounds<double>(
129-
Point<double>(minx, miny),
130-
Point<double>(maxx, maxy),
126+
final (minx, miny) = _transformation.transform(b.min.dx, b.min.dy, s);
127+
final (maxx, maxy) = _transformation.transform(b.max.dx, b.max.dy, s);
128+
return Rect.fromPoints(
129+
Offset(minx, miny),
130+
Offset(maxx, maxy),
131131
);
132132
}
133133
}
@@ -171,13 +171,13 @@ class Epsg3857 extends CrsWithStaticTransformation {
171171
);
172172

173173
@override
174-
Point<double> latLngToPoint(LatLng latlng, double zoom) {
174+
Offset latLngToOffset(LatLng latlng, double zoom) {
175175
final (x, y) = _transformation.transform(
176176
SphericalMercator.projectLng(latlng.longitude),
177177
SphericalMercator.projectLat(latlng.latitude),
178178
scale(zoom),
179179
);
180-
return Point<double>(x, y);
180+
return Offset(x, y);
181181
}
182182

183183
@override
@@ -222,7 +222,7 @@ class Proj4Crs extends Crs {
222222
required String code,
223223
required proj4.Projection proj4Projection,
224224
List<Point<double>>? origins,
225-
Bounds<double>? bounds,
225+
Rect? bounds,
226226
List<double>? scales,
227227
List<double>? resolutions,
228228
}) {
@@ -282,30 +282,29 @@ class Proj4Crs extends Crs {
282282

283283
/// Converts a map point to the sphere coordinate (at a certain zoom).
284284
@override
285-
LatLng pointToLatLng(Point point, double zoom) {
285+
LatLng offsetToLatLng(Offset point, double zoom) {
286286
final (x, y) = _getTransformationByZoom(zoom).untransform(
287-
point.x.toDouble(),
288-
point.y.toDouble(),
287+
point.dx,
288+
point.dy,
289289
scale(zoom),
290290
);
291291
return projection.unprojectXY(x, y);
292292
}
293293

294294
/// Rescales the bounds to a given zoom value.
295295
@override
296-
Bounds<double>? getProjectedBounds(double zoom) {
296+
Rect? getProjectedBounds(double zoom) {
297297
if (infinite) return null;
298298

299299
final b = projection.bounds!;
300300
final zoomScale = scale(zoom);
301301

302302
final transformation = _getTransformationByZoom(zoom);
303-
final (minx, miny) = transformation.transform(b.min.x, b.min.y, zoomScale);
304-
final (maxx, maxy) = transformation.transform(b.max.x, b.max.y, zoomScale);
305-
return Bounds<double>(
306-
Point<double>(minx, miny),
307-
Point<double>(maxx, maxy),
308-
);
303+
final (minx, miny) =
304+
transformation.transform(b.min.dx, b.min.dy, zoomScale);
305+
final (maxx, maxy) =
306+
transformation.transform(b.max.dx, b.max.dy, zoomScale);
307+
return Rect.fromPoints(Offset(minx, miny), Offset(maxx, maxy));
309308
}
310309

311310
/// Zoom to Scale function.
@@ -371,18 +370,18 @@ class Proj4Crs extends Crs {
371370
/// Inherit from this class if you want to create or implement your own CRS.
372371
@immutable
373372
abstract class Projection {
374-
/// The [Bounds] for the coordinates of this [Projection].
375-
final Bounds<double>? bounds;
373+
/// The bounds for the coordinates of this [Projection].
374+
final Rect? bounds;
376375

377376
/// Base constructor for the abstract [Projection] class that sets the
378377
/// required fields.
379378
const Projection(this.bounds);
380379

381-
/// Converts a [LatLng] to a coordinates and returns them as [Point] object.
380+
/// Converts a [LatLng] to a coordinates and returns them as an [Offset].
382381
@nonVirtual
383-
Point<double> project(LatLng latlng) {
382+
Offset project(LatLng latlng) {
384383
final (x, y) = projectXY(latlng);
385-
return Point<double>(x, y);
384+
return Offset(x, y);
386385
}
387386

388387
/// Converts a [LatLng] to geometry coordinates.
@@ -418,10 +417,10 @@ abstract class Projection {
418417
/// longitudes -179 and 179 to be projected each on one side.
419418
/// [referencePoint] is used for polygon holes: we want the holes to be
420419
/// displayed close to the polygon, not on the other side of the world.
421-
List<DoublePoint> projectList(List<LatLng> points, {LatLng? referencePoint}) {
420+
List<Offset> projectList(List<LatLng> points, {LatLng? referencePoint}) {
422421
late double previousX;
423422
final worldWidth = getWorldWidth();
424-
return List<DoublePoint>.generate(
423+
return List<Offset>.generate(
425424
points.length,
426425
(j) {
427426
if (j == 0 && referencePoint != null) {
@@ -436,18 +435,15 @@ abstract class Projection {
436435
}
437436
}
438437
previousX = x;
439-
return DoublePoint(x, y);
438+
return Offset(x, y);
440439
},
441440
growable: false,
442441
);
443442
}
444443
}
445444

446445
class _LonLat extends Projection {
447-
static const _bounds = Bounds<double>.unsafe(
448-
Point<double>(-180, -90),
449-
Point<double>(180, 90),
450-
);
446+
static const _bounds = Rect.fromLTRB(-180, -90, 180, 90);
451447

452448
const _LonLat() : super(_bounds);
453449

@@ -472,9 +468,11 @@ class SphericalMercator extends Projection {
472468
static const double _boundsD = r * math.pi;
473469

474470
/// The constant Bounds of the [SphericalMercator] projection.
475-
static const Bounds<double> _bounds = Bounds<double>.unsafe(
476-
Point<double>(-_boundsD, -_boundsD),
477-
Point<double>(_boundsD, _boundsD),
471+
static const Rect _bounds = Rect.fromLTRB(
472+
-_boundsD,
473+
-_boundsD,
474+
_boundsD,
475+
_boundsD,
478476
);
479477

480478
/// Constant constructor for the [SphericalMercator] projection.
@@ -518,7 +516,7 @@ class _Proj4Projection extends Projection {
518516

519517
_Proj4Projection({
520518
required this.proj4Projection,
521-
required Bounds<double>? bounds,
519+
required Rect? bounds,
522520
}) : epsg4326 = proj4.Projection.WGS84,
523521
super(bounds);
524522

0 commit comments

Comments
 (0)