Skip to content

Commit dc0f37f

Browse files
committed
Fix : Wrong crop value on small output size
1 parent d0c1dba commit dc0f37f

File tree

1 file changed

+36
-9
lines changed

1 file changed

+36
-9
lines changed

src/MapData.php

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -163,15 +163,42 @@ public function __construct(LatLng $centerMap, int $zoom, XY $outputSize)
163163
$x = static::lngToXTile($centerMap->getLng(), $zoom);
164164
$y = static::latToYTile($centerMap->getLat(), $zoom);
165165

166-
$this->tileTopLeft = new XY($x - \ceil($startX / 256), $y - \ceil($startY / 256));
167-
$this->tileBottomRight = new XY($x + \floor(($outputSize->getX() - $startX) / 256), $y + \floor(($outputSize->getY() - $startY) / 256));
168-
$this->mapCropTopLeft = new XY(256 - $startX % 256, 256 - $startY % 256);
169-
$this->mapCropBottomRight = new XY(($outputSize->getX() - $startX) % 256, ($outputSize->getY() - $startY) % 256);
170-
171-
$this->latLngTopLeft = new LatLng(static::tilePxToLat($this->mapCropTopLeft->getY(), $this->tileTopLeft->getY(), $zoom), static::tilePxToLng($this->mapCropTopLeft->getX(), $this->tileTopLeft->getX(), $zoom));
172-
$this->latLngTopRight = new LatLng(static::tilePxToLat($this->mapCropTopLeft->getY(), $this->tileTopLeft->getY(), $zoom), static::tilePxToLng($this->mapCropBottomRight->getX(), $this->tileBottomRight->getX(), $zoom));
173-
$this->latLngBottomLeft = new LatLng(static::tilePxToLat($this->mapCropBottomRight->getY(), $this->tileBottomRight->getY(), $zoom), static::tilePxToLng($this->mapCropTopLeft->getX(), $this->tileTopLeft->getX(), $zoom));
174-
$this->latLngBottomRight = new LatLng(static::tilePxToLat($this->mapCropBottomRight->getY(), $this->tileBottomRight->getY(), $zoom), static::tilePxToLng($this->mapCropBottomRight->getX(), $this->tileBottomRight->getX(), $zoom));
166+
$rightSize = $outputSize->getX() - $startX;
167+
$bottomSize = $outputSize->getY() - $startY;
168+
169+
$this->mapCropTopLeft = new XY(
170+
$startX < 0 ? \abs($startX) : ($startX % 256 == 0 ? 0 : 256 - $startX % 256),
171+
$startY < 0 ? \abs($startY) : ($startY % 256 == 0 ? 0 : 256 - $startY % 256)
172+
);
173+
$this->mapCropBottomRight = new XY(
174+
($rightSize % 256 == 0 ? 0 : 256 - $rightSize % 256),
175+
($bottomSize % 256 == 0 ? 0 : 256 - $bottomSize % 256)
176+
);
177+
$this->tileTopLeft = new XY(
178+
$x - \ceil($startX / 256),
179+
$y - \ceil($startY / 256)
180+
);
181+
$this->tileBottomRight = new XY(
182+
$x - 1 + \ceil($rightSize / 256),
183+
$y - 1 + \ceil($bottomSize / 256)
184+
);
185+
186+
$this->latLngTopLeft = new LatLng(
187+
static::tilePxToLat($this->mapCropTopLeft->getY(), $this->tileTopLeft->getY(), $zoom),
188+
static::tilePxToLng($this->mapCropTopLeft->getX(), $this->tileTopLeft->getX(), $zoom)
189+
);
190+
$this->latLngTopRight = new LatLng(
191+
static::tilePxToLat($this->mapCropTopLeft->getY(), $this->tileTopLeft->getY(), $zoom),
192+
static::tilePxToLng($this->mapCropBottomRight->getX(), $this->tileBottomRight->getX(), $zoom)
193+
);
194+
$this->latLngBottomLeft = new LatLng(
195+
static::tilePxToLat($this->mapCropBottomRight->getY(), $this->tileBottomRight->getY(), $zoom),
196+
static::tilePxToLng($this->mapCropTopLeft->getX(), $this->tileTopLeft->getX(), $zoom)
197+
);
198+
$this->latLngBottomRight = new LatLng(
199+
static::tilePxToLat($this->mapCropBottomRight->getY(), $this->tileBottomRight->getY(), $zoom),
200+
static::tilePxToLng($this->mapCropBottomRight->getX(), $this->tileBottomRight->getX(), $zoom)
201+
);
175202
}
176203

177204

0 commit comments

Comments
 (0)