Skip to content

Commit 54705c6

Browse files
committed
Use Envelope in API; move Bbox to core
1 parent 2a8453b commit 54705c6

File tree

21 files changed

+166
-184
lines changed

21 files changed

+166
-184
lines changed

api/src/main/java/com/graphhopper/ResponsePath.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import com.graphhopper.util.InstructionList;
2121
import com.graphhopper.util.PointList;
2222
import com.graphhopper.util.details.PathDetail;
23-
import com.graphhopper.util.shapes.BBox;
23+
import org.locationtech.jts.geom.Envelope;
2424

2525
import java.math.BigDecimal;
2626
import java.util.*;
@@ -210,11 +210,11 @@ public ResponsePath setRouteWeight(double weight) {
210210
/**
211211
* Calculates the 2D bounding box of this route
212212
*/
213-
public BBox calcBBox2D() {
213+
public Envelope calcBBox2D() {
214214
check("calcRouteBBox");
215-
BBox bounds = BBox.createInverse(false);
215+
Envelope bounds = new Envelope();
216216
for (int i = 0; i < pointList.getSize(); i++) {
217-
bounds.update(pointList.getLatitude(i), pointList.getLongitude(i));
217+
bounds.expandToInclude(pointList.getLongitude(i), pointList.getLatitude(i));
218218
}
219219
return bounds;
220220
}

api/src/main/java/com/graphhopper/json/geo/JsonFeature.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
*/
1818
package com.graphhopper.json.geo;
1919

20-
import com.graphhopper.util.shapes.BBox;
20+
import org.locationtech.jts.geom.Envelope;
2121
import org.locationtech.jts.geom.Geometry;
2222

2323
import java.util.Map;
@@ -30,14 +30,14 @@
3030
public class JsonFeature {
3131
private String id;
3232
private String type = "Feature";
33-
private BBox bbox;
33+
private Envelope bbox;
3434
private Geometry geometry;
3535
private Map<String, Object> properties;
3636

3737
public JsonFeature() {
3838
}
3939

40-
public JsonFeature(String id, String type, BBox bbox, Geometry geometry, Map<String, Object> properties) {
40+
public JsonFeature(String id, String type, Envelope bbox, Geometry geometry, Map<String, Object> properties) {
4141
this.id = id;
4242
this.type = type;
4343
this.bbox = bbox;
@@ -53,7 +53,7 @@ public String getType() {
5353
return type;
5454
}
5555

56-
public BBox getBBox() {
56+
public Envelope getBBox() {
5757
return bbox;
5858
}
5959

@@ -81,7 +81,7 @@ public void setId(String id) {
8181
this.id = id;
8282
}
8383

84-
public void setBbox(BBox bbox) {
84+
public void setBbox(Envelope bbox) {
8585
this.bbox = bbox;
8686
}
8787

client-hc/pom.xml

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,8 @@
4949
<artifactId>slf4j-api</artifactId>
5050
</dependency>
5151
<dependency>
52-
<groupId>org.slf4j</groupId>
53-
<artifactId>slf4j-log4j12</artifactId>
54-
<scope>test</scope>
55-
</dependency>
56-
<dependency>
57-
<groupId>log4j</groupId>
58-
<artifactId>log4j</artifactId>
52+
<groupId>io.dropwizard</groupId>
53+
<artifactId>dropwizard-testing</artifactId>
5954
<scope>test</scope>
6055
</dependency>
6156
</dependencies>

client-hc/src/main/java/com/graphhopper/api/model/GHGeocodingEntry.java

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
2121
import com.fasterxml.jackson.annotation.JsonInclude;
2222
import com.fasterxml.jackson.annotation.JsonProperty;
23-
import com.graphhopper.util.shapes.BBox;
23+
import org.locationtech.jts.geom.Envelope;
2424

2525
/**
2626
* Contains the results of a geocoding request.
@@ -48,9 +48,9 @@ public class GHGeocodingEntry {
4848
private String osmKey;
4949
private String osmValue;
5050

51-
private BBox extent;
51+
private Envelope extent;
5252

53-
public GHGeocodingEntry(Long osmId, String type, double lat, double lng, String name, String osmKey, String osmValue, String country, String city, String state, String street, String houseNumber, String postcode, BBox extent) {
53+
public GHGeocodingEntry(Long osmId, String type, double lat, double lng, String name, String osmKey, String osmValue, String country, String city, String state, String street, String houseNumber, String postcode, Envelope extent) {
5454
this.osmId = osmId;
5555
this.osmType = type;
5656
this.point = new Point(lat, lng);
@@ -189,27 +189,17 @@ public void setOsmValue(String osmValue) {
189189
this.osmValue = osmValue;
190190
}
191191

192-
@JsonProperty
193-
public Double[] getExtent() {
194-
if (this.extent == null) {
195-
// TODO should we return null instead?
196-
return new Double[0];
197-
}
198-
return this.extent.toGeoJson().toArray(new Double[4]);
199-
}
200-
201-
public BBox getExtendBBox(){
192+
public Envelope getExtent(){
202193
return this.extent;
203194
}
204195

205196
@JsonProperty
206197
public void setExtent(Double[] extent) {
207-
if (extent == null || extent.length == 0) {
208-
return;
209-
}
210-
if (extent.length == 4) {
198+
if (extent == null) {
199+
this.extent = null;
200+
} else if (extent.length == 4) {
211201
// Extend is in Left, Top, Right, Bottom; which is very uncommon, Photon uses the same
212-
this.extent = new BBox(extent[0], extent[2], extent[3], extent[1]);
202+
this.extent = new Envelope(extent[0], extent[2], extent[3], extent[1]);
213203
} else {
214204
throw new RuntimeException("Extent had an unexpected length: " + extent.length);
215205
}
Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,10 @@
11
package com.graphhopper.api;
22

3-
import com.fasterxml.jackson.databind.JsonNode;
4-
import com.fasterxml.jackson.databind.ObjectMapper;
5-
import com.fasterxml.jackson.databind.node.ObjectNode;
63
import com.graphhopper.GHRequest;
7-
import com.graphhopper.ResponsePath;
8-
import com.graphhopper.jackson.Jackson;
9-
import com.graphhopper.jackson.ResponsePathDeserializer;
104
import com.graphhopper.util.shapes.GHPoint;
11-
import org.junit.Test;
125
import org.junit.jupiter.params.ParameterizedTest;
136
import org.junit.jupiter.params.provider.ValueSource;
147

15-
import java.io.IOException;
16-
178
import static org.junit.jupiter.api.Assertions.assertEquals;
189

1910
/**
@@ -34,24 +25,4 @@ public void testGetClientForRequest(boolean usePost) {
3425
assertEquals(5, gh.getClientForRequest(req).connectTimeoutMillis());
3526
}
3627

37-
@Test
38-
public void testPutPOJO() {
39-
ObjectNode requestJson = new ObjectMapper().createObjectNode();
40-
requestJson.putPOJO("double", 1.0);
41-
requestJson.putPOJO("int", 1);
42-
requestJson.putPOJO("boolean", true);
43-
// does not work requestJson.putPOJO("string", "test");
44-
assertEquals("{\"double\":1.0,\"int\":1,\"boolean\":true}", requestJson.toString());
45-
}
46-
47-
@Test
48-
public void testUnknownInstructionSign() throws IOException {
49-
// Modified the sign though
50-
ObjectMapper objectMapper = Jackson.newObjectMapper();
51-
JsonNode json = objectMapper.readTree("{\"instructions\":[{\"distance\":1.073,\"sign\":741,\"interval\":[0,1],\"text\":\"Continue onto A 81\",\"time\":32,\"street_name\":\"A 81\"},{\"distance\":0,\"sign\":4,\"interval\":[1,1],\"text\":\"Finish!\",\"time\":0,\"street_name\":\"\"}],\"descend\":0,\"ascend\":0,\"distance\":1.073,\"bbox\":[8.676286,48.354446,8.676297,48.354453],\"weight\":0.032179,\"time\":32,\"points_encoded\":true,\"points\":\"gfcfHwq}s@}c~AAA?\",\"snapped_waypoints\":\"gfcfHwq}s@}c~AAA?\"}");
52-
ResponsePath responsePath = ResponsePathDeserializer.createResponsePath(objectMapper, json, true, true);
53-
54-
assertEquals(741, responsePath.getInstructions().get(0).getSign());
55-
assertEquals("Continue onto A 81", responsePath.getInstructions().get(0).getName());
56-
}
5728
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Licensed to GraphHopper GmbH under one or more contributor
3+
* license agreements. See the NOTICE file distributed with this work for
4+
* additional information regarding copyright ownership.
5+
*
6+
* GraphHopper GmbH licenses this file to you under the Apache License,
7+
* Version 2.0 (the "License"); you may not use this file except in
8+
* compliance with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
package com.graphhopper.api.model;
20+
21+
import com.fasterxml.jackson.core.JsonProcessingException;
22+
import com.fasterxml.jackson.databind.ObjectMapper;
23+
import com.graphhopper.jackson.Jackson;
24+
import org.junit.Assert;
25+
import org.junit.jupiter.api.Test;
26+
import org.locationtech.jts.geom.Envelope;
27+
28+
import static io.dropwizard.testing.FixtureHelpers.fixture;
29+
30+
31+
public class GHGeocodingResponseRepresentationTest {
32+
33+
@Test
34+
public void testGeocodingRepresentation() throws JsonProcessingException {
35+
ObjectMapper objectMapper = Jackson.newObjectMapper();
36+
GHGeocodingResponse geocodingResponse = objectMapper.readValue(fixture("fixtures/geocoding-response.json"), GHGeocodingResponse.class);
37+
Envelope extent = geocodingResponse.getHits().get(0).getExtent();
38+
// Despite the unusual representation of the bounding box...
39+
Assert.assertEquals(10.0598605, extent.getMinX(), 0.0);
40+
Assert.assertEquals(10.0612079, extent.getMaxX(), 0.0);
41+
Assert.assertEquals(53.7445489, extent.getMinY(), 0.0);
42+
Assert.assertEquals(53.7456315, extent.getMaxY(), 0.0);
43+
}
44+
45+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"copyrights": [
3+
"OpenCageData",
4+
"OpenStreetMap",
5+
"GraphHopper"
6+
],
7+
"hits": [
8+
{
9+
"point": {
10+
"lat": 53.7445489,
11+
"lng": 10.0612079
12+
},
13+
"extent": [
14+
10.0598605,
15+
53.7445489,
16+
10.0612079,
17+
53.7456315
18+
],
19+
"name": "Am Heidberg, 22889 Tangstedt, Germany",
20+
"country": "Germany",
21+
"city": "Tangstedt",
22+
"state": "Schleswig-Holstein",
23+
"county": "Stormarn",
24+
"street": "Am Heidberg",
25+
"postcode": "22889",
26+
"osm_id": 25014113,
27+
"osm_type": "W",
28+
"osm_value": "road"
29+
}
30+
],
31+
"locale": "en"
32+
}

client-hc/src/test/resources/log4j.xml

Lines changed: 0 additions & 21 deletions
This file was deleted.

core/src/main/java/com/graphhopper/storage/index/LocationIndexTree.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import com.graphhopper.util.*;
3131
import com.graphhopper.util.shapes.BBox;
3232
import com.graphhopper.util.shapes.GHPoint;
33-
import com.graphhopper.util.shapes.Shape;
3433
import org.slf4j.Logger;
3534
import org.slf4j.LoggerFactory;
3635

@@ -465,7 +464,7 @@ public void onNode(int nodeId) {
465464
}, 0);
466465
}
467466

468-
final void query(int intPointer, Shape queryBBox,
467+
final void query(int intPointer, BBox queryBBox,
469468
double minLat, double minLon,
470469
double deltaLatPerDepth, double deltaLonPerDepth,
471470
Visitor function, int depth) {

api/src/main/java/com/graphhopper/util/shapes/BBox.java renamed to core/src/main/java/com/graphhopper/util/shapes/BBox.java

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -142,24 +142,6 @@ public BBox clone() {
142142
return new BBox(minLon, maxLon, minLat, maxLat, minEle, maxEle, elevation);
143143
}
144144

145-
@Override
146-
public boolean intersects(Shape s) {
147-
if (s instanceof BBox) {
148-
return intersects((BBox) s);
149-
}
150-
151-
throw new UnsupportedOperationException("unsupported shape");
152-
}
153-
154-
@Override
155-
public boolean contains(Shape s) {
156-
if (s instanceof BBox) {
157-
return contains((BBox) s);
158-
}
159-
160-
throw new UnsupportedOperationException("unsupported shape");
161-
}
162-
163145
public static boolean intersects(RectangleLineIntersector intersector, PointList pointList) {
164146
int len = pointList.getSize();
165147
if (len == 0)

core/src/main/java/com/graphhopper/util/shapes/Circle.java

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -65,17 +65,6 @@ private double normDist(double lat1, double lon1) {
6565
return calc.calcNormalizedDist(lat, lon, lat1, lon1);
6666
}
6767

68-
@Override
69-
public boolean intersects(Shape o) {
70-
if (o instanceof Circle) {
71-
return intersects((Circle) o);
72-
} else if (o instanceof BBox) {
73-
return intersects((BBox) o);
74-
}
75-
76-
return o.intersects(this);
77-
}
78-
7968
@Override
8069
public boolean intersects(PointList pointList) {
8170
// similar code to LocationIndexTree.checkAdjacent
@@ -106,17 +95,6 @@ public boolean intersects(PointList pointList) {
10695
return false;
10796
}
10897

109-
@Override
110-
public boolean contains(Shape o) {
111-
if (o instanceof Circle) {
112-
return contains((Circle) o);
113-
} else if (o instanceof BBox) {
114-
return contains((BBox) o);
115-
}
116-
117-
throw new UnsupportedOperationException("unsupported shape");
118-
}
119-
12098
public boolean intersects(BBox b) {
12199
// test top intersects
122100
if (lat > b.maxLat) {
@@ -150,15 +128,6 @@ public boolean intersects(BBox b) {
150128
return true;
151129
}
152130

153-
public boolean intersects(Circle c) {
154-
// necessary to improve speed?
155-
if (!getBounds().intersects(c.getBounds())) {
156-
return false;
157-
}
158-
159-
return normDist(c.lat, c.lon) <= calc.calcNormalizedDist(radiusInMeter + c.radiusInMeter);
160-
}
161-
162131
public boolean contains(BBox b) {
163132
if (bbox.contains(b)) {
164133
return contains(b.maxLat, b.minLon) && contains(b.minLat, b.minLon)

0 commit comments

Comments
 (0)