Skip to content

Commit e62669a

Browse files
author
Karl Hübner
committed
Map only required osm way ids
1 parent ee858eb commit e62669a

File tree

2 files changed

+66
-32
lines changed

2 files changed

+66
-32
lines changed

core/src/main/java/com/graphhopper/reader/OSMReader.java

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,12 @@
2020
import static com.graphhopper.util.Helper.nf;
2121
import gnu.trove.list.TLongList;
2222
import gnu.trove.list.array.TLongArrayList;
23+
import gnu.trove.map.TIntLongMap;
2324
import gnu.trove.map.TLongLongMap;
25+
import gnu.trove.map.hash.TIntLongHashMap;
2426
import gnu.trove.map.hash.TLongLongHashMap;
27+
import gnu.trove.set.TLongSet;
28+
import gnu.trove.set.hash.TLongHashSet;
2529

2630
import java.io.File;
2731
import java.io.IOException;
@@ -44,7 +48,6 @@
4448
import com.graphhopper.storage.GraphHopperStorage;
4549
import com.graphhopper.storage.GraphStorage;
4650
import com.graphhopper.storage.TurnCostStorage;
47-
import com.graphhopper.util.BitUtil;
4851
import com.graphhopper.util.DistanceCalc;
4952
import com.graphhopper.util.DistanceCalcEarth;
5053
import com.graphhopper.util.DouglasPeucker;
@@ -106,20 +109,21 @@ public class OSMReader
106109
private LongIntMap osmNodeIdToInternalNodeMap;
107110
private TLongLongHashMap osmNodeIdToNodeFlagsMap;
108111
private TLongLongHashMap osmWayIdToRouteWeightMap;
112+
private TLongHashSet osmIdStoreRequiredSet; //stores osm ids used by relations to identify which edge ids needs to be mapped later
113+
private TIntLongMap edgeIdToOsmidMap;
109114
private final TLongList barrierNodeIDs = new TLongArrayList();
110115
protected DataAccess pillarLats;
111116
protected DataAccess pillarLons;
112-
protected DataAccess edgeOsmIds;
113117
private final DistanceCalc distCalc = new DistanceCalcEarth();
114118
private final DouglasPeucker simplifyAlgo = new DouglasPeucker();
115119
private int nextTowerId = 0;
116120
private int nextPillarId = 0;
117121
// negative but increasing to avoid clash with custom created OSM files
118122
private long newUniqueOSMId = -Long.MAX_VALUE;
119123
private boolean exitOnlyPillarNodeException = true;
124+
120125
//relation factory specifies an arbitrary osm relation
121126
private OSMRelationFactory relationFactory = new OSMRelationFactory();
122-
private final BitUtil bitUtil;
123127

124128
public OSMReader( GraphStorage storage, long expectedCap )
125129
{
@@ -133,13 +137,10 @@ public OSMReader( GraphStorage storage, long expectedCap )
133137
dir = graphStorage.getDirectory();
134138
pillarLats = dir.find("tmpLatitudes");
135139
pillarLons = dir.find("tmpLongitudes");
136-
edgeOsmIds = dir.find("tmpEdgeOsmIds");
137140
pillarLats.create(Math.max(expectedCap, 100));
138141
pillarLons.create(Math.max(expectedCap, 100));
139-
edgeOsmIds.create(Math.max(expectedCap, 100));
140142

141143
relationFactory = new OSMRelationFactory();
142-
bitUtil = BitUtil.get(dir.getByteOrder());
143144
}
144145

145146
public void doOSM2Graph( File osmFile ) throws IOException
@@ -203,6 +204,11 @@ void preProcess( File osmFile )
203204
prepareWaysWithRelationInfo(relation);
204205
}
205206

