Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: migrate Point<double> to Offset internally #1996

Merged
merged 54 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from 44 commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
e1a894b
initial commit, started work, going well
mootw Dec 2, 2024
2ab131b
more insane progress, still going well
mootw Dec 2, 2024
c918f08
compiler happy
mootw Dec 2, 2024
bd99ca1
deprecate notices, only really needed because of bounds and tilecoord…
mootw Dec 3, 2024
f3c2132
this definitely touches an externally accessible API:
mootw Dec 3, 2024
d726a5e
dart format
mootw Dec 3, 2024
54127a9
remove stuff
mootw Dec 3, 2024
36d83a4
more stuffffffffff
mootw Dec 3, 2024
ac4f22c
oops
mootw Dec 3, 2024
5e29da1
fix tests
mootw Dec 3, 2024
55641a2
Merge branch 'master' into convert-to-offset
mootw Dec 3, 2024
403c431
use double.negativeInfinity no Offset.negitiveInfinity exists
mootw Dec 3, 2024
1dcb372
rename pointToLatLng to offsetToLatLng
mootw Dec 3, 2024
e443272
remove unused duplicate function
mootw Dec 3, 2024
6948c2e
remove todo
mootw Dec 3, 2024
5beb77a
Update lib/src/map/camera/camera.dart
mootw Dec 3, 2024
ca77334
format change
mootw Dec 3, 2024
af5b36b
replace helper function
mootw Dec 3, 2024
19ab34b
start migration guide
mootw Dec 3, 2024
87f123b
mark as internal
mootw Dec 3, 2024
877bcca
Update lib/src/layer/shared/layer_interactivity/internal_hit_detectab…
mootw Dec 3, 2024
c3d01eb
rename
mootw Dec 5, 2024
a3b61e0
re-phrase things
mootw Dec 5, 2024
11c593d
rotateAroundPoint fix
mootw Dec 5, 2024
c74dc14
Merge branch 'master' into convert-to-offset
mootw Dec 5, 2024
7797c41
convert doublepoint to offset
mootw Dec 5, 2024
f64f135
dart format
mootw Dec 5, 2024
14712ad
fix visible scope lol
mootw Dec 5, 2024
7ab7a91
Update flutter_map.dart
mootw Dec 5, 2024
4399137
Update internal_hit_detectable.dart
mootw Dec 5, 2024
c164196
fix straggling Point
mootw Dec 5, 2024
357bada
convert to use size
mootw Dec 5, 2024
4e720dc
migrate easy Bounds<double> to Rect
mootw Dec 5, 2024
de37184
dart format .
mootw Dec 5, 2024
b6d2d95
migrate most methods that use Bounds
mootw Dec 5, 2024
17d1e07
dart format .
mootw Dec 5, 2024
74e5ee4
Update CHANGELOG.md
mootw Dec 5, 2024
b280465
more complete migration of Rect
mootw Dec 5, 2024
a2d88a1
dart format
mootw Dec 5, 2024
57deeab
convert Bounds to IntegerBounds and made internal
mootw Dec 5, 2024
237d5f6
fix lints
mootw Dec 5, 2024
b2ed4f2
fix test
mootw Dec 5, 2024
0a8134b
fix bug introduced in commit b6d2d9557d3d957d674429224ce6733c242cb858
mootw Dec 5, 2024
5e177e4
fix lint
mootw Dec 5, 2024
72f2fd2
rename method
mootw Dec 5, 2024
8603d71
fix some nits
mootw Dec 5, 2024
d506b44
Update lib/src/geo/crs.dart
mootw Dec 5, 2024
7f82a71
Update lib/src/geo/crs.dart
mootw Dec 5, 2024
209eab7
Update lib/src/geo/crs.dart
mootw Dec 5, 2024
bdc1651
dart format
mootw Dec 5, 2024
fc3e527
rename project method
mootw Dec 5, 2024
2d9a42d
dart format .
mootw Dec 5, 2024
fa244a7
fix unused import
mootw Dec 5, 2024
2a13210
Merge branch 'master' into convert-to-offset
JaffaKetchup Dec 5, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,20 @@ Please consider [donating](https://docs.fleaflet.dev/supporters#support-us) or [

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.

## [8.0.0] - 2024/07/02

Migration from `Point` class internally, but this also affects the external API.

Migration Guide:
- any methods that previously required `Point<double>` now require `Offset`, `Size`, or `Rect` as return values and parameters
- `pointToLatLng` -> `offsetToLatLng`
- `PointExtension` and `OffsetToPointExtension` marked as internal
- `MapController.rotateAroundPoint` now only accepts an Offset


Contains the following user-affecting bug fixes:


## [7.0.2] - 2024/07/02

> 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.
Expand Down
18 changes: 9 additions & 9 deletions benchmark/crs.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'dart:async';
import 'dart:math' as math;
import 'dart:ui';

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

results.add(await timedRun('Concrete type: ${crs.code}.latLngToPoint()', () {
results.add(await timedRun('Concrete type: ${crs.code}.latLngToOffset()', () {
double x = 0;
double y = 0;
for (int i = 0; i < N; ++i) {
final latlng = LatLng((i % 90).toDouble(), (i % 180).toDouble());
final p = crs.latLngToPoint(latlng, 1);
x += p.x;
y += p.y;
final p = crs.latLngToOffset(latlng, 1);
x += p.dx;
y += p.dy;
}
return x + y;
}));
Expand Down Expand Up @@ -84,9 +84,9 @@ Future<void> main() async {
double y = 0;
for (int i = 0; i < N; ++i) {
final latlng = LatLng((i % 90).toDouble(), (i % 180).toDouble());
final point = crs.latLngToPoint(latlng, 1);
x += point.x;
y += point.y;
final point = crs.latLngToOffset(latlng, 1);
x += point.dx;
y += point.dy;
}
return x + y;
}));
Expand All @@ -95,7 +95,7 @@ Future<void> main() async {
double x = 0;
double y = 0;
for (int i = 0; i < N; ++i) {
final latlng = crs.pointToLatLng(math.Point<double>(x, y), 1);
final latlng = crs.offsetToLatLng(Offset(x, y), 1);
x += latlng.longitude;
y += latlng.latitude;
}
Expand Down
4 changes: 2 additions & 2 deletions benchmark/point_in_polygon.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ Future<void> main() async {
final circle = makeCircle(1000, 1, 0);

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

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

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

bool noSir = false;
for (int i = 0; i < N; ++i) {
Expand Down
6 changes: 3 additions & 3 deletions example/lib/pages/epsg3996_crs.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ class EPSG3996PageState extends State<EPSG3996Page> {
128,
];

final epsg3413Bounds = Bounds<double>(
const Point<double>(-4511619, -4511336),
const Point<double>(4510883, 4510996),
final epsg3413Bounds = Rect.fromPoints(
const Offset(-4511619, -4511336),
const Offset(4510883, 4510996),
);

maxZoom = resolutions.length - 1;
Expand Down
12 changes: 5 additions & 7 deletions example/lib/pages/latlng_to_screen_point.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import 'dart:math';

import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';
import 'package:flutter_map/flutter_map.dart';
Expand All @@ -23,7 +21,7 @@ class _LatLngToScreenPointPageState extends State<LatLngToScreenPointPage> {
final mapController = MapController();

LatLng? tappedCoords;
Point<double>? tappedPoint;
Offset? tappedPoint;

@override
void initState() {
Expand Down Expand Up @@ -52,8 +50,8 @@ class _LatLngToScreenPointPageState extends State<LatLngToScreenPointPage> {
),
onTap: (_, latLng) {
final point = mapController.camera
.latLngToScreenPoint(tappedCoords = latLng);
setState(() => tappedPoint = Point(point.x, point.y));
.latLngToScreenOffset(tappedCoords = latLng);
setState(() => tappedPoint = Offset(point.dx, point.dy));
},
),
children: [
Expand All @@ -77,8 +75,8 @@ class _LatLngToScreenPointPageState extends State<LatLngToScreenPointPage> {
),
if (tappedPoint != null)
Positioned(
left: tappedPoint!.x - 60 / 2,
top: tappedPoint!.y - 60 / 2,
left: tappedPoint!.dx - 60 / 2,
top: tappedPoint!.dy - 60 / 2,
child: const IgnorePointer(
child: Icon(
Icons.center_focus_strong_outlined,
Expand Down
4 changes: 1 addition & 3 deletions example/lib/pages/screen_point_to_latlng.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import 'dart:math';

import 'package:flutter/material.dart';
import 'package:flutter_map/flutter_map.dart';
import 'package:flutter_map_example/misc/tile_providers.dart';
Expand Down Expand Up @@ -96,7 +94,7 @@ class PointToLatlngPage extends State<ScreenPointToLatLngPage> {
}

void updatePoint(BuildContext context) => setState(() => latLng =
mapController.camera.pointToLatLng(Point(_getPointX(context), pointY)));
mapController.camera.offsetToLatLng(Offset(_getPointX(context), pointY)));

double _getPointX(BuildContext context) =>
MediaQuery.sizeOf(context).width / 2;
Expand Down
2 changes: 0 additions & 2 deletions lib/flutter_map.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,3 @@ export 'package:flutter_map/src/map/options/cursor_keyboard_rotation.dart';
export 'package:flutter_map/src/map/options/interaction.dart';
export 'package:flutter_map/src/map/options/options.dart';
export 'package:flutter_map/src/map/widget.dart';
export 'package:flutter_map/src/misc/bounds.dart';
export 'package:flutter_map/src/misc/extensions.dart';
83 changes: 41 additions & 42 deletions lib/src/geo/crs.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import 'dart:math' as math hide Point;
import 'dart:math' show Point;
import 'dart:ui';

import 'package:flutter_map/src/misc/bounds.dart';
import 'package:flutter_map/src/misc/simplify.dart';
import 'package:flutter_map/src/misc/extensions.dart';
import 'package:latlong2/latlong.dart';
import 'package:meta/meta.dart';
import 'package:proj4dart/proj4dart.dart' as proj4;
Expand Down Expand Up @@ -52,13 +53,13 @@ abstract class Crs {
(double, double) latLngToXY(LatLng latlng, double scale);

/// Similar to [latLngToXY] but converts the XY coordinates to a [Point].
mootw marked this conversation as resolved.
Show resolved Hide resolved
Point<double> latLngToPoint(LatLng latlng, double zoom) {
Offset latLngToOffset(LatLng latlng, double zoom) {
final (x, y) = latLngToXY(latlng, scale(zoom));
return Point<double>(x, y);
return Offset(x, y);
}

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

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

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

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

@override
LatLng pointToLatLng(Point point, double zoom) {
LatLng offsetToLatLng(Offset point, double zoom) {
final (x, y) = _transformation.untransform(
point.x.toDouble(),
point.y.toDouble(),
point.dx,
point.dy,
scale(zoom),
);
return projection.unprojectXY(x, y);
}

@override
Bounds<double>? getProjectedBounds(double zoom) {
Rect? getProjectedBounds(double zoom) {
if (infinite) return null;

final b = projection.bounds!;
final s = scale(zoom);
final (minx, miny) = _transformation.transform(b.min.x, b.min.y, s);
final (maxx, maxy) = _transformation.transform(b.max.x, b.max.y, s);
return Bounds<double>(
Point<double>(minx, miny),
Point<double>(maxx, maxy),
final (minx, miny) = _transformation.transform(b.min.dx, b.min.dy, s);
final (maxx, maxy) = _transformation.transform(b.max.dx, b.max.dy, s);
return Rect.fromPoints(
Offset(minx, miny),
Offset(maxx, maxy),
);
}
}
Expand Down Expand Up @@ -171,13 +172,13 @@ class Epsg3857 extends CrsWithStaticTransformation {
);

@override
Point<double> latLngToPoint(LatLng latlng, double zoom) {
Offset latLngToOffset(LatLng latlng, double zoom) {
final (x, y) = _transformation.transform(
SphericalMercator.projectLng(latlng.longitude),
SphericalMercator.projectLat(latlng.latitude),
scale(zoom),
);
return Point<double>(x, y);
return Offset(x, y);
}

@override
Expand Down Expand Up @@ -222,7 +223,7 @@ class Proj4Crs extends Crs {
required String code,
required proj4.Projection proj4Projection,
List<Point<double>>? origins,
Bounds<double>? bounds,
Rect? bounds,
List<double>? scales,
List<double>? resolutions,
}) {
Expand Down Expand Up @@ -282,30 +283,29 @@ class Proj4Crs extends Crs {

/// Converts a map point to the sphere coordinate (at a certain zoom).
@override
LatLng pointToLatLng(Point point, double zoom) {
LatLng offsetToLatLng(Offset point, double zoom) {
final (x, y) = _getTransformationByZoom(zoom).untransform(
point.x.toDouble(),
point.y.toDouble(),
point.dx,
point.dy,
scale(zoom),
);
return projection.unprojectXY(x, y);
}

/// Rescales the bounds to a given zoom value.
@override
Bounds<double>? getProjectedBounds(double zoom) {
Rect? getProjectedBounds(double zoom) {
if (infinite) return null;

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

final transformation = _getTransformationByZoom(zoom);
final (minx, miny) = transformation.transform(b.min.x, b.min.y, zoomScale);
final (maxx, maxy) = transformation.transform(b.max.x, b.max.y, zoomScale);
return Bounds<double>(
Point<double>(minx, miny),
Point<double>(maxx, maxy),
);
final (minx, miny) =
transformation.transform(b.min.dx, b.min.dy, zoomScale);
final (maxx, maxy) =
transformation.transform(b.max.dx, b.max.dy, zoomScale);
return Rect.fromPoints(Offset(minx, miny), Offset(maxx, maxy));
}

/// Zoom to Scale function.
Expand Down Expand Up @@ -371,18 +371,18 @@ class Proj4Crs extends Crs {
/// Inherit from this class if you want to create or implement your own CRS.
@immutable
abstract class Projection {
/// The [Bounds] for the coordinates of this [Projection].
final Bounds<double>? bounds;
/// The [IntegerBounds] for the coordinates of this [Projection].
mootw marked this conversation as resolved.
Show resolved Hide resolved
final Rect? bounds;

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

/// Converts a [LatLng] to a coordinates and returns them as [Point] object.
mootw marked this conversation as resolved.
Show resolved Hide resolved
@nonVirtual
Point<double> project(LatLng latlng) {
Offset project(LatLng latlng) {
final (x, y) = projectXY(latlng);
return Point<double>(x, y);
return Offset(x, y);
}

/// Converts a [LatLng] to geometry coordinates.
Expand Down Expand Up @@ -418,10 +418,10 @@ abstract class Projection {
/// longitudes -179 and 179 to be projected each on one side.
/// [referencePoint] is used for polygon holes: we want the holes to be
/// displayed close to the polygon, not on the other side of the world.
List<DoublePoint> projectList(List<LatLng> points, {LatLng? referencePoint}) {
List<Offset> projectList(List<LatLng> points, {LatLng? referencePoint}) {
late double previousX;
final worldWidth = getWorldWidth();
return List<DoublePoint>.generate(
return List<Offset>.generate(
points.length,
(j) {
if (j == 0 && referencePoint != null) {
Expand All @@ -436,18 +436,15 @@ abstract class Projection {
}
}
previousX = x;
return DoublePoint(x, y);
return Offset(x, y);
},
growable: false,
);
}
}

class _LonLat extends Projection {
static const _bounds = Bounds<double>.unsafe(
Point<double>(-180, -90),
Point<double>(180, 90),
);
static const _bounds = Rect.fromLTRB(-180, -90, 180, 90);

const _LonLat() : super(_bounds);

Expand All @@ -472,9 +469,11 @@ class SphericalMercator extends Projection {
static const double _boundsD = r * math.pi;

/// The constant Bounds of the [SphericalMercator] projection.
static const Bounds<double> _bounds = Bounds<double>.unsafe(
Point<double>(-_boundsD, -_boundsD),
Point<double>(_boundsD, _boundsD),
static const Rect _bounds = Rect.fromLTRB(
-_boundsD,
-_boundsD,
_boundsD,
_boundsD,
);

/// Constant constructor for the [SphericalMercator] projection.
Expand Down Expand Up @@ -518,7 +517,7 @@ class _Proj4Projection extends Projection {

_Proj4Projection({
required this.proj4Projection,
required Bounds<double>? bounds,
required Rect? bounds,
}) : epsg4326 = proj4.Projection.WGS84,
super(bounds);

Expand Down
Loading
Loading