Skip to content

Commit 46989e2

Browse files
committed
Enhance tile row and column calculation routines with tile origin parameter.
1 parent d9b5aba commit 46989e2

2 files changed

Lines changed: 31 additions & 23 deletions

File tree

worldwind/src/main/java/gov/nasa/worldwind/layer/mercator/MercatorImageTile.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import java.util.Collection;
66

7+
import gov.nasa.worldwind.geom.Location;
78
import gov.nasa.worldwind.render.ImageSource;
89
import gov.nasa.worldwind.render.ImageTile;
910
import gov.nasa.worldwind.util.Level;
@@ -51,19 +52,20 @@ static void assembleMercatorTilesForLevel(Level level, TileFactory tileFactory,
5152

5253
// NOTE LevelSet.sector is final Sector attribute and thus can not be cast to MercatorSector!
5354
MercatorSector sector = MercatorSector.fromSector(level.parent.sector);
55+
Location tileOrigin = level.parent.tileOrigin;
5456
double dLat = level.tileDelta / 2;
5557
double dLon = level.tileDelta;
5658

57-
int firstRow = Tile.computeRow(dLat, sector.minLatitude());
58-
int lastRow = Tile.computeLastRow(dLat, sector.maxLatitude());
59-
int firstCol = Tile.computeColumn(dLon, sector.minLongitude());
60-
int lastCol = Tile.computeLastColumn(dLon, sector.maxLongitude());
59+
int firstRow = Tile.computeRow(dLat, sector.minLatitude(), tileOrigin.latitude);
60+
int lastRow = Tile.computeLastRow(dLat, sector.maxLatitude(), tileOrigin.latitude);
61+
int firstCol = Tile.computeColumn(dLon, sector.minLongitude(), tileOrigin.longitude);
62+
int lastCol = Tile.computeLastColumn(dLon, sector.maxLongitude(), tileOrigin.longitude);
6163

6264
double deltaLat = dLat / 90;
6365
double d1 = sector.minLatPercent() + deltaLat * firstRow;
6466
for (int row = firstRow; row <= lastRow; row++) {
6567
double d2 = d1 + deltaLat;
66-
double t1 = sector.minLongitude() + (firstCol * dLon);
68+
double t1 = tileOrigin.longitude + (firstCol * dLon);
6769
for (int col = firstCol; col <= lastCol; col++) {
6870
double t2;
6971
t2 = t1 + dLon;

worldwind/src/main/java/gov/nasa/worldwind/util/Tile.java

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import gov.nasa.worldwind.WorldWind;
1414
import gov.nasa.worldwind.geom.BoundingBox;
1515
import gov.nasa.worldwind.geom.Frustum;
16+
import gov.nasa.worldwind.geom.Location;
1617
import gov.nasa.worldwind.geom.Sector;
1718
import gov.nasa.worldwind.geom.Vec3;
1819
import gov.nasa.worldwind.render.RenderContext;
@@ -116,13 +117,14 @@ public Tile(Sector sector, Level level, int row, int column) {
116117
*
117118
* @param tileDelta the level's tile delta in degrees
118119
* @param latitude the tile's minimum latitude in degrees
120+
* @param origin the origin of the grid
119121
*
120122
* @return the computed row number
121123
*/
122-
public static int computeRow(double tileDelta, double latitude) {
123-
int row = (int) Math.floor((latitude + 90) / tileDelta);
124+
public static int computeRow(double tileDelta, double latitude, double origin) {
125+
int row = (int) Math.floor((latitude - origin) / tileDelta);
124126

125-
if (latitude == 90) {
127+
if (latitude - origin == 180) {
126128
row -= 1; // if latitude is at the end of the grid, subtract 1 from the computed row to return the last row
127129
}
128130

@@ -134,13 +136,14 @@ public static int computeRow(double tileDelta, double latitude) {
134136
*
135137
* @param tileDelta the level's tile delta in degrees
136138
* @param longitude the tile's minimum longitude in degrees
139+
* @param origin the origin of the grid
137140
*
138141
* @return The computed column number
139142
*/
140-
public static int computeColumn(double tileDelta, double longitude) {
141-
int col = (int) Math.floor((longitude + 180) / tileDelta);
143+
public static int computeColumn(double tileDelta, double longitude, double origin) {
144+
int col = (int) Math.floor((longitude - origin) / tileDelta);
142145

143-
if (longitude == 180) {
146+
if (longitude - origin == 360) {
144147
col -= 1; // if longitude is at the end of the grid, subtract 1 from the computed column to return the last column
145148
}
146149

@@ -152,13 +155,14 @@ public static int computeColumn(double tileDelta, double longitude) {
152155
*
153156
* @param tileDelta the level's tile delta in degrees
154157
* @param maxLatitude the tile's maximum latitude in degrees
158+
* @param origin the origin of the grid
155159
*
156160
* @return the computed row number
157161
*/
158-
public static int computeLastRow(double tileDelta, double maxLatitude) {
159-
int row = (int) Math.ceil((maxLatitude + 90) / tileDelta - 1);
162+
public static int computeLastRow(double tileDelta, double maxLatitude, double origin) {
163+
int row = (int) Math.ceil((maxLatitude - origin) / tileDelta - 1);
160164

161-
if (maxLatitude + 90 < tileDelta) {
165+
if (maxLatitude - origin < tileDelta) {
162166
row = 0; // if max latitude is in the first row, set the max row to 0
163167
}
164168

@@ -170,13 +174,14 @@ public static int computeLastRow(double tileDelta, double maxLatitude) {
170174
*
171175
* @param tileDelta the level's tile delta in degrees
172176
* @param maxLongitude the tile's maximum longitude in degrees
177+
* @param origin the origin of the grid
173178
*
174179
* @return The computed column number
175180
*/
176-
public static int computeLastColumn(double tileDelta, double maxLongitude) {
177-
int col = (int) Math.ceil((maxLongitude + 180) / tileDelta - 1);
181+
public static int computeLastColumn(double tileDelta, double maxLongitude, double origin) {
182+
int col = (int) Math.ceil((maxLongitude - origin) / tileDelta - 1);
178183

179-
if (maxLongitude + 180 < tileDelta) {
184+
if (maxLongitude - origin < tileDelta) {
180185
col = 0; // if max longitude is in the first column, set the max column to 0
181186
}
182187

@@ -211,15 +216,16 @@ public static Collection<Tile> assembleTilesForLevel(Level level, TileFactory ti
211216
}
212217

213218
Sector sector = level.parent.sector;
219+
Location tileOrigin = level.parent.tileOrigin;
214220
double tileDelta = level.tileDelta;
215221

216-
int firstRow = Tile.computeRow(tileDelta, sector.minLatitude());
217-
int lastRow = Tile.computeLastRow(tileDelta, sector.maxLatitude());
218-
int firstCol = Tile.computeColumn(tileDelta, sector.minLongitude());
219-
int lastCol = Tile.computeLastColumn(tileDelta, sector.maxLongitude());
222+
int firstRow = Tile.computeRow(tileDelta, sector.minLatitude(), tileOrigin.latitude);
223+
int lastRow = Tile.computeLastRow(tileDelta, sector.maxLatitude(), tileOrigin.latitude);
224+
int firstCol = Tile.computeColumn(tileDelta, sector.minLongitude(), tileOrigin.longitude);
225+
int lastCol = Tile.computeLastColumn(tileDelta, sector.maxLongitude(), tileOrigin.longitude);
220226

221-
double firstRowLat = -90 + firstRow * tileDelta;
222-
double firstRowLon = -180 + firstCol * tileDelta;
227+
double firstRowLat = tileOrigin.latitude + firstRow * tileDelta;
228+
double firstRowLon = tileOrigin.longitude + firstCol * tileDelta;
223229
double lat = firstRowLat;
224230
double lon;
225231

0 commit comments

Comments
 (0)