Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a309092

Browse files
committed
Mapsforge maps v5: variable tag values improvements #1006
1 parent 7a9b1ca commit a309092

2 files changed

Lines changed: 60 additions & 47 deletions

File tree

mapsforge-map-reader/src/main/java/org/mapsforge/map/reader/MapFile.java

Lines changed: 9 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -640,7 +640,10 @@ private List<PointOfInterest> processPOIs(double tileLatitude, double tileLongit
640640
byte numberOfTags = (byte) (specialByte & POI_NUMBER_OF_TAGS_BITMASK);
641641

642642
// get the tags from IDs (VBE-U)
643-
List<Tag> tags = decodeTags(poiTags, numberOfTags, readBuffer);
643+
List<Tag> tags = readBuffer.readTags(poiTags, numberOfTags);
644+
if (tags == null) {
645+
return null;
646+
}
644647

645648
// get the feature bitmask (1 byte)
646649
byte featureByte = readBuffer.readByte();
@@ -761,7 +764,11 @@ private List<Way> processWays(QueryParameters queryParameters, int numberOfWays,
761764
// bit 5-8 represent the number of tag IDs
762765
byte numberOfTags = (byte) (specialByte & WAY_NUMBER_OF_TAGS_BITMASK);
763766

764-
List<Tag> tags = decodeTags(wayTags, numberOfTags, readBuffer);
767+
// get the tags from IDs (VBE-U)
768+
List<Tag> tags = readBuffer.readTags(wayTags, numberOfTags);
769+
if (tags == null) {
770+
return null;
771+
}
765772

766773
// get the feature bitmask (1 byte)
767774
byte featureByte = readBuffer.readByte();
@@ -813,47 +820,6 @@ private List<Way> processWays(QueryParameters queryParameters, int numberOfWays,
813820
return ways;
814821
}
815822

816-
private List<Tag> decodeTags(Tag[] tagArray, byte numberOfTags, ReadBuffer readBuffer) {
817-
List<Integer> ids = new ArrayList<>();
818-
List<Tag> tags = new ArrayList<>();
819-
820-
for (byte tagIndex = numberOfTags; tagIndex != 0; --tagIndex) {
821-
int id = readBuffer.readUnsignedInt();
822-
if (id < 0 || id >= tagArray.length) {
823-
LOGGER.warning("invalid tag ID: " + id);
824-
return null;
825-
}
826-
ids.add(id);
827-
}
828-
829-
for (Integer id : ids) {
830-
Tag tag = tagArray[id];
831-
// Decode variable values of tags
832-
if (tag.value.charAt(0) == '%' && tag.value.length() == 2) {
833-
String value = tag.value;
834-
if (value.charAt(1) == 'b') {
835-
value = String.valueOf(readBuffer.readByte());
836-
} else if (value.charAt(1) == 'i') {
837-
if (tag.key.contains(":colour")) {
838-
value = "#" + Integer.toHexString(readBuffer.readInt());
839-
} else {
840-
value = String.valueOf(readBuffer.readInt());
841-
}
842-
} else if (value.charAt(1) == 'f') {
843-
value = String.valueOf(readBuffer.readFloat());
844-
} else if (value.charAt(1) == 'h') {
845-
value = String.valueOf(readBuffer.readShort());
846-
} else if (value.charAt(1) == 's') {
847-
value = readBuffer.readUTF8EncodedString();
848-
}
849-
tag = new Tag(tag.key, value);
850-
}
851-
tags.add(tag);
852-
}
853-
854-
return tags;
855-
}
856-
857823
/**
858824
* Reads only labels for tile.
859825
*

mapsforge-map-reader/src/main/java/org/mapsforge/map/reader/ReadBuffer.java

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,16 @@
1818
*/
1919
package org.mapsforge.map.reader;
2020

21+
import org.mapsforge.core.model.Tag;
2122
import org.mapsforge.core.util.Parameters;
2223

2324
import java.io.IOException;
2425
import java.io.RandomAccessFile;
2526
import java.io.UnsupportedEncodingException;
2627
import java.nio.ByteBuffer;
2728
import java.nio.channels.FileChannel;
29+
import java.util.ArrayList;
30+
import java.util.List;
2831
import java.util.logging.Logger;
2932

3033
/**
@@ -40,6 +43,8 @@ public class ReadBuffer {
4043
private ByteBuffer bufferWrapper;
4144
private final FileChannel inputChannel;
4245

46+
private final List<Integer> tagIds = new ArrayList<>();
47+
4348
ReadBuffer(FileChannel inputChannel) {
4449
this.inputChannel = inputChannel;
4550
}
@@ -55,14 +60,13 @@ public byte readByte() {
5560

5661
/**
5762
* Converts four bytes from the read buffer to a float.
63+
* <p/>
64+
* The byte order is big-endian.
5865
*
5966
* @return the float value.
6067
*/
6168
public float readFloat() {
62-
byte[] bytes = new byte[4];
63-
System.arraycopy(bufferData, bufferPosition, bytes, 0, 4);
64-
this.bufferPosition += 4;
65-
return ByteBuffer.wrap(bytes).getFloat();
69+
return Float.intBitsToFloat(readInt());
6670
}
6771

6872
/**
@@ -186,6 +190,49 @@ public int readSignedInt() {
186190
return variableByteDecode | ((this.bufferData[this.bufferPosition++] & 0x3f) << variableByteShift);
187191
}
188192

193+
List<Tag> readTags(Tag[] tagsArray, byte numberOfTags) {
194+
List<Tag> tags = new ArrayList<>();
195+
tagIds.clear();
196+
197+
int maxTag = tagsArray.length;
198+
199+
for (byte tagIndex = numberOfTags; tagIndex != 0; --tagIndex) {
200+
int tagId = readUnsignedInt();
201+
if (tagId < 0 || tagId >= maxTag) {
202+
LOGGER.warning("invalid tag ID: " + tagId);
203+
return null;
204+
}
205+
tagIds.add(tagId);
206+
}
207+
208+
for (int tagId : tagIds) {
209+
Tag tag = tagsArray[tagId];
210+
// Decode variable values of tags
211+
if (tag.value.charAt(0) == '%' && tag.value.length() == 2) {
212+
String value = tag.value;
213+
if (value.charAt(1) == 'b') {
214+
value = String.valueOf(readByte());
215+
} else if (value.charAt(1) == 'i') {
216+
if (tag.key.contains(":colour")) {
217+
value = "#" + Integer.toHexString(readInt());
218+
} else {
219+
value = String.valueOf(readInt());
220+
}
221+
} else if (value.charAt(1) == 'f') {
222+
value = String.valueOf(readFloat());
223+
} else if (value.charAt(1) == 'h') {
224+
value = String.valueOf(readShort());
225+
} else if (value.charAt(1) == 's') {
226+
value = readUTF8EncodedString();
227+
}
228+
tag = new Tag(tag.key, value);
229+
}
230+
tags.add(tag);
231+
}
232+
233+
return tags;
234+
}
235+
189236
/**
190237
* Converts a variable amount of bytes from the read buffer to an unsigned int.
191238
* <p/>

0 commit comments

Comments
 (0)