|
17 | 17 | */
|
18 | 18 | package com.graphhopper.util;
|
19 | 19 |
|
20 |
| -import java.text.DateFormat; |
21 |
| -import java.text.DecimalFormat; |
22 |
| -import java.text.DecimalFormatSymbols; |
23 | 20 | import java.util.*;
|
24 | 21 |
|
25 | 22 | /**
|
26 | 23 | * List of instructions.
|
27 | 24 | */
|
28 | 25 | public class InstructionList extends AbstractList<Instruction> {
|
29 | 26 |
|
30 |
| - static String simpleXMLEscape(String str) { |
31 |
| - // We could even use the 'more flexible' CDATA section but for now do the following. The 'and' could be important sometimes: |
32 |
| - return str.replaceAll("&", "&"). |
33 |
| - // but do not care for: |
34 |
| - replaceAll("[\\<\\>]", "_"); |
35 |
| - } |
36 |
| - |
37 | 27 | private final List<Instruction> instructions;
|
38 | 28 | private final Translation tr;
|
39 | 29 |
|
@@ -71,211 +61,6 @@ public Instruction remove(int index) {
|
71 | 61 | return instructions.remove(index);
|
72 | 62 | }
|
73 | 63 |
|
74 |
| - public void replaceLast(Instruction instr) { |
75 |
| - if (instructions.isEmpty()) |
76 |
| - throw new IllegalStateException("Cannot replace last instruction as list is empty"); |
77 |
| - |
78 |
| - instructions.set(instructions.size() - 1, instr); |
79 |
| - } |
80 |
| - |
81 |
| - public List<Map<String, Object>> createJson() { |
82 |
| - List<Map<String, Object>> instrList = new ArrayList<>(instructions.size()); |
83 |
| - int pointsIndex = 0; |
84 |
| - int counter = 0; |
85 |
| - for (Instruction instruction : instructions) { |
86 |
| - Map<String, Object> instrJson = new HashMap<>(); |
87 |
| - instrList.add(instrJson); |
88 |
| - |
89 |
| - InstructionAnnotation ia = instruction.getAnnotation(); |
90 |
| - String text = instruction.getTurnDescription(tr); |
91 |
| - if (Helper.isEmpty(text)) |
92 |
| - text = ia.getMessage(); |
93 |
| - instrJson.put("text", Helper.firstBig(text)); |
94 |
| - if (!ia.isEmpty()) { |
95 |
| - instrJson.put("annotation_text", ia.getMessage()); |
96 |
| - instrJson.put("annotation_importance", ia.getImportance()); |
97 |
| - } |
98 |
| - |
99 |
| - instrJson.put("street_name", instruction.getName()); |
100 |
| - instrJson.put("time", instruction.getTime()); |
101 |
| - instrJson.put("distance", Helper.round(instruction.getDistance(), 3)); |
102 |
| - instrJson.put("sign", instruction.getSign()); |
103 |
| - instrJson.putAll(instruction.getExtraInfoJSON()); |
104 |
| - |
105 |
| - int tmpIndex = pointsIndex + instruction.getLength(); |
106 |
| - instrJson.put("interval", Arrays.asList(pointsIndex, tmpIndex)); |
107 |
| - pointsIndex = tmpIndex; |
108 |
| - |
109 |
| - counter++; |
110 |
| - } |
111 |
| - return instrList; |
112 |
| - } |
113 |
| - |
114 |
| - /** |
115 |
| - * @return This method returns a list of gpx entries where the time (in millis) is relative to |
116 |
| - * the first which is 0. |
117 |
| - */ |
118 |
| - public List<GPXEntry> createGPXList() { |
119 |
| - if (isEmpty()) |
120 |
| - return Collections.emptyList(); |
121 |
| - |
122 |
| - List<GPXEntry> gpxList = new ArrayList<>(); |
123 |
| - long timeOffset = 0; |
124 |
| - for (int i = 0; i < size() - 1; i++) { |
125 |
| - Instruction prevInstr = (i > 0) ? get(i - 1) : null; |
126 |
| - boolean instrIsFirst = prevInstr == null; |
127 |
| - Instruction nextInstr = get(i + 1); |
128 |
| - nextInstr.checkOne(); |
129 |
| - // current instruction does not contain last point which is equals to first point of next instruction: |
130 |
| - timeOffset = get(i).fillGPXList(gpxList, timeOffset, prevInstr, nextInstr, instrIsFirst); |
131 |
| - } |
132 |
| - Instruction lastI = get(size() - 1); |
133 |
| - if (lastI.points.size() != 1) |
134 |
| - throw new IllegalStateException("Last instruction must have exactly one point but was " + lastI.points.size()); |
135 |
| - double lastLat = lastI.getFirstLat(), lastLon = lastI.getFirstLon(), |
136 |
| - lastEle = lastI.getPoints().is3D() ? lastI.getFirstEle() : Double.NaN; |
137 |
| - gpxList.add(new GPXEntry(lastLat, lastLon, lastEle, timeOffset)); |
138 |
| - return gpxList; |
139 |
| - } |
140 |
| - |
141 |
| - /** |
142 |
| - * Creates the standard GPX string out of the points according to the schema found here: |
143 |
| - * https://graphhopper.com/public/schema/gpx-1.1.xsd |
144 |
| - * <p> |
145 |
| - * |
146 |
| - * @return string to be stored as gpx file |
147 |
| - */ |
148 |
| - public String createGPX(String version) { |
149 |
| - return createGPX("GraphHopper", new Date().getTime(), version); |
150 |
| - } |
151 |
| - |
152 |
| - public String createGPX(String trackName, long startTimeMillis, String version) { |
153 |
| - boolean includeElevation = size() > 0 && get(0).getPoints().is3D(); |
154 |
| - return createGPX(trackName, startTimeMillis, includeElevation, true, true, true, version); |
155 |
| - } |
156 |
| - |
157 |
| - private void createWayPointBlock(StringBuilder output, Instruction instruction, DecimalFormat decimalFormat) { |
158 |
| - output.append("\n<wpt "); |
159 |
| - output.append("lat=\"").append(decimalFormat.format(instruction.getFirstLat())); |
160 |
| - output.append("\" lon=\"").append(decimalFormat.format(instruction.getFirstLon())).append("\">"); |
161 |
| - String name; |
162 |
| - if (instruction.getName().isEmpty()) |
163 |
| - name = instruction.getTurnDescription(tr); |
164 |
| - else |
165 |
| - name = instruction.getName(); |
166 |
| - |
167 |
| - output.append(" <name>").append(simpleXMLEscape(name)).append("</name>"); |
168 |
| - output.append("</wpt>"); |
169 |
| - } |
170 |
| - |
171 |
| - public String createGPX(String trackName, long startTimeMillis, boolean includeElevation, boolean withRoute, boolean withTrack, boolean withWayPoints, String version) { |
172 |
| - DateFormat formatter = Helper.createFormatter(); |
173 |
| - |
174 |
| - DecimalFormat decimalFormat = new DecimalFormat("#", DecimalFormatSymbols.getInstance(Locale.ROOT)); |
175 |
| - decimalFormat.setMinimumFractionDigits(1); |
176 |
| - decimalFormat.setMaximumFractionDigits(6); |
177 |
| - decimalFormat.setMinimumIntegerDigits(1); |
178 |
| - |
179 |
| - String header = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\" ?>" |
180 |
| - + "<gpx xmlns=\"http://www.topografix.com/GPX/1/1\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" |
181 |
| - + " creator=\"Graphhopper version " + version + "\" version=\"1.1\"" |
182 |
| - // This xmlns:gh acts only as ID, no valid URL necessary. |
183 |
| - // Use a separate namespace for custom extensions to make basecamp happy. |
184 |
| - + " xmlns:gh=\"https://graphhopper.com/public/schema/gpx/1.1\">" |
185 |
| - + "\n<metadata>" |
186 |
| - + "<copyright author=\"OpenStreetMap contributors\"/>" |
187 |
| - + "<link href=\"http://graphhopper.com\">" |
188 |
| - + "<text>GraphHopper GPX</text>" |
189 |
| - + "</link>" |
190 |
| - + "<time>" + formatter.format(startTimeMillis) + "</time>" |
191 |
| - + "</metadata>"; |
192 |
| - StringBuilder gpxOutput = new StringBuilder(header); |
193 |
| - if (!isEmpty()) { |
194 |
| - if (withWayPoints) { |
195 |
| - createWayPointBlock(gpxOutput, instructions.get(0), decimalFormat); // Start |
196 |
| - for (Instruction currInstr : instructions) { |
197 |
| - if ((currInstr.getSign() == Instruction.REACHED_VIA) // Via |
198 |
| - || (currInstr.getSign() == Instruction.FINISH)) // End |
199 |
| - { |
200 |
| - createWayPointBlock(gpxOutput, currInstr, decimalFormat); |
201 |
| - } |
202 |
| - } |
203 |
| - } |
204 |
| - if (withRoute) { |
205 |
| - gpxOutput.append("\n<rte>"); |
206 |
| - Instruction nextInstr = null; |
207 |
| - for (Instruction currInstr : instructions) { |
208 |
| - if (null != nextInstr) |
209 |
| - createRteptBlock(gpxOutput, nextInstr, currInstr, decimalFormat); |
210 |
| - |
211 |
| - nextInstr = currInstr; |
212 |
| - } |
213 |
| - createRteptBlock(gpxOutput, nextInstr, null, decimalFormat); |
214 |
| - gpxOutput.append("\n</rte>"); |
215 |
| - } |
216 |
| - } |
217 |
| - if (withTrack) { |
218 |
| - gpxOutput.append("\n<trk><name>").append(trackName).append("</name>"); |
219 |
| - |
220 |
| - gpxOutput.append("<trkseg>"); |
221 |
| - for (GPXEntry entry : createGPXList()) { |
222 |
| - gpxOutput.append("\n<trkpt lat=\"").append(decimalFormat.format(entry.getLat())); |
223 |
| - gpxOutput.append("\" lon=\"").append(decimalFormat.format(entry.getLon())).append("\">"); |
224 |
| - if (includeElevation) |
225 |
| - gpxOutput.append("<ele>").append(Helper.round2(entry.getEle())).append("</ele>"); |
226 |
| - gpxOutput.append("<time>").append(formatter.format(startTimeMillis + entry.getTime())).append("</time>"); |
227 |
| - gpxOutput.append("</trkpt>"); |
228 |
| - } |
229 |
| - gpxOutput.append("\n</trkseg>"); |
230 |
| - gpxOutput.append("\n</trk>"); |
231 |
| - } |
232 |
| - |
233 |
| - // we could now use 'wpt' for via points |
234 |
| - gpxOutput.append("\n</gpx>"); |
235 |
| - return gpxOutput.toString(); |
236 |
| - } |
237 |
| - |
238 |
| - public void createRteptBlock(StringBuilder output, Instruction instruction, Instruction nextI, DecimalFormat decimalFormat) { |
239 |
| - output.append("\n<rtept lat=\"").append(decimalFormat.format(instruction.getFirstLat())). |
240 |
| - append("\" lon=\"").append(decimalFormat.format(instruction.getFirstLon())).append("\">"); |
241 |
| - |
242 |
| - if (!instruction.getName().isEmpty()) |
243 |
| - output.append("<desc>").append(simpleXMLEscape(instruction.getTurnDescription(tr))).append("</desc>"); |
244 |
| - |
245 |
| - output.append("<extensions>"); |
246 |
| - output.append("<gh:distance>").append(Helper.round(instruction.getDistance(), 1)).append("</gh:distance>"); |
247 |
| - output.append("<gh:time>").append(instruction.getTime()).append("</gh:time>"); |
248 |
| - |
249 |
| - String direction = instruction.calcDirection(nextI); |
250 |
| - if (!direction.isEmpty()) |
251 |
| - output.append("<gh:direction>").append(direction).append("</gh:direction>"); |
252 |
| - |
253 |
| - double azimuth = instruction.calcAzimuth(nextI); |
254 |
| - if (!Double.isNaN(azimuth)) |
255 |
| - output.append("<gh:azimuth>").append(Helper.round2(azimuth)).append("</gh:azimuth>"); |
256 |
| - |
257 |
| - if (instruction instanceof RoundaboutInstruction) { |
258 |
| - RoundaboutInstruction ri = (RoundaboutInstruction) instruction; |
259 |
| - |
260 |
| - output.append("<gh:exit_number>").append(ri.getExitNumber()).append("</gh:exit_number>"); |
261 |
| - } |
262 |
| - |
263 |
| - output.append("<gh:sign>").append(instruction.getSign()).append("</gh:sign>"); |
264 |
| - output.append("</extensions>"); |
265 |
| - output.append("</rtept>"); |
266 |
| - } |
267 |
| - |
268 |
| - /** |
269 |
| - * @return list of lat lon |
270 |
| - */ |
271 |
| - List<List<Double>> createStartPoints() { |
272 |
| - List<List<Double>> res = new ArrayList<>(instructions.size()); |
273 |
| - for (Instruction instruction : instructions) { |
274 |
| - res.add(Arrays.asList(instruction.getFirstLat(), instruction.getFirstLon())); |
275 |
| - } |
276 |
| - return res; |
277 |
| - } |
278 |
| - |
279 | 64 | /**
|
280 | 65 | * This method is useful for navigation devices to find the next instruction for the specified
|
281 | 66 | * coordinate (e.g. the current position).
|
@@ -339,4 +124,8 @@ public Instruction find(double lat, double lon, double maxDistance) {
|
339 | 124 | return get(foundInstruction);
|
340 | 125 | }
|
341 | 126 |
|
| 127 | + public Translation getTr() { |
| 128 | + return tr; |
| 129 | + } |
| 130 | + |
342 | 131 | }
|
0 commit comments