Skip to content

Commit

Permalink
adding features for converting between tile number, x-/y-coordinate a…
Browse files Browse the repository at this point in the history
…nd quadkey

- add new functionality and a whole bunch of unit tests
- add polygon refinement functionality
- add polygon refinement and polygon to map test
  • Loading branch information
beauof committed Feb 28, 2020
1 parent 1e98765 commit 1a3ca83
Show file tree
Hide file tree
Showing 8 changed files with 1,165 additions and 0 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@
<artifactId>junit</artifactId>
<version>4.1</version>
</dependency>
<dependency>
<groupId>com.vividsolutions</groupId>
<artifactId>jts</artifactId>
<version>1.13</version>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/de/rondiplomatico/nds/NDSBBox.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ public class NDSBBox {
private final int south;
private final int west;

// constructor to get global bounding box in NDS coordinates
public NDSBBox() {
north = 90;
east = 180;
south = -90;
west = -180;
}

/**
*
* Gets the south west corner of the bounding box
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/de/rondiplomatico/nds/NDSCoordinate.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,21 @@ public NDSCoordinate(long ndsMortonCoordinates) {
latitude = lat;
longitude = lon;
}

/*
* Compute midpoint of this and given NDSCoordinate
*
* @param p
* the given coordinate
*/
public NDSCoordinate getMidpoint(NDSCoordinate p) {
double lat = p.toWGS84().getLatitude();
double lon = p.toWGS84().getLongitude();
WGS84Coordinate lonlat = toWGS84();
double longitude = lonlat.getLongitude();
double latitude = lonlat.getLatitude();
return new NDSCoordinate(longitude + 0.5 * (lon - longitude), latitude + 0.5 * (lat - latitude));
}

private void verify(int lon, int lat) {
if (lat < MIN_LATITUDE || MAX_LATITUDE < lat) {
Expand Down
86 changes: 86 additions & 0 deletions src/main/java/de/rondiplomatico/nds/NDSEnvelope.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package de.rondiplomatico.nds;

import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.OctagonalEnvelope;

/**
* Class to get a vividsolutions envelope of coordinates.
*
* No warranties for correctness, use at own risk.
*
* @author Andreas Hessenthaler
* @since 13.02.2020
*/
public class NDSEnvelope {

private static OctagonalEnvelope vividEnvelope = new OctagonalEnvelope();

public NDSEnvelope(double[][] polygonCoordinates) {
for(int i = 0; i < polygonCoordinates.length; i++) {
Coordinate vividCoord = new Coordinate(polygonCoordinates[i][0], polygonCoordinates[i][1]);
vividEnvelope.expandToInclude(vividCoord);
}
}

public NDSEnvelope(OctagonalEnvelope e) {
vividEnvelope = e;
}

public OctagonalEnvelope getEnvelope() {
return vividEnvelope;
}

public NDSCoordinate getSouthWest() {
return new NDSCoordinate(vividEnvelope.getMinX(), vividEnvelope.getMinY());
}

public NDSCoordinate getSouthEast() {
return new NDSCoordinate(vividEnvelope.getMaxX(), vividEnvelope.getMinY());
}

public NDSCoordinate getNorthEast() {
return new NDSCoordinate(vividEnvelope.getMinX(), vividEnvelope.getMaxY());
}

public NDSCoordinate getNorthWest() {
return new NDSCoordinate(vividEnvelope.getMaxX(), vividEnvelope.getMaxY());
}

public int[] getMasterTileInfo(int maxLevels) {
NDSCoordinate point0 = getSouthWest();
NDSCoordinate point1 = getSouthEast();
NDSCoordinate point2 = getNorthEast();
NDSCoordinate point3 = getNorthWest();
return getMasterTileInfo(point0, point1, point2, point3, maxLevels);
}

public int[] getMasterTileInfo(NDSCoordinate point0, NDSCoordinate point1, NDSCoordinate point2, NDSCoordinate point3, int maxLevels) {
int masterTileLevel = -1;
int masterTileID = -1;
for (int li = 0; li < maxLevels; li++) {
NDSTile currTile0 = new NDSTile(li, point0);
NDSTile currTile1 = new NDSTile(li, point1);
NDSTile currTile2 = new NDSTile(li, point2);
NDSTile currTile3 = new NDSTile(li, point3);
int currTileID0 = currTile0.getTileNumber();
int currTileID1 = currTile1.getTileNumber();
int currTileID2 = currTile2.getTileNumber();
int currTileID3 = currTile3.getTileNumber();
boolean singleTileID = (currTileID0 == currTileID1) && (currTileID0 == currTileID2) && (currTileID0 == currTileID3);
// if at least one tile ID is different, we discard the tile IDs and keep our previously detected tile ID (i.e. on the previous level)
if(!singleTileID) {
break;
}
// store tile info
masterTileLevel = li;
masterTileID = currTileID0;
}
// check if valid result
if (masterTileID == -1) {
System.out.println(">>>ERROR: Invalid master tile ID.");
System.exit(1);
}
int[] masterTileInfo = {masterTileLevel, masterTileID};
return masterTileInfo;
}
}
Loading

0 comments on commit 1a3ca83

Please sign in to comment.