forked from graphhopper/graphhopper
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
NoisyRoadNearby encoded value implementation
- Loading branch information
Showing
14 changed files
with
494 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
core/src/main/java/com/graphhopper/reader/osm/OSMNoisyRoad.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Licensed to GraphHopper GmbH under one or more contributor | ||
* license agreements. See the NOTICE file distributed with this work for | ||
* additional information regarding copyright ownership. | ||
* | ||
* GraphHopper GmbH licenses this file to you under the Apache License, | ||
* Version 2.0 (the "License"); you may not use this file except in | ||
* compliance with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.graphhopper.reader.osm; | ||
|
||
import com.graphhopper.routing.util.NoiseIndex; | ||
import org.locationtech.jts.geom.GeometryFactory; | ||
import org.locationtech.jts.geom.LineString; | ||
import org.locationtech.jts.geom.impl.PackedCoordinateSequence; | ||
|
||
import java.util.Arrays; | ||
|
||
public class OSMNoisyRoad implements NoiseIndex.Road { | ||
private static final GeometryFactory GEOMETRY_FACTORY = new GeometryFactory(); | ||
final PackedCoordinateSequence.Float road; | ||
|
||
public OSMNoisyRoad(int numNodes) { | ||
float[] coords = new float[numNodes * 2]; | ||
Arrays.fill(coords, Float.NaN); | ||
road = new PackedCoordinateSequence.Float(coords, 2, 0); | ||
} | ||
|
||
public void setCoordinate(int index, double lat, double lon) { | ||
// todonow: precision/double->float cast? | ||
road.setX(index, (float) lon); | ||
road.setY(index, (float) lat); | ||
} | ||
|
||
public boolean isValid() { | ||
float[] coords = road.getRawCoordinates(); | ||
for (float f : coords) | ||
if (Float.isNaN(f)) | ||
return false; | ||
return coords.length >= 2 ; | ||
} | ||
|
||
public LineString getRoad() { | ||
return GEOMETRY_FACTORY.createLineString(this.road); | ||
} | ||
} |
113 changes: 113 additions & 0 deletions
113
core/src/main/java/com/graphhopper/reader/osm/OSMNoisyRoadData.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
/* | ||
* Licensed to GraphHopper GmbH under one or more contributor | ||
* license agreements. See the NOTICE file distributed with this work for | ||
* additional information regarding copyright ownership. | ||
* | ||
* GraphHopper GmbH licenses this file to you under the Apache License, | ||
* Version 2.0 (the "License"); you may not use this file except in | ||
* compliance with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.graphhopper.reader.osm; | ||
|
||
import com.carrotsearch.hppc.IntArrayList; | ||
import com.carrotsearch.hppc.LongArrayList; | ||
import com.carrotsearch.hppc.cursors.LongCursor; | ||
import com.graphhopper.coll.GHLongLongBTree; | ||
import com.graphhopper.coll.LongLongMap; | ||
import com.graphhopper.reader.ReaderNode; | ||
import com.graphhopper.reader.ReaderWay; | ||
import com.graphhopper.storage.Directory; | ||
import com.graphhopper.util.BitUtil; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class OSMNoisyRoadData { | ||
|
||
private final List<OSMNoisyRoad> osmNoisyRoads; | ||
private final LongToTwoIntsMap osmNoisyRoadsNodeIndicesByOSMNodeIds; | ||
|
||
public OSMNoisyRoadData() { | ||
osmNoisyRoads = new ArrayList<>(); | ||
osmNoisyRoadsNodeIndicesByOSMNodeIds = new LongToTwoIntsMap(); | ||
} | ||
|
||
public void addOSMNoisyRoadWithoutCoordinates(LongArrayList osmNodeIds) { | ||
osmNoisyRoads.add(new OSMNoisyRoad(osmNodeIds.size())); | ||
for (LongCursor node : osmNodeIds) | ||
osmNoisyRoadsNodeIndicesByOSMNodeIds.put(node.value, osmNoisyRoads.size() - 1, node.index); | ||
} | ||
|
||
public void fillOSMNoisyRoadNodeCoordinates(ReaderNode node) { | ||
long index = osmNoisyRoadsNodeIndicesByOSMNodeIds.get(node.getId()); | ||
if (index >= 0) { | ||
int osmNoisyRoadIndex = BitUtil.LITTLE.getIntLow(index); | ||
int nodeIndex = BitUtil.LITTLE.getIntHigh(index); | ||
OSMNoisyRoad osmNoisyRoad = osmNoisyRoads.get(osmNoisyRoadIndex); | ||
// Note that we set the coordinates only for one particular node for one particular | ||
// osm area, even though the same osm node might be used in multiple such areas. We will | ||
// fix the next time we get to see the osm area ways. | ||
osmNoisyRoad.setCoordinate(nodeIndex, node.getLat(), node.getLon()); | ||
} | ||
} | ||
|
||
public void clear(){ | ||
osmNoisyRoadsNodeIndicesByOSMNodeIds.clear(); | ||
} | ||
|
||
public void fixOSMNoisyRoads(int osmNoisyWayIndex, ReaderWay way) { | ||
// The problem we solve here is that some osm nodes are used by multiple noisy ways. | ||
// At the very least this is the case for the first and last node of the ways, | ||
// but there are also many junctions that share a common node. Since we only store one | ||
// index into the coordinate array of the noisy roads, the coordinates for some noisy roads | ||
// nodes won't be set. Therefore, we need to make up for this here where we get to see the | ||
// ways a second time. If this wasn't the case we could read the noisy ways&nodes in pass1 | ||
// instead of pass0 which would allow us to skip nodes and ways in pass0 (faster import). | ||
OSMNoisyRoad actual = osmNoisyRoads.get(osmNoisyWayIndex); | ||
for (LongCursor node : way.getNodes()) { | ||
long index = osmNoisyRoadsNodeIndicesByOSMNodeIds.get(node.value); | ||
int osmNoisyRoadIndex = BitUtil.LITTLE.getIntLow(index); | ||
int nodeIndex = BitUtil.LITTLE.getIntHigh(index); | ||
OSMNoisyRoad osmNoisyRoad = osmNoisyRoads.get(osmNoisyRoadIndex); | ||
actual.road.setX(node.index, osmNoisyRoad.road.getX(nodeIndex)); | ||
actual.road.setY(node.index, osmNoisyRoad.road.getY(nodeIndex)); | ||
} | ||
} | ||
|
||
public List<OSMNoisyRoad> getOsmNoisyRoads() { | ||
return osmNoisyRoads; | ||
} | ||
|
||
public static class LongToTwoIntsMap { | ||
private final LongLongMap internalIdsByKey = new GHLongLongBTree(200, 4, -1); | ||
private final IntArrayList vals1 = new IntArrayList(); | ||
private final IntArrayList vals2 = new IntArrayList(); | ||
|
||
public void put(long key, int val1, int val2) { | ||
vals1.add(val1); | ||
vals2.add(val2); | ||
internalIdsByKey.put(key, vals1.size() - 1); | ||
} | ||
|
||
public long get(long key) { | ||
long id = internalIdsByKey.get(key); | ||
if (id < 0) return -1; | ||
return BitUtil.LITTLE.toLong(vals1.get((int)id), vals2.get((int)id)); | ||
} | ||
|
||
public void clear() { | ||
internalIdsByKey.clear(); | ||
vals1.release(); | ||
vals2.release(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.