navibyte / geospatial

Geospatial data structures, tools and utilities for Dart and Flutter.
Other
52 stars 5 forks source link

FR: GeoBox.center2D #157

Closed tomquas closed 1 year ago

tomquas commented 1 year ago

hi, first: thank you very much for this great addition to the dart world.

i am mapping tile coordinates from one zoom level to another like this:

    const quad = WebMercatorQuad.epsg3857();
    const destZoom = 7;

    // map current tile to the one with destination zoom level
    final corner = quad.tileToBounds(Scalable2i(
      zoom: tile.coords.z.floor(),
      x: tile.coords.x.floor(),
      y: tile.coords.y.floor())).corners2D.first;
    final destTile = quad.positionToTile(corner, zoom: destZoom);

now if instead i could say .center2D instead of .corners2D.first my subequent calculations would be easier.

should Box or GeoBox get a .center2d method?

navispatial commented 1 year ago

Thanks for a good proposal!

Box interface (and GeoBox + ProjBox implementations) should have method to calculate center point as suggested.

Related to this use case there is also the following method (along with zoomIn and zoomOut) in Scalable2i:

  @override
  Scalable2i zoomTo(int zoom) {
    assert(zoom >= 0, 'Zoom must be >= 0');
    if (this.zoom == zoom) {
      return this;
    }
    final shift = zoom - this.zoom;
    return shift > 0
        ? Scalable2i(
            zoom: zoom,
            x: x << shift,
            y: y << shift,
          )
        : Scalable2i(
            zoom: zoom,
            x: x >> shift.abs(),
            y: y >> shift.abs(),
          );
  }

A sample:

  const tile = Scalable2i(zoom: 2, x: 1, y: 1);
  print(tile.zoomTo(3)); // zoom=3 x=2 y=2
  print(tile.zoomTo(4)); // zoom=4 x=4 y=4

However even if this returns a tile in a zoomed level that is inside the tile in the original zoom, the returned tile is not in center of original tile (as maybe needed).

For a tile (zoom=2 x=1 y=1) and target zoom=4, this gives (zoom=4 x=4 y=4) even if the range of target tiles inside the source tile would be (zoom=4 x=4..7 y=4..7).

So yes, either Box could have that center point method and it would have other use cases too.

Or for this use case geographic tile matrix sets new methods like drafted in the issue #158.

Not related to this use case, but similar proposal is also in issue #135.

tomquas commented 1 year ago

oh cool, thanks for the hint; i wasn't aware of zoomTo. an alignment parameter as specified in #158 sounds like the way to solve this.

navispatial commented 1 year ago

New geobase version 0.4.0 started with prelese 0.4.0-dev.0 implementing #158.

navispatial commented 1 year ago

Implemented in version 0.4.0.