Skip to content

Commit

Permalink
fix non-javadoc checkstyle warnings in oshdb-util
Browse files Browse the repository at this point in the history
  • Loading branch information
joker234 committed Nov 3, 2020
1 parent 232beea commit b232f77
Show file tree
Hide file tree
Showing 13 changed files with 118 additions and 79 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package org.heigit.bigspatialdata.oshdb.util;

import org.heigit.bigspatialdata.oshdb.osm.OSMType;

import java.util.Optional;
import org.heigit.bigspatialdata.oshdb.osm.OSMType;

/**
* Names for JDBC-Tables.
Expand Down Expand Up @@ -54,13 +53,14 @@ public String toString() {
}

public String toString(String prefix) {
if(prefix != null && !prefix.trim().isEmpty())
return prefix+"_"+this.toString();
if (prefix != null && !prefix.trim().isEmpty()) {
return prefix + "_" + this.toString();
}
return this.toString();
}

public static Optional<TableNames> forOSMType(OSMType type) {
switch(type) {
switch (type) {
case NODE: return Optional.of(T_NODES);
case WAY: return Optional.of(T_WAYS);
case RELATION: return Optional.of(T_RELATIONS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ public Stream<IterateByTimestampEntry> iterateByTimestamps(GridOSHEntity cell) {
|| !allFullyInside && (
!oshEntity.getBoundingBox().intersects(boundingBox)
|| (isBoundByPolygon && bboxOutsidePolygon.test(oshEntity.getBoundingBox()))
)) {
)) {
// this osh entity doesn't match the prefilter or is fully outside the requested
// area of interest -> skip it
return Stream.empty();
Expand Down Expand Up @@ -312,18 +312,19 @@ public Stream<IterateByTimestampEntry> iterateByTimestamps(GridOSHEntity cell) {
OSHEntities.getByTimestamps(oshEntity, new ArrayList<>(queryTs.keySet()));

List<IterateByTimestampEntry> results = new LinkedList<>();
osmEntityLoop:
for (Map.Entry<OSHDBTimestamp, OSMEntity> entity : osmEntityByTimestamps.entrySet()) {
osmEntityLoop: for (Map.Entry<OSHDBTimestamp, OSMEntity> entity :
osmEntityByTimestamps.entrySet()) {
OSHDBTimestamp timestamp = entity.getKey();
OSMEntity osmEntity = entity.getValue();

if (!osmEntity.isVisible()) {
// skip because this entity is deleted at this timestamp
continue;
}
if (osmEntity instanceof OSMWay && ((OSMWay)osmEntity).getRefs().length == 0
if (osmEntity instanceof OSMWay && (
(OSMWay)osmEntity).getRefs().length == 0
|| osmEntity instanceof OSMRelation
&& ((OSMRelation)osmEntity).getMembers().length == 0) {
&& ((OSMRelation)osmEntity).getMembers().length == 0) {
// skip way/relation with zero nodes/members
continue;
}
Expand Down Expand Up @@ -556,7 +557,7 @@ public Stream<IterateAllEntry> iterateByContribution(GridOSHEntity cell) {
if (!oshEntityPreFilter.test(oshEntity)
|| !allFullyInside && (
!oshEntity.getBoundingBox().intersects(boundingBox)
|| (isBoundByPolygon && bboxOutsidePolygon.test(oshEntity.getBoundingBox()))
|| (isBoundByPolygon && bboxOutsidePolygon.test(oshEntity.getBoundingBox()))
)) {
// this osh entity doesn't match the prefilter or is fully outside the requested
// area of interest -> skip it
Expand All @@ -569,7 +570,7 @@ public Stream<IterateAllEntry> iterateByContribution(GridOSHEntity cell) {

boolean fullyInside = allFullyInside || (
oshEntity.getBoundingBox().isInside(boundingBox)
&& (!isBoundByPolygon || bboxInPolygon.test(oshEntity.getBoundingBox()))
&& (!isBoundByPolygon || bboxInPolygon.test(oshEntity.getBoundingBox()))
);

Map<OSHDBTimestamp, Long> changesetTs = OSHEntities.getChangesetTimestamps(oshEntity);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
public class OSHDBKeytablesNotFoundException extends Exception {
public OSHDBKeytablesNotFoundException() {
super("Keytables database not found, or db doesn't contain the required \"keytables\" tables. "
+ "Make sure you have specified the right keytables database, for example by calling `keytables()` when using the OSHDB-API.");
+ "Make sure you have specified the right keytables database, for example by calling "
+ "`keytables()` when using the OSHDB-API.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,12 @@ public <P extends Geometry & Polygonal> FastBboxInPolygon(P geom) {
polys.add((Polygon)geom);
} else if (geom instanceof MultiPolygon) {
MultiPolygon mp = (MultiPolygon)geom;
for (int i=0; i<mp.getNumGeometries(); i++)
polys.add((Polygon)mp.getGeometryN(i));
for (int i = 0; i < mp.getNumGeometries(); i++) {
polys.add((Polygon) mp.getGeometryN(i));
}
}
for (Polygon poly : polys) {
for (int i=0; i<poly.getNumInteriorRing(); i++) {
for (int i = 0; i < poly.getNumInteriorRing(); i++) {
innerBboxes.add(poly.getInteriorRingN(i).getEnvelopeInternal());
}
}
Expand All @@ -55,15 +56,17 @@ public boolean test(OSHDBBoundingBox boundingBox) {
Point p2 = gf.createPoint(new Coordinate(boundingBox.getMaxLon(), boundingBox.getMinLat()));
Point p3 = gf.createPoint(new Coordinate(boundingBox.getMaxLon(), boundingBox.getMaxLat()));
Point p4 = gf.createPoint(new Coordinate(boundingBox.getMinLon(), boundingBox.getMaxLat()));
if (crossingNumber(p1, true) != crossingNumber(p2, true) ||
crossingNumber(p3, true) != crossingNumber(p4, true) ||
crossingNumber(p2, false) != crossingNumber(p3, false) ||
crossingNumber(p4, false) != crossingNumber(p1, false)) {
if (crossingNumber(p1, true) != crossingNumber(p2, true)
|| crossingNumber(p3, true) != crossingNumber(p4, true)
|| crossingNumber(p2, false) != crossingNumber(p3, false)
|| crossingNumber(p4, false) != crossingNumber(p1, false)) {
return false; // at least one of the bbox'es edges crosses the polygon
}
for (Envelope innerBBox : innerBboxes) {
if (boundingBox.getMinLat() <= innerBBox.getMinY() && boundingBox.getMaxLat() >= innerBBox.getMaxY() &&
boundingBox.getMinLon() <= innerBBox.getMinX() && boundingBox.getMaxLon() >= innerBBox.getMaxX()) {
if (boundingBox.getMinLat() <= innerBBox.getMinY()
&& boundingBox.getMaxLat() >= innerBBox.getMaxY()
&& boundingBox.getMinLon() <= innerBBox.getMinX()
&& boundingBox.getMaxLon() >= innerBBox.getMaxX()) {
return false; // the bounding box fully covers at least one of the polygon's inner rings
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ public <P extends Geometry & Polygonal> FastBboxOutsidePolygon(P geom) {
polys.add((Polygon)geom);
} else if (geom instanceof MultiPolygon) {
MultiPolygon mp = (MultiPolygon)geom;
for (int i=0; i<mp.getNumGeometries(); i++)
polys.add((Polygon)mp.getGeometryN(i));
for (int i = 0; i < mp.getNumGeometries(); i++) {
polys.add((Polygon) mp.getGeometryN(i));
}
}
for (Polygon poly : polys) {
outerBboxes.add(poly.getEnvelopeInternal());
Expand All @@ -53,16 +54,19 @@ public boolean test(OSHDBBoundingBox boundingBox) {
Point p2 = gf.createPoint(new Coordinate(boundingBox.getMaxLon(), boundingBox.getMinLat()));
Point p3 = gf.createPoint(new Coordinate(boundingBox.getMaxLon(), boundingBox.getMaxLat()));
Point p4 = gf.createPoint(new Coordinate(boundingBox.getMinLon(), boundingBox.getMaxLat()));
if (crossingNumber(p1, true) != crossingNumber(p2, true) ||
crossingNumber(p3, true) != crossingNumber(p4, true) ||
crossingNumber(p2, false) != crossingNumber(p3, false) ||
crossingNumber(p4, false) != crossingNumber(p1, false)) {
if (crossingNumber(p1, true) != crossingNumber(p2, true)
|| crossingNumber(p3, true) != crossingNumber(p4, true)
|| crossingNumber(p2, false) != crossingNumber(p3, false)
|| crossingNumber(p4, false) != crossingNumber(p1, false)) {
return false; // at least one of the bbox'es edges crosses the polygon
}
for (Envelope innerBBox : outerBboxes) {
if (boundingBox.getMinLat() <= innerBBox.getMinY() && boundingBox.getMaxLat() >= innerBBox.getMaxY() &&
boundingBox.getMinLon() <= innerBBox.getMinX() && boundingBox.getMaxLon() >= innerBBox.getMaxX()) {
return false; // the bounding box fully covers at least one of the (multi)polygon's outer rings
if (boundingBox.getMinLat() <= innerBBox.getMinY()
&& boundingBox.getMaxLat() >= innerBBox.getMaxY()
&& boundingBox.getMinLon() <= innerBBox.getMinX()
&& boundingBox.getMaxLon() >= innerBBox.getMaxX()) {
// the bounding box fully covers at least one of the (multi)polygon's outer rings
return false;
}
}
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ private class Segment implements Serializable {
}
}

private final int AVERAGE_SEGMENTS_PER_BAND = 10; // something in the order of 10-20 works fine according to the link above
// something in the order of 10-20 works fine according to the link above
private static final int AVERAGE_SEGMENTS_PER_BAND = 10;

private int numBands;

Expand All @@ -46,10 +47,11 @@ private class Segment implements Serializable {

protected <P extends Geometry & Polygonal> FastInPolygon(P geom) {
MultiPolygon mp;
if (geom instanceof Polygon)
if (geom instanceof Polygon) {
mp = (new GeometryFactory()).createMultiPolygon(new Polygon[]{(Polygon) geom});
else
} else {
mp = (MultiPolygon) geom;
}

List<Segment> segments = new LinkedList<>();
for (int i = 0; i < mp.getNumGeometries(); i++) {
Expand All @@ -69,24 +71,36 @@ protected <P extends Geometry & Polygonal> FastInPolygon(P geom) {
}
}
}
this.numBands = Math.max(1, segments.size() / AVERAGE_SEGMENTS_PER_BAND); // possible optimization: start with this value of numBands, and if the result has over-full bands, increase numBands (e.g. x2) and do it again
// possible optimization: start with this value of numBands, and if the result has over-full
// bands, increase numBands (e.g. x2) and do it again
this.numBands = Math.max(1, segments.size() / AVERAGE_SEGMENTS_PER_BAND);
this.horizBands = new ArrayList<>(numBands);
for (int i = 0; i < numBands; i++) this.horizBands.add(new LinkedList<>());
for (int i = 0; i < numBands; i++) {
this.horizBands.add(new LinkedList<>());
}
this.vertBands = new ArrayList<>(numBands);
for (int i = 0; i < numBands; i++) this.vertBands.add(new LinkedList<>());
for (int i = 0; i < numBands; i++) {
this.vertBands.add(new LinkedList<>());
}

this.env = mp.getEnvelopeInternal();
this.envWidth = env.getMaxX() - env.getMinX();
this.envHeight = env.getMaxY() - env.getMinY();
segments.forEach(segment -> {
int startHorizBand = Math.max(0, Math.min(numBands - 1, (int) Math.floor(((segment.startY - env.getMinY()) / envHeight) * numBands)));
int endHorizBand = Math.max(0, Math.min(numBands - 1, (int) Math.floor(((segment.endY - env.getMinY()) / envHeight) * numBands)));
for (int i = Math.min(startHorizBand, endHorizBand); i <= Math.max(startHorizBand, endHorizBand); i++) {
int startHorizBand = Math.max(0, Math.min(numBands - 1,
(int) Math.floor(((segment.startY - env.getMinY()) / envHeight) * numBands)));
int endHorizBand = Math.max(0, Math.min(numBands - 1,
(int) Math.floor(((segment.endY - env.getMinY()) / envHeight) * numBands)));
for (int i = Math.min(startHorizBand, endHorizBand);
i <= Math.max(startHorizBand, endHorizBand); i++) {
horizBands.get(i).add(segment);
}
int startVertBand = Math.max(0, Math.min(numBands - 1, (int) Math.floor(((segment.startX - env.getMinX()) / envWidth) * numBands)));
int endVertBand = Math.max(0, Math.min(numBands - 1, (int) Math.floor(((segment.endX - env.getMinX()) / envWidth) * numBands)));
for (int i = Math.min(startVertBand, endVertBand); i <= Math.max(startVertBand, endVertBand); i++) {
int startVertBand = Math.max(0, Math.min(numBands - 1,
(int) Math.floor(((segment.startX - env.getMinX()) / envWidth) * numBands)));
int endVertBand = Math.max(0, Math.min(numBands - 1,
(int) Math.floor(((segment.endX - env.getMinX()) / envWidth) * numBands)));
for (int i = Math.min(startVertBand, endVertBand);
i <= Math.max(startVertBand, endVertBand); i++) {
vertBands.get(i).add(segment);
}
});
Expand All @@ -98,7 +112,8 @@ protected <P extends Geometry & Polygonal> FastInPolygon(P geom) {
*
* @param point
* @param dir boolean: true -&gt; horizontal test, false -&gt; vertical test
* @return crossing number of this point in the chosen direction, if the value is even the point is outside of the polygon, otherwise it is inside
* @return crossing number of this point in the chosen direction,
* if the value is even the point is outside of the polygon, otherwise it is inside
*/
protected int crossingNumber(Point point, boolean dir) {
return dir ? crossingNumberX(point) : crossingNumberY(point);
Expand All @@ -121,7 +136,8 @@ private int crossingNumberX(Point point) {
if (P.x < V[i].x + vt * (V[i+1].x - V[i].x)) // P.x < intersect
++cn; // a valid crossing of y=P.y right of P.x*/
double vt = (point.getY() - segment.startY) / (segment.endY - segment.startY);
if (point.getX() < segment.startX + vt * (segment.endX - segment.startX)) { // P.x < intersect
if (point.getX() < segment.startX
+ vt * (segment.endX - segment.startX)) { // P.x < intersect
cn++; // a valid crossing of y=P.y right of P.x
}
}
Expand All @@ -137,11 +153,14 @@ private int crossingNumberY(Point point) {

int cn = 0; // crossing number counter
for (Segment segment : band) {
if ((segment.startX <= point.getX() && segment.endX > point.getX()) || // an "upward" crossing
(segment.startX > point.getX() && segment.endX <= point.getX())) { // a "downward" crossing
if ((segment.startX <= point.getX()
&& segment.endX > point.getX()) // an "upward" crossing
|| (segment.startX > point.getX()
&& segment.endX <= point.getX())) { // a "downward" crossing
// compute the actual edge-ray intersect x-coordinate
double vt = (point.getX() - segment.startX) / (segment.endX - segment.startX);
if (point.getY() < segment.startY + vt * (segment.endY - segment.startY)) { // P.y < intersect
if (point.getY() < segment.startY
+ vt * (segment.endY - segment.startY)) { // P.y < intersect
cn++; // a valid crossing of x=P.x below of P.y
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
package org.heigit.bigspatialdata.oshdb.util.geometry.fip;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.locationtech.jts.geom.GeometryCollection;
import org.locationtech.jts.geom.GeometryFactory;
import org.locationtech.jts.geom.Envelope;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.GeometryCollection;
import org.locationtech.jts.geom.GeometryFactory;
import org.locationtech.jts.geom.MultiPolygon;
import org.locationtech.jts.geom.Polygon;
import org.locationtech.jts.geom.Polygonal;
import java.io.Serializable;
import java.util.ArrayList;

public class FastPolygonOperations implements Serializable {
private final int AVERAGE_VERTICES_PER_BLOCK = 40; // todo: finetune this value
private static final int AVERAGE_VERTICES_PER_BLOCK = 40; // todo: finetune this value

private int numBands;

Expand All @@ -24,7 +24,8 @@ public class FastPolygonOperations implements Serializable {
private double envHeight;

public <P extends Geometry & Polygonal> FastPolygonOperations(P geom) {
double optNumBands = Math.max(1.0, Math.sqrt(1.0 * geom.getNumPoints() / AVERAGE_VERTICES_PER_BLOCK));
double optNumBands = Math.max(1.0,
Math.sqrt(1.0 * geom.getNumPoints() / AVERAGE_VERTICES_PER_BLOCK));
final int bandIterations = (int) Math.ceil(Math.log(optNumBands) / Math.log(2));
numBands = (int) Math.pow(2, bandIterations);

Expand All @@ -34,7 +35,7 @@ public <P extends Geometry & Polygonal> FastPolygonOperations(P geom) {

GeometryFactory gf = new GeometryFactory();

Geometry[] result = new Geometry[numBands*numBands];
Geometry[] result = new Geometry[numBands * numBands];
traverseQuads(bandIterations, 0,0, env, geom, gf, result);

blocks = new ArrayList<>(Arrays.asList(result));
Expand Down Expand Up @@ -77,25 +78,25 @@ private void traverseQuads(
int index = y + x * numBands;
resultBuffer[index] = theGeom;
} else {
Envelope bottomLeftPart = new Envelope(
final Envelope bottomLeftPart = new Envelope(
quadEnv.getMinX(),
(quadEnv.getMinX() + quadEnv.getMaxX()) / 2,
quadEnv.getMinY(),
(quadEnv.getMinY() + quadEnv.getMaxY()) / 2
);
Envelope topLeftPart = new Envelope(
final Envelope topLeftPart = new Envelope(
quadEnv.getMinX(),
(quadEnv.getMinX() + quadEnv.getMaxX()) / 2,
(quadEnv.getMinY() + quadEnv.getMaxY()) / 2,
quadEnv.getMaxY()
);
Envelope bottomRightPart = new Envelope(
final Envelope bottomRightPart = new Envelope(
(quadEnv.getMinX() + quadEnv.getMaxX()) / 2,
quadEnv.getMaxX(),
quadEnv.getMinY(),
(quadEnv.getMinY() + quadEnv.getMaxY()) / 2
);
Envelope topRightPart = new Envelope(
final Envelope topRightPart = new Envelope(
(quadEnv.getMinX() + quadEnv.getMaxX()) / 2,
quadEnv.getMaxX(),
(quadEnv.getMinY() + quadEnv.getMaxY()) / 2,
Expand Down Expand Up @@ -133,19 +134,25 @@ private void traverseQuads(
}

public Geometry intersection(Geometry other) {
if (other == null || other.isEmpty()) return other;
if (other == null || other.isEmpty()) {
return other;
}
Envelope otherEnv = other.getEnvelopeInternal();

int minBandX = Math.max(0, Math.min(numBands - 1, (int)Math.floor((otherEnv.getMinX() - env.getMinX())/envWidth * numBands)));
int maxBandX = Math.max(0, Math.min(numBands - 1, (int)Math.floor((otherEnv.getMaxX() - env.getMinX())/envWidth * numBands)));
int minBandY = Math.max(0, Math.min(numBands - 1, (int)Math.floor((otherEnv.getMinY() - env.getMinY())/envHeight * numBands)));
int maxBandY = Math.max(0, Math.min(numBands - 1, (int)Math.floor((otherEnv.getMaxY() - env.getMinY())/envHeight * numBands)));
int minBandX = Math.max(0, Math.min(numBands - 1,
(int) Math.floor((otherEnv.getMinX() - env.getMinX()) / envWidth * numBands)));
int maxBandX = Math.max(0, Math.min(numBands - 1,
(int) Math.floor((otherEnv.getMaxX() - env.getMinX()) / envWidth * numBands)));
int minBandY = Math.max(0, Math.min(numBands - 1,
(int) Math.floor((otherEnv.getMinY() - env.getMinY()) / envHeight * numBands)));
int maxBandY = Math.max(0, Math.min(numBands - 1,
(int) Math.floor((otherEnv.getMaxY() - env.getMinY()) / envHeight * numBands)));

Geometry intersector = null;

for (int x = minBandX; x <= maxBandX; x++) {
for (int y = minBandY; y <= maxBandY; y++) {
Geometry block = blocks.get(y + x*numBands);
Geometry block = blocks.get(y + x * numBands);
if (intersector == null) {
intersector = block;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.util.Map;
import java.util.Set;

import org.heigit.bigspatialdata.oshdb.osh.OSHWay;
import org.heigit.bigspatialdata.oshdb.osm.OSMEntity;
import org.heigit.bigspatialdata.oshdb.osm.OSMMember;
Expand Down
Loading

0 comments on commit b232f77

Please sign in to comment.