Skip to content

Commit c229503

Browse files
committed
Merge pull request #3 from ngageoint/develop
web mercator precision in comparisons, get pixel size methods, get we…
2 parents 2bee4fe + ab21428 commit c229503

File tree

2 files changed

+68
-15
lines changed

2 files changed

+68
-15
lines changed

src/main/java/mil/nga/geopackage/projection/ProjectionConstants.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,9 @@ public class ProjectionConstants {
4242
*/
4343
public static double WEB_MERCATOR_HALF_WORLD_WIDTH = 20037508.342789244;
4444

45+
/**
46+
* Web mercator precision
47+
*/
48+
public static double WEB_MERCATOR_PRECISION = 0.0000000001;
49+
4550
}

src/main/java/mil/nga/geopackage/tiles/TileBoundingBoxUtils.java

Lines changed: 63 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -352,19 +352,13 @@ public static TileGrid getTileGrid(BoundingBox webMercatorBoundingBox,
352352
int minX = (int) ((webMercatorBoundingBox.getMinLongitude() + ProjectionConstants.WEB_MERCATOR_HALF_WORLD_WIDTH) / tileSize);
353353
double tempMaxX = (webMercatorBoundingBox.getMaxLongitude() + ProjectionConstants.WEB_MERCATOR_HALF_WORLD_WIDTH)
354354
/ tileSize;
355-
int maxX = (int) tempMaxX;
356-
if (tempMaxX % 1 == 0) {
357-
maxX--;
358-
}
355+
int maxX = (int) (tempMaxX - ProjectionConstants.WEB_MERCATOR_PRECISION);
359356
maxX = Math.min(maxX, tilesPerSide - 1);
360357

361358
int minY = (int) (((webMercatorBoundingBox.getMaxLatitude() - ProjectionConstants.WEB_MERCATOR_HALF_WORLD_WIDTH) * -1) / tileSize);
362359
double tempMaxY = ((webMercatorBoundingBox.getMinLatitude() - ProjectionConstants.WEB_MERCATOR_HALF_WORLD_WIDTH) * -1)
363360
/ tileSize;
364-
int maxY = (int) tempMaxY;
365-
if (tempMaxY % 1 == 0) {
366-
maxY--;
367-
}
361+
int maxY = (int) (tempMaxY - ProjectionConstants.WEB_MERCATOR_PRECISION);
368362
maxY = Math.min(maxY, tilesPerSide - 1);
369363

370364
TileGrid grid = new TileGrid(minX, maxX, minY, maxY);
@@ -444,17 +438,18 @@ public static int tilesPerSide(int zoom) {
444438
}
445439

446440
/**
447-
* Get the standard y tile location as TMS
441+
* Get the standard y tile location as TMS or a TMS y location as standard
442+
*
448443
* @param zoom
449444
* @param y
450445
* @return
451446
*/
452-
public static int getYAsTMS(int zoom, int y){
447+
public static int getYAsOppositeTileFormat(int zoom, int y) {
453448
int tilesPerSide = tilesPerSide(zoom);
454-
int tmsY = tilesPerSide - y - 1;
455-
return tmsY;
449+
int oppositeY = tilesPerSide - y - 1;
450+
return oppositeY;
456451
}
457-
452+
458453
/**
459454
* Get the zoom level from the tiles per side
460455
*
@@ -587,11 +582,31 @@ public static BoundingBox getWebMercatorBoundingBox(
587582
BoundingBox webMercatorTotalBox, TileMatrix tileMatrix,
588583
long tileColumn, long tileRow) {
589584

585+
return getWebMercatorBoundingBox(webMercatorTotalBox,
586+
tileMatrix.getMatrixWidth(), tileMatrix.getMatrixHeight(),
587+
tileColumn, tileRow);
588+
}
589+
590+
/**
591+
* Get the web mercator bounding box of the Tile Row from the Tile Matrix
592+
* zoom level
593+
*
594+
* @param webMercatorTotalBox
595+
* @param matrixWidth
596+
* @param matrixHeight
597+
* @param tileColumn
598+
* @param tileRow
599+
* @return
600+
*/
601+
public static BoundingBox getWebMercatorBoundingBox(
602+
BoundingBox webMercatorTotalBox, long tileMatrixWidth,
603+
long tileMatrixHeight, long tileColumn, long tileRow) {
604+
590605
// Get the tile width
591606
double matrixMinX = webMercatorTotalBox.getMinLongitude();
592607
double matrixMaxX = webMercatorTotalBox.getMaxLongitude();
593608
double matrixWidth = matrixMaxX - matrixMinX;
594-
double tileWidth = matrixWidth / tileMatrix.getMatrixWidth();
609+
double tileWidth = matrixWidth / tileMatrixWidth;
595610

596611
// Find the longitude range
597612
double minLon = matrixMinX + (tileWidth * tileColumn);
@@ -601,7 +616,7 @@ public static BoundingBox getWebMercatorBoundingBox(
601616
double matrixMinY = webMercatorTotalBox.getMinLatitude();
602617
double matrixMaxY = webMercatorTotalBox.getMaxLatitude();
603618
double matrixHeight = matrixMaxY - matrixMinY;
604-
double tileHeight = matrixHeight / tileMatrix.getMatrixHeight();
619+
double tileHeight = matrixHeight / tileMatrixHeight;
605620

606621
// Find the latitude range
607622
double maxLat = matrixMaxY - (tileHeight * tileRow);
@@ -636,4 +651,37 @@ public static int getZoomLevel(BoundingBox webMercatorBoundingBox) {
636651

637652
return zoom;
638653
}
654+
655+
/**
656+
* Get the pixel x size for the bounding box with matrix width and tile
657+
* width
658+
*
659+
* @param webMercatorBoundingBox
660+
* @param matrixWidth
661+
* @param tileWidth
662+
* @return
663+
*/
664+
public static double getPixelXSize(BoundingBox webMercatorBoundingBox,
665+
long matrixWidth, int tileWidth) {
666+
double pixelXSize = (webMercatorBoundingBox.getMaxLongitude() - webMercatorBoundingBox
667+
.getMinLongitude()) / matrixWidth / tileWidth;
668+
return pixelXSize;
669+
}
670+
671+
/**
672+
* Get the pixel y size for the bounding box with matrix height and tile
673+
* height
674+
*
675+
* @param webMercatorBoundingBox
676+
* @param matrixHeight
677+
* @param tileHeight
678+
* @return
679+
*/
680+
public static double getPixelYSize(BoundingBox webMercatorBoundingBox,
681+
long matrixHeight, int tileHeight) {
682+
double pixelYSize = (webMercatorBoundingBox.getMaxLatitude() - webMercatorBoundingBox
683+
.getMinLatitude()) / matrixHeight / tileHeight;
684+
return pixelYSize;
685+
}
686+
639687
}

0 commit comments

Comments
 (0)