Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature branch for tiles xy-to-quadkey conversion #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
18 changes: 18 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,24 @@ public NDSCoordinate(long ndsMortonCoordinates) {
latitude = lat;
longitude = lon;
}

/*
* Computes a point along the distance of this and given NDSCoordinate
*
* @param p
* the given coordinate
* @param fraction
* the fraction
*/
public NDSCoordinate fractionBetween(NDSCoordinate p, double fraction) {
assert (0.0 <= fraction) && (fraction <= 1.0);
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 + fraction * (lon - longitude), latitude + fraction * (lat - latitude));
}

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

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

/**
* NDSEnvelope allows to compute an envelope for a set of NDSCoordinates.
*
* No warranties for correctness, use at own risk.
*
* @author Andreas Hessenthaler
* @since 13.02.2020
*/
public class NDSEnvelope {

private static OctagonalEnvelope vividEnvelope = new OctagonalEnvelope();

/**
* NDSEnvelope of a given set of polygon points
*
* @param polygonCoordinates
* the polygonCoordinates
*/
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);
}
}

/**
* NDSEnvelope constructor for OctagonalEnvelope
*
* @param e
* the OctagonalEnvelope e
*/
public NDSEnvelope(OctagonalEnvelope e) {
vividEnvelope = e;
}

/**
* Get the NDSEnvelope
*
* @return
*/
public OctagonalEnvelope getEnvelope() {
return vividEnvelope;
}

/**
* Get the SouthWest corner of the envelope
*
* @return
*/
public NDSCoordinate getSouthWest() {
return new NDSCoordinate(vividEnvelope.getMinX(), vividEnvelope.getMinY());
}

/**
* Get the SouthEast corner of the envelope
*
* @return
*/
public NDSCoordinate getSouthEast() {
return new NDSCoordinate(vividEnvelope.getMaxX(), vividEnvelope.getMinY());
}

/**
* Get the NorthEast corner of the envelope
*
* @return
*/
public NDSCoordinate getNorthEast() {
return new NDSCoordinate(vividEnvelope.getMinX(), vividEnvelope.getMaxY());
}

/**
* Get the NorthWest corner of the envelope
*
* @return
*/
public NDSCoordinate getNorthWest() {
return new NDSCoordinate(vividEnvelope.getMaxX(), vividEnvelope.getMaxY());
}

/**
* Get the master tile for the SouthWest, SouthEast, NorthEast, NorthWest corners for a given number of levels
*
* @param maxLevels
* the maxLevels
* @return masterTileInfo
* the masterTileInfo consisting of the {masterTileLevel, masterTileNumber}
*/
public int[] getMasterTileInfo(int maxLevels) {
beauof marked this conversation as resolved.
Show resolved Hide resolved
NDSCoordinate point0 = getSouthWest();
NDSCoordinate point1 = getSouthEast();
NDSCoordinate point2 = getNorthEast();
NDSCoordinate point3 = getNorthWest();
return getMasterTileInfo(point0, point1, point2, point3, maxLevels);
}

/**
* Get the master tile for a set of four NDSCoordinates for a given number of levels
*
* @param maxLevels
* the maxLevels
* @return masterTileInfo
* the masterTileInfo consisting of the {masterTileLevel, masterTileNumber}
*/
public int[] getMasterTileInfo(NDSCoordinate point0, NDSCoordinate point1, NDSCoordinate point2, NDSCoordinate point3, int maxLevels) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what does the master tile info consist of? what does it give me if i invoke this method?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It returns the {masterTileLevel, masterTileNumber}, i.e. the tile level and number of the tile fully enclosing these points.

int masterTileLevel = -1;
int masterTileNumber = -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 currTileNumber0 = currTile0.getTileNumber();
int currTileNumber1 = currTile1.getTileNumber();
int currTileNumber2 = currTile2.getTileNumber();
int currTileNumber3 = currTile3.getTileNumber();
boolean singleTileNumber = (currTileNumber0 == currTileNumber1) && (currTileNumber0 == currTileNumber2) && (currTileNumber0 == currTileNumber3);
// 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(!singleTileNumber) {
break;
}
// store tile info
masterTileLevel = li;
masterTileNumber = currTileNumber0;
}
// check if valid result
if (masterTileNumber == -1) {
System.out.println(">>>ERROR: Invalid master tile ID.");
System.exit(1);
}
return new int[] {masterTileLevel, masterTileNumber};
}
}
214 changes: 214 additions & 0 deletions src/main/java/de/rondiplomatico/nds/NDSFloodFill.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
package de.rondiplomatico.nds;

import java.awt.Point;
import java.util.LinkedList;
import java.util.Queue;

/**
* Utility class for flood fill algorithm applied to marked NDSTiles deduced from polygon points.
*
* @author Andreas Hessenthaler
* @since 13.02.2020
*/
public class NDSFloodFill {
int[][] ff = null;
int minX = Integer.MAX_VALUE;
int maxX = Integer.MIN_VALUE;
int minY = Integer.MAX_VALUE;
int maxY = Integer.MIN_VALUE;
int dimX = -1;
int dimY = -1;
int bg = 0;
int bound = 1;
int fill = bound;
int startAtX = 0;
int startAtY = 0;

/**
* Constructor
*
* @param f
* the image input to perform flood fill on
* assumes:
* - non-empty array
* - closed polygon without intersections with boundary color 1 and background color 0
* @param a
* the minimum x-index
* @param b
* the maximum x-index
* @param c
* the minimum y-index
* @param d
* the maximum y-index
*/
public NDSFloodFill(int[][] f, int a, int b, int c, int d) {
ff = f;
minX = a;
maxX = b;
minY = c;
maxY = d;
dimX = maxX - minX + 1;
dimY = maxY - minY + 1;
setStartingPoint();
}

/*
* Iterative four-way flood fill algorithm
*
*/
public void iterativeFloodFill() {
Queue<Point> q = new LinkedList<Point>();
q.add(new Point(startAtX, startAtY));
while (!q.isEmpty()) {
Point p = q.remove();
if ((p.x < 0) || (p.y < 0) || (p.x > dimX-1 || (p.y > dimY-1))) {
continue;
}
if (ff[p.x][p.y] == bg) {
ff[p.x][p.y] = fill;
q.add(new Point(p.x+1, p.y )); // right
q.add(new Point(p.x, p.y+1)); // top
q.add(new Point(p.x-1, p.y )); // left
q.add(new Point(p.x, p.y-1)); // bottom
}
}
}

/*
* Safety net for filling left over lines after applying flood fill algorithm
*
*/
public void fillHoles() {
// first, find vertical lines
boolean vline = false;
boolean vlineEnded = false;
int currStartY = -1;
int currStopY = -1;
for (int col = 1; col < dimY-1; col++) {
vline = false;
vlineEnded = false;
for (int row = 1; row < dimX-1; row++) {
// minimum requirement: left and right neighbors are not background
if ((ff[row][col-1] != bg) && (ff[row][col+1] != bg)) {
if ((ff[row][col] == bg)
&& (ff[row-1][col] != bg)) {
// beginning of vertical line
currStartY = row;
vline = true;
} else if (vline && (ff[row][col] == bg)) {
// vertical line continues
} else if (vline && (ff[row][col] != bg)) {
// end of vertical line
currStopY = row;
vlineEnded = true;
}
} else if (vline && ((ff[row][col+1] != bg) || (ff[row][col-1] != bg)) && (ff[row][col] != bg)) {
// end of horizontal line
currStopY = row;
vlineEnded = true;
} else {
vline = false;
vlineEnded = false;
}
if (vline && vlineEnded) {
// fill line and reset booleans
System.out.println(">>>>>>INFO: Filling vertical line.");
for (int idx = currStartY; idx < currStopY; idx++) {
ff[idx][col] = fill;
}
vline = false;
vlineEnded = false;
}
}
}
// second, find horizontal lines
boolean hline = false;
boolean hlineEnded = false;
for (int row = 1; row < dimX-1; row++) {
hline = false;
hlineEnded = false;
for (int col = 1; col < dimY-1; col++) {
// minimum requirement: top and bottom neighbors are not background
if ((ff[row+1][col] != bg) && (ff[row-1][col] != bg)) {
if ((ff[row][col] == bg)
&& (ff[row][col-1] != bg)) {
// beginning of horizontal line
currStartY = col;
hline = true;
} else if (hline && (ff[row][col] == bg)) {
// vertical line continues
} else if (hline && (ff[row][col] != bg)) {
// end of vertical line
currStopY = col;
hlineEnded = true;
}
} else if (hline && ((ff[row+1][col] != bg) || (ff[row-1][col] != bg)) && (ff[row][col] != bg)) {
// end of vertical line
currStopY = col;
hlineEnded = true;
} else {
hline = false;
hlineEnded = false;
}
if (hline && hlineEnded) {
// fill line and reset booleans
System.out.println(">>>>>>INFO: Filling horizontal line.");
for (int idx = currStartY; idx < currStopY; idx++) {
ff[row][idx] = fill;
}
hline = false;
hlineEnded = false;
}
}
}

return;
}

/*
* Set initial point for flood fill algorithm
*
* @todo need to do more extensive checks if we can always find an initial point if we only check the right half of the box
*/
private void setStartingPoint() {
startAtX = (int) (0.5 * dimX);
startAtY = 0;
// we're starting on background, skip that..
while (ff[startAtX][startAtY] == bg) {
startAtY++;
}
// we're continuing on an edge, skip that..
while (ff[startAtX][startAtY] == bound) {
startAtY++;
}
for (int row = startAtY; row < dimY; row++, startAtY++) {
int rowSum = 0;
for (int col = startAtX; col < dimX; col++) {
if (ff[col][row-1] != ff[col][row]) {
rowSum = rowSum + ff[col][row];
}
}
if ((rowSum % 2) == 1) {
// we've found a point that's inside
break;
}
}
return;
}

/*
* Get initial point for flood fill algorithm
*
*/
public int[] getStartingPoint() {
return new int[] {startAtX, startAtY};
}

/*
* Get flood filled image
*
*/
public int[][] getFloodFill(){
return ff;
}
}
Loading