207+
if (relation.hasTag("type", "restriction"))
208+
{
209+
prepareRestrictionRelation(relation);
210+
}
211+
206212
if (++tmpRelationCounter % 50000 == 0)
207213
{
208214
logger.info(nf(tmpRelationCounter) + " (preprocess), osmWayMap:" + nf(getRelFlagsMap().size())
@@ -220,6 +226,34 @@ void preProcess( File osmFile )
220226
}
221227
}
222228

229+
private void prepareRestrictionRelation( OSMRelation relation )
230+
{
231+
OSMRelation specifiedRelation = relationFactory.specify(relation);
232+
if (specifiedRelation != null && specifiedRelation instanceof OSMTurnRelation)
233+
{
234+
getOsmIdStoreRequiredSet().add(((OSMTurnRelation) specifiedRelation).getOsmIdFrom());
235+
getOsmIdStoreRequiredSet().add(((OSMTurnRelation) specifiedRelation).getOsmIdTo());
236+
}
237+
}
238+
239+
private TLongSet getOsmIdStoreRequiredSet()
240+
{
241+
if (osmIdStoreRequiredSet == null)
242+
{
243+
osmIdStoreRequiredSet = new TLongHashSet();
244+
}
245+
return osmIdStoreRequiredSet;
246+
}
247+
248+
private TIntLongMap getEdgeIdToOsmidMap()
249+
{
250+
if (edgeIdToOsmidMap == null)
251+
{
252+
edgeIdToOsmidMap = new TIntLongHashMap(getOsmIdStoreRequiredSet().size());
253+
}
254+
return edgeIdToOsmidMap;
255+
}
256+
223257
/**
224258
* Filter ways but do not analyze properties wayNodes will be filled with participating node
225259
* ids.
@@ -453,9 +487,7 @@ public void processRelation( OSMRelation relation ) throws XMLStreamException
453487

454488
public long getOsmIdOfInternalEdge( int edgeId )
455489
{
456-
byte[] bytesOsmWayId = new byte[8];
457-
edgeOsmIds.getBytes(edgeId * 8, bytesOsmWayId, 8);
458-
return bitUtil.toLong(bytesOsmWayId);
490+
return getEdgeIdToOsmidMap().get(edgeId);
459491
}
460492

461493
public int getInternalNodeIdOfOsmNode( long nodeOsmId )
@@ -735,9 +767,10 @@ EdgeIteratorState addEdge( int fromIndex, int toIndex, PointList pointList, long
735767

736768
private void storeOSMWayID( int edgeId, long osmWayID )
737769
{
738-
long ptr = (long) edgeId * 8;
739-
edgeOsmIds.incCapacity(ptr + 8);
740-
edgeOsmIds.setBytes(ptr, bitUtil.fromLong(osmWayID), 8);
770+
if (getOsmIdStoreRequiredSet().contains(osmWayID))
771+
{
772+
getEdgeIdToOsmidMap().put(edgeId, osmWayID);
773+
}
741774
}
742775

743776
/**
@@ -776,10 +809,8 @@ void finishedReading()
776809
printInfo("way");
777810
dir.remove(pillarLats);
778811
dir.remove(pillarLons);
779-
dir.remove(edgeOsmIds);
780812
pillarLons = null;
781813
pillarLats = null;
782-
edgeOsmIds = null;
783814
osmNodeIdToInternalNodeMap = null;
784815
osmNodeIdToNodeFlagsMap = null;
785816
osmWayIdToRouteWeightMap = null;

core/src/main/java/com/graphhopper/reader/OSMTurnRelation.java

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,8 @@ public class OSMTurnRelation extends OSMRelation
1818
{
1919

2020
public static final int TYPE_UNSUPPORTED = 0;
21-
public static final int TYPE_NO_LEFT_TURN = 1;
22-
public static final int TYPE_NO_RIGHT_TURN = 2;
23-
public static final int TYPE_NO_STRAIGHT_ON = 3;
24-
public static final int TYPE_ONLY_RIGHT_TURN = 4;
25-
public static final int TYPE_ONLY_LEFT_TURN = 5;
26-
public static final int TYPE_ONLY_STRAIGHT_ON = 6;
27-
public static final int TYPE_NO_U_TURN = 7;
21+
public static final int TYPE_NOT = 1;
22+
public static final int TYPE_ONLY = 2;
2823

2924
private long fromOsm;
3025
private long viaOsm;
@@ -42,6 +37,16 @@ private boolean isValid()
4237
return restriction != TYPE_UNSUPPORTED && viaOsm >= 0 && fromOsm >= 0 && toOsm >= 0;
4338
}
4439

40+
long getOsmIdFrom()
41+
{
42+
return fromOsm;
43+
}
44+
45+
long getOsmIdTo()
46+
{
47+
return toOsm;
48+
}
49+
4550
/**
4651
* transforms this relation into a collection of node cost entries
4752
* <p>
@@ -81,8 +86,7 @@ public Collection<TurnCostTableEntry> getRestrictionAsEntries( TurnCostEncoder e
8186
iter = edgeOutExplorer.setBaseNode(viaNodeId);
8287
if (edgeIdFrom != EdgeIterator.NO_EDGE)
8388
{
84-
if (this.restriction == TYPE_NO_U_TURN || this.restriction == TYPE_NO_LEFT_TURN
85-
|| this.restriction == TYPE_NO_RIGHT_TURN || this.restriction == TYPE_NO_STRAIGHT_ON)
89+
if (this.restriction == TYPE_NOT)
8690
{
8791
// if we have a restriction of TYPE_NO_* we add restriction only to
8892
// the given turn (from, via, to)
@@ -99,8 +103,7 @@ public Collection<TurnCostTableEntry> getRestrictionAsEntries( TurnCostEncoder e
99103
}
100104
}
101105

102-
} else if (this.restriction == TYPE_ONLY_RIGHT_TURN || this.restriction == TYPE_ONLY_LEFT_TURN
103-
|| this.restriction == TYPE_ONLY_STRAIGHT_ON)
106+
} else if (this.restriction == TYPE_ONLY)
104107
{
105108
// if we have a restriction of TYPE_ONLY_* we add restriction to
106109
// any turn possibility (from, via, * ) except the given turn
@@ -188,25 +191,25 @@ private int getRestrictionType( String restrictionType )
188191
{
189192
if ("no_left_turn".equals(restrictionType))
190193
{
191-
return OSMTurnRelation.TYPE_NO_LEFT_TURN;
194+
return OSMTurnRelation.TYPE_NOT;
192195
} else if ("no_right_turn".equals(restrictionType))
193196
{
194-
return OSMTurnRelation.TYPE_NO_RIGHT_TURN;
197+
return OSMTurnRelation.TYPE_NOT;
195198
} else if ("no_straight_on".equals(restrictionType))
196199
{
197-
return OSMTurnRelation.TYPE_NO_STRAIGHT_ON;
200+
return OSMTurnRelation.TYPE_NOT;
198201
} else if ("no_u_turn".equals(restrictionType))
199202
{
200-
return OSMTurnRelation.TYPE_NO_U_TURN;
203+
return OSMTurnRelation.TYPE_ONLY;
201204
} else if ("only_right_turn".equals(restrictionType))
202205
{
203-
return OSMTurnRelation.TYPE_ONLY_RIGHT_TURN;
206+
return OSMTurnRelation.TYPE_ONLY;
204207
} else if ("only_left_turn".equals(restrictionType))
205208
{
206-
return OSMTurnRelation.TYPE_ONLY_LEFT_TURN;
209+
return OSMTurnRelation.TYPE_ONLY;
207210
} else if ("only_straight_on".equals(restrictionType))
208211
{
209-
return OSMTurnRelation.TYPE_ONLY_STRAIGHT_ON;
212+
return OSMTurnRelation.TYPE_ONLY;
210213
}
211214
return OSMTurnRelation.TYPE_UNSUPPORTED;
212215
}

0 commit comments

Comments
 (0)