/
MapFileWriter.java
1075 lines (902 loc) · 47.5 KB
/
MapFileWriter.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2010, 2011, 2012, 2013 mapsforge.org
* Copyright 2015 lincomatic
* Copyright 2016 Andrey Novikov
* Copyright 2016 mikes222
* Copyright 2016-2018 devemux86
* Copyright 2017 Ludwig M Brinckmann
* Copyright 2017-2019 Gustl22
*
* This program 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 3 of the License, or (at your option) any later version.
*
* This program 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 program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.mapsforge.map.writer;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.CacheStats;
import com.google.common.cache.LoadingCache;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.LineString;
import org.locationtech.jts.geom.MultiLineString;
import org.locationtech.jts.geom.MultiPolygon;
import org.locationtech.jts.geom.Point;
import org.locationtech.jts.geom.Polygon;
import org.mapsforge.core.model.LatLong;
import org.mapsforge.core.util.LatLongUtils;
import org.mapsforge.core.util.MercatorProjection;
import org.mapsforge.map.writer.model.Encoding;
import org.mapsforge.map.writer.model.MapWriterConfiguration;
import org.mapsforge.map.writer.model.OSMTag;
import org.mapsforge.map.writer.model.TDNode;
import org.mapsforge.map.writer.model.TDWay;
import org.mapsforge.map.writer.model.TileBasedDataProcessor;
import org.mapsforge.map.writer.model.TileCoordinate;
import org.mapsforge.map.writer.model.TileData;
import org.mapsforge.map.writer.model.TileInfo;
import org.mapsforge.map.writer.model.WayDataBlock;
import org.mapsforge.map.writer.model.ZoomIntervalConfiguration;
import org.mapsforge.map.writer.util.Constants;
import org.mapsforge.map.writer.util.GeoUtils;
import org.mapsforge.map.writer.util.JTSUtils;
import org.mapsforge.map.writer.util.OSMUtils;
import org.mapsforge.map.writer.util.PolyLabel;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* Writes the binary file format for mapsforge maps.
*/
public final class MapFileWriter {
private static class JTSGeometryCacheLoader extends CacheLoader<TDWay, Geometry> {
private final TileBasedDataProcessor datastore;
JTSGeometryCacheLoader(TileBasedDataProcessor datastore) {
super();
this.datastore = datastore;
}
@Override
public Geometry load(TDWay way) throws Exception {
if (way.isInvalid()) {
throw new Exception("way is known to be invalid: " + way.getId());
}
List<TDWay> innerWaysOfMultipolygon = this.datastore.getInnerWaysOfMultipolygon(way.getId());
Geometry geometry = JTSUtils.toJtsGeometry(way, innerWaysOfMultipolygon);
if (geometry == null) {
way.setInvalid(true);
throw new Exception("cannot create geometry for way with id: " + way.getId());
}
return geometry;
}
}
private static class WayPreprocessingCallable implements Callable<WayPreprocessingResult> {
private final MapWriterConfiguration configuration;
private final LoadingCache<TDWay, Geometry> jtsGeometryCache;
private final byte maxZoomInterval;
private final TileCoordinate tile;
private final TDWay way;
/**
* @param way the {@link TDWay}
* @param tile the {@link TileCoordinate}
* @param maxZoomInterval the maximum zoom
* @param jtsGeometryCache the {@link LoadingCache} for {@link Geometry} objects
* @param configuration the {@link MapWriterConfiguration}
*/
WayPreprocessingCallable(TDWay way, TileCoordinate tile, byte maxZoomInterval,
LoadingCache<TDWay, Geometry> jtsGeometryCache, MapWriterConfiguration configuration) {
super();
this.way = way;
this.tile = tile;
this.maxZoomInterval = maxZoomInterval;
this.jtsGeometryCache = jtsGeometryCache;
this.configuration = configuration;
}
@Override
public WayPreprocessingResult call() {
// TODO more sophisticated clipping of polygons needed
// we have a problem when clipping polygons which border needs to be
// rendered
// the problem does not occur with polygons that do not have a border
// imagine an administrative border, such a polygon is not filled, but its
// border is rendered
// in case the polygon spans multiple base zoom tiles, clipping
// introduces connections between
// nodes that haven't existed before (exactly at the borders of a base
// tile)
// in case of filled polygons we do not care about these connections
// polygons that represent a border must be clipped as simple ways and
// not as polygons
Geometry originalGeometry;
try {
originalGeometry = this.jtsGeometryCache.get(this.way);
} catch (ExecutionException e) {
this.way.setInvalid(true);
return null;
}
Geometry processedGeometry = originalGeometry;
if ((originalGeometry instanceof Polygon || originalGeometry instanceof MultiPolygon) && this.configuration.isPolygonClipping()
|| (originalGeometry instanceof LineString || originalGeometry instanceof MultiLineString)
&& this.configuration.isWayClipping()) {
processedGeometry = GeoUtils.clipToTile(this.way, originalGeometry, this.tile,
this.configuration.getBboxEnlargement());
if (processedGeometry == null) {
return null;
}
}
// TODO is this the right place to simplify, or is it better before clipping?
if (this.configuration.getSimplification() > 0
&& this.tile.getZoomlevel() <= this.configuration.getSimplificationMaxZoom()) {
processedGeometry = GeoUtils.simplifyGeometry(this.way, processedGeometry, this.maxZoomInterval,
tileSize, this.configuration.getSimplification());
if (processedGeometry == null) {
return null;
}
}
if (processedGeometry.getCoordinates().length > 2000) {
LOGGER.info("Large geometry " + this.way.getId() + " (" + processedGeometry.getCoordinates().length + " coords, down from " + originalGeometry.getCoordinates().length + " coords)");
}
List<WayDataBlock> blocks = GeoUtils.toWayDataBlockList(processedGeometry);
if (blocks == null) {
return null;
}
if (blocks.isEmpty()) {
LOGGER.finer("empty list of way data blocks after preprocessing way: " + this.way.getId());
return null;
}
short subtileMask = GeoUtils.computeBitmask(processedGeometry, this.tile,
this.configuration.getBboxEnlargement());
// Compute the label/symbol coordinates of the non clipped polygon
LatLong labelCoordinate = null;
if (this.way.isValidClosedLine()) {
boolean labelPosition = this.configuration.isLabelPosition();
if (!labelPosition) {
List<OSMTag> tags = this.configuration.getTagMapping().getWayTags(this.way.getTags().keySet());
for (int i = 0, n = tags.size(); i < n; i++) {
labelPosition = tags.get(i).isLabelPosition();
if (labelPosition)
break;
}
}
if (labelPosition) {
if (this.configuration.isPolylabel()) {
Point labelPoint = PolyLabel.get(originalGeometry);
labelCoordinate = new LatLong(labelPoint.getY(), labelPoint.getX());
} else {
labelCoordinate = GeoUtils.computeInteriorPoint(originalGeometry);
}
}
}
switch (this.configuration.getEncodingChoice()) {
case SINGLE:
blocks = DeltaEncoder.encode(blocks, Encoding.DELTA);
break;
case DOUBLE:
blocks = DeltaEncoder.encode(blocks, Encoding.DOUBLE_DELTA);
break;
case AUTO:
List<WayDataBlock> blocksDelta = DeltaEncoder.encode(blocks, Encoding.DELTA);
List<WayDataBlock> blocksDoubleDelta = DeltaEncoder.encode(blocks, Encoding.DOUBLE_DELTA);
int simDelta = DeltaEncoder.simulateSerialization(blocksDelta);
int simDoubleDelta = DeltaEncoder.simulateSerialization(blocksDoubleDelta);
if (simDelta <= simDoubleDelta) {
blocks = blocksDelta;
} else {
blocks = blocksDoubleDelta;
}
break;
}
return new WayPreprocessingResult(this.way, blocks, labelCoordinate, subtileMask);
}
}
private static class WayPreprocessingResult {
final LatLong labelPosition;
final short subtileMask;
final TDWay way;
final List<WayDataBlock> wayDataBlocks;
WayPreprocessingResult(TDWay way, List<WayDataBlock> wayDataBlocks, LatLong labelPosition, short subtileMask) {
super();
this.way = way;
this.wayDataBlocks = wayDataBlocks;
this.labelPosition = labelPosition;
this.subtileMask = subtileMask;
}
LatLong getLabelPosition() {
return this.labelPosition;
}
short getSubtileMask() {
return this.subtileMask;
}
TDWay getWay() {
return this.way;
}
List<WayDataBlock> getWayDataBlocks() {
return this.wayDataBlocks;
}
}
// IO
static final int HEADER_BUFFER_SIZE = 0x100000; // 1MB
static final Logger LOGGER = Logger.getLogger(MapFileWriter.class.getName());
static final int MIN_TILE_BUFFER_SIZE = 0xF00000; // 15MB
static final int POI_DATA_BUFFER_SIZE = 0xA00000; // 10MB
static final int TILE_BUFFER_SIZE = 0xA00000; // 10MB
// private static final int PIXEL_COMPRESSION_MAX_DELTA = 5;
static final int TILES_BUFFER_SIZE = 0x3200000; // 50MB
static final int WAY_BUFFER_SIZE = 0x100000; // 10MB
static final int WAY_DATA_BUFFER_SIZE = 0xA00000; // 10MB
// private static final CoastlineHandler COASTLINE_HANDLER = new
// CoastlineHandler();
// bitmap flags for file features
private static final short BITMAP_COMMENT = 8;
private static final short BITMAP_CREATED_WITH = 4;
private static final short BITMAP_DEBUG = 128;
private static final short BITMAP_MAP_START_POSITION = 64;
private static final short BITMAP_MAP_START_ZOOM = 32;
private static final short BITMAP_PREFERRED_LANGUAGES = 16;
// bitmap flags for poi and way features
private static final short BITMAP_HOUSENUMBER = 64;
private static final short BITMAP_NAME = 128;
// bitmap flags for poi features
private static final short BITMAP_ELEVATION = 32;
// bitmap flags for way features
private static final short BITMAP_ENCODING = 4;
private static final short BITMAP_LABEL = 16;
private static final short BITMAP_MULTIPLE_WAY_BLOCKS = 8;
private static final short BITMAP_REF = 32;
private static final int BITMAP_INDEX_ENTRY_WATER = 0x80;
// Constants
private static final int BYTE_AMOUNT_SUBFILE_INDEX_PER_TILE = 5;
private static final int BYTES_INT = 4;
private static final int DEBUG_BLOCK_SIZE = 32;
private static final int HALF_BYTE_SHIFT = 4;
// DEBUG STRINGS
private static final String DEBUG_INDEX_START_STRING = "+++IndexStart+++";
private static final String DEBUG_STRING_POI_HEAD = "***POIStart";
private static final String DEBUG_STRING_POI_TAIL = "***";
private static final String DEBUG_STRING_TILE_HEAD = "###TileStart";
private static final String DEBUG_STRING_TILE_TAIL = "###";
private static final String DEBUG_STRING_WAY_HEAD = "---WayStart";
private static final String DEBUG_STRING_WAY_TAIL = "---";
private static final int DUMMY_INT = 0xf0f0f0f0;
private static final long DUMMY_LONG = 0xf0f0f0f0f0f0f0f0L;
private static ExecutorService EXECUTOR_SERVICE;
private static final int JTS_GEOMETRY_CACHE_SIZE = 50000;
private static final String MAGIC_BYTE = "mapsforge binary OSM";
private static final int OFFSET_FILE_SIZE = 28;
private static final float PROGRESS_PERCENT_STEP = 10f;
private static final String PROJECTION = "Mercator";
private static final int SIZE_ZOOMINTERVAL_CONFIGURATION = 19;
private static final TileInfo TILE_INFO = TileInfo.getInstance();
private static final int tileSize = 256; // needed for optimal simplification, but set to constant here TODO
private static final Charset UTF8_CHARSET = Charset.forName("utf8");
/**
* Writes the map file according to the given configuration using the given data processor.
*
* @param configuration the configuration
* @param dataProcessor the data processor
* @throws IOException thrown if any IO error occurs
*/
public static void writeFile(MapWriterConfiguration configuration, TileBasedDataProcessor dataProcessor)
throws IOException {
EXECUTOR_SERVICE = Executors.newFixedThreadPool(configuration.getThreads());
RandomAccessFile randomAccessFile = new RandomAccessFile(configuration.getOutputFile(), "rw");
int amountOfZoomIntervals = dataProcessor.getZoomIntervalConfiguration().getNumberOfZoomIntervals();
ByteBuffer containerHeaderBuffer = ByteBuffer.allocate(HEADER_BUFFER_SIZE);
// CONTAINER HEADER
int totalHeaderSize = writeHeaderBuffer(configuration, dataProcessor, containerHeaderBuffer);
// set to mark where zoomIntervalConfig starts
containerHeaderBuffer.reset();
final LoadingCache<TDWay, Geometry> jtsGeometryCache = CacheBuilder.newBuilder()
.maximumSize(JTS_GEOMETRY_CACHE_SIZE).concurrencyLevel(Runtime.getRuntime().availableProcessors() * 2)
.build(new JTSGeometryCacheLoader(dataProcessor));
// SUB FILES
// for each zoom interval write a sub file
long currentFileSize = totalHeaderSize;
for (int i = 0; i < amountOfZoomIntervals; i++) {
// SUB FILE INDEX AND DATA
long subfileSize = writeSubfile(currentFileSize, i, dataProcessor, jtsGeometryCache, randomAccessFile,
configuration);
// SUB FILE META DATA IN CONTAINER HEADER
writeSubfileMetaDataToContainerHeader(dataProcessor.getZoomIntervalConfiguration(), i, currentFileSize,
subfileSize, containerHeaderBuffer);
currentFileSize += subfileSize;
}
randomAccessFile.seek(0);
randomAccessFile.write(containerHeaderBuffer.array(), 0, totalHeaderSize);
// WRITE FILE SIZE TO HEADER
long fileSize = randomAccessFile.length();
randomAccessFile.seek(OFFSET_FILE_SIZE);
randomAccessFile.writeLong(fileSize);
randomAccessFile.close();
CacheStats stats = jtsGeometryCache.stats();
LOGGER.fine("Tag values stats:\n" + OSMUtils.logValueTypeCount());
LOGGER.info("JTS Geometry cache hit rate: " + stats.hitRate());
LOGGER.info("JTS Geometry total load time: " + stats.totalLoadTime() / 1000);
LOGGER.info("Finished writing file.");
}
/**
* Cleans up thread pool. Must only be called at the end of processing.
*/
public static void release() {
EXECUTOR_SERVICE.shutdown();
}
static byte infoByteOptmizationParams(MapWriterConfiguration configuration) {
byte infoByte = 0;
if (configuration.isDebugStrings()) {
infoByte |= BITMAP_DEBUG;
}
if (configuration.getMapStartPosition() != null) {
infoByte |= BITMAP_MAP_START_POSITION;
}
if (configuration.hasMapStartZoomLevel()) {
infoByte |= BITMAP_MAP_START_ZOOM;
}
if (configuration.getPreferredLanguages() != null && !configuration.getPreferredLanguages().isEmpty()) {
infoByte |= BITMAP_PREFERRED_LANGUAGES;
}
if (configuration.getComment() != null) {
infoByte |= BITMAP_COMMENT;
}
infoByte |= BITMAP_CREATED_WITH;
return infoByte;
}
static byte infoBytePOIFeatures(String name, int elevation, String housenumber) {
byte infoByte = 0;
if (name != null && !name.isEmpty()) {
infoByte |= BITMAP_NAME;
}
if (housenumber != null && !housenumber.isEmpty()) {
infoByte |= BITMAP_HOUSENUMBER;
}
if (elevation != 0) {
infoByte |= BITMAP_ELEVATION;
}
return infoByte;
}
static byte infoBytePoiLayerAndTagAmount(TDNode node) {
byte layer = node.getLayer();
// make sure layer is in [0,10]
layer = layer < 0 ? 0 : layer > 10 ? 10 : layer;
int tagAmount = node.getTags() == null ? 0 : node.getTags().size();
if (tagAmount > (1 << HALF_BYTE_SHIFT) - 1) {
// See #971
LOGGER.severe("Too many tags: " + node.toStringDetailed());
throw new RuntimeException("more than 15 tags aren't supported");
}
return (byte) (layer << HALF_BYTE_SHIFT | (short) tagAmount);
}
static byte infoByteWayFeatures(TDWay way, WayPreprocessingResult wpr) {
byte infoByte = 0;
if (way.getName() != null && !way.getName().isEmpty()) {
infoByte |= BITMAP_NAME;
}
if (way.getHouseNumber() != null && !way.getHouseNumber().isEmpty()) {
infoByte |= BITMAP_HOUSENUMBER;
}
if (way.getRef() != null && !way.getRef().isEmpty()) {
infoByte |= BITMAP_REF;
}
if (wpr.getLabelPosition() != null) {
infoByte |= BITMAP_LABEL;
}
if (wpr.getWayDataBlocks().size() > 1) {
infoByte |= BITMAP_MULTIPLE_WAY_BLOCKS;
}
if (!wpr.getWayDataBlocks().isEmpty()) {
WayDataBlock wayDataBlock = wpr.getWayDataBlocks().get(0);
if (wayDataBlock.getEncoding() == Encoding.DOUBLE_DELTA) {
infoByte |= BITMAP_ENCODING;
}
}
return infoByte;
}
static byte infoByteWayLayerAndTagAmount(TDWay way) {
byte layer = way.getLayer();
// make sure layer is in [0,10]
layer = layer < 0 ? 0 : layer > 10 ? 10 : layer;
int tagAmount = way.getTags() == null ? 0 : way.getTags().size();
if (tagAmount > (1 << HALF_BYTE_SHIFT) - 1) {
// See #971
LOGGER.severe("Too many tags: " + way.toStringDetailed());
throw new RuntimeException("more than 15 tags aren't supported");
}
return (byte) (layer << HALF_BYTE_SHIFT | (short) tagAmount);
}
static void processPOI(TDNode poi, int currentTileLat, int currentTileLon, boolean debugStrings,
ByteBuffer poiBuffer) {
if (debugStrings) {
StringBuilder sb = new StringBuilder();
sb.append(DEBUG_STRING_POI_HEAD).append(poi.getId()).append(DEBUG_STRING_POI_TAIL);
poiBuffer.put(sb.toString().getBytes(UTF8_CHARSET));
// append whitespaces so that block has 32 bytes
appendWhitespace(DEBUG_BLOCK_SIZE - sb.toString().getBytes(UTF8_CHARSET).length, poiBuffer);
}
// write poi features to the file
poiBuffer.put(Serializer.getVariableByteSigned(poi.getLatitude() - currentTileLat));
poiBuffer.put(Serializer.getVariableByteSigned(poi.getLongitude() - currentTileLon));
// write byte with layer and tag id amount (max 15 ids)
poiBuffer.put(infoBytePoiLayerAndTagAmount(poi));
// tag values aren't amounted, (but can be calculated out of tag's wildcard) to not unnecessarily decrease amount space
// write tag ids to the file
Map<Short, Object> tags = poi.getTags();
if (tags != null) {
List<Object> values = new ArrayList<>();
for (Map.Entry<Short, Object> tag : tags.entrySet()) {
poiBuffer.put(Serializer.getVariableByteUnsigned(mappedPoiTagID(tag.getKey())));
if (tag.getValue() != null) {
values.add(tag.getValue());
}
}
// write optional tag values (all values are null if tag values are disabled)
for (Object value : values) {
if (value instanceof Byte) {
poiBuffer.putShort((Byte) value);
} else if (value instanceof Integer) {
poiBuffer.putInt((Integer) value);
} else if (value instanceof Float) {
poiBuffer.putFloat((Float) value);
} else if (value instanceof Short) {
poiBuffer.putShort((Short) value);
} else if (value instanceof String) {
writeUTF8((String) value, poiBuffer);
}
}
}
// write byte with bits set to 1 if the poi has a
// name, an elevation
// or a housenumber
poiBuffer.put(infoBytePOIFeatures(poi.getName(), poi.getElevation(), poi.getHouseNumber()));
if (poi.getName() != null && !poi.getName().isEmpty()) {
writeUTF8(poi.getName(), poiBuffer);
}
if (poi.getHouseNumber() != null && !poi.getHouseNumber().isEmpty()) {
writeUTF8(poi.getHouseNumber(), poiBuffer);
}
if (poi.getElevation() != 0) {
poiBuffer.put(Serializer.getVariableByteSigned(poi.getElevation()));
}
}
static void processWay(WayPreprocessingResult wpr, TDWay way, int currentTileLat, int currentTileLon,
ByteBuffer wayBuffer) {
// write subtile bitmask of way
wayBuffer.putShort(wpr.getSubtileMask());
// write byte with layer and tag id amount (max 15 ids)
wayBuffer.put(infoByteWayLayerAndTagAmount(way));
// tag values aren't amounted, (but can be calculated out of tag's wildcard) to not unnecessarily decrease amount space
// write tag ids
Map<Short, Object> tags = way.getTags();
if (tags != null) {
List<Object> values = new ArrayList<>();
for (Map.Entry<Short, Object> tag : tags.entrySet()) {
wayBuffer.put(Serializer.getVariableByteUnsigned(mappedWayTagID(tag.getKey())));
if (tag.getValue() != null) {
values.add(tag.getValue());
}
}
// write optional tag values (all values are null if tag values are disabled)
for (Object value : values) {
if (value instanceof Byte) {
wayBuffer.put((Byte) value);
} else if (value instanceof Integer) {
wayBuffer.putInt((Integer) value);
} else if (value instanceof Float) {
wayBuffer.putFloat((Float) value);
} else if (value instanceof Short) {
wayBuffer.putShort((Short) value);
} else if (value instanceof String) {
writeUTF8((String) value, wayBuffer);
}
}
}
// write a byte with flags for existence of name,
// ref, label position, and multiple blocks
wayBuffer.put(infoByteWayFeatures(way, wpr));
// if the way has a name, write it to the file
if (way.getName() != null && !way.getName().isEmpty()) {
writeUTF8(way.getName(), wayBuffer);
}
// if the way has a house number, write it to the file
if (way.getHouseNumber() != null && !way.getHouseNumber().isEmpty()) {
writeUTF8(way.getHouseNumber(), wayBuffer);
}
// if the way has a ref, write it to the file
if (way.getRef() != null && !way.getRef().isEmpty()) {
writeUTF8(way.getRef(), wayBuffer);
}
if (wpr.getLabelPosition() != null) {
int firstWayStartLat = wpr.getWayDataBlocks().get(0).getOuterWay().get(0).intValue();
int firstWayStartLon = wpr.getWayDataBlocks().get(0).getOuterWay().get(1).intValue();
wayBuffer.put(Serializer.getVariableByteSigned(LatLongUtils.degreesToMicrodegrees(wpr.getLabelPosition().latitude)
- firstWayStartLat));
wayBuffer.put(Serializer.getVariableByteSigned(LatLongUtils.degreesToMicrodegrees(wpr.getLabelPosition().longitude)
- firstWayStartLon));
}
if (wpr.getWayDataBlocks().size() > 1) {
// write the amount of way data blocks
wayBuffer.put(Serializer.getVariableByteUnsigned(wpr.getWayDataBlocks().size()));
}
// write the way data blocks
// case 1: simple way or simple polygon --> the way
// block consists of
// exactly one way
// case 2: multi polygon --> the way consists of
// exactly one outer way and
// one or more inner ways
for (WayDataBlock wayDataBlock : wpr.getWayDataBlocks()) {
// write the amount of coordinate blocks
// we have at least one block (potentially
// interpreted as outer way) and
// possible blocks for inner ways
if (wayDataBlock.getInnerWays() != null && !wayDataBlock.getInnerWays().isEmpty()) {
// multi polygon: outer way + number of
// inner ways
wayBuffer.put(Serializer.getVariableByteUnsigned(1 + wayDataBlock.getInnerWays().size()));
} else {
// simply a single way (not a multi polygon)
wayBuffer.put(Serializer.getVariableByteUnsigned(1));
}
// write block for (outer/simple) way
writeWay(wayDataBlock.getOuterWay(), currentTileLat, currentTileLon, wayBuffer);
// write blocks for inner ways
if (wayDataBlock.getInnerWays() != null && !wayDataBlock.getInnerWays().isEmpty()) {
for (List<Integer> innerWayCoordinates : wayDataBlock.getInnerWays()) {
writeWay(innerWayCoordinates, currentTileLat, currentTileLon, wayBuffer);
}
}
}
}
static int writeHeaderBuffer(final MapWriterConfiguration configuration,
final TileBasedDataProcessor dataProcessor, final ByteBuffer containerHeaderBuffer) {
LOGGER.fine("writing header");
LOGGER.fine("Bounding box for file: " + dataProcessor.getBoundingBox().toString());
// write file header
// MAGIC BYTE
byte[] magicBytes = MAGIC_BYTE.getBytes(UTF8_CHARSET);
containerHeaderBuffer.put(magicBytes);
// HEADER SIZE: Write dummy pattern as header size. It will be replaced
// later in time
int headerSizePosition = containerHeaderBuffer.position();
containerHeaderBuffer.putInt(DUMMY_INT);
// FILE VERSION
containerHeaderBuffer.putInt(configuration.getFileSpecificationVersion());
// FILE SIZE: Write dummy pattern as file size. It will be replaced
// later in time
containerHeaderBuffer.putLong(DUMMY_LONG);
// DATE OF CREATION
containerHeaderBuffer.putLong(System.currentTimeMillis());
// BOUNDING BOX
containerHeaderBuffer.putInt(LatLongUtils.degreesToMicrodegrees(dataProcessor.getBoundingBox().minLatitude));
containerHeaderBuffer.putInt(LatLongUtils.degreesToMicrodegrees(dataProcessor.getBoundingBox().minLongitude));
containerHeaderBuffer.putInt(LatLongUtils.degreesToMicrodegrees(dataProcessor.getBoundingBox().maxLatitude));
containerHeaderBuffer.putInt(LatLongUtils.degreesToMicrodegrees(dataProcessor.getBoundingBox().maxLongitude));
// TILE SIZE
containerHeaderBuffer.putShort((short) Constants.DEFAULT_TILE_SIZE);
// PROJECTION
writeUTF8(PROJECTION, containerHeaderBuffer);
// check whether zoom start is a valid zoom level
// FLAGS
containerHeaderBuffer.put(infoByteOptmizationParams(configuration));
// MAP START POSITION
LatLong mapStartPosition = configuration.getMapStartPosition();
if (mapStartPosition != null) {
containerHeaderBuffer.putInt(LatLongUtils.degreesToMicrodegrees(mapStartPosition.latitude));
containerHeaderBuffer.putInt(LatLongUtils.degreesToMicrodegrees(mapStartPosition.longitude));
}
// MAP START ZOOM
if (configuration.hasMapStartZoomLevel()) {
containerHeaderBuffer.put((byte) configuration.getMapStartZoomLevel());
}
// PREFERRED LANGUAGE
if (configuration.getPreferredLanguages() != null && !configuration.getPreferredLanguages().isEmpty()) {
String langStr = "";
for (String preferredLanguage : configuration.getPreferredLanguages()) {
langStr += (langStr.length() > 0 ? "," : "") + preferredLanguage;
}
writeUTF8(langStr, containerHeaderBuffer);
}
// COMMENT
if (configuration.getComment() != null) {
writeUTF8(configuration.getComment(), containerHeaderBuffer);
}
// CREATED WITH
writeUTF8(configuration.getWriterVersion(), containerHeaderBuffer);
OSMTagMapping mapping = configuration.getTagMapping();
// AMOUNT POI TAGS
containerHeaderBuffer.putShort((short) mapping.getOptimizedPoiIds().size());
// POI TAGS
// retrieves tag ids in order of frequency, most frequent come first
for (short tagId : mapping.getOptimizedPoiIds().keySet()) {
OSMTag tag = mapping.getPoiTag(tagId);
writeUTF8(tag.tagKey(), containerHeaderBuffer);
}
// AMOUNT OF WAY TAGS
containerHeaderBuffer.putShort((short) mapping.getOptimizedWayIds().size());
// WAY TAGS
for (short tagId : mapping.getOptimizedWayIds().keySet()) {
OSMTag tag = mapping.getWayTag(tagId);
writeUTF8(tag.tagKey(), containerHeaderBuffer);
}
// AMOUNT OF ZOOM INTERVALS
int numberOfZoomIntervals = dataProcessor.getZoomIntervalConfiguration().getNumberOfZoomIntervals();
containerHeaderBuffer.put((byte) numberOfZoomIntervals);
// SET MARK OF THIS BUFFER AT POSITION FOR WRITING ZOOM INTERVAL CONFIG
containerHeaderBuffer.mark();
// ZOOM INTERVAL CONFIGURATION: SKIP COMPUTED AMOUNT OF BYTES
containerHeaderBuffer.position(containerHeaderBuffer.position() + SIZE_ZOOMINTERVAL_CONFIGURATION
* numberOfZoomIntervals);
// now write header size
// -4 bytes of header size variable itself
int headerSize = containerHeaderBuffer.position() - headerSizePosition - BYTES_INT;
containerHeaderBuffer.putInt(headerSizePosition, headerSize);
return containerHeaderBuffer.position();
}
static void writeWayNodes(List<Integer> waynodes, int currentTileLat, int currentTileLon, ByteBuffer buffer) {
if (!waynodes.isEmpty() && waynodes.size() % 2 == 0) {
Iterator<Integer> waynodeIterator = waynodes.iterator();
buffer.put(Serializer.getVariableByteSigned(waynodeIterator.next().intValue() - currentTileLat));
buffer.put(Serializer.getVariableByteSigned(waynodeIterator.next().intValue() - currentTileLon));
while (waynodeIterator.hasNext()) {
buffer.put(Serializer.getVariableByteSigned(waynodeIterator.next().intValue()));
}
}
}
static void writeZoomLevelTable(int[][] entitiesPerZoomLevel, ByteBuffer tileBuffer) {
// write cumulated number of POIs and ways for this tile on
// each zoom level
for (int[] entityCount : entitiesPerZoomLevel) {
tileBuffer.put(Serializer.getVariableByteUnsigned(entityCount[0]));
tileBuffer.put(Serializer.getVariableByteUnsigned(entityCount[1]));
}
}
private static void appendWhitespace(int amount, ByteBuffer buffer) {
for (int i = 0; i < amount; i++) {
buffer.put((byte) ' ');
}
}
private static int mappedPoiTagID(short original) {
return OSMTagMapping.getInstance().getOptimizedPoiIds().get(Short.valueOf(original)).intValue();
}
private static int mappedWayTagID(short original) {
return OSMTagMapping.getInstance().getOptimizedWayIds().get(Short.valueOf(original)).intValue();
}
private static void processIndexEntry(TileCoordinate tileCoordinate, ByteBuffer indexBuffer,
long currentSubfileOffset) {
byte[] indexBytes = Serializer.getFiveBytes(currentSubfileOffset);
if (TILE_INFO.isWaterTile(tileCoordinate)) {
indexBytes[0] |= BITMAP_INDEX_ENTRY_WATER;
}
indexBuffer.put(indexBytes);
}
private static void processTile(MapWriterConfiguration configuration, TileCoordinate tileCoordinate,
TileBasedDataProcessor dataProcessor, LoadingCache<TDWay, Geometry> jtsGeometryCache,
int zoomIntervalIndex, ByteBuffer tileBuffer, ByteBuffer poiDataBuffer, ByteBuffer wayDataBuffer,
ByteBuffer wayBuffer) {
tileBuffer.clear();
poiDataBuffer.clear();
wayDataBuffer.clear();
wayBuffer.clear();
final TileData currentTile = dataProcessor.getTile(zoomIntervalIndex, tileCoordinate.getX(),
tileCoordinate.getY());
final int currentTileLat = LatLongUtils.degreesToMicrodegrees(MercatorProjection.tileYToLatitude(
tileCoordinate.getY(), tileCoordinate.getZoomlevel()));
final int currentTileLon = LatLongUtils.degreesToMicrodegrees(MercatorProjection.tileXToLongitude(
tileCoordinate.getX(), tileCoordinate.getZoomlevel()));
final byte minZoomCurrentInterval = dataProcessor.getZoomIntervalConfiguration().getMinZoom(zoomIntervalIndex);
final byte maxZoomCurrentInterval = dataProcessor.getZoomIntervalConfiguration().getMaxZoom(zoomIntervalIndex);
// write amount of POIs and ways for each zoom level
Map<Byte, List<TDNode>> poisByZoomlevel = currentTile.poisByZoomlevel(minZoomCurrentInterval,
maxZoomCurrentInterval);
Map<Byte, List<TDWay>> waysByZoomlevel = currentTile.waysByZoomlevel(minZoomCurrentInterval,
maxZoomCurrentInterval);
if (!poisByZoomlevel.isEmpty() || !waysByZoomlevel.isEmpty()) {
if (configuration.isDebugStrings()) {
writeTileSignature(tileCoordinate, tileBuffer);
}
int amountZoomLevels = maxZoomCurrentInterval - minZoomCurrentInterval + 1;
int[][] entitiesPerZoomLevel = new int[amountZoomLevels][2];
// WRITE POIS
for (byte zoomlevel = minZoomCurrentInterval; zoomlevel <= maxZoomCurrentInterval; zoomlevel++) {
int indexEntitiesPerZoomLevelTable = zoomlevel - minZoomCurrentInterval;
List<TDNode> pois = poisByZoomlevel.get(Byte.valueOf(zoomlevel));
if (pois != null) {
for (TDNode poi : pois) {
processPOI(poi, currentTileLat, currentTileLon, configuration.isDebugStrings(), poiDataBuffer);
}
// increment count of POIs on this zoom level
entitiesPerZoomLevel[indexEntitiesPerZoomLevelTable][0] += pois.size();
}
}
// WRITE WAYS
for (byte zoomlevel = minZoomCurrentInterval; zoomlevel <= maxZoomCurrentInterval; zoomlevel++) {
int indexEntitiesPerZoomLevelTable = zoomlevel - minZoomCurrentInterval;
List<TDWay> ways = waysByZoomlevel.get(Byte.valueOf(zoomlevel));
if (ways != null) {
List<WayPreprocessingCallable> callables = new ArrayList<>();
for (TDWay way : ways) {
if (!way.isInvalid()) {
callables.add(new WayPreprocessingCallable(way, tileCoordinate, maxZoomCurrentInterval,
jtsGeometryCache, configuration));
}
}
try {
List<Future<WayPreprocessingResult>> futures = EXECUTOR_SERVICE.invokeAll(callables);
for (Future<WayPreprocessingResult> wprFuture : futures) {
WayPreprocessingResult wpr;
try {
wpr = wprFuture.get();
} catch (ExecutionException e) {
LOGGER.log(Level.WARNING, "error in parallel preprocessing of ways", e);
continue;
}
if (wpr != null) {
wayBuffer.clear();
// increment count of ways on this zoom level
entitiesPerZoomLevel[indexEntitiesPerZoomLevelTable][1]++;
if (configuration.isDebugStrings()) {
writeWaySignature(wpr.getWay(), wayDataBuffer);
}
processWay(wpr, wpr.getWay(), currentTileLat, currentTileLon, wayBuffer);
// write size of way to way data buffer
wayDataBuffer.put(Serializer.getVariableByteUnsigned(wayBuffer.position()));
// write way data to way data buffer
wayDataBuffer.put(wayBuffer.array(), 0, wayBuffer.position());
}
}
} catch (InterruptedException e) {
LOGGER.log(Level.WARNING, "error in parallel preprocessing of ways", e);
throw new RuntimeException(e);
}
}
}
// write zoom table
writeZoomLevelTable(entitiesPerZoomLevel, tileBuffer);
// write offset to first way in the tile header
tileBuffer.put(Serializer.getVariableByteUnsigned(poiDataBuffer.position()));
// write POI data to buffer
tileBuffer.put(poiDataBuffer.array(), 0, poiDataBuffer.position());
// write way data to buffer
tileBuffer.put(wayDataBuffer.array(), 0, wayDataBuffer.position());
}
}
private static void writeIndex(ByteBuffer indexBuffer, long startPositionSubfile, long subFileSize,
RandomAccessFile randomAccessFile) throws IOException {
randomAccessFile.seek(startPositionSubfile);
randomAccessFile.write(indexBuffer.array());
randomAccessFile.seek(subFileSize);
}
private static long writeSubfile(final long startPositionSubfile, final int zoomIntervalIndex,
final TileBasedDataProcessor dataStore, final LoadingCache<TDWay, Geometry> jtsGeometryCache,
final RandomAccessFile randomAccessFile, final MapWriterConfiguration configuration) throws IOException {
LOGGER.fine("writing data for zoom interval " + zoomIntervalIndex + ", number of tiles: "
+ dataStore.getTileGridLayout(zoomIntervalIndex).getAmountTilesHorizontal()
* dataStore.getTileGridLayout(zoomIntervalIndex).getAmountTilesVertical());
final TileCoordinate upperLeft = dataStore.getTileGridLayout(zoomIntervalIndex).getUpperLeft();
final int lengthX = dataStore.getTileGridLayout(zoomIntervalIndex).getAmountTilesHorizontal();
final int lengthY = dataStore.getTileGridLayout(zoomIntervalIndex).getAmountTilesVertical();
final int amountTiles = lengthX * lengthY;
// used to monitor progress
double amountOfTilesInPercentStep = amountTiles;
if (amountTiles > PROGRESS_PERCENT_STEP) {
amountOfTilesInPercentStep = Math.ceil(amountTiles / PROGRESS_PERCENT_STEP);
}
int processedTiles = 0;
final byte baseZoomCurrentInterval = dataStore.getZoomIntervalConfiguration().getBaseZoom(zoomIntervalIndex);
final int tileAmountInBytes = lengthX * lengthY * BYTE_AMOUNT_SUBFILE_INDEX_PER_TILE;
final int indexBufferSize = tileAmountInBytes
+ (configuration.isDebugStrings() ? DEBUG_INDEX_START_STRING.getBytes(UTF8_CHARSET).length : 0);
final ByteBuffer indexBuffer = ByteBuffer.allocate(indexBufferSize);
final ByteBuffer tileBuffer = ByteBuffer.allocate(TILE_BUFFER_SIZE);
final ByteBuffer wayDataBuffer = ByteBuffer.allocate(WAY_DATA_BUFFER_SIZE);
final ByteBuffer wayBuffer = ByteBuffer.allocate(WAY_BUFFER_SIZE);
final ByteBuffer poiDataBuffer = ByteBuffer.allocate(POI_DATA_BUFFER_SIZE);
final ByteBuffer multipleTilesBuffer = ByteBuffer.allocate(TILES_BUFFER_SIZE);
// write debug strings for tile index segment if necessary
if (configuration.isDebugStrings()) {
indexBuffer.put(DEBUG_INDEX_START_STRING.getBytes(UTF8_CHARSET));
}
long currentSubfileOffset = indexBufferSize;
randomAccessFile.seek(startPositionSubfile + indexBufferSize);
for (int tileY = upperLeft.getY(); tileY < upperLeft.getY() + lengthY; tileY++) {
for (int tileX = upperLeft.getX(); tileX < upperLeft.getX() + lengthX; tileX++) {
TileCoordinate tileCoordinate = new TileCoordinate(tileX, tileY, baseZoomCurrentInterval);
processIndexEntry(tileCoordinate, indexBuffer, currentSubfileOffset);
processTile(configuration, tileCoordinate, dataStore, jtsGeometryCache, zoomIntervalIndex, tileBuffer,
poiDataBuffer, wayDataBuffer, wayBuffer);
currentSubfileOffset += tileBuffer.position();
writeTile(multipleTilesBuffer, tileBuffer, randomAccessFile);
if (++processedTiles % amountOfTilesInPercentStep == 0) {
if (processedTiles == amountTiles) {
LOGGER.info("written 100% of sub file for zoom interval " + (zoomIntervalIndex + 1) + " (" + dataStore.getZoomIntervalConfiguration().toString(zoomIntervalIndex) + ")");
} else {
LOGGER.info("written " + Math.round(processedTiles / amountOfTilesInPercentStep / PROGRESS_PERCENT_STEP * 100)
+ "% of sub file for zoom interval " + (zoomIntervalIndex + 1) + " (" + dataStore.getZoomIntervalConfiguration().toString(zoomIntervalIndex) + ")");
}
}
// TODO accounting for progress information
} // end for loop over tile columns
} // /end for loop over tile rows
// write remaining tiles
if (multipleTilesBuffer.position() > 0) {
// byte buffer was not previously cleared
randomAccessFile.write(multipleTilesBuffer.array(), 0, multipleTilesBuffer.position());
}