Skip to content

Commit

Permalink
chore: cosmetics
Browse files Browse the repository at this point in the history
  • Loading branch information
takb committed Dec 10, 2024
1 parent 5ba1d71 commit 2d49044
Show file tree
Hide file tree
Showing 10 changed files with 105 additions and 103 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ RELEASING:
## [unreleased]

### Added
- TopoJSON graph export ([#1926](https://github.com/GIScience/openrouteservice/pull/1926))

### Changed

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public JsonExportResponse getJsonExport(
responseCode = "200",
description = "TopoJSON Response.",
content = {@Content(
mediaType = "application/geo+json",
mediaType = "application/json",
schema = @Schema(implementation = TopoJsonExportResponse.class)
)
})
Expand All @@ -150,7 +150,7 @@ public TopoJsonExportResponse getTopoJsonExport(

ExportResult result = exportService.generateExportFromRequest(request);

return TopoJsonExportResponse.fromExportResult(result, "network");
return TopoJsonExportResponse.fromExportResult(result);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
public class APIRequest {
public static final String PARAM_ID = "id";
public static final String PARAM_PROFILE = "profile";
public static final String PARAM_PROFILE_NAME = "profile_name";

@Schema(name = PARAM_ID, description = "Arbitrary identification string of the request reflected in the meta information.",
example = "my_request")
Expand All @@ -19,7 +20,7 @@ public class APIRequest {
@Schema(name = PARAM_PROFILE, hidden = true)
protected APIEnums.Profile profile;


@Schema(name = PARAM_PROFILE_NAME, hidden = true)
protected String profileName;

public boolean hasId() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ public class Geometry implements Serializable {
@JsonProperty("type")
private String type;
@JsonProperty("properties")
@JsonSerialize(using = TopoJsonPropertiesSerializer.class)
private transient Map<String, Object> properties;
private transient Properties properties;
@JsonProperty("arcs")
private List<Integer> arcs;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

package org.heigit.ors.api.responses.export.topojson;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import lombok.Builder;
import lombok.Getter;

import java.io.Serializable;
import java.util.List;

@JsonInclude(JsonInclude.Include.NON_NULL)
@Getter
@Builder
public class Network implements Serializable {
@JsonProperty("network")
private Layer network;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.heigit.ors.api.responses.export.topojson;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import lombok.Builder;
import lombok.Getter;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

@JsonInclude(JsonInclude.Include.NON_NULL)
@Getter
@Builder
public class Properties implements Serializable {
@JsonProperty("weight")
private Double weight;
@JsonProperty("osm_id")
private Long osmId;
@JsonProperty("both_directions")
private Boolean bothDirections;
@JsonProperty("speed")
private Double speed;
@JsonProperty("speed_reverse")
private Double speedReverse;
@JsonProperty("ors_ids")
private List<Integer> orsIds;
@JsonProperty("ors_nodes")
private List<Integer> orsNodes;
@JsonProperty("distances")
private List<Double> distances;
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
import org.locationtech.jts.geom.LineString;

import java.io.Serializable;
import java.util.*;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import static org.heigit.ors.export.ExportResult.TopoGeometry;

Expand All @@ -21,20 +24,20 @@
@Builder
public class TopoJsonExportResponse implements Serializable {

@JsonProperty("type")
@JsonProperty(value = "type")
@Builder.Default
private String type = "Topology";
@JsonProperty("objects")
@Builder.Default
private HashMap<String, Layer> objects = new HashMap<>();
private Network objects = null;
@JsonProperty("arcs")
@Builder.Default
private List<Arc> arcs = new LinkedList<>();
@JsonProperty("bbox")
@Builder.Default
private List<Double> bbox = new ArrayList<>(4);

public static TopoJsonExportResponse fromExportResult(ExportResult exportResult, String topologyLayerName) {
public static TopoJsonExportResponse fromExportResult(ExportResult exportResult) {
BBox bbox = new BBox();
LinkedList<Geometry> geometries = new LinkedList<>();
LinkedList<Arc> arcsLocal = new LinkedList<>();
Expand All @@ -45,12 +48,13 @@ public static TopoJsonExportResponse fromExportResult(ExportResult exportResult,
Pair<Integer, Integer> fromTo = edgeWeight.getKey();

LineString lineString = (LineString) exportResult.getEdgeExtras().get(fromTo).get("geometry");
arcsLocal.add(Arc.builder().coordinates(makeCoordinates(lineString, bbox)).build());
arcsLocal.add(Arc.builder().coordinates(makeCoordinateList(lineString, bbox)).build());
arcCount++;

List<Integer> arcList = List.of(arcCount);
Map<String, Object> properties = new HashMap<>();
properties.put("weight", edgeWeight.getValue());
Properties properties = Properties.builder()
.weight(edgeWeight.getValue())
.build();
Geometry geometry = Geometry.builder()
.type("LineString")
.properties(properties)
Expand All @@ -66,7 +70,7 @@ public static TopoJsonExportResponse fromExportResult(ExportResult exportResult,
List<Integer> nodeList = new LinkedList<>();
List<Double> distanceList = new LinkedList<>();
for (int orsId : orsIdList) {
arcsLocal.add(Arc.builder().coordinates(makeCoordinates(topoGeometry.getArcs().get(orsId).geometry(), bbox)).build());
arcsLocal.add(Arc.builder().coordinates(makeCoordinateList(topoGeometry.getArcs().get(orsId).geometry(), bbox)).build());
arcList.add(arcCount);
if (nodeList.isEmpty()) {
nodeList.add(topoGeometry.getArcs().get(orsId).from());
Expand All @@ -75,16 +79,15 @@ public static TopoJsonExportResponse fromExportResult(ExportResult exportResult,
distanceList.add(topoGeometry.getArcs().get(orsId).length());
arcCount++;
}
Map<String, Object> properties = new HashMap<>();
properties.put("osm_id", osmId);
properties.put("ors_ids", orsIdList);
properties.put("ors_nodes", nodeList);
properties.put("speed", topoGeometry.getSpeed());
properties.put("distances", distanceList);
properties.put("both_directions", topoGeometry.isBothDirections());
if (topoGeometry.isBothDirections()) {
properties.put("speed_reverse", topoGeometry.getSpeedReverse());
}
Properties properties = Properties.builder()
.osmId(osmId)
.bothDirections(topoGeometry.isBothDirections())
.speed(topoGeometry.getSpeed())
.speedReverse(topoGeometry.isBothDirections() ? topoGeometry.getSpeedReverse() : null)
.orsIds(orsIdList)
.orsNodes(nodeList)
.distances(distanceList)
.build();

Geometry geometry = Geometry.builder()
.type("LineString")
Expand All @@ -96,16 +99,16 @@ public static TopoJsonExportResponse fromExportResult(ExportResult exportResult,
}
return TopoJsonExportResponse.builder()
.type("Topology")
.objects(new HashMap<>(Map.of(topologyLayerName, Layer.builder()
.objects(Network.builder().network(Layer.builder()
.type("GeometryCollection")
.geometries(geometries)
.build())))
.build()).build())
.arcs(arcsLocal)
.bbox(bbox.toList())
.build();
}

private static List<List<Double>> makeCoordinates(LineString geometry, BBox bbox) {
private static List<List<Double>> makeCoordinateList(LineString geometry, BBox bbox) {
List<List<Double>> coordinates = new LinkedList<>();
for (Coordinate coordinate : geometry.getCoordinates()) {
coordinates.add(List.of(coordinate.x, coordinate.y));
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.annotation.JsonAppend;
import org.heigit.ors.common.Pair;
import org.heigit.ors.export.ExportResult;
import org.junit.jupiter.api.Assertions;
Expand All @@ -22,19 +23,15 @@ class TopoJsonExportResponseTest {
// setup function
@BeforeEach
void setUp() {
Map<String, Object> properties1 = new LinkedHashMap<>();
properties1.put("OBJECTID", 41106);
Map<String, Object> properties2 = new LinkedHashMap<>();
properties2.put("OBJECTID", 41107);
Geometry geometry1 = Geometry.builder()
.type("LineString")
.arcs(List.of(0, 1))
.properties(properties1)
.properties(Properties.builder().osmId(41106L).build())
.build();
Geometry geometry2 = Geometry.builder()
.type("LineString")
.arcs(List.of(2))
.properties(properties2)
.properties(Properties.builder().osmId(41107L).build())
.build();
Layer layer = Layer.builder()
.type("GeometryCollection")
Expand Down Expand Up @@ -68,7 +65,7 @@ void setUp() {
.build();
topoJsonExportResponse = TopoJsonExportResponse.builder()
.type("Topology")
.objects(new HashMap<>(Map.of(topologyLayerName, layer)))
.objects(Network.builder().network(layer).build())
.bbox(List.of(-72.822573, 19.947123, -72.81259, 19.952703))
.arcs(List.of(arc1, arc2, arc3))
.build();
Expand Down Expand Up @@ -96,15 +93,13 @@ void testTopoJsonSerialization() throws JsonProcessingException {
}
}
Assertions.assertEquals(1, jsonNode.get("objects").size());
Assertions.assertEquals(jsonNode.get("objects").get("network").get("type").asText(), topoJsonExportResponse.getObjects().get("network").getType());
for (int j = 0; j < topoJsonExportResponse.getObjects().get("network").getGeometries().size(); j++) {
Assertions.assertEquals(jsonNode.get("objects").get("network").get("geometries").get(j).get("type").asText(), topoJsonExportResponse.getObjects().get("network").getGeometries().get(j).getType());
for (int k = 0; k < topoJsonExportResponse.getObjects().get("network").getGeometries().get(j).getArcs().size(); k++) {
Assertions.assertEquals(jsonNode.get("objects").get("network").get("geometries").get(j).get("arcs").get(k).asInt(), topoJsonExportResponse.getObjects().get("network").getGeometries().get(j).getArcs().get(k));
}
for (String key : topoJsonExportResponse.getObjects().get("network").getGeometries().get(j).getProperties().keySet()) {
Assertions.assertEquals(jsonNode.get("objects").get("network").get("geometries").get(j).get("properties").get(key).asText(), topoJsonExportResponse.getObjects().get("network").getGeometries().get(j).getProperties().get(key).toString());
Assertions.assertEquals(jsonNode.get("objects").get("network").get("type").asText(), topoJsonExportResponse.getObjects().getNetwork().getType());
for (int j = 0; j < topoJsonExportResponse.getObjects().getNetwork().getGeometries().size(); j++) {
Assertions.assertEquals(jsonNode.get("objects").get("network").get("geometries").get(j).get("type").asText(), topoJsonExportResponse.getObjects().getNetwork().getGeometries().get(j).getType());
for (int k = 0; k < topoJsonExportResponse.getObjects().getNetwork().getGeometries().get(j).getArcs().size(); k++) {
Assertions.assertEquals(jsonNode.get("objects").get("network").get("geometries").get(j).get("arcs").get(k).asInt(), topoJsonExportResponse.getObjects().getNetwork().getGeometries().get(j).getArcs().get(k));
}
Assertions.assertEquals(jsonNode.get("objects").get("network").get("geometries").get(j).get("properties").get("osm_id").asText(), topoJsonExportResponse.getObjects().getNetwork().getGeometries().get(j).getProperties().getOsmId().toString());
}
}

Expand All @@ -117,7 +112,6 @@ void testEmptyTopoJsonObjectSerialization() throws JsonProcessingException {
Assertions.assertEquals("Topology", emptyTopoJsonExportResponse.getType());
Assertions.assertEquals(0, jsonNode.get("bbox").size());
Assertions.assertEquals(0, jsonNode.get("arcs").size());
Assertions.assertEquals(0, jsonNode.get("objects").size());
}

@Test
Expand All @@ -137,33 +131,32 @@ void testFromExportResult() {
exportResult.addEdgeExtra(new Pair<>(2, 3), new HashMap<>(Map.of("osm_id", 1L, "foo", "bla")));
exportResult.addEdgeExtra(new Pair<>(0, 4), new HashMap<>(Map.of("osm_id", 2L, "foo", "bub")));

ExportResult.TopoGeometry topoGeometry1 = new ExportResult.TopoGeometry(1.0, 1.0);
ExportResult.TopoGeometry topoGeometry1 = new ExportResult.TopoGeometry(1.0F, 1.0F);
topoGeometry1.getArcs().put(1, new ExportResult.TopoArc(geometryFactory.createLineString(new Coordinate[]{new Coordinate(0.0, 0.0), new Coordinate(1.0, 1.0)}), 1.0, 0, 1));
topoGeometry1.getArcs().put(2, new ExportResult.TopoArc(geometryFactory.createLineString(new Coordinate[]{new Coordinate(1.0, 1.0), new Coordinate(2.0, 2.0)}), 2.0, 1, 2));
topoGeometry1.getArcs().put(3, new ExportResult.TopoArc(geometryFactory.createLineString(new Coordinate[]{new Coordinate(2.0, 2.0), new Coordinate(3.0, 3.0)}), 3.0, 2, 3));
topoGeometry1.setBothDirections(true);
exportResult.getTopoGeometries().put(1L,topoGeometry1);
ExportResult.TopoGeometry topoGeometry2 = new ExportResult.TopoGeometry(2.0, 2.0);
ExportResult.TopoGeometry topoGeometry2 = new ExportResult.TopoGeometry(2.0F, 2.0F);
topoGeometry2.getArcs().put(4, new ExportResult.TopoArc(geometryFactory.createLineString(new Coordinate[]{new Coordinate(0.0, 0.0), new Coordinate(4.0, 5.0)}), 4.0, 0, 4));
exportResult.getTopoGeometries().put(2L, topoGeometry2);

TopoJsonExportResponse exportResultToTopoJson = TopoJsonExportResponse.fromExportResult(exportResult, topologyLayerName);
HashMap<String, Layer> layers = exportResultToTopoJson.getObjects();
Layer network = layers.get(topologyLayerName);
TopoJsonExportResponse exportResultToTopoJson = TopoJsonExportResponse.fromExportResult(exportResult);
Layer network = exportResultToTopoJson.getObjects().getNetwork();
Assertions.assertEquals("GeometryCollection", network.getType());
Assertions.assertEquals(2, network.getGeometries().size());
Geometry geometry1 = network.getGeometries().get(0);
Assertions.assertEquals("LineString", geometry1.getType());
Assertions.assertEquals(List.of(0, 1, 2), geometry1.getArcs());
Assertions.assertEquals(1.0, geometry1.getProperties().get("speed"));
Assertions.assertEquals(1.0, geometry1.getProperties().get("speed_reverse"));
Assertions.assertEquals(1L, geometry1.getProperties().get("osm_id"));
Assertions.assertEquals(1.0, geometry1.getProperties().getSpeed());
Assertions.assertEquals(1.0, geometry1.getProperties().getSpeedReverse());
Assertions.assertEquals(1L, geometry1.getProperties().getOsmId());
Geometry geometry2 = network.getGeometries().get(1);
Assertions.assertEquals("LineString", geometry2.getType());
Assertions.assertEquals(List.of(3), geometry2.getArcs());
Assertions.assertEquals(2.0, geometry2.getProperties().get("speed"));
Assertions.assertNull( geometry2.getProperties().get("speed_reverse"));
Assertions.assertEquals(2L, geometry2.getProperties().get("osm_id"));
Assertions.assertEquals(2.0, geometry2.getProperties().getSpeed());
Assertions.assertNull( geometry2.getProperties().getSpeedReverse());
Assertions.assertEquals(2L, geometry2.getProperties().getOsmId());
List<Arc> arcs = exportResultToTopoJson.getArcs();
Assertions.assertEquals(4, arcs.size());
Assertions.assertEquals(List.of(List.of(0.0, 0.0), List.of(1.0, 1.0)), arcs.get(0).getCoordinates());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ void expectMissingBbox() {
.pathParam("profile", "driving-car")
.body(body.toString())
.when()
.post(getEndPointPath() + "/{profile}/json")
.post(getEndPointPath() + "/{profile}/topojson")
.then()
.assertThat()
//not sure mismatched input is correct here, missing parameter seems more intuitive?
Expand Down Expand Up @@ -143,7 +143,7 @@ void expectNodesAndEdges() {
.body(body.toString())
.when()
.post(getEndPointPath() + "/{profile}/json")
.then()
.then().log().ifValidationFails()
.assertThat()
.body("containsKey('nodes')", is(true))
.body("containsKey('edges')", is(true))
Expand Down

0 comments on commit 2d49044

Please sign in to comment.