/
HillIndexGraphStorage.java
137 lines (107 loc) 路 4.36 KB
/
HillIndexGraphStorage.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/* This file is part of Openrouteservice.
*
* Openrouteservice is free software; you can redistribute it and/or modify it under the terms of the
* GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1
* of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public License along with this library;
* if not, see <https://www.gnu.org/licenses/>.
*/
package org.heigit.ors.routing.graphhopper.extensions.storages;
import com.graphhopper.storage.DataAccess;
import com.graphhopper.storage.Directory;
import com.graphhopper.storage.Graph;
import com.graphhopper.storage.GraphExtension;
import java.util.Map;
public class HillIndexGraphStorage implements GraphExtension {
private final int efHillIndex;
private DataAccess orsEdges;
protected int edgeEntryIndex = 0;
protected int edgeEntryBytes;
protected int edgesCount; // number of edges with custom values
private int maxHillIndex = 15;
private final byte[] byteValues;
public HillIndexGraphStorage(Map<String, String> parameters) {
efHillIndex = 0;
if (parameters.containsKey("maximum_slope"))
maxHillIndex = (int) Double.parseDouble(parameters.get("maximum_slope"));
edgeEntryBytes = edgeEntryIndex + (maxHillIndex > 15 ? 2 : 1);
edgesCount = 0;
byteValues = new byte[2];
}
public void init(Graph graph, Directory dir) {
if (edgesCount > 0)
throw new AssertionError("The ORS storage must be initialized only once.");
this.orsEdges = dir.find("ext_hillindex");
}
public HillIndexGraphStorage create(long initBytes) {
orsEdges.create(initBytes * edgeEntryBytes);
return this;
}
public void flush() {
orsEdges.setHeader(0, edgeEntryBytes);
orsEdges.setHeader(4, edgesCount);
orsEdges.flush();
}
@Override
public long getCapacity() {
return orsEdges.getCapacity();
}
public void close() {
orsEdges.close();
}
public int entries() {
return edgesCount;
}
public boolean loadExisting() {
if (!orsEdges.loadExisting())
throw new IllegalStateException("Unable to load storage 'ext_hillindex'. corrupt file or directory?");
edgeEntryBytes = orsEdges.getHeader(0);
edgesCount = orsEdges.getHeader(4);
return true;
}
private void ensureEdgesIndex(int edgeIndex) {
orsEdges.ensureCapacity(((long) edgeIndex + 1) * edgeEntryBytes);
}
private int getHillIndex(int value) {
return Math.min(value, maxHillIndex);
}
public void setEdgeValue(int edgeId, int hillIndex, int reverseHillIndex) {
edgesCount++;
ensureEdgesIndex(edgeId);
if (hillIndex != 0 || reverseHillIndex != 0) {
// add entry
long edgePointer = (long) edgeId * edgeEntryBytes;
if (maxHillIndex <= 15) {
byteValues[0] = (byte) (getHillIndex(hillIndex) << 4 | (0x0F & getHillIndex(reverseHillIndex))); //hillIndex | (reverseHillIndex << 4))
orsEdges.setBytes(edgePointer + efHillIndex, byteValues, 1);
} else {
byteValues[0] = (byte) getHillIndex(hillIndex);
byteValues[1] = (byte) getHillIndex(reverseHillIndex);
orsEdges.setBytes(edgePointer + efHillIndex, byteValues, 2);
}
}
}
public int getEdgeValue(int edgeId, boolean reverse, byte[] buffer) {
long edgePointer = (long) edgeId * edgeEntryBytes;
if (maxHillIndex <= 15) {
orsEdges.getBytes(edgePointer + efHillIndex, buffer, 1);
int value = buffer[0];
if (value < 0)
value = 256 + value;
if (reverse)
return (value >> 4) & 0xF;
else
return value & 0xF;
} else {
orsEdges.getBytes(edgePointer + efHillIndex, buffer, 2);
return reverse ? buffer[1] : buffer[0];
}
}
@Override
public boolean isClosed() {
return false;
}
}