Skip to content
Snippets Groups Projects
Commit 21c139b1 authored by lazarog98's avatar lazarog98
Browse files

Merge branch 'add_maxspeed' into 'main'

Add maxspeed

See merge request !19
parents fd17fef0 f0632486
Branches
Tags
1 merge request!19Add maxspeed
...@@ -15,10 +15,19 @@ public class App { ...@@ -15,10 +15,19 @@ public class App {
RoadNetwork.Builder roadNetworkBuilder = RoadNetwork.newBuilder(); RoadNetwork.Builder roadNetworkBuilder = RoadNetwork.newBuilder();
OSMParser parser = new OSMParser(roadNetworkBuilder); OSMParser parser = new OSMParser(roadNetworkBuilder);
// A small BBox inside Cottbus
/*
float minLat = 51.765120241998865f; float minLat = 51.765120241998865f;
float minLon = 14.32669617537409f; float minLon = 14.32669617537409f;
float maxLat = 51.77116774623326f; float maxLat = 51.77116774623326f;
float maxLon = 14.330334220133722f; float maxLon = 14.330334220133722f;
*/
// BBox around Cottbus
float minLat = 51.714692361306376f;
float minLon = 14.261627197265625f;
float maxLat = 51.79120499625462f;
float maxLon = 14.391403198242188f;
BoundingBox bbox = new BoundingBox(minLat, minLon, maxLat, maxLon); BoundingBox bbox = new BoundingBox(minLat, minLon, maxLat, maxLon);
......
package map.builder.osm; package map.builder.osm;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONObject; import org.json.JSONObject;
...@@ -19,7 +19,6 @@ public class OSMParser { ...@@ -19,7 +19,6 @@ public class OSMParser {
private final Coordinates.Builder coordinatesBuilder; private final Coordinates.Builder coordinatesBuilder;
private final HashMap<Long, Node> nodesDump; private final HashMap<Long, Node> nodesDump;
RoadNetwork.Builder roadNetworkBuilder; RoadNetwork.Builder roadNetworkBuilder;
private final List<String> drivableRoadTypes = Arrays.asList("tertiary", "residential", "living_street");
public OSMParser(RoadNetwork.Builder roadNetworkBuilder) { public OSMParser(RoadNetwork.Builder roadNetworkBuilder) {
this.roadNetworkBuilder = roadNetworkBuilder; this.roadNetworkBuilder = roadNetworkBuilder;
...@@ -64,15 +63,17 @@ public class OSMParser { ...@@ -64,15 +63,17 @@ public class OSMParser {
long startNodeId = nodes.getLong(0); long startNodeId = nodes.getLong(0);
long endNodeId = nodes.getLong(nodes.length() - 1); long endNodeId = nodes.getLong(nodes.length() - 1);
boolean oneway = this.isOneWay(element); boolean oneWay = this.isOneWay(element);
int maxSpeed = SegmentUtils.getMaxSpeed(element);
System.out.printf("Max speed: %d \n", maxSpeed);
System.out.printf("Way id: %d, Start node id : %d, End node id: %d \n", osmId, startNodeId, endNodeId); System.out.printf("Way id: %d, Start node id : %d, End node id: %d \n", osmId, startNodeId, endNodeId);
Segment segment = Segment.newBuilder() Segment segment = Segment.newBuilder()
.setOsmId(osmId) .setOsmId(osmId)
.setOneWay(oneway)
.addAllGeometry(line) .addAllGeometry(line)
.setOneWay(oneway) .setOneWay(oneWay)
.setMaxSpeed(maxSpeed)
.setId(this.roadNetworkBuilder.getSegmentsCount()) .setId(this.roadNetworkBuilder.getSegmentsCount())
.setCategory(SegmentUtils.parseRoadType(element)) .setCategory(SegmentUtils.parseRoadType(element))
.setStartNode(startNodeId) .setStartNode(startNodeId)
......
package map.builder.osm; package map.builder.osm;
import de.fuberlin.navigator.protos.map_builder.RoadCategory; import de.fuberlin.navigator.protos.map_builder.RoadCategory;
import de.fuberlin.navigator.protos.map_builder.RoadNetwork;
import org.json.JSONObject; import org.json.JSONObject;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class SegmentUtils { public class SegmentUtils {
private SegmentUtils() {} private SegmentUtils() {}
...@@ -27,6 +32,15 @@ public class SegmentUtils { ...@@ -27,6 +32,15 @@ public class SegmentUtils {
"road" "road"
); );
private static final Map<RoadCategory, Integer> speedMap = Map.ofEntries(
Map.entry(RoadCategory.ROAD_CATEGORY_HIGHWAY, 120),
Map.entry(RoadCategory.ROAD_CATEGORY_MAIN, 90),
Map.entry(RoadCategory.ROAD_CATEGORY_LOCAL, 50),
Map.entry(RoadCategory.ROAD_CATEGORY_RESIDENTIAL, 30),
Map.entry(RoadCategory.ROAD_CATEGORY_INVALID, 0)
);
private static final Pattern patternMaxSpeed = Pattern.compile("^([0-9][\\.0-9]+?)(?:[ ]?(?:km/h|kmh|kph|mph|knots))?$");
public static boolean isSegmentDrivable(JSONObject element) { public static boolean isSegmentDrivable(JSONObject element) {
return OSMJSONUtils.isTagValueOneOf(element, "highway", drivableRoads); return OSMJSONUtils.isTagValueOneOf(element, "highway", drivableRoads);
} }
...@@ -44,4 +58,22 @@ public class SegmentUtils { ...@@ -44,4 +58,22 @@ public class SegmentUtils {
default -> RoadCategory.ROAD_CATEGORY_INVALID; default -> RoadCategory.ROAD_CATEGORY_INVALID;
}; };
} }
public static int getMaxSpeed(JSONObject element) {
String maxSpeedTag = OSMTag.maxspeed.name();
// if there's a max speed tag, use it
if (OSMJSONUtils.hasTag(element, maxSpeedTag)) {
String maxspeed = OSMJSONUtils.getTag(element, maxSpeedTag);
Matcher matcherMaxSpeed = SegmentUtils.patternMaxSpeed.matcher(maxspeed);
if (matcherMaxSpeed.find()) {
return Integer.parseInt(matcherMaxSpeed.group(1));
}
}
// if no tag is available, parse the max speed based on road category
RoadCategory roadCategory = SegmentUtils.parseRoadType(element);
return SegmentUtils.speedMap.get(roadCategory);
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment