Debian Codegraph @

src/uk/me/parabola/imgfmt/app/trergn/Polyline.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2006 - 2012.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 3 or
6 * version 2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 */
13
14 package uk.me.parabola.imgfmt.app.trergn;
15
16 import java.io.IOException;
17 import java.io.OutputStream;
18 import java.util.ArrayList;
19 import java.util.List;
20
21 import uk.me.parabola.imgfmt.app.BitWriter;
22 import uk.me.parabola.imgfmt.app.Coord;
23 import uk.me.parabola.imgfmt.app.ImgFileWriter;
24 import uk.me.parabola.imgfmt.app.Label;
25 import uk.me.parabola.imgfmt.app.net.RoadDef;
26 import uk.me.parabola.log.Logger;
27
28 /**
29 * Represents a multi-segment line. Eg for a road. As with all map objects
30 * it can only exist as part of a subdivision.
31 *
32 * Writing these out is particularly tricky as deltas between points are packed
33 * into the smallest number of bits possible.
34 *
35 * I am not trying to make the smallest map, so it will not be totally optimum.
36 *
37 * @author Steve Ratcliffe
38 */
39 public class Polyline extends MapObject {
40 private static final Logger log = Logger.getLogger(Polyline.class);
41
42 // flags in the label offset
43 private static final int FLAG_NETINFO = 0x800000;
44 private static final int FLAG_EXTRABIT = 0x400000;
45
46 // flags in the type
47 private static final int FLAG_DIR = 0x40;
48 private static final int FLAG_2BYTE_LEN = 0x80;
49
50 // Reference to NET section, if any
51 private RoadDef roaddef;
52
53 // If a road gets subdivided into several segments, this
54 // says whether this line is the last segment. Need this
55 // for writing extra bits.
56 private boolean lastSegment = true;
57
58 // Set if it is a one-way street for example.
59 private boolean direction;
60
61 // further labels, if any
62 private List<Label> refLabels;
63
64 // The actual points that make up the line.
65 private final List<Coord> points = new ArrayList<>();
66
67 public Polyline(Subdivision div) {
68 setSubdiv(div);
69 }
70
71 /**
72 * Format and write the contents of the object to the given
73 * file.
74 *
75 * @param file A reference to the file that should be written to.
76 */
77 public void write(ImgFileWriter file) {
78
79 // Prepare for writing by doing all the required calculations.
80
81 LinePreparer w;
82 try {
83 // Prepare the information that we need.
84 w = new LinePreparer(this);
85 } catch (AssertionError ae) {
86 log.error("Problem writing line (" + getClass() + ") of type 0x" + Integer.toHexString(getType()) + " containing " + points.size() + " points and starting at " + points.get(0).toOSMURL());
87 log.error(" Subdivision shift is " + getSubdiv().getShift() +
88 " and its centre is at " + getSubdiv().getCenter().toOSMURL());
89 log.error(" " + ae.getMessage());
90 if(roaddef != null)
91 log.error(" Way is " + roaddef);
92 return;
93 }
94
95 int minPointsRequired = (this instanceof Polygon)? 3 : 2;
96 BitWriter bw = w.makeShortestBitStream(minPointsRequired);
97 if (bw == null) {
98 log.error("Level " + getSubdiv().getZoom().getLevel() + " " + ((this instanceof Polygon)? "polygon" : "polyline") + " has less than " + minPointsRequired + " points, discarding");
99 return;
100 }
101
102 // The type of feature, also contains a couple of flags hidden inside.
103 int b1 = getType();
104 if (direction)
105 b1 |= FLAG_DIR; // Polylines only.
106
107 int blen = bw.getLength() - 1; // allow for the sizes
108 assert blen > 0 : "zero length bitstream";
109 assert blen < 0x10000 : "bitstream too long " + blen;
110 if (blen >= 0x100)
111 b1 |= FLAG_2BYTE_LEN;
112
113 file.put1u(b1);
114
115 // The label, contains a couple of flags within it.
116 int loff = getLabel().getOffset();
117 if (w.isExtraBit())
118 loff |= FLAG_EXTRABIT;
119
120 // If this is a road, then we need to save the offset of the label
121 // so that we can change it to the index in the net section
122 if (roaddef != null) {
123 roaddef.addLabel(getLabel());
124 roaddef.addOffsetTarget(file.position(),
125 FLAG_NETINFO | (loff & FLAG_EXTRABIT));
126 // also add ref label(s) if present
127 if(refLabels != null)
128 refLabels.forEach(roaddef::addLabel);
129 }
130
131 file.put3u(loff);
132
133 // The delta of the longitude from the subdivision centre point
134 // note that this has already been calculated.
135 file.put2s(getDeltaLong());
136 file.put2s(getDeltaLat());
137 if(log.isDebugEnabled())
138 log.debug("out center", getDeltaLat(), getDeltaLong());
139
140 file.putNu(blen < 0x100 ? 1 : 2, blen);
141
142 file.put(bw.getBytes(), 0, blen+1);
143 }
144
145 /*
146 * write the polyline to an OutputStream - only use for outputting
147 * lines with extended (3 byte) types.
148 *
149 */
150 public void write(OutputStream stream) throws IOException {
151 assert hasExtendedType();
152 int type = getType();
153 int labelOff = getLabel().getOffset();
154 byte[] extraBytes = getExtTypeExtraBytes();
155
156 LinePreparer w;
157 try {
158 // need to prepare line info before outputing lat/lon
159 w = new LinePreparer(this);
160 }
161 catch (AssertionError ae) {
162 log.error("Problem writing line (" + getClass() + ") of type 0x" + Integer.toHexString(getType()) + " containing " + points.size() + " points and starting at " + points.get(0).toOSMURL());
163 log.error(" Subdivision shift is " + getSubdiv().getShift() +
164 " and its centre is at " + getSubdiv().getCenter().toOSMURL());
165 log.error(" " + ae.getMessage());
166 if(roaddef != null)
167 log.error(" Way is " + roaddef);
168 return;
169 }
170 int minPointsRequired = (this instanceof Polygon)? 3 : 2;
171 BitWriter bw = w.makeShortestBitStream(minPointsRequired);
172 if(bw == null) {
173 log.error("Level " + getSubdiv().getZoom().getLevel() + " " + ((this instanceof Polygon)? "polygon" : "polyline") + " has less than " + minPointsRequired + " points, discarding");
174 return;
175 }
176 int blen = bw.getLength();
177 assert blen > 1 : "zero length bitstream";
178 assert blen < 0x10000 : "bitstream too long " + blen;
179
180 if(labelOff != 0)
181 type |= 0x20; // has label
182 if (direction)
183 type |= FLAG_DIR;
184 if(extraBytes != null)
185 type |= 0x80; // has extra bytes
186 stream.write(type >> 8);
187 stream.write(type);
188
189 int deltaLong = getDeltaLong();
190 int deltaLat = getDeltaLat();
191 stream.write(deltaLong);
192 stream.write(deltaLong >> 8);
193 stream.write(deltaLat);
194 stream.write(deltaLat >> 8);
195
196 if (blen >= 0x7f) {
197 stream.write((blen << 2) | 2);
198 stream.write((blen << 2) >> 8);
199 }
200 else {
201 stream.write((blen << 1) | 1);
202 }
203
204 stream.write(bw.getBytes(), 0, blen);
205
206 if(labelOff != 0) {
207 stream.write(labelOff);
208 stream.write(labelOff >> 8);
209 stream.write(labelOff >> 16);
210 }
211
212 if(extraBytes != null)
213 stream.write(extraBytes);
214 }
215
216 public void addCoord(Coord co) {
217 points.add(co);
218 }
219
220 public void addCoords(List<Coord> coords) {
221 points.addAll(coords);
222 }
223
224 public List<Coord> getPoints() {
225 return points;
226 }
227
228 public void setDirection(boolean direction) {
229 this.direction = direction;
230 }
231
232 public boolean isRoad() {
233 return roaddef != null;
234 }
235
236 /**
237 * @return true if this polyline contanins special nodes between the first and last node
238 */
239 boolean hasInternalNodes() {
240 if (isRoad() && (roaddef.hasHouseNumbers() || !roaddef.skipAddToNOD())) {
241 // exclude first and last node
242 for (int i = 1; i < points.size() - 1; i++) {
243 Coord co = points.get(i);
244 if (co.getId() > 0 || (roaddef.hasHouseNumbers() && co.isNumberNode())) {
245 return true;
246 }
247 }
248 }
249 return false;
250 }
251
252 public void setLastSegment(boolean last) {
253 lastSegment = last;
254 }
255
256 public boolean isLastSegment() {
257 return lastSegment;
258 }
259
260 public void setRoadDef(RoadDef rd) {
261 this.roaddef = rd;
262 }
263
264 public int getOffsetNet1() {
265 if (!isRoad())
266 return 0;
267 return roaddef.getOffsetNet1();
268 }
269
270 public boolean sharesNodeWith(Polyline other) {
271 for (Coord p1 : points) {
272 if (p1.getId() != 0) {
273 // point is a node, see if the other line contains the
274 // same node
275 for (Coord p2 : other.points)
276 if (p1.getId() == p2.getId())
277 return true;
278 }
279 }
280
281 return false;
282 }
283
284 public int getLat() {
285 return getSubdiv().getLatitude() + (getDeltaLat() << getSubdiv().getShift());
286 }
287
288 public int getLong() {
289 return getSubdiv().getLongitude() + (getDeltaLong() << getSubdiv().getShift());
290 }
291
292 /**
293 * Count special nodes between (excluding) first and last node.
294 * @param countAllNodes : false: count only coord nodes, true: count number nodes
295 * @return number of special nodes in points, ignoring the first and last node
296 */
297 public int getNodeCount(boolean countAllNodes ) {
298 int count = 0;
299
300 for (int i = 1; i< points.size() -1; i++) {
301 Coord co = points.get(i);
302 if (co.getId() > 0 || countAllNodes && co.isNumberNode())
303 count++;
304 }
305 return count;
306 }
307
308 public boolean hasHouseNumbers() {
309 if (!isRoad())
310 return false;
311 return roaddef.hasHouseNumbers();
312 }
313
314 public void addRefLabel(Label refLabel) {
315 if (refLabels == null)
316 refLabels = new ArrayList<>();
317 refLabels.add(refLabel);
318 }
319
320
321 }