finish first part of database -> tilesource refactoring

- now there is a clean separation between TileSource type
  and TileDataSource(worker instances)
This commit is contained in:
Hannes Janetzek
2013-05-27 03:13:24 +02:00
parent 2be2ab111a
commit 4abec66e39
47 changed files with 666 additions and 643 deletions

View File

@@ -0,0 +1,28 @@
/*
* Copyright 2013 Hannes Janetzek
*
* 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.oscim.tilesource;
import org.oscim.core.MapElement;
/**
* MapDatabase callback (implemented by MapTileLoader)
* .
* NOTE: MapElement passed belong to the caller! i.e. dont hold
* references to its arrays after callback function returns.
*/
public interface ITileDataSink {
void process(MapElement element);
}

View File

@@ -0,0 +1,47 @@
/*
* Copyright 2010, 2011, 2012 mapsforge.org
* Copyright 2012 Hannes Janetzek
*
* 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.oscim.tilesource;
import org.oscim.layers.tile.MapTile;
/**
*
*
*/
public interface ITileDataSource {
/**
* Starts a database query with the given parameters.
*
* @param tile
* the tile to read.
* @param mapDataSink
* the callback which handles the extracted map elements.
* @return true if successful
*/
abstract QueryResult executeQuery(MapTile tile,
ITileDataSink mapDataSink);
abstract void destroy();
public static enum QueryResult {
SUCCESS,
FAILED,
TILE_NOT_FOUND,
DELAYED,
}
}

View File

@@ -0,0 +1,129 @@
/*
* Copyright 2010, 2011, 2012 mapsforge.org
*
* 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.oscim.tilesource;
import org.oscim.core.BoundingBox;
import org.oscim.core.GeoPoint;
/**
* Contains the immutable metadata of a map file.
*
*/
public class MapInfo {
/**
* The bounding box of the map file.
*/
public final BoundingBox boundingBox;
/**
* The comment field of the map file (may be null).
*/
public final String comment;
/**
* The created by field of the map file (may be null).
*/
public final String createdBy;
/**
* The size of the map file, measured in bytes.
*/
public final long fileSize;
/**
* The file version number of the map file.
*/
public final int fileVersion;
/**
* The preferred language for names as defined in ISO 3166-1 (may be null).
*/
public final String languagePreference;
/**
* The center point of the map file.
*/
public final GeoPoint mapCenter;
/**
* The date of the map data in milliseconds since January 1, 1970.
*/
public final long mapDate;
/**
* The name of the projection used in the map file.
*/
public final String projectionName;
/**
* The map start position from the file header (may be null).
*/
public final GeoPoint startPosition;
/**
* The map start zoom level from the file header (may be null).
*/
public final Byte startZoomLevel;
/**
* Zoomlevels provided by this Database, if null then any zoomlevel can be
* queried.
*/
public final int[] zoomLevel;
/**
* @param bbox
* ...
* @param zoom
* ...
* @param start
* ...
* @param projection
* ...
* @param date
* ...
* @param size
* ...
* @param version
* ...
* @param language
* ...
* @param comment
* ...
* @param createdBy
* ...
* @param zoomLevel
* TODO
*/
public MapInfo(BoundingBox bbox, Byte zoom, GeoPoint start, String projection,
long date, long size, int version, String language, String comment,
String createdBy, int[] zoomLevel) {
this.startZoomLevel = zoom;
this.zoomLevel = zoomLevel;
this.startPosition = start;
this.projectionName = projection;
this.mapDate = date;
this.boundingBox = bbox;
this.mapCenter = bbox.getCenterPoint();
this.languagePreference = language;
this.fileSize = size;
this.fileVersion = version;
this.comment = comment;
this.createdBy = createdBy;
}
}

View File

@@ -0,0 +1,43 @@
/*
* Copyright 2012 Hannes Janetzek
*
* 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.oscim.tilesource;
import java.util.HashMap;
public class MapOptions extends HashMap<String, String> {
private static final long serialVersionUID = 1L;
public final TileSources db;
public MapOptions(TileSources db) {
this.db = db;
}
@Override
public boolean equals(Object other) {
if (!(other instanceof MapOptions))
return false;
if (this.db != ((MapOptions) other).db)
return false;
// FIXME test if this is correct!
if (!this.entrySet().equals(((MapOptions) other).entrySet()))
return false;
return true;
}
}

View File

@@ -0,0 +1,119 @@
/*
* Copyright 2013 Hannes Janetzek
*
* 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.oscim.tilesource;
import java.util.HashMap;
public abstract class TileSource {
public abstract ITileDataSource getDataSource();
public abstract OpenResult open();
public abstract void close();
protected final Options options = new Options();
public void setOption(String key, String value){
options.put(key, value);
}
public String getOption(String key){
return options.get(key);
}
/**
* @return the metadata for the current map file.
* @throws IllegalStateException
* if no map is currently opened.
*/
public abstract MapInfo getMapInfo();
public static class Options extends HashMap<String, String> {
private static final long serialVersionUID = 1L;
@Override
public boolean equals(Object other) {
if (!(other instanceof MapOptions))
return false;
//if (this.db != ((MapOptions) other).db)
// return false;
// FIXME test if this is correct!
if (!this.entrySet().equals(((MapOptions) other).entrySet()))
return false;
return true;
}
}
/**
* A FileOpenResult is a simple DTO which is returned by
* IMapDatabase#open().
*/
public static class OpenResult {
/**
* Singleton for a FileOpenResult instance with {@code success=true}.
*/
public static final OpenResult SUCCESS = new OpenResult();
private final String errorMessage;
private final boolean success;
/**
* @param errorMessage
* a textual message describing the error, must not be null.
*/
public OpenResult(String errorMessage) {
if (errorMessage == null) {
throw new IllegalArgumentException("error message must not be null");
}
this.success = false;
this.errorMessage = errorMessage;
}
public OpenResult() {
this.success = true;
this.errorMessage = null;
}
/**
* @return a textual error description (might be null).
*/
public String getErrorMessage() {
return this.errorMessage;
}
/**
* @return true if the file could be opened successfully, false
* otherwise.
*/
public boolean isSuccess() {
return this.success;
}
@Override
public String toString() {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("FileOpenResult [success=");
stringBuilder.append(this.success);
stringBuilder.append(", errorMessage=");
stringBuilder.append(this.errorMessage);
stringBuilder.append("]");
return stringBuilder.toString();
}
}
}

View File

@@ -0,0 +1,55 @@
/*
* Copyright 2010, 2011, 2012 mapsforge.org
*
* 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.oscim.tilesource;
/**
* MapDatabase Implementations
*/
public enum TileSources {
/**
* ...
*/
TEST,
/**
* ...
*/
MAPSFORGE,
/**
* ...
*/
POSTGIS,
/**
* ...
*/
OPENSCIENCEMAP1,
/**
* ...
*/
OPENSCIENCEMAP2,
/**
* ...
*/
OPENSCIENCEMAP4,
/**
* ...
*/
MAPNIK_VECTOR,
}

View File

@@ -0,0 +1,351 @@
/*
* Copyright 2013 Hannes Janetzek
*
* 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.oscim.tilesource.common;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.URL;
import java.util.zip.InflaterInputStream;
import org.oscim.core.Tile;
import android.os.SystemClock;
import android.util.Log;
public class LwHttp {
private static final String TAG = LwHttp.class.getName();
//private static final boolean DEBUG = false;
private final static byte[] HEADER_HTTP_OK = "200 OK".getBytes();
private final static byte[] HEADER_CONTENT_TYPE = "Content-Type".getBytes();
private final static byte[] HEADER_CONTENT_LENGTH = "Content-Length".getBytes();
private final static int RESPONSE_EXPECTED_LIVES = 100;
private final static int RESPONSE_TIMEOUT = 10000;
private final static int BUFFER_SIZE = 1024;
private final byte[] buffer = new byte[BUFFER_SIZE];
private final String mHost;
private final int mPort;
private int mMaxReq = 0;
private Socket mSocket;
private OutputStream mCommandStream;
private InputStream mResponseStream;
private long mLastRequest = 0;
private SocketAddress mSockAddr;
private final byte[] REQUEST_GET_START;
private final byte[] REQUEST_GET_END;
private final byte[] mRequestBuffer;
private final boolean mInflateContent;
private final byte[] mContentType;
//private final String mExtension;
private int mContentLength = -1;
public LwHttp(URL url, String contentType, String extension, boolean deflate) {
//mExtension = extension;
mContentType = contentType.getBytes();
mInflateContent = deflate;
int port = url.getPort();
if (port < 0)
port = 80;
String host = url.getHost();
String path = url.getPath();
Log.d(TAG, "open database: " + host + " " + port + " " + path);
REQUEST_GET_START = ("GET " + path).getBytes();
REQUEST_GET_END = ("." + extension + " HTTP/1.1" +
"\nHost: " + host +
"\nConnection: Keep-Alive" +
"\n\n").getBytes();
mHost = host;
mPort = port;
mRequestBuffer = new byte[1024];
System.arraycopy(REQUEST_GET_START, 0,
mRequestBuffer, 0, REQUEST_GET_START.length);
}
static class Buffer extends BufferedInputStream {
public Buffer(InputStream is) {
super(is, 4096);
}
@Override
public synchronized int read() throws IOException {
return super.read();
}
@Override
public synchronized int read(byte[] buffer, int offset, int byteCount) throws IOException {
return super.read(buffer, offset, byteCount);
}
}
// public void setServer(URL url) {
//
// int port = url.getPort();
// if (port < 0)
// port = 80;
//
// String host = url.getHost();
// String path = url.getPath();
// Log.d(TAG, "open database: " + host + " " + port + " " + path);
//
// REQUEST_GET_START = ("GET " + path).getBytes();
//
// REQUEST_GET_END = ("." + mExtension + " HTTP/1.1" +
// "\nHost: " + host +
// "\nConnection: Keep-Alive" +
// "\n\n").getBytes();
//
// mHost = host;
// mPort = port;
//
// mRequestBuffer = new byte[1024];
// System.arraycopy(REQUEST_GET_START, 0,
// mRequestBuffer, 0, REQUEST_GET_START.length);
// }
public void close() {
if (mSocket != null) {
try {
mSocket.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
mSocket = null;
}
}
}
public InputStream readHeader() throws IOException {
InputStream is = mResponseStream;
is.mark(4096);
byte[] buf = buffer;
boolean first = true;
int read = 0;
int pos = 0;
int end = 0;
int len = 0;
mContentLength = -1;
// header cannot be larger than BUFFER_SIZE for this to work
for (; pos < read || (len = is.read(buf, read, BUFFER_SIZE - read)) >= 0; len = 0) {
read += len;
// end of header lines
while (end < read && (buf[end] != '\n'))
end++;
if (buf[end] == '\n') {
if (first) {
// check only for OK
first = false;
if (!check(HEADER_HTTP_OK, 6, buf, pos + 9, end)) {
String line = new String(buf, pos, end - pos - 1);
Log.d(TAG, ">" + line + "< ");
return null;
}
} else if (end - pos == 1) {
// check empty line (header end)
end += 1;
break;
} else if (check(HEADER_CONTENT_TYPE, 12, buf, pos, end)) {
if (!check(mContentType, mContentType.length, buf, pos + 14, end))
return null;
} else if (check(HEADER_CONTENT_LENGTH, 14, buf, pos, end)) {
mContentLength = parseInt(pos + 16, end-1, buf);
}
//String line = new String(buf, pos, end - pos - 1);
//Log.d(TAG, ">" + line + "< " + mContentLength);
pos += (end - pos) + 1;
end = pos;
}
}
// back to start of content
is.reset();
is.mark(0);
is.skip(end);
if (mInflateContent)
return new InflaterInputStream(is);
return is;
}
public boolean sendRequest(Tile tile) throws IOException {
if (mSocket != null && ((mMaxReq-- <= 0)
|| (SystemClock.elapsedRealtime() - mLastRequest > RESPONSE_TIMEOUT))) {
try {
mSocket.close();
} catch (IOException e) {
}
// Log.d(TAG, "not alive - recreate connection " + mMaxReq);
mSocket = null;
}
if (mSocket == null) {
lwHttpConnect();
// we know our server
mMaxReq = RESPONSE_EXPECTED_LIVES;
// Log.d(TAG, "create connection");
} else {
int avail = mResponseStream.available();
if (avail > 0) {
Log.d(TAG, "Consume left-over bytes: " + avail);
while ((avail = mResponseStream.available()) > 0)
mResponseStream.read(buffer);
Log.d(TAG, "Consumed bytes");
}
}
byte[] request = mRequestBuffer;
int pos = REQUEST_GET_START.length;
int newPos = 0;
if ((newPos = formatTilePath(tile, request, pos)) == 0) {
request[pos++] = '/';
pos = writeInt(tile.zoomLevel, pos, request);
request[pos++] = '/';
pos = writeInt(tile.tileX, pos, request);
request[pos++] = '/';
pos = writeInt(tile.tileY, pos, request);
} else {
pos = newPos;
}
int len = REQUEST_GET_END.length;
System.arraycopy(REQUEST_GET_END, 0, request, pos, len);
len += pos;
try {
mCommandStream.write(request, 0, len);
mCommandStream.flush();
return true;
} catch (IOException e) {
Log.d(TAG, "recreate connection");
}
lwHttpConnect();
mCommandStream.write(request, 0, len);
mCommandStream.flush();
return true;
}
private boolean lwHttpConnect() throws IOException {
if (mSockAddr == null)
mSockAddr = new InetSocketAddress(mHost, mPort);
mSocket = new Socket();
mSocket.connect(mSockAddr, 30000);
mSocket.setTcpNoDelay(true);
mCommandStream = mSocket.getOutputStream();
mResponseStream = new BufferedInputStream(mSocket.getInputStream());
return true;
}
// write (positive) integer to byte array
protected static int writeInt(int val, int pos, byte[] buf) {
if (val == 0) {
buf[pos] = '0';
return pos + 1;
}
int i = 0;
for (int n = val; n > 0; n = n / 10, i++)
buf[pos + i] = (byte) ('0' + n % 10);
// reverse bytes
for (int j = pos, end = pos + i - 1, mid = pos + i / 2; j < mid; j++, end--) {
byte tmp = buf[j];
buf[j] = buf[end];
buf[end] = tmp;
}
return pos + i;
}
// parse (positive) integer from byte array
protected static int parseInt(int pos, int end, byte[] buf) {
int val = 0;
for (; pos < end; pos++)
val = val * 10 + (buf[pos]) - '0';
return val;
}
private static boolean check(byte[] string, int length, byte[] buffer,
int position, int available) {
if (available - position < length)
return false;
for (int i = 0; i < length; i++)
if (buffer[position + i] != string[i])
return false;
return true;
}
public void requestCompleted() {
mLastRequest = SystemClock.elapsedRealtime();
}
public int getContentLength(){
return mContentLength;
}
/**
* Write custom tile url
*
* @param tile Tile
* @param path to write url string
* @param curPos current position
* @return new position
*/
protected int formatTilePath(Tile tile, byte[] path, int curPos) {
return 0;
}
}

View File

@@ -0,0 +1,522 @@
/*
* Copyright 2013
*
* 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.oscim.tilesource.common;
import java.io.IOException;
import java.io.InputStream;
import org.oscim.core.Tile;
import org.oscim.tilesource.ITileDataSink;
import org.oscim.utils.UTF8Decoder;
import android.util.Log;
public abstract class PbfDecoder {
private final static String TAG = PbfDecoder.class.getName();
protected static final boolean debug = false;
static class ProtobufException extends IOException {
private static final long serialVersionUID = 1L;
public ProtobufException(String detailMessage) {
super(detailMessage);
}
}
final static ProtobufException TRUNCATED_MSG = new ProtobufException("truncated msg");
protected final static ProtobufException INVALID_VARINT = new ProtobufException("invalid varint");
protected final static ProtobufException INVALID_PACKED_SIZE = new ProtobufException(
"invalid message size");
protected void error(String msg) throws IOException {
throw new ProtobufException(msg);
}
private final static int BUFFER_SIZE = 1 << 15; // 32kb
protected byte[] buffer = new byte[BUFFER_SIZE];
// position in buffer
protected int bufferPos;
// bytes available in buffer
protected int bufferFill;
// offset of buffer in message
private int mBufferOffset;
// max bytes to read: message = header + content
private int mMsgEnd;
// overall bytes of message read
private int mMsgPos;
private InputStream mInputStream;
private final UTF8Decoder mStringDecoder;
public PbfDecoder() {
mStringDecoder = new UTF8Decoder();
}
public abstract boolean decode(Tile tile, ITileDataSink sink,
InputStream is, int contentLength) throws IOException;
public void setInputStream(InputStream is, int contentLength) {
mInputStream = is;
bufferFill = 0;
bufferPos = 0;
mBufferOffset = 0;
mMsgPos = 0;
mMsgEnd = contentLength;
}
protected int decodeVarint32() throws IOException {
int bytesLeft = 0;
int val = 0;
for (int shift = 0; shift < 32; shift += 7) {
if (bytesLeft == 0)
bytesLeft = fillBuffer(1);
byte b = buffer[bufferPos++];
val |= (b & 0x7f) << shift;
if (b >= 0)
return val;
bytesLeft--;
}
throw INVALID_VARINT;
}
protected long decodeVarint64() throws IOException {
int bytesLeft = 0;
long val = 0;
for (int shift = 0; shift < 64; shift += 7) {
if (bytesLeft == 0)
bytesLeft = fillBuffer(1);
byte b = buffer[bufferPos++];
val |= (long) (b & 0x7f) << shift;
if (b >= 0)
return val;
bytesLeft--;
}
throw INVALID_VARINT;
}
protected String decodeString() throws IOException {
String result;
final int size = decodeVarint32();
fillBuffer(size);
if (mStringDecoder == null)
result = new String(buffer, bufferPos, size, "UTF-8");
else
result = mStringDecoder.decode(buffer, bufferPos, size);
bufferPos += size;
return result;
}
protected float decodeFloat() throws IOException {
if (bufferPos + 4 > bufferFill)
fillBuffer(4);
int val = (buffer[bufferPos++] & 0xFF
| (buffer[bufferPos++] & 0xFF) << 8
| (buffer[bufferPos++] & 0xFF) << 16
| (buffer[bufferPos++] & 0xFF) << 24);
return Float.intBitsToFloat(val);
}
protected double decodeDouble() throws IOException {
if (bufferPos + 8 > bufferFill)
fillBuffer(8);
long val = (buffer[bufferPos++] & 0xFF
| (buffer[bufferPos++] & 0xFF) << 8
| (buffer[bufferPos++] & 0xFF) << 16
| (buffer[bufferPos++] & 0xFF) << 24
| (buffer[bufferPos++] & 0xFF) << 32
| (buffer[bufferPos++] & 0xFF) << 40
| (buffer[bufferPos++] & 0xFF) << 48
| (buffer[bufferPos++] & 0xFF) << 56);
return Double.longBitsToDouble(val);
}
protected boolean decodeBool() throws IOException {
if (bufferPos + 1 > bufferFill)
fillBuffer(1);
return buffer[bufferPos++] != 0;
}
protected int decodeInterleavedPoints(float[] coords, float scale)
throws IOException {
int bytes = decodeVarint32();
fillBuffer(bytes);
int cnt = 0;
int lastX = 0;
int lastY = 0;
boolean even = true;
byte[] buf = buffer;
int pos = bufferPos;
int end = pos + bytes;
int val;
while (pos < end) {
if (buf[pos] >= 0) {
val = buf[pos++];
} else if (buf[pos + 1] >= 0) {
val = (buf[pos++] & 0x7f)
| buf[pos++] << 7;
} else if (buf[pos + 2] >= 0) {
val = (buf[pos++] & 0x7f)
| (buf[pos++] & 0x7f) << 7
| (buf[pos++]) << 14;
} else if (buf[pos + 3] >= 0) {
val = (buf[pos++] & 0x7f)
| (buf[pos++] & 0x7f) << 7
| (buf[pos++] & 0x7f) << 14
| (buf[pos++]) << 21;
} else {
val = (buf[pos++] & 0x7f)
| (buf[pos++] & 0x7f) << 7
| (buf[pos++] & 0x7f) << 14
| (buf[pos++] & 0x7f) << 21
| (buf[pos]) << 28;
if (buf[pos++] < 0)
throw INVALID_VARINT;
}
// zigzag decoding
int s = ((val >>> 1) ^ -(val & 1));
if (even) {
lastX = lastX + s;
coords[cnt++] = lastX / scale;
even = false;
} else {
lastY = lastY + s;
coords[cnt++] = lastY / scale;
even = true;
}
}
if (pos != bufferPos + bytes)
throw INVALID_PACKED_SIZE;
bufferPos = pos;
// return number of points read
return (cnt >> 1);
}
protected static int deZigZag(int val) {
return ((val >>> 1) ^ -(val & 1));
}
public void decodeVarintArray(int num, short[] array) throws IOException {
int bytes = decodeVarint32();
fillBuffer(bytes);
byte[] buf = buffer;
int pos = bufferPos;
int end = pos + bytes;
int val;
int cnt = 0;
while (pos < end) {
if (cnt == num)
throw new ProtobufException("invalid array size " + num);
if (buf[pos] >= 0) {
val = buf[pos++];
} else if (buf[pos + 1] >= 0) {
val = (buf[pos++] & 0x7f)
| buf[pos++] << 7;
} else if (buf[pos + 2] >= 0) {
val = (buf[pos++] & 0x7f)
| (buf[pos++] & 0x7f) << 7
| (buf[pos++]) << 14;
} else if (buf[pos + 3] >= 0) {
val = (buf[pos++] & 0x7f)
| (buf[pos++] & 0x7f) << 7
| (buf[pos++] & 0x7f) << 14
| (buf[pos++]) << 21;
} else {
val = (buf[pos++] & 0x7f)
| (buf[pos++] & 0x7f) << 7
| (buf[pos++] & 0x7f) << 14
| (buf[pos++] & 0x7f) << 21
| (buf[pos]) << 28;
if (buf[pos++] < 0)
throw INVALID_VARINT;
}
array[cnt++] = (short) val;
}
if (pos != bufferPos + bytes)
throw INVALID_PACKED_SIZE;
bufferPos = pos;
}
/**
* fill short array from packed uint32. Array values must be positive
* as the end will be marked by -1 if the resulting array is larger
* than the input!
*/
protected short[] decodeUnsignedVarintArray(short[] array) throws IOException {
int bytes = decodeVarint32();
int arrayLength = 0;
if (array == null) {
arrayLength = 32;
array = new short[32];
}
fillBuffer(bytes);
int cnt = 0;
byte[] buf = buffer;
int pos = bufferPos;
int end = pos + bytes;
int val;
while (pos < end) {
if (buf[pos] >= 0) {
val = buf[pos++];
} else if (buf[pos + 1] >= 0) {
val = (buf[pos++] & 0x7f)
| buf[pos++] << 7;
} else if (buf[pos + 2] >= 0) {
val = (buf[pos++] & 0x7f)
| (buf[pos++] & 0x7f) << 7
| (buf[pos++]) << 14;
} else if (buf[pos + 3] >= 0) {
val = (buf[pos++] & 0x7f)
| (buf[pos++] & 0x7f) << 7
| (buf[pos++] & 0x7f) << 14
| (buf[pos++]) << 21;
} else {
val = (buf[pos++] & 0x7f)
| (buf[pos++] & 0x7f) << 7
| (buf[pos++] & 0x7f) << 14
| (buf[pos++] & 0x7f) << 21
| (buf[pos]) << 28;
if (buf[pos++] < 0)
throw INVALID_VARINT;
}
if (arrayLength <= cnt) {
arrayLength = cnt + 16;
short[] tmp = array;
array = new short[arrayLength];
System.arraycopy(tmp, 0, array, 0, cnt);
}
array[cnt++] = (short) val;
}
if (pos != bufferPos + bytes)
throw INVALID_PACKED_SIZE;
bufferPos = pos;
if (arrayLength > cnt)
array[cnt] = -1;
return array;
}
// for use int packed varint decoders
protected int decodeVarint32Filled() throws IOException {
byte[] buf = buffer;
int pos = bufferPos;
int val;
if (buf[pos] >= 0) {
val = buf[pos++];
} else {
if (buf[pos + 1] >= 0) {
val = (buf[pos++] & 0x7f)
| (buf[pos++]) << 7;
} else if (buf[pos + 2] >= 0) {
val = (buf[pos++] & 0x7f)
| (buf[pos++] & 0x7f) << 7
| (buf[pos++]) << 14;
} else if (buf[pos + 3] >= 0) {
val = (buf[pos++] & 0x7f)
| (buf[pos++] & 0x7f) << 7
| (buf[pos++] & 0x7f) << 14
| (buf[pos++]) << 21;
} else {
val = (buf[pos++] & 0x7f)
| (buf[pos++] & 0x7f) << 7
| (buf[pos++] & 0x7f) << 14
| (buf[pos++] & 0x7f) << 21
| (buf[pos]) << 28;
if (buf[pos++] < 0)
throw INVALID_VARINT;
}
}
bufferPos = pos;
return val;
}
public boolean hasData() throws IOException {
if (mBufferOffset + bufferPos >= mMsgEnd)
return false;
return fillBuffer(1) > 0;
}
public int position() {
return mBufferOffset + bufferPos;
}
public int fillBuffer(int size) throws IOException {
int bytesLeft = bufferFill - bufferPos;
// check if buffer already contains the request bytes
if (bytesLeft >= size)
return bytesLeft;
// check if inputstream is read to the end
if (mMsgPos >= mMsgEnd)
return bytesLeft;
int maxSize = buffer.length;
if (size > maxSize) {
if (debug)
Log.d(TAG, "increase read buffer to " + size + " bytes");
maxSize = size;
byte[] tmp = buffer;
buffer = new byte[maxSize];
System.arraycopy(tmp, bufferPos, buffer, 0, bytesLeft);
mBufferOffset += bufferPos;
bufferPos = 0;
bufferFill = bytesLeft;
} else if (bytesLeft == 0) {
// just advance buffer offset and reset buffer
mBufferOffset += bufferPos;
bufferPos = 0;
bufferFill = 0;
} else if (bufferPos + size > maxSize) {
// copy bytes left to the beginning of buffer
if (debug)
Log.d(TAG, "shift " + bufferFill + " " + bufferPos + " " + size);
System.arraycopy(buffer, bufferPos, buffer, 0, bytesLeft);
mBufferOffset += bufferPos;
bufferPos = 0;
bufferFill = bytesLeft;
}
while ((bufferFill - bufferPos) < size) {
int max = maxSize - bufferFill;
if (max > mMsgEnd - mMsgPos)
max = mMsgEnd - mMsgPos;
if (max <= 0) {
// should not be possible
throw new IOException("burp");
}
// read until requested size is available in buffer
int len = mInputStream.read(buffer, bufferFill, max);
if (len < 0) {
mMsgEnd = mMsgPos;
if (debug)
Log.d(TAG, " finished reading " + mMsgPos);
// finished reading, mark end
buffer[bufferFill] = 0;
return bufferFill - bufferPos;
}
mMsgPos += len;
bufferFill += len;
if (mMsgPos == mMsgEnd)
break;
}
return bufferFill - bufferPos;
}
protected static int readUnsignedInt(InputStream is, byte[] buf) throws IOException {
// check 4 bytes available..
int read = 0;
int len = 0;
while (read < 4 && (len = is.read(buf, read, 4 - read)) >= 0)
read += len;
if (read < 4)
return read < 0 ? (read * 10) : read;
return decodeInt(buf, 0);
}
static int decodeInt(byte[] buffer, int offset) {
return buffer[offset] << 24 | (buffer[offset + 1] & 0xff) << 16
| (buffer[offset + 2] & 0xff) << 8
| (buffer[offset + 3] & 0xff);
}
}

View File

@@ -0,0 +1,86 @@
/*
* Copyright 2012 Hannes Janetzek
*
* 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.oscim.tilesource.common;
import java.io.InputStream;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import org.oscim.layers.tile.MapTile;
import org.oscim.tilesource.ITileDataSink;
import org.oscim.tilesource.ITileDataSource;
import android.util.Log;
/**
*
*
*/
public abstract class PbfTileDataSource implements ITileDataSource {
private static final String TAG = PbfTileDataSource.class.getName();
protected LwHttp mConn;
protected final PbfDecoder mTileDecoder;
public PbfTileDataSource(PbfDecoder tileDecoder) {
mTileDecoder = tileDecoder;
}
@Override
public QueryResult executeQuery(MapTile tile, ITileDataSink sink) {
QueryResult result = QueryResult.SUCCESS;
try {
InputStream is;
if (!mConn.sendRequest(tile)) {
Log.d(TAG, tile + " Request Failed");
result = QueryResult.FAILED;
} else if ((is = mConn.readHeader()) != null) {
boolean win = mTileDecoder.decode(tile, sink, is, mConn.getContentLength());
if (!win)
Log.d(TAG, tile + " failed");
} else {
Log.d(TAG, tile + " Network Error");
result = QueryResult.FAILED;
}
} catch (SocketException e) {
Log.d(TAG, tile + " Socket exception: " + e.getMessage());
result = QueryResult.FAILED;
} catch (SocketTimeoutException e) {
Log.d(TAG, tile + " Socket Timeout");
result = QueryResult.FAILED;
} catch (UnknownHostException e) {
Log.d(TAG, tile + " No Network");
result = QueryResult.FAILED;
} catch (Exception e) {
e.printStackTrace();
result = QueryResult.FAILED;
}
mConn.requestCompleted();
if (result != QueryResult.SUCCESS)
mConn.close();
return result;
}
@Override
public void destroy() {
mConn.close();
}
}

View File

@@ -0,0 +1,67 @@
/*
* Copyright 2013 Hannes Janetzek
*
* 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.oscim.tilesource.common;
import java.net.MalformedURLException;
import java.net.URL;
import org.oscim.core.BoundingBox;
import org.oscim.core.GeoPoint;
import org.oscim.tilesource.MapInfo;
import org.oscim.tilesource.TileSource;
public abstract class UrlTileSource extends TileSource{
private final static String KEY_URL = "url";
private static final MapInfo mMapInfo =
new MapInfo(new BoundingBox(-180, -90, 180, 90),
new Byte((byte) 4), new GeoPoint(53.11, 8.85),
null, 0, 0, 0, "de", "comment", "author", null);
protected URL mUrl;
@Override
public OpenResult open() {
if (!options.containsKey(KEY_URL))
return new OpenResult("no url set");
String urlString = options.get(KEY_URL);
try {
mUrl = new URL(urlString);
} catch (MalformedURLException e) {
e.printStackTrace();
return new OpenResult("invalid url " + urlString);
}
return OpenResult.SUCCESS;
}
@Override
public void close() {
}
public boolean setUrl(String urlString){
options.put("url", urlString);
return open() == OpenResult.SUCCESS;
}
@Override
public MapInfo getMapInfo() {
return mMapInfo;
}
}

View File

@@ -0,0 +1,95 @@
/*
* Copyright 2010, 2011, 2012 mapsforge.org
*
* 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.oscim.tilesource.mapfile;
/**
* This utility class contains methods to convert byte arrays to numbers.
*/
final class Deserializer {
/**
* Converts five bytes of a byte array to an unsigned long.
* <p>
* The byte order is big-endian.
*
* @param buffer
* the byte array.
* @param offset
* the offset in the array.
* @return the long value.
*/
static long getFiveBytesLong(byte[] buffer, int offset) {
return (buffer[offset] & 0xffL) << 32 | (buffer[offset + 1] & 0xffL) << 24
| (buffer[offset + 2] & 0xffL) << 16
| (buffer[offset + 3] & 0xffL) << 8 | (buffer[offset + 4] & 0xffL);
}
/**
* Converts four bytes of a byte array to a signed int.
* <p>
* The byte order is big-endian.
*
* @param buffer
* the byte array.
* @param offset
* the offset in the array.
* @return the int value.
*/
static int getInt(byte[] buffer, int offset) {
return buffer[offset] << 24 | (buffer[offset + 1] & 0xff) << 16
| (buffer[offset + 2] & 0xff) << 8
| (buffer[offset + 3] & 0xff);
}
/**
* Converts eight bytes of a byte array to a signed long.
* <p>
* The byte order is big-endian.
*
* @param buffer
* the byte array.
* @param offset
* the offset in the array.
* @return the long value.
*/
static long getLong(byte[] buffer, int offset) {
return (buffer[offset] & 0xffL) << 56 | (buffer[offset + 1] & 0xffL) << 48
| (buffer[offset + 2] & 0xffL) << 40
| (buffer[offset + 3] & 0xffL) << 32 | (buffer[offset + 4] & 0xffL) << 24
| (buffer[offset + 5] & 0xffL) << 16 | (buffer[offset + 6] & 0xffL) << 8
| (buffer[offset + 7] & 0xffL);
}
/**
* Converts two bytes of a byte array to a signed int.
* <p>
* The byte order is big-endian.
*
* @param buffer
* the byte array.
* @param offset
* the offset in the array.
* @return the int value.
*/
static int getShort(byte[] buffer, int offset) {
return buffer[offset] << 8 | (buffer[offset + 1] & 0xff);
}
/**
* Private constructor to prevent instantiation from other classes.
*/
private Deserializer() {
throw new IllegalStateException();
}
}

View File

@@ -0,0 +1,125 @@
/*
* Copyright 2010, 2011, 2012 mapsforge.org
*
* 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.oscim.tilesource.mapfile;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Collections;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.oscim.tilesource.mapfile.header.SubFileParameter;
import org.oscim.utils.LRUCache;
/**
* A cache for database index blocks with a fixed size and LRU policy.
*/
class IndexCache {
/**
* Number of index entries that one index block consists of.
*/
private static final int INDEX_ENTRIES_PER_BLOCK = 128;
private static final Logger LOG = Logger.getLogger(IndexCache.class.getName());
/**
* Maximum size in bytes of one index block.
*/
private static final int SIZE_OF_INDEX_BLOCK = INDEX_ENTRIES_PER_BLOCK
* SubFileParameter.BYTES_PER_INDEX_ENTRY;
private final Map<IndexCacheEntryKey, byte[]> map;
private final RandomAccessFile randomAccessFile;
/**
* @param randomAccessFile
* the map file from which the index should be read and cached.
* @param capacity
* the maximum number of entries in the cache.
* @throws IllegalArgumentException
* if the capacity is negative.
*/
IndexCache(RandomAccessFile randomAccessFile, int capacity) {
this.randomAccessFile = randomAccessFile;
this.map = Collections.synchronizedMap(new LRUCache<IndexCacheEntryKey, byte[]>(capacity));
}
/**
* Destroy the cache at the end of its lifetime.
*/
void destroy() {
this.map.clear();
}
/**
* Returns the index entry of a block in the given map file. If the required
* index entry is not cached, it will be
* read from the map file index and put in the cache.
*
* @param subFileParameter
* the parameters of the map file for which the index entry is
* needed.
* @param blockNumber
* the number of the block in the map file.
* @return the index entry or -1 if the block number is invalid.
*/
synchronized long getIndexEntry(SubFileParameter subFileParameter, long blockNumber) {
try {
// check if the block number is out of bounds
if (blockNumber >= subFileParameter.numberOfBlocks) {
return -1;
}
// calculate the index block number
long indexBlockNumber = blockNumber / INDEX_ENTRIES_PER_BLOCK;
// create the cache entry key for this request
IndexCacheEntryKey indexCacheEntryKey = new IndexCacheEntryKey(subFileParameter,
indexBlockNumber);
// check for cached index block
byte[] indexBlock = this.map.get(indexCacheEntryKey);
if (indexBlock == null) {
// cache miss, seek to the correct index block in the file and read it
long indexBlockPosition = subFileParameter.indexStartAddress + indexBlockNumber
* SIZE_OF_INDEX_BLOCK;
int remainingIndexSize = (int) (subFileParameter.indexEndAddress - indexBlockPosition);
int indexBlockSize = Math.min(SIZE_OF_INDEX_BLOCK, remainingIndexSize);
indexBlock = new byte[indexBlockSize];
this.randomAccessFile.seek(indexBlockPosition);
if (this.randomAccessFile.read(indexBlock, 0, indexBlockSize) != indexBlockSize) {
LOG.warning("reading the current index block has failed");
return -1;
}
// put the index block in the map
this.map.put(indexCacheEntryKey, indexBlock);
}
// calculate the address of the index entry inside the index block
long indexEntryInBlock = blockNumber % INDEX_ENTRIES_PER_BLOCK;
int addressInIndexBlock = (int) (indexEntryInBlock * SubFileParameter.BYTES_PER_INDEX_ENTRY);
// return the real index entry
return Deserializer.getFiveBytesLong(indexBlock, addressInIndexBlock);
} catch (IOException e) {
LOG.log(Level.SEVERE, null, e);
return -1;
}
}
}

View File

@@ -0,0 +1,75 @@
/*
* Copyright 2010, 2011, 2012 mapsforge.org
*
* 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.oscim.tilesource.mapfile;
import org.oscim.tilesource.mapfile.header.SubFileParameter;
/**
* An immutable container class which is the key for the index cache.
*/
class IndexCacheEntryKey {
private final int hashCodeValue;
private final long indexBlockNumber;
private final SubFileParameter subFileParameter;
/**
* Creates an immutable key to be stored in a map.
*
* @param subFileParameter
* the parameters of the map file.
* @param indexBlockNumber
* the number of the index block.
*/
IndexCacheEntryKey(SubFileParameter subFileParameter, long indexBlockNumber) {
this.subFileParameter = subFileParameter;
this.indexBlockNumber = indexBlockNumber;
this.hashCodeValue = calculateHashCode();
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
} else if (!(obj instanceof IndexCacheEntryKey)) {
return false;
}
IndexCacheEntryKey other = (IndexCacheEntryKey) obj;
if (this.subFileParameter == null && other.subFileParameter != null) {
return false;
} else if (this.subFileParameter != null
&& !this.subFileParameter.equals(other.subFileParameter)) {
return false;
} else if (this.indexBlockNumber != other.indexBlockNumber) {
return false;
}
return true;
}
@Override
public int hashCode() {
return this.hashCodeValue;
}
/**
* @return the hash code of this object.
*/
private int calculateHashCode() {
int result = 7;
result = 31 * result
+ ((this.subFileParameter == null) ? 0 : this.subFileParameter.hashCode());
result = 31 * result + (int) (this.indexBlockNumber ^ (this.indexBlockNumber >>> 32));
return result;
}
}

View File

@@ -0,0 +1,998 @@
/*
* Copyright 2010, 2011, 2012 mapsforge.org
* Copyright 2013 Hannes Janetzek
*
* 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.oscim.tilesource.mapfile;
import java.io.IOException;
import java.io.RandomAccessFile;
import org.oscim.core.GeometryBuffer.GeometryType;
import org.oscim.core.MapElement;
import org.oscim.core.Tag;
import org.oscim.core.Tile;
import org.oscim.layers.tile.MapTile;
import org.oscim.tilesource.ITileDataSink;
import org.oscim.tilesource.ITileDataSource;
import org.oscim.tilesource.mapfile.header.SubFileParameter;
import android.util.Log;
/**
* A class for reading binary map files.
*
* @see <a
* href="http://code.google.com/p/mapsforge/wiki/SpecificationBinaryMapFile">Specification</a>
*/
public class MapDatabase implements ITileDataSource {
/**
* Bitmask to extract the block offset from an index entry.
*/
private static final long BITMASK_INDEX_OFFSET = 0x7FFFFFFFFFL;
/**
* Bitmask to extract the water information from an index entry.
*/
private static final long BITMASK_INDEX_WATER = 0x8000000000L;
/**
* Debug message prefix for the block signature.
*/
private static final String DEBUG_SIGNATURE_BLOCK = "block signature: ";
/**
* Debug message prefix for the POI signature.
*/
// private static final String DEBUG_SIGNATURE_POI = "POI signature: ";
/**
* Debug message prefix for the way signature.
*/
private static final String DEBUG_SIGNATURE_WAY = "way signature: ";
/**
* Error message for an invalid first way offset.
*/
private static final String INVALID_FIRST_WAY_OFFSET = "invalid first way offset: ";
private static final String TAG = MapDatabase.class.getName();
/**
* Maximum way nodes sequence length which is considered as valid.
*/
private static final int MAXIMUM_WAY_NODES_SEQUENCE_LENGTH = 8192;
/**
* Maximum number of map objects in the zoom table which is considered as
* valid.
*/
private static final int MAXIMUM_ZOOM_TABLE_OBJECTS = 65536;
/**
* Bitmask for the optional POI feature "elevation".
*/
private static final int POI_FEATURE_ELEVATION = 0x20;
/**
* Bitmask for the optional POI feature "house number".
*/
private static final int POI_FEATURE_HOUSE_NUMBER = 0x40;
/**
* Bitmask for the optional POI feature "name".
*/
private static final int POI_FEATURE_NAME = 0x80;
/**
* Bitmask for the POI layer.
*/
private static final int POI_LAYER_BITMASK = 0xf0;
/**
* Bit shift for calculating the POI layer.
*/
private static final int POI_LAYER_SHIFT = 4;
/**
* Bitmask for the number of POI tags.
*/
private static final int POI_NUMBER_OF_TAGS_BITMASK = 0x0f;
/**
* Length of the debug signature at the beginning of each block.
*/
private static final byte SIGNATURE_LENGTH_BLOCK = 32;
/**
* Length of the debug signature at the beginning of each POI.
*/
private static final byte SIGNATURE_LENGTH_POI = 32;
/**
* Length of the debug signature at the beginning of each way.
*/
private static final byte SIGNATURE_LENGTH_WAY = 32;
/**
* Bitmask for the optional way data blocks byte.
*/
private static final int WAY_FEATURE_DATA_BLOCKS_BYTE = 0x08;
/**
* Bitmask for the optional way double delta encoding.
*/
private static final int WAY_FEATURE_DOUBLE_DELTA_ENCODING = 0x04;
/**
* Bitmask for the optional way feature "house number".
*/
private static final int WAY_FEATURE_HOUSE_NUMBER = 0x40;
/**
* Bitmask for the optional way feature "label position".
*/
private static final int WAY_FEATURE_LABEL_POSITION = 0x10;
/**
* Bitmask for the optional way feature "name".
*/
private static final int WAY_FEATURE_NAME = 0x80;
/**
* Bitmask for the optional way feature "reference".
*/
private static final int WAY_FEATURE_REF = 0x20;
/**
* Bitmask for the way layer.
*/
private static final int WAY_LAYER_BITMASK = 0xf0;
/**
* Bit shift for calculating the way layer.
*/
private static final int WAY_LAYER_SHIFT = 4;
/**
* Bitmask for the number of way tags.
*/
private static final int WAY_NUMBER_OF_TAGS_BITMASK = 0x0f;
private long mFileSize;
private boolean mDebugFile;
private RandomAccessFile mInputFile;
private ReadBuffer mReadBuffer;
private String mSignatureBlock;
private String mSignaturePoi;
private String mSignatureWay;
private int mTileLatitude;
private int mTileLongitude;
private int[] mIntBuffer;
private final MapElement mElem = new MapElement();
private int minLat, minLon;
private Tile mTile;
private final MapFileTileSource mTileSource;
@Override
public QueryResult executeQuery(MapTile tile, ITileDataSink mapDataSink) {
if (mTileSource.fileHeader == null)
return QueryResult.FAILED;
if (mIntBuffer == null)
mIntBuffer = new int[MAXIMUM_WAY_NODES_SEQUENCE_LENGTH * 2];
try {
mTile = tile;
QueryParameters queryParameters = new QueryParameters();
queryParameters.queryZoomLevel = mTileSource.fileHeader
.getQueryZoomLevel(tile.zoomLevel);
// get and check the sub-file for the query zoom level
SubFileParameter subFileParameter = mTileSource.fileHeader
.getSubFileParameter(queryParameters.queryZoomLevel);
if (subFileParameter == null) {
Log.w(TAG, "no sub-file for zoom level: "
+ queryParameters.queryZoomLevel);
return QueryResult.FAILED;
}
QueryCalculations.calculateBaseTiles(queryParameters, tile, subFileParameter);
QueryCalculations.calculateBlocks(queryParameters, subFileParameter);
processBlocks(mapDataSink, queryParameters, subFileParameter);
} catch (IOException e) {
Log.e(TAG, e.getMessage());
return QueryResult.FAILED;
}
return QueryResult.SUCCESS;
}
public MapDatabase(MapFileTileSource tileSource) throws IOException {
mTileSource = tileSource;
try {
// open the file in read only mode
mInputFile = new RandomAccessFile(tileSource.mapFile, "r");
mFileSize = mInputFile.length();
mReadBuffer = new ReadBuffer(mInputFile);
} catch (IOException e) {
Log.e(TAG, e.getMessage());
// make sure that the file is closed
destroy();
throw new IOException();
}
}
@Override
public void destroy() {
mReadBuffer = null;
if (mInputFile != null) {
try {
mInputFile.close();
mInputFile = null;
} catch (IOException e) {
Log.e(TAG, e.getMessage());
}
}
}
/**
* Logs the debug signatures of the current way and block.
*/
private void logDebugSignatures() {
if (mDebugFile) {
Log.w(TAG, DEBUG_SIGNATURE_WAY + mSignatureWay);
Log.w(TAG, DEBUG_SIGNATURE_BLOCK + mSignatureBlock);
}
}
/**
* Processes a single block and executes the callback functions on all map
* elements.
*
* @param queryParameters
* the parameters of the current query.
* @param subFileParameter
* the parameters of the current map file.
* @param mapDataSink
* the callback which handles the extracted map elements.
*/
private void processBlock(QueryParameters queryParameters,
SubFileParameter subFileParameter,
ITileDataSink mapDataSink) {
if (!processBlockSignature()) {
return;
}
int[][] zoomTable = readZoomTable(subFileParameter);
if (zoomTable == null) {
return;
}
int zoomTableRow = queryParameters.queryZoomLevel - subFileParameter.zoomLevelMin;
int poisOnQueryZoomLevel = zoomTable[zoomTableRow][0];
int waysOnQueryZoomLevel = zoomTable[zoomTableRow][1];
// get the relative offset to the first stored way in the block
int firstWayOffset = mReadBuffer.readUnsignedInt();
if (firstWayOffset < 0) {
Log.w(TAG, INVALID_FIRST_WAY_OFFSET + firstWayOffset);
if (mDebugFile) {
Log.w(TAG, DEBUG_SIGNATURE_BLOCK + mSignatureBlock);
}
return;
}
// add the current buffer position to the relative first way offset
firstWayOffset += mReadBuffer.getBufferPosition();
if (firstWayOffset > mReadBuffer.getBufferSize()) {
Log.w(TAG, INVALID_FIRST_WAY_OFFSET + firstWayOffset);
if (mDebugFile) {
Log.w(TAG, DEBUG_SIGNATURE_BLOCK + mSignatureBlock);
}
return;
}
if (!processPOIs(mapDataSink, poisOnQueryZoomLevel)) {
return;
}
// finished reading POIs, check if the current buffer position is valid
if (mReadBuffer.getBufferPosition() > firstWayOffset) {
Log.w(TAG, "invalid buffer position: " + mReadBuffer.getBufferPosition());
if (mDebugFile) {
Log.w(TAG, DEBUG_SIGNATURE_BLOCK + mSignatureBlock);
}
return;
}
// move the pointer to the first way
mReadBuffer.setBufferPosition(firstWayOffset);
if (!processWays(queryParameters, mapDataSink, waysOnQueryZoomLevel)) {
return;
}
}
private void processBlocks(ITileDataSink mapDataSink,
QueryParameters queryParameters,
SubFileParameter subFileParameter) throws IOException {
boolean queryIsWater = true;
// boolean queryReadWaterInfo = false;
// read and process all blocks from top to bottom and from left to right
for (long row = queryParameters.fromBlockY; row <= queryParameters.toBlockY; ++row) {
for (long column = queryParameters.fromBlockX; column <= queryParameters.toBlockX; ++column) {
// calculate the actual block number of the needed block in the
// file
long blockNumber = row * subFileParameter.blocksWidth + column;
// get the current index entry
long currentBlockIndexEntry = mTileSource.databaseIndexCache.getIndexEntry(
subFileParameter, blockNumber);
// check if the current query would still return a water tile
if (queryIsWater) {
// check the water flag of the current block in its index
// entry
queryIsWater &= (currentBlockIndexEntry & BITMASK_INDEX_WATER) != 0;
// queryReadWaterInfo = true;
}
// get and check the current block pointer
long currentBlockPointer = currentBlockIndexEntry & BITMASK_INDEX_OFFSET;
if (currentBlockPointer < 1
|| currentBlockPointer > subFileParameter.subFileSize) {
Log.w(TAG, "invalid current block pointer: " + currentBlockPointer);
Log.w(TAG, "subFileSize: " + subFileParameter.subFileSize);
return;
}
long nextBlockPointer;
// check if the current block is the last block in the file
if (blockNumber + 1 == subFileParameter.numberOfBlocks) {
// set the next block pointer to the end of the file
nextBlockPointer = subFileParameter.subFileSize;
} else {
// get and check the next block pointer
nextBlockPointer = mTileSource.databaseIndexCache.getIndexEntry(
subFileParameter, blockNumber + 1)
& BITMASK_INDEX_OFFSET;
if (nextBlockPointer < 1
|| nextBlockPointer > subFileParameter.subFileSize) {
Log.w(TAG, "invalid next block pointer: " + nextBlockPointer);
Log.w(TAG, "sub-file size: " + subFileParameter.subFileSize);
return;
}
}
// calculate the size of the current block
int currentBlockSize = (int) (nextBlockPointer - currentBlockPointer);
if (currentBlockSize < 0) {
Log.w(TAG, "current block size must not be negative: "
+ currentBlockSize);
return;
} else if (currentBlockSize == 0) {
// the current block is empty, continue with the next block
continue;
} else if (currentBlockSize > ReadBuffer.MAXIMUM_BUFFER_SIZE) {
// the current block is too large, continue with the next
// block
Log.w(TAG, "current block size too large: " + currentBlockSize);
continue;
} else if (currentBlockPointer + currentBlockSize > mFileSize) {
Log.w(TAG, "current block larger than file size: "
+ currentBlockSize);
return;
}
// seek to the current block in the map file
mInputFile.seek(subFileParameter.startAddress + currentBlockPointer);
// read the current block into the buffer
if (!mReadBuffer.readFromFile(currentBlockSize)) {
// skip the current block
Log.w(TAG, "reading current block has failed: " + currentBlockSize);
return;
}
// calculate the top-left coordinates of the underlying tile
double tileLatitudeDeg = Projection.tileYToLatitude(
subFileParameter.boundaryTileTop + row,
subFileParameter.baseZoomLevel);
double tileLongitudeDeg = Projection.tileXToLongitude(
subFileParameter.boundaryTileLeft
+ column, subFileParameter.baseZoomLevel);
mTileLatitude = (int) (tileLatitudeDeg * 1000000);
mTileLongitude = (int) (tileLongitudeDeg * 1000000);
processBlock(queryParameters, subFileParameter, mapDataSink);
}
}
// the query is finished, was the water flag set for all blocks?
// if (queryIsWater && queryReadWaterInfo) {
// Tag[] tags = new Tag[1];
// tags[0] = TAG_NATURAL_WATER;
//
// System.arraycopy(WATER_TILE_COORDINATES, 0, mWayNodes,
// mWayNodePosition, 8);
// mWayNodePosition += 8;
// mapDatabaseCallback.renderWaterBackground(tags, wayDataContainer);
// }
}
/**
* Processes the block signature, if present.
*
* @return true if the block signature could be processed successfully,
* false otherwise.
*/
private boolean processBlockSignature() {
if (mDebugFile) {
// get and check the block signature
mSignatureBlock = mReadBuffer.readUTF8EncodedString(SIGNATURE_LENGTH_BLOCK);
if (!mSignatureBlock.startsWith("###TileStart")) {
Log.w(TAG, "invalid block signature: " + mSignatureBlock);
return false;
}
}
return true;
}
/**
* Processes the given number of POIs.
*
* @param mapDataSink
* the callback which handles the extracted POIs.
* @param numberOfPois
* how many POIs should be processed.
* @return true if the POIs could be processed successfully, false
* otherwise.
*/
private boolean processPOIs(ITileDataSink mapDataSink, int numberOfPois) {
Tag[] poiTags = mTileSource.fileInfo.poiTags;
Tag[] tags = null;
Tag[] curTags;
long x = mTile.tileX * Tile.SIZE;
long y = mTile.tileY * Tile.SIZE + Tile.SIZE;
long z = Tile.SIZE << mTile.zoomLevel;
long dx = (x - (z >> 1));
long dy = (y - (z >> 1));
double divx = 180000000.0 / (z >> 1);
double divy = z / PIx4;
for (int elementCounter = numberOfPois; elementCounter != 0; --elementCounter) {
if (mDebugFile) {
// get and check the POI signature
mSignaturePoi = mReadBuffer.readUTF8EncodedString(SIGNATURE_LENGTH_POI);
if (!mSignaturePoi.startsWith("***POIStart")) {
Log.w(TAG, "invalid POI signature: " + mSignaturePoi);
Log.w(TAG, DEBUG_SIGNATURE_BLOCK + mSignatureBlock);
return false;
}
}
// get the POI latitude offset (VBE-S)
int latitude = mTileLatitude + mReadBuffer.readSignedInt();
// get the POI longitude offset (VBE-S)
int longitude = mTileLongitude + mReadBuffer.readSignedInt();
// get the special byte which encodes multiple flags
byte specialByte = mReadBuffer.readByte();
// bit 1-4 represent the layer
byte layer = (byte) ((specialByte & POI_LAYER_BITMASK) >>> POI_LAYER_SHIFT);
// bit 5-8 represent the number of tag IDs
byte numberOfTags = (byte) (specialByte & POI_NUMBER_OF_TAGS_BITMASK);
if (numberOfTags != 0) {
tags = mReadBuffer.readTags(poiTags, numberOfTags);
}
if (tags == null)
return false;
curTags = tags;
// get the feature bitmask (1 byte)
byte featureByte = mReadBuffer.readByte();
// bit 1-3 enable optional features
// check if the POI has a name
if ((featureByte & POI_FEATURE_NAME) != 0) {
String str = mReadBuffer.readUTF8EncodedString();
curTags = new Tag[tags.length + 1];
System.arraycopy(tags, 0, curTags, 0, tags.length);
curTags[tags.length] = new Tag(Tag.TAG_KEY_NAME, str, false);
}
// check if the POI has a house number
if ((featureByte & POI_FEATURE_HOUSE_NUMBER) != 0) {
// mReadBuffer.getPositionAndSkip();
// String str =
mReadBuffer.readUTF8EncodedString();
}
// check if the POI has an elevation
if ((featureByte & POI_FEATURE_ELEVATION) != 0) {
mReadBuffer.readSignedInt();
// mReadBuffer.getPositionAndSkip();// tags.add(new
// Tag(Tag.TAG_KEY_ELE,
// Integer.toString(mReadBuffer.readSignedInt())));
}
longitude = (int) (longitude / divx - dx);
double sinLat = Math.sin(latitude * PI180);
latitude = (int) (Math.log((1.0 + sinLat) / (1.0 - sinLat)) * divy + dy);
mElem.clear();
mElem.startPoints();
mElem.addPoint(longitude, latitude);
mElem.type = GeometryType.POINT;
mElem.set(curTags, layer);
mapDataSink.process(mElem);
// mGeom.points[0] = longitude;
// mGeom.points[1] = latitude;
// mGeom.index[0] = 2;
// mapDatabaseCallback.renderPOI(layer, curTags, mGeom);
}
return true;
}
private boolean processWayDataBlock(boolean doubleDeltaEncoding) {
// get and check the number of way coordinate blocks (VBE-U)
int numBlocks = mReadBuffer.readUnsignedInt();
if (numBlocks < 1 || numBlocks > Short.MAX_VALUE) {
Log.w(TAG, "invalid number of way coordinate blocks: " + numBlocks);
return false;
}
//short[] wayLengths = new short[numBlocks];
short[] wayLengths = mElem.ensureIndexSize(numBlocks, false);
if (wayLengths.length > numBlocks)
wayLengths[numBlocks] = -1;
mElem.pointPos = 0;
// read the way coordinate blocks
for (int coordinateBlock = 0; coordinateBlock < numBlocks; ++coordinateBlock) {
// get and check the number of way nodes (VBE-U)
int numWayNodes = mReadBuffer.readUnsignedInt();
if (numWayNodes < 2 || numWayNodes > MAXIMUM_WAY_NODES_SEQUENCE_LENGTH) {
Log.w(TAG, "invalid number of way nodes: " + numWayNodes);
logDebugSignatures();
return false;
}
// each way node consists of latitude and longitude
int len = numWayNodes * 2;
if (doubleDeltaEncoding) {
len = decodeWayNodesDoubleDelta(len);
} else {
len = decodeWayNodesSingleDelta(len);
}
wayLengths[coordinateBlock] = (short) len;
}
return true;
}
private int decodeWayNodesDoubleDelta(int length) {
int[] buffer = mIntBuffer;
mReadBuffer.readSignedInt(buffer, length);
float[] outBuffer = mElem.ensurePointSize(mElem.pointPos + length, true);
int pointPos = mElem.pointPos;
// get the first way node latitude offset (VBE-S)
int wayNodeLatitude = mTileLatitude + buffer[0];
// get the first way node longitude offset (VBE-S)
int wayNodeLongitude = mTileLongitude + buffer[1];
// store the first way node
outBuffer[pointPos++] = wayNodeLongitude;
outBuffer[pointPos++] = wayNodeLatitude;
int singleDeltaLatitude = 0;
int singleDeltaLongitude = 0;
int cnt = 2, nLon, nLat, dLat, dLon;
for (int pos = 2; pos < length; pos += 2) {
singleDeltaLatitude = buffer[pos] + singleDeltaLatitude;
nLat = wayNodeLatitude + singleDeltaLatitude;
dLat = nLat - wayNodeLatitude;
wayNodeLatitude = nLat;
singleDeltaLongitude = buffer[pos + 1] + singleDeltaLongitude;
nLon = wayNodeLongitude + singleDeltaLongitude;
dLon = nLon - wayNodeLongitude;
wayNodeLongitude = nLon;
if (dLon > minLon || dLon < -minLon || dLat > minLat || dLat < -minLat
|| (pos == length - 2)) {
outBuffer[pointPos++] = nLon;
outBuffer[pointPos++] = nLat;
cnt += 2;
}
}
mElem.pointPos = pointPos;
return cnt;
}
private int decodeWayNodesSingleDelta(int length) {
int[] buffer = mIntBuffer;
mReadBuffer.readSignedInt(buffer, length);
float[] outBuffer = mElem.ensurePointSize(mElem.pointPos + length, true);
int pointPos = mElem.pointPos;
// get the first way node latitude single-delta offset (VBE-S)
int wayNodeLatitude = mTileLatitude + buffer[0];
// get the first way node longitude single-delta offset (VBE-S)
int wayNodeLongitude = mTileLongitude + buffer[1];
// store the first way node
outBuffer[pointPos++] = wayNodeLongitude;
outBuffer[pointPos++] = wayNodeLatitude;
int cnt = 2, nLon, nLat, dLat, dLon;
for (int pos = 2; pos < length; pos += 2) {
nLat = wayNodeLatitude + buffer[pos];
dLat = nLat - wayNodeLatitude;
wayNodeLatitude = nLat;
nLon = wayNodeLongitude + buffer[pos + 1];
dLon = nLon - wayNodeLongitude;
wayNodeLongitude = nLon;
if (dLon > minLon || dLon < -minLon || dLat > minLat || dLat < -minLat
|| (pos == length - 2)) {
outBuffer[pointPos++] = nLon;
outBuffer[pointPos++] = nLat;
cnt += 2;
}
}
mElem.pointPos = pointPos;
return cnt;
}
private int stringOffset = -1;
/**
* Processes the given number of ways.
*
* @param queryParameters
* the parameters of the current query.
* @param mapDataSink
* the callback which handles the extracted ways.
* @param numberOfWays
* how many ways should be processed.
* @return true if the ways could be processed successfully, false
* otherwise.
*/
private boolean processWays(QueryParameters queryParameters,
ITileDataSink mapDataSink,
int numberOfWays) {
Tag[] tags = null;
Tag[] curTags;
Tag[] wayTags = mTileSource.fileInfo.wayTags;
int wayDataBlocks;
// skip string block
int stringsSize = 0;
stringOffset = 0;
if (mTileSource.experimental) {
stringsSize = mReadBuffer.readUnsignedInt();
stringOffset = mReadBuffer.getBufferPosition();
mReadBuffer.skipBytes(stringsSize);
}
for (int elementCounter = numberOfWays; elementCounter != 0; --elementCounter) {
if (mDebugFile) {
// get and check the way signature
mSignatureWay = mReadBuffer.readUTF8EncodedString(SIGNATURE_LENGTH_WAY);
if (!mSignatureWay.startsWith("---WayStart")) {
Log.w(TAG, "invalid way signature: " + mSignatureWay);
Log.w(TAG, DEBUG_SIGNATURE_BLOCK + mSignatureBlock);
return false;
}
}
if (queryParameters.useTileBitmask) {
elementCounter = mReadBuffer.skipWays(queryParameters.queryTileBitmask,
elementCounter);
if (elementCounter == 0)
return true;
if (elementCounter < 0)
return false;
if (mTileSource.experimental && mReadBuffer.lastTagPosition > 0) {
int pos = mReadBuffer.getBufferPosition();
mReadBuffer.setBufferPosition(mReadBuffer.lastTagPosition);
byte numberOfTags = (byte) (mReadBuffer.readByte() & WAY_NUMBER_OF_TAGS_BITMASK);
tags = mReadBuffer.readTags(wayTags, numberOfTags);
if (tags == null)
return false;
mReadBuffer.setBufferPosition(pos);
}
} else {
int wayDataSize = mReadBuffer.readUnsignedInt();
if (wayDataSize < 0) {
Log.w(TAG, "invalid way data size: " + wayDataSize);
if (mDebugFile) {
Log.w(TAG, DEBUG_SIGNATURE_BLOCK + mSignatureBlock);
}
Log.e(TAG, "BUG way 2");
return false;
}
// ignore the way tile bitmask (2 bytes)
mReadBuffer.skipBytes(2);
}
// get the special byte which encodes multiple flags
byte specialByte = mReadBuffer.readByte();
// bit 1-4 represent the layer
byte layer = (byte) ((specialByte & WAY_LAYER_BITMASK) >>> WAY_LAYER_SHIFT);
// bit 5-8 represent the number of tag IDs
byte numberOfTags = (byte) (specialByte & WAY_NUMBER_OF_TAGS_BITMASK);
if (numberOfTags != 0)
tags = mReadBuffer.readTags(wayTags, numberOfTags);
if (tags == null)
return false;
curTags = tags;
// get the feature bitmask (1 byte)
byte featureByte = mReadBuffer.readByte();
// bit 1-6 enable optional features
boolean featureWayDoubleDeltaEncoding = (featureByte & WAY_FEATURE_DOUBLE_DELTA_ENCODING) != 0;
boolean hasName = (featureByte & WAY_FEATURE_NAME) != 0;
boolean hasHouseNr = (featureByte & WAY_FEATURE_HOUSE_NUMBER) != 0;
boolean hasRef = (featureByte & WAY_FEATURE_REF) != 0;
int add = (hasName ? 1 : 0) + (hasHouseNr ? 1 : 0) + (hasRef ? 1 : 0);
int addTag = tags.length;
if (add > 0) {
curTags = new Tag[tags.length + add];
System.arraycopy(tags, 0, curTags, 0, tags.length);
}
if (mTileSource.experimental) {
if (hasName) {
int textPos = mReadBuffer.readUnsignedInt();
String str = mReadBuffer.readUTF8EncodedStringAt(stringOffset + textPos);
curTags[addTag++] = new Tag(Tag.TAG_KEY_NAME, str, false);
}
if (hasHouseNr) {
int textPos = mReadBuffer.readUnsignedInt();
String str = mReadBuffer.readUTF8EncodedStringAt(stringOffset + textPos);
curTags[addTag++] = new Tag(Tag.TAG_KEY_HOUSE_NUMBER, str, false);
}
if (hasRef) {
int textPos = mReadBuffer.readUnsignedInt();
String str = mReadBuffer.readUTF8EncodedStringAt(stringOffset + textPos);
curTags[addTag++] = new Tag(Tag.TAG_KEY_REF, str, false);
}
} else {
if (hasName) {
String str = mReadBuffer.readUTF8EncodedString();
curTags[addTag++] = new Tag(Tag.TAG_KEY_NAME, str, false);
}
if (hasHouseNr) {
String str = mReadBuffer.readUTF8EncodedString();
curTags[addTag++] = new Tag(Tag.TAG_KEY_HOUSE_NUMBER, str, false);
}
if (hasRef) {
String str = mReadBuffer.readUTF8EncodedString();
curTags[addTag++] = new Tag(Tag.TAG_KEY_REF, str, false);
}
}
if ((featureByte & WAY_FEATURE_LABEL_POSITION) != 0)
// labelPosition =
readOptionalLabelPosition();
if ((featureByte & WAY_FEATURE_DATA_BLOCKS_BYTE) != 0) {
wayDataBlocks = mReadBuffer.readUnsignedInt();
if (wayDataBlocks < 1) {
Log.w(TAG, "invalid number of way data blocks: " + wayDataBlocks);
logDebugSignatures();
return false;
}
} else {
wayDataBlocks = 1;
}
for (int wayDataBlock = 0; wayDataBlock < wayDataBlocks; ++wayDataBlock) {
if (!processWayDataBlock(featureWayDoubleDeltaEncoding))
return false;
// wayDataContainer.textPos = textPos;
int l = mElem.index[0];
boolean closed = mElem.points[0] == mElem.points[l - 2]
&& mElem.points[1] == mElem.points[l - 1];
projectToTile(mElem.points, mElem.index);
mElem.layer = layer;
mElem.type = closed ? GeometryType.POLY : GeometryType.LINE;
mElem.set(curTags, layer);
mapDataSink.process(mElem);
}
}
return true;
}
private float[] readOptionalLabelPosition() {
float[] labelPosition = new float[2];
// get the label position latitude offset (VBE-S)
labelPosition[1] = mTileLatitude + mReadBuffer.readSignedInt();
// get the label position longitude offset (VBE-S)
labelPosition[0] = mTileLongitude + mReadBuffer.readSignedInt();
return labelPosition;
}
// private int readOptionalWayDataBlocksByte(boolean
// featureWayDataBlocksByte) {
// if (featureWayDataBlocksByte) {
// // get and check the number of way data blocks (VBE-U)
// return mReadBuffer.readUnsignedInt();
// }
// // only one way data block exists
// return 1;
// }
private int[][] readZoomTable(SubFileParameter subFileParameter) {
int rows = subFileParameter.zoomLevelMax - subFileParameter.zoomLevelMin + 1;
int[][] zoomTable = new int[rows][2];
int cumulatedNumberOfPois = 0;
int cumulatedNumberOfWays = 0;
for (int row = 0; row < rows; ++row) {
cumulatedNumberOfPois += mReadBuffer.readUnsignedInt();
cumulatedNumberOfWays += mReadBuffer.readUnsignedInt();
if (cumulatedNumberOfPois < 0
|| cumulatedNumberOfPois > MAXIMUM_ZOOM_TABLE_OBJECTS) {
Log.w(TAG, "invalid cumulated number of POIs in row " + row + ' '
+ cumulatedNumberOfPois);
if (mDebugFile) {
Log.w(TAG, DEBUG_SIGNATURE_BLOCK + mSignatureBlock);
}
return null;
} else if (cumulatedNumberOfWays < 0
|| cumulatedNumberOfWays > MAXIMUM_ZOOM_TABLE_OBJECTS) {
Log.w(TAG, "invalid cumulated number of ways in row " + row + ' '
+ cumulatedNumberOfWays);
if (mTileSource.fileInfo.debugFile) {
Log.w(TAG, DEBUG_SIGNATURE_BLOCK + mSignatureBlock);
}
return null;
}
zoomTable[row][0] = cumulatedNumberOfPois;
zoomTable[row][1] = cumulatedNumberOfWays;
}
return zoomTable;
}
private static final double PI180 = (Math.PI / 180) / 1000000.0;
private static final double PIx4 = Math.PI * 4;
private boolean projectToTile(float[] coords, short[] indices) {
long x = mTile.tileX * Tile.SIZE;
long y = mTile.tileY * Tile.SIZE + Tile.SIZE;
long z = Tile.SIZE << mTile.zoomLevel;
double divx, divy = 0;
long dx = (x - (z >> 1));
long dy = (y - (z >> 1));
divx = 180000000.0 / (z >> 1);
divy = z / PIx4;
for (int pos = 0, outPos = 0, i = 0, m = indices.length; i < m; i++) {
int len = indices[i];
if (len == 0)
continue;
if (len < 0)
break;
int cnt = 0;
float lat, lon, prevLon = 0, prevLat = 0;
int first = outPos;
for (int end = pos + len; pos < end; pos += 2) {
lon = (float) ((coords[pos]) / divx - dx);
double sinLat = Math.sin(coords[pos + 1] * PI180);
lat = (float) (Tile.SIZE - (Math.log((1.0 + sinLat) / (1.0 - sinLat)) * divy + dy));
if (cnt != 0) {
// drop small distance intermediate nodes
if (lat == prevLat && lon == prevLon)
continue;
}
coords[outPos++] = prevLon = lon;
coords[outPos++] = prevLat = lat;
cnt += 2;
}
if (coords[first] == coords[outPos - 2] && coords[first + 1] == coords[outPos - 1]) {
//Log.d(TAG, "drop closed");
indices[i] = (short) (cnt - 2);
outPos -= 2;
}
else
indices[i] = (short) cnt;
}
return true;
}
}

View File

@@ -0,0 +1,137 @@
/*
* Copyright 2013 Hannes Janetzek
* Copyright 2013 mapsforge.org
*
* 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.oscim.tilesource.mapfile;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import org.oscim.tilesource.ITileDataSource;
import org.oscim.tilesource.MapInfo;
import org.oscim.tilesource.TileSource;
import org.oscim.tilesource.mapfile.header.MapFileHeader;
import org.oscim.tilesource.mapfile.header.MapFileInfo;
import android.util.Log;
public class MapFileTileSource extends TileSource {
private final static String TAG = MapFileTileSource.class.getName();
/**
* Amount of cache blocks that the index cache should store.
*/
private static final int INDEX_CACHE_SIZE = 64;
private static final String READ_ONLY_MODE = "r";
MapFileHeader fileHeader;
MapFileInfo fileInfo;
IndexCache databaseIndexCache;
boolean experimental;
File mapFile;
public boolean setMapFile(String filename) {
setOption("file", filename);
File file = new File(filename);
if (!file.exists()) {
return false;
} else if (!file.isFile()) {
return false;
} else if (!file.canRead()) {
return false;
}
return true;
}
@Override
public OpenResult open() {
if (!options.containsKey("file"))
return new OpenResult("no map file set");
try {
// make sure to close any previously opened file first
//close();
File file = new File(options.get("file"));
// check if the file exists and is readable
if (!file.exists()) {
return new OpenResult("file does not exist: " + file);
} else if (!file.isFile()) {
return new OpenResult("not a file: " + file);
} else if (!file.canRead()) {
return new OpenResult("cannot read file: " + file);
}
// open the file in read only mode
RandomAccessFile mInputFile = new RandomAccessFile(file, READ_ONLY_MODE);
long mFileSize = mInputFile.length();
ReadBuffer mReadBuffer = new ReadBuffer(mInputFile);
fileHeader = new MapFileHeader();
OpenResult openResult = fileHeader.readHeader(mReadBuffer, mFileSize);
if (!openResult.isSuccess()) {
close();
return openResult;
}
fileInfo = fileHeader.getMapFileInfo();
mapFile = file;
databaseIndexCache = new IndexCache(mInputFile, INDEX_CACHE_SIZE);
experimental = fileInfo.fileVersion == 4;
Log.d(TAG, "File version: " + fileInfo.fileVersion);
return OpenResult.SUCCESS;
} catch (IOException e) {
Log.e(TAG, e.getMessage());
// make sure that the file is closed
close();
return new OpenResult(e.getMessage());
}
}
@Override
public ITileDataSource getDataSource() {
try {
return new MapDatabase(this);
} catch (IOException e) {
Log.d(TAG, e.getMessage());
}
return null;
}
@Override
public void close() {
fileHeader = null;
fileInfo = null;
mapFile = null;
if (databaseIndexCache != null) {
databaseIndexCache.destroy();
databaseIndexCache = null;
}
}
@Override
public MapInfo getMapInfo() {
return fileInfo;
}
}

View File

@@ -0,0 +1,166 @@
/*
* Copyright 2010, 2011, 2012 mapsforge.org
*
* 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.oscim.tilesource.mapfile;
import org.oscim.core.Tile;
public class Projection {
/**
* Converts a tile X number at a certain zoom level to a longitude
* coordinate.
*
* @param tileX
* the tile X number that should be converted.
* @param zoomLevel
* the zoom level at which the number should be converted.
* @return the longitude value of the tile X number.
*/
public static double tileXToLongitude(long tileX, int zoomLevel) {
return pixelXToLongitude(tileX * Tile.SIZE, zoomLevel);
}
/**
* Converts a tile Y number at a certain zoom level to a latitude
* coordinate.
*
* @param tileY
* the tile Y number that should be converted.
* @param zoomLevel
* the zoom level at which the number should be converted.
* @return the latitude value of the tile Y number.
*/
public static double tileYToLatitude(long tileY, int zoomLevel) {
return pixelYToLatitude(tileY * Tile.SIZE, zoomLevel);
}
/**
* Converts a latitude coordinate (in degrees) to a tile Y number at a
* certain zoom level.
*
* @param latitude
* the latitude coordinate that should be converted.
* @param zoomLevel
* the zoom level at which the coordinate should be converted.
* @return the tile Y number of the latitude value.
*/
public static long latitudeToTileY(double latitude, int zoomLevel) {
return pixelYToTileY(latitudeToPixelY(latitude, zoomLevel), zoomLevel);
}
/**
* Converts a longitude coordinate (in degrees) to the tile X number at a
* certain zoom level.
*
* @param longitude
* the longitude coordinate that should be converted.
* @param zoomLevel
* the zoom level at which the coordinate should be converted.
* @return the tile X number of the longitude value.
*/
public static long longitudeToTileX(double longitude, int zoomLevel) {
return pixelXToTileX(longitudeToPixelX(longitude, zoomLevel), zoomLevel);
}
/**
* Converts a pixel X coordinate to the tile X number.
*
* @param pixelX
* the pixel X coordinate that should be converted.
* @param zoomLevel
* the zoom level at which the coordinate should be converted.
* @return the tile X number.
*/
public static int pixelXToTileX(double pixelX, int zoomLevel) {
return (int) Math.min(Math.max(pixelX / Tile.SIZE, 0),
Math.pow(2, zoomLevel) - 1);
}
/**
* Converts a pixel Y coordinate to the tile Y number.
*
* @param pixelY
* the pixel Y coordinate that should be converted.
* @param zoomLevel
* the zoom level at which the coordinate should be converted.
* @return the tile Y number.
*/
public static int pixelYToTileY(double pixelY, int zoomLevel) {
return (int) Math.min(Math.max(pixelY / Tile.SIZE, 0),
Math.pow(2, zoomLevel) - 1);
}
/**
* Converts a pixel X coordinate at a certain zoom level to a longitude
* coordinate.
*
* @param pixelX
* the pixel X coordinate that should be converted.
* @param zoomLevel
* the zoom level at which the coordinate should be converted.
* @return the longitude value of the pixel X coordinate.
*/
public static double pixelXToLongitude(double pixelX, int zoomLevel) {
return 360 * ((pixelX / ((long) Tile.SIZE << zoomLevel)) - 0.5);
}
/**
* Converts a longitude coordinate (in degrees) to a pixel X coordinate at a
* certain zoom level.
*
* @param longitude
* the longitude coordinate that should be converted.
* @param zoomLevel
* the zoom level at which the coordinate should be converted.
* @return the pixel X coordinate of the longitude value.
*/
public static double longitudeToPixelX(double longitude, int zoomLevel) {
return (longitude + 180) / 360 * ((long) Tile.SIZE << zoomLevel);
}
/**
* Converts a pixel Y coordinate at a certain zoom level to a latitude
* coordinate.
*
* @param pixelY
* the pixel Y coordinate that should be converted.
* @param zoomLevel
* the zoom level at which the coordinate should be converted.
* @return the latitude value of the pixel Y coordinate.
*/
public static double pixelYToLatitude(double pixelY, int zoomLevel) {
double y = 0.5 - (pixelY / ((long) Tile.SIZE << zoomLevel));
return 90 - 360 * Math.atan(Math.exp(-y * (2 * Math.PI))) / Math.PI;
}
/**
* Converts a latitude coordinate (in degrees) to a pixel Y coordinate at a
* certain zoom level.
*
* @param latitude
* the latitude coordinate that should be converted.
* @param zoomLevel
* the zoom level at which the coordinate should be converted.
* @return the pixel Y coordinate of the latitude value.
*/
public static double latitudeToPixelY(double latitude, int zoomLevel) {
double sinLatitude = Math.sin(latitude * (Math.PI / 180));
return (0.5 - Math.log((1 + sinLatitude) / (1 - sinLatitude)) / (4 * Math.PI))
* ((long) Tile.SIZE << zoomLevel);
}
private Projection(){
}
}

View File

@@ -0,0 +1,175 @@
/*
* Copyright 2010, 2011, 2012 mapsforge.org
*
* 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.oscim.tilesource.mapfile;
import org.oscim.core.Tile;
import org.oscim.tilesource.mapfile.header.SubFileParameter;
final class QueryCalculations {
private static int getFirstLevelTileBitmask(Tile tile) {
if (tile.tileX % 2 == 0 && tile.tileY % 2 == 0) {
// upper left quadrant
return 0xcc00;
} else if ((tile.tileX & 1) == 1 && tile.tileY % 2 == 0) {
// upper right quadrant
return 0x3300;
} else if (tile.tileX % 2 == 0 && (tile.tileY & 1) == 1) {
// lower left quadrant
return 0xcc;
} else {
// lower right quadrant
return 0x33;
}
}
private static int getSecondLevelTileBitmaskLowerLeft(long subtileX, long subtileY) {
if (subtileX % 2 == 0 && subtileY % 2 == 0) {
// upper left sub-tile
return 0x80;
} else if (subtileX % 2 == 1 && subtileY % 2 == 0) {
// upper right sub-tile
return 0x40;
} else if (subtileX % 2 == 0 && subtileY % 2 == 1) {
// lower left sub-tile
return 0x8;
} else {
// lower right sub-tile
return 0x4;
}
}
private static int getSecondLevelTileBitmaskLowerRight(long subtileX, long subtileY) {
if (subtileX % 2 == 0 && subtileY % 2 == 0) {
// upper left sub-tile
return 0x20;
} else if (subtileX % 2 == 1 && subtileY % 2 == 0) {
// upper right sub-tile
return 0x10;
} else if (subtileX % 2 == 0 && subtileY % 2 == 1) {
// lower left sub-tile
return 0x2;
} else {
// lower right sub-tile
return 0x1;
}
}
private static int getSecondLevelTileBitmaskUpperLeft(long subtileX, long subtileY) {
if (subtileX % 2 == 0 && subtileY % 2 == 0) {
// upper left sub-tile
return 0x8000;
} else if (subtileX % 2 == 1 && subtileY % 2 == 0) {
// upper right sub-tile
return 0x4000;
} else if (subtileX % 2 == 0 && subtileY % 2 == 1) {
// lower left sub-tile
return 0x800;
} else {
// lower right sub-tile
return 0x400;
}
}
private static int getSecondLevelTileBitmaskUpperRight(long subtileX, long subtileY) {
if (subtileX % 2 == 0 && subtileY % 2 == 0) {
// upper left sub-tile
return 0x2000;
} else if (subtileX % 2 == 1 && subtileY % 2 == 0) {
// upper right sub-tile
return 0x1000;
} else if (subtileX % 2 == 0 && subtileY % 2 == 1) {
// lower left sub-tile
return 0x200;
} else {
// lower right sub-tile
return 0x100;
}
}
static void calculateBaseTiles(QueryParameters queryParameters, Tile tile,
SubFileParameter subFileParameter) {
if (tile.zoomLevel < subFileParameter.baseZoomLevel) {
// calculate the XY numbers of the upper left and lower right
// sub-tiles
int zoomLevelDifference = subFileParameter.baseZoomLevel - tile.zoomLevel;
queryParameters.fromBaseTileX = tile.tileX << zoomLevelDifference;
queryParameters.fromBaseTileY = tile.tileY << zoomLevelDifference;
queryParameters.toBaseTileX = queryParameters.fromBaseTileX
+ (1 << zoomLevelDifference) - 1;
queryParameters.toBaseTileY = queryParameters.fromBaseTileY
+ (1 << zoomLevelDifference) - 1;
queryParameters.useTileBitmask = false;
} else if (tile.zoomLevel > subFileParameter.baseZoomLevel) {
// calculate the XY numbers of the parent base tile
int zoomLevelDifference = tile.zoomLevel - subFileParameter.baseZoomLevel;
queryParameters.fromBaseTileX = tile.tileX >>> zoomLevelDifference;
queryParameters.fromBaseTileY = tile.tileY >>> zoomLevelDifference;
queryParameters.toBaseTileX = queryParameters.fromBaseTileX;
queryParameters.toBaseTileY = queryParameters.fromBaseTileY;
queryParameters.useTileBitmask = true;
queryParameters.queryTileBitmask = calculateTileBitmask(tile, zoomLevelDifference);
} else {
// use the tile XY numbers of the requested tile
queryParameters.fromBaseTileX = tile.tileX;
queryParameters.fromBaseTileY = tile.tileY;
queryParameters.toBaseTileX = queryParameters.fromBaseTileX;
queryParameters.toBaseTileY = queryParameters.fromBaseTileY;
queryParameters.useTileBitmask = false;
}
}
static void calculateBlocks(QueryParameters queryParameters, SubFileParameter subFileParameter) {
// calculate the blocks in the file which need to be read
queryParameters.fromBlockX = Math.max(queryParameters.fromBaseTileX
- subFileParameter.boundaryTileLeft, 0);
queryParameters.fromBlockY = Math.max(queryParameters.fromBaseTileY
- subFileParameter.boundaryTileTop, 0);
queryParameters.toBlockX = Math.min(queryParameters.toBaseTileX
- subFileParameter.boundaryTileLeft,
subFileParameter.blocksWidth - 1);
queryParameters.toBlockY = Math.min(queryParameters.toBaseTileY
- subFileParameter.boundaryTileTop,
subFileParameter.blocksHeight - 1);
}
static int calculateTileBitmask(Tile tile, int zoomLevelDifference) {
if (zoomLevelDifference == 1) {
return getFirstLevelTileBitmask(tile);
}
// calculate the XY numbers of the second level sub-tile
long subtileX = tile.tileX >>> (zoomLevelDifference - 2);
long subtileY = tile.tileY >>> (zoomLevelDifference - 2);
// calculate the XY numbers of the parent tile
long parentTileX = subtileX >>> 1;
long parentTileY = subtileY >>> 1;
// determine the correct bitmask for all 16 sub-tiles
if (parentTileX % 2 == 0 && parentTileY % 2 == 0) {
return getSecondLevelTileBitmaskUpperLeft(subtileX, subtileY);
} else if (parentTileX % 2 == 1 && parentTileY % 2 == 0) {
return getSecondLevelTileBitmaskUpperRight(subtileX, subtileY);
} else if (parentTileX % 2 == 0 && parentTileY % 2 == 1) {
return getSecondLevelTileBitmaskLowerLeft(subtileX, subtileY);
} else {
return getSecondLevelTileBitmaskLowerRight(subtileX, subtileY);
}
}
private QueryCalculations() {
throw new IllegalStateException();
}
}

View File

@@ -0,0 +1,58 @@
/*
* Copyright 2010, 2011, 2012 mapsforge.org
*
* 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.oscim.tilesource.mapfile;
class QueryParameters {
long fromBaseTileX;
long fromBaseTileY;
long fromBlockX;
long fromBlockY;
int queryTileBitmask;
int queryZoomLevel;
long toBaseTileX;
long toBaseTileY;
long toBlockX;
long toBlockY;
boolean useTileBitmask;
@Override
public String toString() {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("QueryParameters [fromBaseTileX=");
stringBuilder.append(this.fromBaseTileX);
stringBuilder.append(", fromBaseTileY=");
stringBuilder.append(this.fromBaseTileY);
stringBuilder.append(", fromBlockX=");
stringBuilder.append(this.fromBlockX);
stringBuilder.append(", fromBlockY=");
stringBuilder.append(this.fromBlockY);
stringBuilder.append(", queryTileBitmask=");
stringBuilder.append(this.queryTileBitmask);
stringBuilder.append(", queryZoomLevel=");
stringBuilder.append(this.queryZoomLevel);
stringBuilder.append(", toBaseTileX=");
stringBuilder.append(this.toBaseTileX);
stringBuilder.append(", toBaseTileY=");
stringBuilder.append(this.toBaseTileY);
stringBuilder.append(", toBlockX=");
stringBuilder.append(this.toBlockX);
stringBuilder.append(", toBlockY=");
stringBuilder.append(this.toBlockY);
stringBuilder.append(", useTileBitmask=");
stringBuilder.append(this.useTileBitmask);
stringBuilder.append("]");
return stringBuilder.toString();
}
}

View File

@@ -0,0 +1,481 @@
/*
* Copyright 2010, 2011, 2012 mapsforge.org
*
* 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.oscim.tilesource.mapfile;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.io.UnsupportedEncodingException;
import java.util.logging.Logger;
import org.oscim.core.Tag;
/**
* Reads from a {@link RandomAccessFile} into a buffer and decodes the data.
*/
public class ReadBuffer {
private static final String CHARSET_UTF8 = "UTF-8";
private static final Logger LOG = Logger.getLogger(ReadBuffer.class.getName());
/**
* Maximum buffer size which is supported by this implementation.
*/
static final int MAXIMUM_BUFFER_SIZE = 8000000;
private byte[] mBufferData;
private int mBufferPosition;
private final RandomAccessFile mInputFile;
ReadBuffer(RandomAccessFile inputFile) {
mInputFile = inputFile;
}
/**
* Returns one signed byte from the read buffer.
*
* @return the byte value.
*/
public byte readByte() {
return mBufferData[mBufferPosition++];
}
/**
* Reads the given amount of bytes from the file into the read buffer and
* resets the internal buffer position. If
* the capacity of the read buffer is too small, a larger one is created
* automatically.
*
* @param length
* the amount of bytes to read from the file.
* @return true if the whole data was read successfully, false otherwise.
* @throws IOException
* if an error occurs while reading the file.
*/
public boolean readFromFile(int length) throws IOException {
// ensure that the read buffer is large enough
if (mBufferData == null || mBufferData.length < length) {
// ensure that the read buffer is not too large
if (length > MAXIMUM_BUFFER_SIZE) {
LOG.warning("invalid read length: " + length);
return false;
}
mBufferData = new byte[length];
}
mBufferPosition = 0;
// reset the buffer position and read the data into the buffer
// bufferPosition = 0;
return mInputFile.read(mBufferData, 0, length) == length;
}
/**
* Converts four bytes from the read buffer to a signed int.
* <p>
* The byte order is big-endian.
*
* @return the int value.
*/
public int readInt() {
int pos = mBufferPosition;
byte[] data = mBufferData;
mBufferPosition += 4;
return data[pos] << 24
| (data[pos + 1] & 0xff) << 16
| (data[pos + 2] & 0xff) << 8
| (data[pos + 3] & 0xff);
}
/**
* Converts eight bytes from the read buffer to a signed long.
* <p>
* The byte order is big-endian.
*
* @return the long value.
*/
public long readLong() {
int pos = mBufferPosition;
byte[] data = mBufferData;
mBufferPosition += 8;
return (data[pos] & 0xffL) << 56
| (data[pos + 1] & 0xffL) << 48
| (data[pos + 2] & 0xffL) << 40
| (data[pos + 3] & 0xffL) << 32
| (data[pos + 4] & 0xffL) << 24
| (data[pos + 5] & 0xffL) << 16
| (data[pos + 6] & 0xffL) << 8
| (data[pos + 7] & 0xffL);
}
/**
* Converts two bytes from the read buffer to a signed int.
* <p>
* The byte order is big-endian.
*
* @return the int value.
*/
public int readShort() {
mBufferPosition += 2;
return mBufferData[mBufferPosition - 2] << 8 | (mBufferData[mBufferPosition - 1] & 0xff);
}
/**
* Converts a variable amount of bytes from the read buffer to a signed int.
* <p>
* The first bit is for continuation info, the other six (last byte) or
* seven (all other bytes) bits are for data. The second bit in the last
* byte indicates the sign of the number.
*
* @return the value.
*/
public int readSignedInt() {
int pos = mBufferPosition;
byte[] data = mBufferData;
int flag;
if ((data[pos] & 0x80) == 0) {
mBufferPosition += 1;
flag = ((data[pos] & 0x40) >> 6);
return ((data[pos] & 0x3f) ^ -flag) + flag;
}
if ((data[pos + 1] & 0x80) == 0) {
mBufferPosition += 2;
flag = ((data[pos + 1] & 0x40) >> 6);
return (((data[pos] & 0x7f)
| (data[pos + 1] & 0x3f) << 7) ^ -flag) + flag;
}
if ((data[pos + 2] & 0x80) == 0) {
mBufferPosition += 3;
flag = ((data[pos + 2] & 0x40) >> 6);
return (((data[pos] & 0x7f)
| (data[pos + 1] & 0x7f) << 7
| (data[pos + 2] & 0x3f) << 14) ^ -flag) + flag;
}
if ((data[pos + 3] & 0x80) == 0) {
mBufferPosition += 4;
flag = ((data[pos + 3] & 0x40) >> 6);
return (((data[pos] & 0x7f)
| ((data[pos + 1] & 0x7f) << 7)
| ((data[pos + 2] & 0x7f) << 14)
| ((data[pos + 3] & 0x3f) << 21)) ^ -flag) + flag;
}
mBufferPosition += 5;
flag = ((data[pos + 4] & 0x40) >> 6);
return ((((data[pos] & 0x7f)
| (data[pos + 1] & 0x7f) << 7
| (data[pos + 2] & 0x7f) << 14
| (data[pos + 3] & 0x7f) << 21
| (data[pos + 4] & 0x3f) << 28)) ^ -flag) + flag;
}
/**
* Converts a variable amount of bytes from the read buffer to a signed int
* array.
* <p>
* The first bit is for continuation info, the other six (last byte) or
* seven (all other bytes) bits are for data. The second bit in the last
* byte indicates the sign of the number.
*
* @param values
* result values
* @param length
* number of values to read
*/
public void readSignedInt(int[] values, int length) {
int pos = mBufferPosition;
byte[] data = mBufferData;
int flag;
for (int i = 0; i < length; i++) {
if ((data[pos] & 0x80) == 0) {
flag = ((data[pos] & 0x40) >> 6);
values[i] = ((data[pos] & 0x3f) ^ -flag) + flag;
pos += 1;
} else if ((data[pos + 1] & 0x80) == 0) {
flag = ((data[pos + 1] & 0x40) >> 6);
values[i] = (((data[pos] & 0x7f)
| ((data[pos + 1] & 0x3f) << 7)) ^ -flag) + flag;
pos += 2;
} else if ((data[pos + 2] & 0x80) == 0) {
flag = ((data[pos + 2] & 0x40) >> 6);
values[i] = (((data[pos] & 0x7f)
| ((data[pos + 1] & 0x7f) << 7)
| ((data[pos + 2] & 0x3f) << 14)) ^ -flag) + flag;
pos += 3;
} else if ((data[pos + 3] & 0x80) == 0) {
flag = ((data[pos + 3] & 0x40) >> 6);
values[i] = (((data[pos] & 0x7f)
| ((data[pos + 1] & 0x7f) << 7)
| ((data[pos + 2] & 0x7f) << 14)
| ((data[pos + 3] & 0x3f) << 21)) ^ -flag) + flag;
pos += 4;
} else {
flag = ((data[pos + 4] & 0x40) >> 6);
values[i] = ((((data[pos] & 0x7f)
| ((data[pos + 1] & 0x7f) << 7)
| ((data[pos + 2] & 0x7f) << 14)
| ((data[pos + 3] & 0x7f) << 21)
| ((data[pos + 4] & 0x3f) << 28))) ^ -flag) + flag;
pos += 5;
}
}
mBufferPosition = pos;
}
/**
* Converts a variable amount of bytes from the read buffer to an unsigned
* int.
* <p>
* The first bit is for continuation info, the other seven bits are for
* data.
*
* @return the int value.
*/
public int readUnsignedInt() {
int pos = mBufferPosition;
byte[] data = mBufferData;
if ((data[pos] & 0x80) == 0) {
mBufferPosition += 1;
return (data[pos] & 0x7f);
}
if ((data[pos + 1] & 0x80) == 0) {
mBufferPosition += 2;
return (data[pos] & 0x7f)
| (data[pos + 1] & 0x7f) << 7;
}
if ((data[pos + 2] & 0x80) == 0) {
mBufferPosition += 3;
return (data[pos] & 0x7f)
| ((data[pos + 1] & 0x7f) << 7)
| ((data[pos + 2] & 0x7f) << 14);
}
if ((data[pos + 3] & 0x80) == 0) {
mBufferPosition += 4;
return (data[pos] & 0x7f)
| ((data[pos + 1] & 0x7f) << 7)
| ((data[pos + 2] & 0x7f) << 14)
| ((data[pos + 3] & 0x7f) << 21);
}
mBufferPosition += 5;
return (data[pos] & 0x7f)
| ((data[pos + 1] & 0x7f) << 7)
| ((data[pos + 2] & 0x7f) << 14)
| ((data[pos + 3] & 0x7f) << 21)
| ((data[pos + 4] & 0x7f) << 28);
}
/**
* Decodes a variable amount of bytes from the read buffer to a string.
*
* @return the UTF-8 decoded string (may be null).
*/
public String readUTF8EncodedString() {
return readUTF8EncodedString(readUnsignedInt());
}
/**
* @return ...
*/
public int getPositionAndSkip() {
int pos = mBufferPosition;
int length = readUnsignedInt();
skipBytes(length);
return pos;
}
/**
* Decodes the given amount of bytes from the read buffer to a string.
*
* @param stringLength
* the length of the string in bytes.
* @return the UTF-8 decoded string (may be null).
*/
public String readUTF8EncodedString(int stringLength) {
if (stringLength > 0 && mBufferPosition + stringLength <= mBufferData.length) {
mBufferPosition += stringLength;
try {
return new String(mBufferData, mBufferPosition - stringLength, stringLength,
CHARSET_UTF8);
} catch (UnsupportedEncodingException e) {
throw new IllegalStateException(e);
}
}
LOG.warning("invalid string length: " + stringLength);
return null;
}
/**
* Decodes a variable amount of bytes from the read buffer to a string.
*
* @param position
* buffer offset position of string
* @return the UTF-8 decoded string (may be null).
*/
public String readUTF8EncodedStringAt(int position) {
int curPosition = mBufferPosition;
mBufferPosition = position;
String result = readUTF8EncodedString(readUnsignedInt());
mBufferPosition = curPosition;
return result;
}
/**
* @return the current buffer position.
*/
int getBufferPosition() {
return mBufferPosition;
}
/**
* @return the current size of the read buffer.
*/
int getBufferSize() {
return mBufferData.length;
}
/**
* Sets the buffer position to the given offset.
*
* @param bufferPosition
* the buffer position.
*/
void setBufferPosition(int bufferPosition) {
mBufferPosition = bufferPosition;
}
/**
* Skips the given number of bytes in the read buffer.
*
* @param bytes
* the number of bytes to skip.
*/
void skipBytes(int bytes) {
mBufferPosition += bytes;
}
Tag[] readTags(Tag[] wayTags, byte numberOfTags) {
Tag[] tags = new Tag[numberOfTags];
int maxTag = wayTags.length;
for (byte i = 0; i < numberOfTags; i++) {
int tagId = readUnsignedInt();
if (tagId < 0 || tagId >= maxTag) {
LOG.warning("invalid tag ID: " + tagId);
return null;
}
tags[i] = wayTags[tagId];
}
return tags;
}
private static final int WAY_NUMBER_OF_TAGS_BITMASK = 0x0f;
int lastTagPosition;
int skipWays(int queryTileBitmask, int elements) {
int pos = mBufferPosition;
byte[] data = mBufferData;
int cnt = elements;
int skip;
lastTagPosition = -1;
while (cnt > 0) {
// read way size (unsigned int)
if ((data[pos] & 0x80) == 0) {
skip = (data[pos] & 0x7f);
pos += 1;
} else if ((data[pos + 1] & 0x80) == 0) {
skip = (data[pos] & 0x7f)
| (data[pos + 1] & 0x7f) << 7;
pos += 2;
} else if ((data[pos + 2] & 0x80) == 0) {
skip = (data[pos] & 0x7f)
| ((data[pos + 1] & 0x7f) << 7)
| ((data[pos + 2] & 0x7f) << 14);
pos += 3;
} else if ((data[pos + 3] & 0x80) == 0) {
skip = (data[pos] & 0x7f)
| ((data[pos + 1] & 0x7f) << 7)
| ((data[pos + 2] & 0x7f) << 14)
| ((data[pos + 3] & 0x7f) << 21);
pos += 4;
} else {
skip = (data[pos] & 0x7f)
| ((data[pos + 1] & 0x7f) << 7)
| ((data[pos + 2] & 0x7f) << 14)
| ((data[pos + 3] & 0x7f) << 21)
| ((data[pos + 4] & 0x7f) << 28);
pos += 5;
}
// invalid way size
if (skip < 0) {
mBufferPosition = pos;
return -1;
}
// check if way matches queryTileBitmask
if ((((data[pos] << 8) | (data[pos + 1] & 0xff)) & queryTileBitmask) == 0) {
// remember last tags position
if ((data[pos + 2] & WAY_NUMBER_OF_TAGS_BITMASK) != 0)
lastTagPosition = pos + 2;
pos += skip;
cnt--;
} else {
pos += 2;
break;
}
}
mBufferPosition = pos;
return cnt;
}
}

View File

@@ -0,0 +1,260 @@
/*
* Copyright 2010, 2011, 2012 mapsforge.org
*
* 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.oscim.tilesource.mapfile.header;
import java.io.IOException;
import org.oscim.tilesource.TileSource.OpenResult;
import org.oscim.tilesource.mapfile.ReadBuffer;
/**
* Reads and validates the header data from a binary map file.
*/
public class MapFileHeader {
/**
* Maximum valid base zoom level of a sub-file.
*/
private static final int BASE_ZOOM_LEVEL_MAX = 20;
/**
* Minimum size of the file header in bytes.
*/
private static final int HEADER_SIZE_MIN = 70;
/**
* Length of the debug signature at the beginning of the index.
*/
private static final byte SIGNATURE_LENGTH_INDEX = 16;
/**
* A single whitespace character.
*/
private static final char SPACE = ' ';
private MapFileInfo mapFileInfo;
private SubFileParameter[] subFileParameters;
private byte zoomLevelMaximum;
private byte zoomLevelMinimum;
/**
* @return a MapFileInfo containing the header data.
*/
public MapFileInfo getMapFileInfo() {
return this.mapFileInfo;
}
/**
* @param zoomLevel
* the originally requested zoom level.
* @return the closest possible zoom level which is covered by a sub-file.
*/
public byte getQueryZoomLevel(byte zoomLevel) {
if (zoomLevel > this.zoomLevelMaximum) {
return this.zoomLevelMaximum;
} else if (zoomLevel < this.zoomLevelMinimum) {
return this.zoomLevelMinimum;
}
return zoomLevel;
}
/**
* @param queryZoomLevel
* the zoom level for which the sub-file parameters are needed.
* @return the sub-file parameters for the given zoom level.
*/
public SubFileParameter getSubFileParameter(int queryZoomLevel) {
return this.subFileParameters[queryZoomLevel];
}
/**
* Reads and validates the header block from the map file.
*
* @param readBuffer
* the ReadBuffer for the file data.
* @param fileSize
* the size of the map file in bytes.
* @return a FileOpenResult containing an error message in case of a
* failure.
* @throws IOException
* if an error occurs while reading the file.
*/
public OpenResult readHeader(ReadBuffer readBuffer, long fileSize) throws IOException {
OpenResult openResult = RequiredFields.readMagicByte(readBuffer);
if (!openResult.isSuccess()) {
return openResult;
}
openResult = RequiredFields.readRemainingHeader(readBuffer);
if (!openResult.isSuccess()) {
return openResult;
}
MapFileInfoBuilder mapFileInfoBuilder = new MapFileInfoBuilder();
openResult = RequiredFields.readFileVersion(readBuffer, mapFileInfoBuilder);
if (!openResult.isSuccess()) {
return openResult;
}
openResult = RequiredFields
.readFileSize(readBuffer, fileSize, mapFileInfoBuilder);
if (!openResult.isSuccess()) {
return openResult;
}
openResult = RequiredFields.readMapDate(readBuffer, mapFileInfoBuilder);
if (!openResult.isSuccess()) {
return openResult;
}
openResult = RequiredFields.readBoundingBox(readBuffer, mapFileInfoBuilder);
if (!openResult.isSuccess()) {
return openResult;
}
openResult = RequiredFields.readTilePixelSize(readBuffer, mapFileInfoBuilder);
if (!openResult.isSuccess()) {
return openResult;
}
openResult = RequiredFields.readProjectionName(readBuffer, mapFileInfoBuilder);
if (!openResult.isSuccess()) {
return openResult;
}
openResult = OptionalFields.readOptionalFields(readBuffer, mapFileInfoBuilder);
if (!openResult.isSuccess()) {
return openResult;
}
openResult = RequiredFields.readPoiTags(readBuffer, mapFileInfoBuilder);
if (!openResult.isSuccess()) {
return openResult;
}
openResult = RequiredFields.readWayTags(readBuffer, mapFileInfoBuilder);
if (!openResult.isSuccess()) {
return openResult;
}
openResult = readSubFileParameters(readBuffer, fileSize, mapFileInfoBuilder);
if (!openResult.isSuccess()) {
return openResult;
}
this.mapFileInfo = mapFileInfoBuilder.build();
return OpenResult.SUCCESS;
}
private OpenResult readSubFileParameters(ReadBuffer readBuffer, long fileSize,
MapFileInfoBuilder mapFileInfoBuilder) {
// get and check the number of sub-files (1 byte)
byte numberOfSubFiles = readBuffer.readByte();
if (numberOfSubFiles < 1) {
return new OpenResult("invalid number of sub-files: " + numberOfSubFiles);
}
mapFileInfoBuilder.numberOfSubFiles = numberOfSubFiles;
SubFileParameter[] tempSubFileParameters = new SubFileParameter[numberOfSubFiles];
this.zoomLevelMinimum = Byte.MAX_VALUE;
this.zoomLevelMaximum = Byte.MIN_VALUE;
// get and check the information for each sub-file
for (byte currentSubFile = 0; currentSubFile < numberOfSubFiles; ++currentSubFile) {
SubFileParameterBuilder subFileParameterBuilder = new SubFileParameterBuilder();
// get and check the base zoom level (1 byte)
byte baseZoomLevel = readBuffer.readByte();
if (baseZoomLevel < 0 || baseZoomLevel > BASE_ZOOM_LEVEL_MAX) {
return new OpenResult("invalid base zooom level: " + baseZoomLevel);
}
subFileParameterBuilder.baseZoomLevel = baseZoomLevel;
// get and check the minimum zoom level (1 byte)
byte zoomLevelMin = readBuffer.readByte();
if (zoomLevelMin < 0 || zoomLevelMin > 22) {
return new OpenResult("invalid minimum zoom level: " + zoomLevelMin);
}
subFileParameterBuilder.zoomLevelMin = zoomLevelMin;
// get and check the maximum zoom level (1 byte)
byte zoomLevelMax = readBuffer.readByte();
if (zoomLevelMax < 0 || zoomLevelMax > 22) {
return new OpenResult("invalid maximum zoom level: " + zoomLevelMax);
}
subFileParameterBuilder.zoomLevelMax = zoomLevelMax;
// check for valid zoom level range
if (zoomLevelMin > zoomLevelMax) {
return new OpenResult("invalid zoom level range: " + zoomLevelMin + SPACE
+ zoomLevelMax);
}
// get and check the start address of the sub-file (8 bytes)
long startAddress = readBuffer.readLong();
if (startAddress < HEADER_SIZE_MIN || startAddress >= fileSize) {
return new OpenResult("invalid start address: " + startAddress);
}
subFileParameterBuilder.startAddress = startAddress;
long indexStartAddress = startAddress;
if (mapFileInfoBuilder.optionalFields.isDebugFile) {
// the sub-file has an index signature before the index
indexStartAddress += SIGNATURE_LENGTH_INDEX;
}
subFileParameterBuilder.indexStartAddress = indexStartAddress;
// get and check the size of the sub-file (8 bytes)
long subFileSize = readBuffer.readLong();
if (subFileSize < 1) {
return new OpenResult("invalid sub-file size: " + subFileSize);
}
subFileParameterBuilder.subFileSize = subFileSize;
subFileParameterBuilder.boundingBox = mapFileInfoBuilder.boundingBox;
// add the current sub-file to the list of sub-files
tempSubFileParameters[currentSubFile] = subFileParameterBuilder.build();
updateZoomLevelInformation(tempSubFileParameters[currentSubFile]);
}
mapFileInfoBuilder.zoomLevel = new int[numberOfSubFiles];
// create and fill the lookup table for the sub-files
this.subFileParameters = new SubFileParameter[this.zoomLevelMaximum + 1];
for (int currentMapFile = 0; currentMapFile < numberOfSubFiles; ++currentMapFile) {
SubFileParameter subFileParameter = tempSubFileParameters[currentMapFile];
mapFileInfoBuilder.zoomLevel[currentMapFile] = subFileParameter.baseZoomLevel;
for (byte zoomLevel = subFileParameter.zoomLevelMin; zoomLevel <= subFileParameter.zoomLevelMax; ++zoomLevel) {
this.subFileParameters[zoomLevel] = subFileParameter;
}
}
return OpenResult.SUCCESS;
}
private void updateZoomLevelInformation(SubFileParameter subFileParameter) {
// update the global minimum and maximum zoom level information
if (this.zoomLevelMinimum > subFileParameter.zoomLevelMin) {
this.zoomLevelMinimum = subFileParameter.zoomLevelMin;
}
if (this.zoomLevelMaximum < subFileParameter.zoomLevelMax) {
this.zoomLevelMaximum = subFileParameter.zoomLevelMax;
}
}
}

View File

@@ -0,0 +1,71 @@
/*
* Copyright 2010, 2011, 2012 mapsforge.org
*
* 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.oscim.tilesource.mapfile.header;
import org.oscim.core.Tag;
/**
* Contains the immutable metadata of a map file.
*
*/
public class MapFileInfo extends org.oscim.tilesource.MapInfo {
/**
* True if the map file includes debug information, false otherwise.
*/
public final boolean debugFile;
/**
* The number of sub-files in the map file.
*/
public final byte numberOfSubFiles;
/**
* The POI tags.
*/
public final Tag[] poiTags;
/**
* The way tags.
*/
public final Tag[] wayTags;
/**
* The size of the tiles in pixels.
*/
public final int tilePixelSize;
MapFileInfo(MapFileInfoBuilder mapFileInfoBuilder) {
super(mapFileInfoBuilder.boundingBox,
mapFileInfoBuilder.optionalFields.startZoomLevel,
mapFileInfoBuilder.optionalFields.startPosition,
mapFileInfoBuilder.projectionName,
mapFileInfoBuilder.mapDate,
mapFileInfoBuilder.fileSize,
mapFileInfoBuilder.fileVersion,
mapFileInfoBuilder.optionalFields.languagePreference,
mapFileInfoBuilder.optionalFields.comment,
mapFileInfoBuilder.optionalFields.createdBy,
mapFileInfoBuilder.zoomLevel);
debugFile = mapFileInfoBuilder.optionalFields.isDebugFile;
numberOfSubFiles = mapFileInfoBuilder.numberOfSubFiles;
poiTags = mapFileInfoBuilder.poiTags;
tilePixelSize = mapFileInfoBuilder.tilePixelSize;
wayTags = mapFileInfoBuilder.wayTags;
}
}

View File

@@ -0,0 +1,36 @@
/*
* Copyright 2010, 2011, 2012 mapsforge.org
*
* 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.oscim.tilesource.mapfile.header;
import org.oscim.core.BoundingBox;
import org.oscim.core.Tag;
class MapFileInfoBuilder {
BoundingBox boundingBox;
long fileSize;
int fileVersion;
long mapDate;
byte numberOfSubFiles;
OptionalFields optionalFields;
Tag[] poiTags;
String projectionName;
int tilePixelSize;
Tag[] wayTags;
int[] zoomLevel;
MapFileInfo build() {
return new MapFileInfo(this);
}
}

View File

@@ -0,0 +1,166 @@
/*
* Copyright 2010, 2011, 2012 mapsforge.org
*
* 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.oscim.tilesource.mapfile.header;
import org.oscim.core.GeoPoint;
import org.oscim.tilesource.TileSource.OpenResult;
import org.oscim.tilesource.mapfile.ReadBuffer;
final class OptionalFields {
/**
* Bitmask for the comment field in the file header.
*/
private static final int HEADER_BITMASK_COMMENT = 0x08;
/**
* Bitmask for the created by field in the file header.
*/
private static final int HEADER_BITMASK_CREATED_BY = 0x04;
/**
* Bitmask for the debug flag in the file header.
*/
private static final int HEADER_BITMASK_DEBUG = 0x80;
/**
* Bitmask for the language preference field in the file header.
*/
private static final int HEADER_BITMASK_LANGUAGE_PREFERENCE = 0x10;
/**
* Bitmask for the start position field in the file header.
*/
private static final int HEADER_BITMASK_START_POSITION = 0x40;
/**
* Bitmask for the start zoom level field in the file header.
*/
private static final int HEADER_BITMASK_START_ZOOM_LEVEL = 0x20;
/**
* The length of the language preference string.
*/
private static final int LANGUAGE_PREFERENCE_LENGTH = 2;
/**
* Maximum valid start zoom level.
*/
private static final int START_ZOOM_LEVEL_MAX = 22;
static OpenResult readOptionalFields(ReadBuffer readBuffer,
MapFileInfoBuilder mapFileInfoBuilder) {
OptionalFields optionalFields = new OptionalFields(readBuffer.readByte());
mapFileInfoBuilder.optionalFields = optionalFields;
OpenResult openResult = optionalFields.readOptionalFields(readBuffer);
if (!openResult.isSuccess()) {
return openResult;
}
return OpenResult.SUCCESS;
}
String comment;
String createdBy;
final boolean hasComment;
final boolean hasCreatedBy;
final boolean hasLanguagePreference;
final boolean hasStartPosition;
final boolean hasStartZoomLevel;
final boolean isDebugFile;
String languagePreference;
GeoPoint startPosition;
Byte startZoomLevel;
private OptionalFields(byte flags) {
this.isDebugFile = (flags & HEADER_BITMASK_DEBUG) != 0;
this.hasStartPosition = (flags & HEADER_BITMASK_START_POSITION) != 0;
this.hasStartZoomLevel = (flags & HEADER_BITMASK_START_ZOOM_LEVEL) != 0;
this.hasLanguagePreference = (flags & HEADER_BITMASK_LANGUAGE_PREFERENCE) != 0;
this.hasComment = (flags & HEADER_BITMASK_COMMENT) != 0;
this.hasCreatedBy = (flags & HEADER_BITMASK_CREATED_BY) != 0;
}
private OpenResult readLanguagePreference(ReadBuffer readBuffer) {
if (this.hasLanguagePreference) {
String countryCode = readBuffer.readUTF8EncodedString();
if (countryCode.length() != LANGUAGE_PREFERENCE_LENGTH) {
return new OpenResult("invalid language preference: " + countryCode);
}
this.languagePreference = countryCode;
}
return OpenResult.SUCCESS;
}
private OpenResult readMapStartPosition(ReadBuffer readBuffer) {
if (this.hasStartPosition) {
// get and check the start position latitude (4 byte)
int mapStartLatitude = readBuffer.readInt();
if (mapStartLatitude < RequiredFields.LATITUDE_MIN
|| mapStartLatitude > RequiredFields.LATITUDE_MAX) {
return new OpenResult("invalid map start latitude: " + mapStartLatitude);
}
// get and check the start position longitude (4 byte)
int mapStartLongitude = readBuffer.readInt();
if (mapStartLongitude < RequiredFields.LONGITUDE_MIN
|| mapStartLongitude > RequiredFields.LONGITUDE_MAX) {
return new OpenResult("invalid map start longitude: " + mapStartLongitude);
}
this.startPosition = new GeoPoint(mapStartLatitude, mapStartLongitude);
}
return OpenResult.SUCCESS;
}
private OpenResult readMapStartZoomLevel(ReadBuffer readBuffer) {
if (this.hasStartZoomLevel) {
// get and check the start zoom level (1 byte)
byte mapStartZoomLevel = readBuffer.readByte();
if (mapStartZoomLevel < 0 || mapStartZoomLevel > START_ZOOM_LEVEL_MAX) {
return new OpenResult("invalid map start zoom level: " + mapStartZoomLevel);
}
this.startZoomLevel = Byte.valueOf(mapStartZoomLevel);
}
return OpenResult.SUCCESS;
}
private OpenResult readOptionalFields(ReadBuffer readBuffer) {
OpenResult openResult = readMapStartPosition(readBuffer);
if (!openResult.isSuccess()) {
return openResult;
}
openResult = readMapStartZoomLevel(readBuffer);
if (!openResult.isSuccess()) {
return openResult;
}
openResult = readLanguagePreference(readBuffer);
if (!openResult.isSuccess()) {
return openResult;
}
if (this.hasComment) {
this.comment = readBuffer.readUTF8EncodedString();
}
if (this.hasCreatedBy) {
this.createdBy = readBuffer.readUTF8EncodedString();
}
return OpenResult.SUCCESS;
}
}

View File

@@ -0,0 +1,239 @@
/*
* Copyright 2010, 2011, 2012 mapsforge.org
*
* 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.oscim.tilesource.mapfile.header;
import java.io.IOException;
import org.oscim.core.BoundingBox;
import org.oscim.core.Tag;
import org.oscim.tilesource.TileSource.OpenResult;
import org.oscim.tilesource.mapfile.ReadBuffer;
final class RequiredFields {
/**
* Magic byte at the beginning of a valid binary map file.
*/
private static final String BINARY_OSM_MAGIC_BYTE = "mapsforge binary OSM";
/**
* Maximum size of the file header in bytes.
*/
private static final int HEADER_SIZE_MAX = 1000000;
/**
* Minimum size of the file header in bytes.
*/
private static final int HEADER_SIZE_MIN = 70;
/**
* The name of the Mercator projection as stored in the file header.
*/
private static final String MERCATOR = "Mercator";
/**
* A single whitespace character.
*/
private static final char SPACE = ' ';
/**
* Version of the map file format which is supported by this implementation.
*/
private static final int FILE_VERSION_3 = 3;
private static final int FILE_VERSION_4 = 4;
/**
* The maximum latitude values in microdegrees.
*/
static final int LATITUDE_MAX = 90000000;
/**
* The minimum latitude values in microdegrees.
*/
static final int LATITUDE_MIN = -90000000;
/**
* The maximum longitude values in microdegrees.
*/
static final int LONGITUDE_MAX = 180000000;
/**
* The minimum longitude values in microdegrees.
*/
static final int LONGITUDE_MIN = -180000000;
static OpenResult readBoundingBox(ReadBuffer readBuffer, MapFileInfoBuilder mapFileInfoBuilder) {
// get and check the minimum latitude (4 bytes)
int minLatitude = readBuffer.readInt();
if (minLatitude < LATITUDE_MIN || minLatitude > LATITUDE_MAX) {
return new OpenResult("invalid minimum latitude: " + minLatitude);
}
// get and check the minimum longitude (4 bytes)
int minLongitude = readBuffer.readInt();
if (minLongitude < LONGITUDE_MIN || minLongitude > LONGITUDE_MAX) {
return new OpenResult("invalid minimum longitude: " + minLongitude);
}
// get and check the maximum latitude (4 bytes)
int maxLatitude = readBuffer.readInt();
if (maxLatitude < LATITUDE_MIN || maxLatitude > LATITUDE_MAX) {
return new OpenResult("invalid maximum latitude: " + maxLatitude);
}
// get and check the maximum longitude (4 bytes)
int maxLongitude = readBuffer.readInt();
if (maxLongitude < LONGITUDE_MIN || maxLongitude > LONGITUDE_MAX) {
return new OpenResult("invalid maximum longitude: " + maxLongitude);
}
// check latitude and longitude range
if (minLatitude > maxLatitude) {
return new OpenResult("invalid latitude range: " + minLatitude + SPACE + maxLatitude);
} else if (minLongitude > maxLongitude) {
return new OpenResult("invalid longitude range: " + minLongitude + SPACE + maxLongitude);
}
mapFileInfoBuilder.boundingBox = new BoundingBox(minLatitude, minLongitude, maxLatitude,
maxLongitude);
return OpenResult.SUCCESS;
}
static OpenResult readFileSize(ReadBuffer readBuffer, long fileSize,
MapFileInfoBuilder mapFileInfoBuilder) {
// get and check the file size (8 bytes)
long headerFileSize = readBuffer.readLong();
if (headerFileSize != fileSize) {
return new OpenResult("invalid file size: " + headerFileSize);
}
mapFileInfoBuilder.fileSize = fileSize;
return OpenResult.SUCCESS;
}
static OpenResult readFileVersion(ReadBuffer readBuffer, MapFileInfoBuilder mapFileInfoBuilder) {
// get and check the file version (4 bytes)
int fileVersion = readBuffer.readInt();
if (fileVersion != FILE_VERSION_3 && fileVersion != FILE_VERSION_4) {
return new OpenResult("unsupported file version: " + fileVersion);
}
mapFileInfoBuilder.fileVersion = fileVersion;
return OpenResult.SUCCESS;
}
static OpenResult readMagicByte(ReadBuffer readBuffer) throws IOException {
// read the the magic byte and the file header size into the buffer
int magicByteLength = BINARY_OSM_MAGIC_BYTE.length();
if (!readBuffer.readFromFile(magicByteLength + 4)) {
return new OpenResult("reading magic byte has failed");
}
// get and check the magic byte
String magicByte = readBuffer.readUTF8EncodedString(magicByteLength);
if (!BINARY_OSM_MAGIC_BYTE.equals(magicByte)) {
return new OpenResult("invalid magic byte: " + magicByte);
}
return OpenResult.SUCCESS;
}
static OpenResult readMapDate(ReadBuffer readBuffer, MapFileInfoBuilder mapFileInfoBuilder) {
// get and check the the map date (8 bytes)
long mapDate = readBuffer.readLong();
// is the map date before 2010-01-10 ?
if (mapDate < 1200000000000L) {
return new OpenResult("invalid map date: " + mapDate);
}
mapFileInfoBuilder.mapDate = mapDate;
return OpenResult.SUCCESS;
}
static OpenResult readPoiTags(ReadBuffer readBuffer, MapFileInfoBuilder mapFileInfoBuilder) {
// get and check the number of POI tags (2 bytes)
int numberOfPoiTags = readBuffer.readShort();
if (numberOfPoiTags < 0) {
return new OpenResult("invalid number of POI tags: " + numberOfPoiTags);
}
Tag[] poiTags = new Tag[numberOfPoiTags];
for (int currentTagId = 0; currentTagId < numberOfPoiTags; ++currentTagId) {
// get and check the POI tag
String tag = readBuffer.readUTF8EncodedString();
if (tag == null) {
return new OpenResult("POI tag must not be null: " + currentTagId);
}
poiTags[currentTagId] = new Tag(tag);
}
mapFileInfoBuilder.poiTags = poiTags;
return OpenResult.SUCCESS;
}
static OpenResult readProjectionName(ReadBuffer readBuffer,
MapFileInfoBuilder mapFileInfoBuilder) {
// get and check the projection name
String projectionName = readBuffer.readUTF8EncodedString();
if (!MERCATOR.equals(projectionName)) {
return new OpenResult("unsupported projection: " + projectionName);
}
mapFileInfoBuilder.projectionName = projectionName;
return OpenResult.SUCCESS;
}
static OpenResult readRemainingHeader(ReadBuffer readBuffer) throws IOException {
// get and check the size of the remaining file header (4 bytes)
int remainingHeaderSize = readBuffer.readInt();
if (remainingHeaderSize < HEADER_SIZE_MIN || remainingHeaderSize > HEADER_SIZE_MAX) {
return new OpenResult("invalid remaining header size: " + remainingHeaderSize);
}
// read the header data into the buffer
if (!readBuffer.readFromFile(remainingHeaderSize)) {
return new OpenResult("reading header data has failed: " + remainingHeaderSize);
}
return OpenResult.SUCCESS;
}
static OpenResult readTilePixelSize(ReadBuffer readBuffer, MapFileInfoBuilder mapFileInfoBuilder) {
// get and check the tile pixel size (2 bytes)
int tilePixelSize = readBuffer.readShort();
// if (tilePixelSize != Tile.SIZE) {
// return new FileOpenResult("unsupported tile pixel size: " + tilePixelSize);
// }
mapFileInfoBuilder.tilePixelSize = tilePixelSize;
return OpenResult.SUCCESS;
}
static OpenResult readWayTags(ReadBuffer readBuffer, MapFileInfoBuilder mapFileInfoBuilder) {
// get and check the number of way tags (2 bytes)
int numberOfWayTags = readBuffer.readShort();
if (numberOfWayTags < 0) {
return new OpenResult("invalid number of way tags: " + numberOfWayTags);
}
Tag[] wayTags = new Tag[numberOfWayTags];
for (int currentTagId = 0; currentTagId < numberOfWayTags; ++currentTagId) {
// get and check the way tag
String tag = readBuffer.readUTF8EncodedString();
if (tag == null) {
return new OpenResult("way tag must not be null: " + currentTagId);
}
wayTags[currentTagId] = new Tag(tag);
}
mapFileInfoBuilder.wayTags = wayTags;
return OpenResult.SUCCESS;
}
private RequiredFields() {
throw new IllegalStateException();
}
}

View File

@@ -0,0 +1,218 @@
/*
* Copyright 2010, 2011, 2012 mapsforge.org
*
* 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.oscim.tilesource.mapfile.header;
import org.oscim.tilesource.mapfile.Projection;
/**
* Holds all parameters of a sub-file.
*/
public class SubFileParameter {
/**
* Number of bytes a single index entry consists of.
*/
public static final byte BYTES_PER_INDEX_ENTRY = 5;
/**
* Divisor for converting coordinates stored as integers to double values.
*/
private static final double COORDINATES_DIVISOR = 1000000d;
/**
* Base zoom level of the sub-file, which equals to one block.
*/
public final byte baseZoomLevel;
/**
* Size of the entries table at the beginning of each block in bytes.
*/
public final int blockEntriesTableSize;
/**
* Vertical amount of blocks in the grid.
*/
public final long blocksHeight;
/**
* Horizontal amount of blocks in the grid.
*/
public final long blocksWidth;
/**
* Y number of the tile at the bottom boundary in the grid.
*/
public final long boundaryTileBottom;
/**
* X number of the tile at the left boundary in the grid.
*/
public final long boundaryTileLeft;
/**
* X number of the tile at the right boundary in the grid.
*/
public final long boundaryTileRight;
/**
* Y number of the tile at the top boundary in the grid.
*/
public final long boundaryTileTop;
/**
* Absolute end address of the index in the enclosing file.
*/
public final long indexEndAddress;
/**
* Absolute start address of the index in the enclosing file.
*/
public final long indexStartAddress;
/**
* Total number of blocks in the grid.
*/
public final long numberOfBlocks;
/**
* Absolute start address of the sub-file in the enclosing file.
*/
public final long startAddress;
/**
* Size of the sub-file in bytes.
*/
public final long subFileSize;
/**
* Maximum zoom level for which the block entries tables are made.
*/
public final byte zoomLevelMax;
/**
* Minimum zoom level for which the block entries tables are made.
*/
public final byte zoomLevelMin;
/**
* Stores the hash code of this object.
*/
private final int hashCodeValue;
SubFileParameter(SubFileParameterBuilder subFileParameterBuilder) {
this.startAddress = subFileParameterBuilder.startAddress;
this.indexStartAddress = subFileParameterBuilder.indexStartAddress;
this.subFileSize = subFileParameterBuilder.subFileSize;
this.baseZoomLevel = subFileParameterBuilder.baseZoomLevel;
this.zoomLevelMin = subFileParameterBuilder.zoomLevelMin;
this.zoomLevelMax = subFileParameterBuilder.zoomLevelMax;
this.hashCodeValue = calculateHashCode();
// calculate the XY numbers of the boundary tiles in this sub-file
this.boundaryTileBottom = Projection.latitudeToTileY(
subFileParameterBuilder.boundingBox.minLatitudeE6
/ COORDINATES_DIVISOR, this.baseZoomLevel);
this.boundaryTileLeft = Projection.longitudeToTileX(
subFileParameterBuilder.boundingBox.minLongitudeE6
/ COORDINATES_DIVISOR, this.baseZoomLevel);
this.boundaryTileTop = Projection.latitudeToTileY(
subFileParameterBuilder.boundingBox.maxLatitudeE6
/ COORDINATES_DIVISOR, this.baseZoomLevel);
this.boundaryTileRight = Projection.longitudeToTileX(
subFileParameterBuilder.boundingBox.maxLongitudeE6
/ COORDINATES_DIVISOR, this.baseZoomLevel);
// calculate the horizontal and vertical amount of blocks in this sub-file
this.blocksWidth = this.boundaryTileRight - this.boundaryTileLeft + 1;
this.blocksHeight = this.boundaryTileBottom - this.boundaryTileTop + 1;
// calculate the total amount of blocks in this sub-file
this.numberOfBlocks = this.blocksWidth * this.blocksHeight;
this.indexEndAddress = this.indexStartAddress + this.numberOfBlocks * BYTES_PER_INDEX_ENTRY;
// calculate the size of the tile entries table
this.blockEntriesTableSize = 2 * (this.zoomLevelMax - this.zoomLevelMin + 1) * 2;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
} else if (!(obj instanceof SubFileParameter)) {
return false;
}
SubFileParameter other = (SubFileParameter) obj;
if (this.startAddress != other.startAddress) {
return false;
} else if (this.subFileSize != other.subFileSize) {
return false;
} else if (this.baseZoomLevel != other.baseZoomLevel) {
return false;
}
return true;
}
@Override
public int hashCode() {
return this.hashCodeValue;
}
@Override
public String toString() {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("SubFileParameter [baseZoomLevel=");
stringBuilder.append(this.baseZoomLevel);
stringBuilder.append(", blockEntriesTableSize=");
stringBuilder.append(this.blockEntriesTableSize);
stringBuilder.append(", blocksHeight=");
stringBuilder.append(this.blocksHeight);
stringBuilder.append(", blocksWidth=");
stringBuilder.append(this.blocksWidth);
stringBuilder.append(", boundaryTileBottom=");
stringBuilder.append(this.boundaryTileBottom);
stringBuilder.append(", boundaryTileLeft=");
stringBuilder.append(this.boundaryTileLeft);
stringBuilder.append(", boundaryTileRight=");
stringBuilder.append(this.boundaryTileRight);
stringBuilder.append(", boundaryTileTop=");
stringBuilder.append(this.boundaryTileTop);
stringBuilder.append(", indexStartAddress=");
stringBuilder.append(this.indexStartAddress);
stringBuilder.append(", numberOfBlocks=");
stringBuilder.append(this.numberOfBlocks);
stringBuilder.append(", startAddress=");
stringBuilder.append(this.startAddress);
stringBuilder.append(", subFileSize=");
stringBuilder.append(this.subFileSize);
stringBuilder.append(", zoomLevelMax=");
stringBuilder.append(this.zoomLevelMax);
stringBuilder.append(", zoomLevelMin=");
stringBuilder.append(this.zoomLevelMin);
stringBuilder.append("]");
return stringBuilder.toString();
}
/**
* @return the hash code of this object.
*/
private int calculateHashCode() {
int result = 7;
result = 31 * result + (int) (this.startAddress ^ (this.startAddress >>> 32));
result = 31 * result + (int) (this.subFileSize ^ (this.subFileSize >>> 32));
result = 31 * result + this.baseZoomLevel;
return result;
}
}

View File

@@ -0,0 +1,31 @@
/*
* Copyright 2010, 2011, 2012 mapsforge.org
*
* 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.oscim.tilesource.mapfile.header;
import org.oscim.core.BoundingBox;
class SubFileParameterBuilder {
byte baseZoomLevel;
BoundingBox boundingBox;
long indexStartAddress;
long startAddress;
long subFileSize;
byte zoomLevelMax;
byte zoomLevelMin;
SubFileParameter build() {
return new SubFileParameter(this);
}
}

View File

@@ -0,0 +1,62 @@
/*
* Copyright 2013 Hannes Janetzek
*
* 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.oscim.tilesource.mapnik;
import java.net.URL;
import org.oscim.core.Tile;
import org.oscim.tilesource.ITileDataSource;
import org.oscim.tilesource.common.LwHttp;
import org.oscim.tilesource.common.PbfTileDataSource;
import org.oscim.tilesource.common.UrlTileSource;
public class MapnikVectorTileSource extends UrlTileSource {
@Override
public ITileDataSource getDataSource() {
return new TileDataSource(mUrl);
}
static class TileDataSource extends PbfTileDataSource {
public TileDataSource(URL url) {
super(new TileDecoder());
mConn = new LwHttp(url, "image/png", "vector.pbf", true) {
@Override
protected int formatTilePath(Tile tile, byte[] path, int pos) {
// url formatter for mapbox streets
byte[] hexTable = {
'0', '1', '2', '3',
'4', '5', '6', '7',
'8', '9', 'a', 'b',
'c', 'd', 'e', 'f'
};
path[pos++] = '/';
path[pos++] = hexTable[(tile.tileX) % 16];
path[pos++] = hexTable[(tile.tileY) % 16];
path[pos++] = '/';
pos = LwHttp.writeInt(tile.zoomLevel, pos, path);
path[pos++] = '/';
pos = LwHttp.writeInt(tile.tileX, pos, path);
path[pos++] = '/';
pos = LwHttp.writeInt(tile.tileY, pos, path);
return pos;
}
};
}
}
}

View File

@@ -0,0 +1,599 @@
/*
* Copyright 2013
*
* 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.oscim.tilesource.mapnik;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import org.oscim.core.GeometryBuffer.GeometryType;
import org.oscim.core.MapElement;
import org.oscim.core.Tag;
import org.oscim.core.TagSet;
import org.oscim.core.Tile;
import org.oscim.tilesource.common.PbfDecoder;
import org.oscim.tilesource.ITileDataSink;
import org.oscim.utils.pool.Inlist;
import org.oscim.utils.pool.Pool;
import android.util.Log;
public class TileDecoder extends PbfDecoder {
private final static String TAG = TileDecoder.class.getName();
private static final int TAG_TILE_LAYERS = 3;
private static final int TAG_LAYER_VERSION = 15;
private static final int TAG_LAYER_NAME = 1;
private static final int TAG_LAYER_FEATURES = 2;
private static final int TAG_LAYER_KEYS = 3;
private static final int TAG_LAYER_VALUES = 4;
private static final int TAG_LAYER_EXTENT = 5;
private static final int TAG_FEATURE_ID = 1;
private static final int TAG_FEATURE_TAGS = 2;
private static final int TAG_FEATURE_TYPE = 3;
private static final int TAG_FEATURE_GEOMETRY = 4;
private static final int TAG_VALUE_STRING = 1;
private static final int TAG_VALUE_FLOAT = 2;
private static final int TAG_VALUE_DOUBLE = 3;
private static final int TAG_VALUE_LONG = 4;
private static final int TAG_VALUE_UINT = 5;
private static final int TAG_VALUE_SINT = 6;
private static final int TAG_VALUE_BOOL = 7;
private static final int TAG_GEOM_UNKNOWN = 0;
private static final int TAG_GEOM_POINT = 1;
private static final int TAG_GEOM_LINE = 2;
private static final int TAG_GEOM_POLYGON = 3;
private short[] mTmpTags = new short[1024];
private Tile mTile;
private final String mLocale = "de";
private ITileDataSink mMapDataCallback;
private final static float REF_TILE_SIZE = 4096.0f;
private float mScale;
@Override
public boolean decode(Tile tile, ITileDataSink mapDataCallback, InputStream is, int contentLength)
throws IOException {
if (debug)
Log.d(TAG, tile + " decode");
setInputStream(is, Integer.MAX_VALUE);
mTile = tile;
mMapDataCallback = mapDataCallback;
mScale = REF_TILE_SIZE / Tile.SIZE;
int val;
while (hasData() && (val = decodeVarint32()) > 0) {
// read tag and wire type
int tag = (val >> 3);
switch (tag) {
case TAG_TILE_LAYERS:
decodeLayer();
break;
default:
error(mTile + " invalid type for tile: " + tag);
return false;
}
}
if (hasData()){
error(tile + " invalid tile");
return false;
}
return true;
}
private boolean decodeLayer() throws IOException {
//int version = 0;
//int extent = 4096;
int bytes = decodeVarint32();
ArrayList<String> keys = new ArrayList<String>();
ArrayList<String> values = new ArrayList<String>();
String name = null;
int numFeatures = 0;
ArrayList<Feature> features = new ArrayList<Feature>();
int end = position() + bytes;
while (position() < end) {
// read tag and wire type
int val = decodeVarint32();
if (val == 0)
break;
int tag = (val >> 3);
switch (tag) {
case TAG_LAYER_KEYS:
keys.add(decodeString());
break;
case TAG_LAYER_VALUES:
values.add(decodeValue());
break;
case TAG_LAYER_FEATURES:
numFeatures++;
decodeFeature(features);
break;
case TAG_LAYER_VERSION:
//version =
decodeVarint32();
break;
case TAG_LAYER_NAME:
name = decodeString();
break;
case TAG_LAYER_EXTENT:
//extent =
decodeVarint32();
break;
default:
error(mTile + " invalid type for layer: " + tag);
break;
}
}
Tag layerTag = new Tag(name, Tag.VALUE_YES);
if (numFeatures == 0)
return true;
int[] ignoreLocal = new int[20];
int numIgnore = 0;
int fallBackLocal = -1;
int matchedLocal = -1;
for (int i = 0; i < keys.size(); i++) {
String key = keys.get(i);
if (!key.startsWith(Tag.TAG_KEY_NAME))
continue;
int len = key.length();
if (len == 4) {
fallBackLocal = i;
continue;
}
if (len < 7) {
ignoreLocal[numIgnore++] = i;
continue;
}
if (mLocale.equals(key.substring(5))) {
//Log.d(TAG, "found local " + key);
matchedLocal = i;
} else
ignoreLocal[numIgnore++] = i;
}
for (Feature f : features) {
if (f.elem.type == GeometryType.NONE)
continue;
mTagSet.clear();
mTagSet.add(layerTag);
boolean hasName = false;
String fallbackName = null;
tagLoop: for (int j = 0; j < (f.numTags << 1); j += 2) {
int keyIdx = f.tags[j];
for (int i = 0; i < numIgnore; i++)
if (keyIdx == ignoreLocal[i])
continue tagLoop;
if (keyIdx == fallBackLocal) {
fallbackName = values.get(f.tags[j + 1]);
continue;
}
String key;
String val = values.get(f.tags[j + 1]);
if (keyIdx == matchedLocal) {
hasName = true;
mTagSet.add(new Tag(Tag.TAG_KEY_NAME, val, false));
} else {
key = keys.get(keyIdx);
mTagSet.add(new Tag(key, val));
}
}
if (!hasName && fallbackName != null)
mTagSet.add(new Tag(Tag.TAG_KEY_NAME, fallbackName, false));
// FIXME extract layer tag here
f.elem.set(mTagSet.asArray(), 5);
mMapDataCallback.process(f.elem);
mFeaturePool.release(f);
}
return true;
}
private final TagSet mTagSet = new TagSet();
private final Pool<Feature> mFeaturePool = new Pool<Feature>() {
int count;
@Override
protected Feature createItem() {
count++;
return new Feature();
}
@Override
protected boolean clearItem(Feature item) {
if (count > 100) {
count--;
return false;
}
item.elem.tags = null;
item.elem.clear();
item.tags = null;
item.type = 0;
item.numTags = 0;
return true;
}
};
static class Feature extends Inlist<Feature> {
short[] tags;
int numTags;
int type;
final MapElement elem;
Feature() {
elem = new MapElement();
}
boolean match(short otherTags[], int otherNumTags, int otherType) {
if (numTags != otherNumTags)
return false;
if (type != otherType)
return false;
for (int i = 0; i < numTags << 1; i++) {
if (tags[i] != otherTags[i])
return false;
}
return true;
}
}
private void decodeFeature(ArrayList<Feature> features) throws IOException {
int bytes = decodeVarint32();
int end = position() + bytes;
int type = 0;
//long id;
lastX = 0;
lastY = 0;
mTmpTags[0] = -1;
Feature curFeature = null;
int numTags = 0;
//Log.d(TAG, "start feature");
while (position() < end) {
// read tag and wire type
int val = decodeVarint32();
if (val == 0)
break;
int tag = (val >>> 3);
switch (tag) {
case TAG_FEATURE_ID:
//id =
decodeVarint32();
break;
case TAG_FEATURE_TAGS:
mTmpTags = decodeUnsignedVarintArray(mTmpTags);
for (; numTags < mTmpTags.length && mTmpTags[numTags] >= 0;)
numTags += 2;
numTags >>= 1;
break;
case TAG_FEATURE_TYPE:
type = decodeVarint32();
//Log.d(TAG, "got type " + type);
break;
case TAG_FEATURE_GEOMETRY:
for (Feature f : features) {
if (f.match(mTmpTags, numTags, type)) {
curFeature = f;
break;
}
}
if (curFeature == null) {
curFeature = mFeaturePool.get();
curFeature.tags = new short[numTags << 1];
System.arraycopy(mTmpTags, 0, curFeature.tags, 0, numTags << 1);
curFeature.numTags = numTags;
curFeature.type = type;
features.add(curFeature);
}
decodeCoordinates(type, curFeature);
break;
default:
error(mTile + " invalid type for feature: " + tag);
break;
}
}
}
private final static int CLOSE_PATH = 0x07;
private final static int MOVE_TO = 0x01;
//private final static int LINE_TO = 0x02;
private int lastX, lastY;
private int decodeCoordinates(int type, Feature feature) throws IOException {
int bytes = decodeVarint32();
fillBuffer(bytes);
if (feature == null) {
bufferPos += bytes;
return 0;
}
MapElement elem = feature.elem;
boolean isPoint = false;
boolean isPoly = false;
boolean isLine = false;
if (type == TAG_GEOM_LINE) {
elem.startLine();
isLine = true;
}
else if (type == TAG_GEOM_POLYGON) {
elem.startPolygon();
isPoly = true;
} else if (type == TAG_GEOM_POINT) {
isPoint = true;
elem.startPoints();
} else if (type == TAG_GEOM_UNKNOWN)
elem.startPoints();
boolean even = true;
int val;
int curX = 0;
int curY = 0;
int prevX = 0;
int prevY = 0;
int cmd = 0;
int num = 0, cnt = 0;
boolean first = true;
boolean lastClip = false;
// test bbox for outer..
boolean isOuter = true;
boolean simplify = mTile.zoomLevel < 14;
int pixel = simplify ? 7 : 3;
int xmin = Integer.MAX_VALUE, xmax = Integer.MIN_VALUE;
int ymin = Integer.MAX_VALUE, ymax = Integer.MIN_VALUE;
for (int end = bufferPos + bytes; bufferPos < end;) {
val = decodeVarint32Filled();
if (num == 0) {
// number of points
num = val >>> 3;
cnt = 0;
// path command
cmd = val & 0x07;
if (isLine && lastClip) {
elem.addPoint(curX / mScale, curY / mScale);
lastClip = false;
}
if (cmd == CLOSE_PATH) {
num = 0;
continue;
}
if (first) {
first = false;
continue;
}
if (cmd == MOVE_TO) {
if (type == TAG_GEOM_LINE)
elem.startLine();
else if (type == TAG_GEOM_POLYGON) {
isOuter = false;
elem.startHole();
}
}
continue;
}
// zigzag decoding
int s = ((val >>> 1) ^ -(val & 1));
if (even) {
// get x coordinate
even = false;
curX = lastX = lastX + s;
continue;
}
// get y coordinate and add point to geometry
num--;
even = true;
curY = lastY = lastY + s;
int dx = (curX - prevX);
int dy = (curY - prevY);
if (isPoly && num == 0 && cnt > 0){
prevX = curX;
prevY = curY;
// only add last point if it is di
int ppos = cnt * 2;
if (elem.points[elem.pointPos - ppos] != curX
|| elem.points[elem.pointPos - ppos + 1] != curY)
elem.addPoint(curX / mScale, curY / mScale);
lastClip = false;
continue;
}
if ((isPoint || cmd == MOVE_TO)
|| (dx > pixel || dx < -pixel)
|| (dy > pixel || dy < -pixel)
// hack to not clip at tile boundaries
|| (curX <= 0 || curX >= 4095)
|| (curY <= 0 || curY >= 4095)) {
prevX = curX;
prevY = curY;
elem.addPoint(curX / mScale, curY / mScale);
cnt++;
if (simplify && isOuter) {
if (curX < xmin)
xmin = curX;
if (curX > xmax)
xmax = curX;
if (curY < ymin)
ymin = curY;
if (curY > ymax)
ymax = curY;
}
lastClip = false;
continue;
}
lastClip = true;
}
if (isPoly && isOuter && simplify && !testBBox(xmax - xmin, ymax - ymin)) {
//Log.d(TAG, "skip small poly "+ elem.indexPos + " > "
// + (xmax - xmin) * (ymax - ymin));
elem.pointPos -= elem.index[elem.indexPos];
if (elem.indexPos > 0) {
elem.indexPos -= 3;
elem.index[elem.indexPos + 1] = -1;
} else {
elem.type = GeometryType.NONE;
}
return 0;
}
if (isLine && lastClip)
elem.addPoint(curX / mScale, curY / mScale);
return 1;
}
private static boolean testBBox(int dx, int dy) {
return dx * dy > 64 * 64;
}
private String decodeValue() throws IOException {
int bytes = decodeVarint32();
String value = null;
int end = position() + bytes;
while (position() < end) {
// read tag and wire type
int val = decodeVarint32();
if (val == 0)
break;
int tag = (val >> 3);
switch (tag) {
case TAG_VALUE_STRING:
value = decodeString();
break;
case TAG_VALUE_UINT:
value = String.valueOf(decodeVarint32());
break;
case TAG_VALUE_SINT:
value = String.valueOf(deZigZag(decodeVarint32()));
break;
case TAG_VALUE_LONG:
value = String.valueOf(decodeVarint64());
break;
case TAG_VALUE_FLOAT:
value = String.valueOf(decodeFloat());
break;
case TAG_VALUE_DOUBLE:
value = String.valueOf(decodeDouble());
break;
case TAG_VALUE_BOOL:
value = decodeBool() ? "yes" : "no";
break;
default:
break;
}
}
return value;
}
}

View File

@@ -0,0 +1,40 @@
/*
* Copyright 2013 Hannes Janetzek
*
* 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.oscim.tilesource.oscimap;
import java.net.URL;
import org.oscim.tilesource.ITileDataSource;
import org.oscim.tilesource.common.LwHttp;
import org.oscim.tilesource.common.PbfTileDataSource;
import org.oscim.tilesource.common.UrlTileSource;
/**
* Deprecated
*
*/
public class OSciMap1TileSource extends UrlTileSource {
@Override
public ITileDataSource getDataSource() {
return new TileDataSource(mUrl);
}
class TileDataSource extends PbfTileDataSource {
public TileDataSource(URL url) {
super(new TileDecoder());
mConn = new LwHttp(url, "application/osmtile", "osmtile", false);
}
}
}

View File

@@ -0,0 +1,958 @@
/*
* Copyright 2012 Hannes Janetzek
*
* 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.oscim.tilesource.oscimap;
import org.oscim.core.Tag;
public class Tags {
public final static int MAX = 654;
public final static int LIMIT = 1024;
private static final String s_limited = "limited".intern();
private static final String s_chain = "chain".intern();
private static final String s_viaduct = "viaduct".intern();
private static final String s_department_store = "department_store".intern();
private static final String s_factory = "factory".intern();
private static final String s_recreation_ground = "recreation_ground".intern();
private static final String s_nature_reserve = "nature_reserve".intern();
private static final String s_apartment = "apartment".intern();
private static final String s_preserved = "preserved".intern();
private static final String s_stationery = "stationery".intern();
private static final String s_gravel = "gravel".intern();
private static final String s_hill = "hill".intern();
private static final String s_water_well = "water_well".intern();
private static final String s_garden = "garden".intern();
private static final String s_permissive = "permissive".intern();
private static final String s_deli = "deli".intern();
private static final String s_industrial_retail = "industrial;retail".intern();
private static final String s_city_wall = "city_wall".intern();
private static final String s_artwork = "artwork".intern();
private static final String s_chapel = "chapel".intern();
private static final String s_school = "school".intern();
private static final String s_caravan_site = "caravan_site".intern();
private static final String s_reservoir_watershed = "reservoir_watershed".intern();
private static final String s_local_authority = "local_authority".intern();
private static final String s_miniature_golf = "miniature_golf".intern();
private static final String s_bus_stop = "bus_stop".intern();
private static final String s_convenience = "convenience".intern();
private static final String s_kissing_gate = "kissing_gate".intern();
private static final String s_subway = "subway".intern();
private static final String s_cutline = "cutline".intern();
private static final String s_disused = "disused".intern();
private static final String s_clothes = "clothes".intern();
private static final String s_bicycle = "bicycle".intern();
private static final String s_meadow = "meadow".intern();
private static final String s_fence = "fence".intern();
private static final String s_video = "video".intern();
private static final String s_monorail = "monorail".intern();
private static final String s_clock = "clock".intern();
private static final String s_dirt = "dirt".intern();
private static final String s_border_control = "border_control".intern();
private static final String s_access = "access".intern();
private static final String s_public = "public".intern();
private static final String s_fast_food = "fast_food".intern();
private static final String s_transportation = "transportation".intern();
private static final String s_commercial = "commercial".intern();
private static final String s_water = "water".intern();
private static final String s_beacon = "beacon".intern();
private static final String s_trunk = "trunk".intern();
private static final String s_path = "path".intern();
private static final String s_bicycle_rental = "bicycle_rental".intern();
private static final String s_miniature = "miniature".intern();
private static final String s_car_parts = "car_parts".intern();
private static final String s_light_rail = "light_rail".intern();
private static final String s_military = "military".intern();
private static final String s_bog = "bog".intern();
private static final String s_hiking = "hiking".intern();
private static final String s_lift_gate = "lift_gate".intern();
private static final String s_private = "private".intern();
private static final String s_county = "county".intern();
private static final String s_secondary_link = "secondary_link".intern();
private static final String s_marker = "marker".intern();
private static final String s_islet = "islet".intern();
private static final String s_holding_position = "holding_position".intern();
private static final String s_tertiary = "tertiary".intern();
private static final String s_water_park = "water_park".intern();
private static final String s_stream = "stream".intern();
private static final String s_hospital = "hospital".intern();
private static final String s_destination = "destination".intern();
private static final String s_MDF = "MDF".intern();
private static final String s_sports = "sports".intern();
private static final String s_vineyard = "vineyard".intern();
private static final String s_music = "music".intern();
private static final String s_6 = "6".intern();
private static final String s_entrance = "entrance".intern();
private static final String s_beauty = "beauty".intern();
private static final String s_give_way = "give_way".intern();
private static final String s_kiosk = "kiosk".intern();
private static final String s_stone = "stone".intern();
private static final String s_grass_paver = "grass_paver".intern();
private static final String s_deciduous = "deciduous".intern();
private static final String s_train = "train".intern();
private static final String s_organic = "organic".intern();
private static final String s_farmyard = "farmyard".intern();
private static final String s_riverbank = "riverbank".intern();
private static final String s_doityourself = "doityourself".intern();
private static final String s_town = "town".intern();
private static final String s_dog_park = "dog_park".intern();
private static final String s_village_green = "village_green".intern();
private static final String s_tunnel = "tunnel".intern();
private static final String s_car = "car".intern();
private static final String s_roof = "roof".intern();
private static final String s_mall = "mall".intern();
private static final String s_ferry_terminal = "ferry_terminal".intern();
private static final String s_cave_entrance = "cave_entrance".intern();
private static final String s_detached = "detached".intern();
private static final String s_concrete_plates = "concrete:plates".intern();
private static final String s_public_building = "public_building".intern();
private static final String s_buffer_stop = "buffer_stop".intern();
private static final String s_lock = "lock".intern();
private static final String s_dolphin = "dolphin".intern();
private static final String s_taxiway = "taxiway".intern();
private static final String s_hunting_stand = "hunting_stand".intern();
private static final String s_estate_agent = "estate_agent".intern();
private static final String s_station = "station".intern();
private static final String s_car_repair = "car_repair".intern();
private static final String s_dyke = "dyke".intern();
private static final String s_hangar = "hangar".intern();
private static final String s_information = "information".intern();
private static final String s_1 = "1".intern();
private static final String s_forest = "forest".intern();
private static final String s_gate = "gate".intern();
private static final String s_beach = "beach".intern();
private static final String s_laundry = "laundry".intern();
private static final String s_speed_camera = "speed_camera".intern();
private static final String s_staircase = "staircase".intern();
private static final String s_farm = "farm".intern();
private static final String s_stop = "stop".intern();
private static final String s_bump_gate = "bump_gate".intern();
private static final String s_motorway = "motorway".intern();
private static final String s_water_tower = "water_tower".intern();
private static final String s_abutters = "abutters".intern();
private static final String s_driving_school = "driving_school".intern();
private static final String s_natural = "natural".intern();
private static final String s_orchard = "orchard".intern();
private static final String s_wheelchair = "wheelchair".intern();
private static final String s_swimming_pool = "swimming_pool".intern();
private static final String s_switch = "switch".intern();
private static final String s_block = "block".intern();
private static final String s_turnstile = "turnstile".intern();
private static final String s_camp_site = "camp_site".intern();
private static final String s_shoes = "shoes".intern();
private static final String s_reservoir = "reservoir".intern();
private static final String s_pebblestone = "pebblestone".intern();
private static final String s_stile = "stile".intern();
private static final String s_embassy = "embassy".intern();
private static final String s_postal_code = "postal_code".intern();
private static final String s_retaining_wall = "retaining_wall".intern();
private static final String s_bridleway = "bridleway".intern();
private static final String s_pitch = "pitch".intern();
private static final String s_agricultural = "agricultural".intern();
private static final String s_post_office = "post_office".intern();
private static final String s_parking_fuel = "parking;fuel".intern();
private static final String s_bureau_de_change = "bureau_de_change".intern();
private static final String s_mini_roundabout = "mini_roundabout".intern();
private static final String s_hov = "hov".intern();
private static final String s_police = "police".intern();
private static final String s_courthouse = "courthouse".intern();
private static final String s_raceway = "raceway".intern();
private static final String s_kindergarten = "kindergarten".intern();
private static final String s_attraction = "attraction".intern();
private static final String s_marsh = "marsh".intern();
private static final String s_reservoir_covered = "reservoir_covered".intern();
private static final String s_petroleum_well = "petroleum_well".intern();
private static final String s_silo = "silo".intern();
private static final String s_toys = "toys".intern();
private static final String s_apron = "apron".intern();
private static final String s_halt = "halt".intern();
private static final String s_dam = "dam".intern();
private static final String s_golf_course = "golf_course".intern();
private static final String s_detour = "detour".intern();
private static final String s_tree_row = "tree_row".intern();
private static final String s_copyshop = "copyshop".intern();
private static final String s_milestone = "milestone".intern();
private static final String s_foot = "foot".intern();
private static final String s_tourism = "tourism".intern();
private static final String s_bank = "bank".intern();
private static final String s_dry_cleaning = "dry_cleaning".intern();
private static final String s_tram = "tram".intern();
private static final String s_trolleybus = "trolleybus".intern();
private static final String s_university = "university".intern();
private static final String s_hampshire_gate = "hampshire_gate".intern();
private static final String s_embankment = "embankment".intern();
private static final String s_rock = "rock".intern();
private static final String s_crossing = "crossing".intern();
private static final String s_volcano = "volcano".intern();
private static final String s_greengrocer = "greengrocer".intern();
private static final String s_kerb = "kerb".intern();
private static final String s_waste_disposal = "waste_disposal".intern();
private static final String s_grave_yard = "grave_yard".intern();
private static final String s_coniferous = "coniferous".intern();
private static final String s_house = "house".intern();
private static final String s_books = "books".intern();
private static final String s_neighbourhood = "neighbourhood".intern();
private static final String s_hostel = "hostel".intern();
private static final String s_alcohol = "alcohol".intern();
private static final String s_restricted = "restricted".intern();
private static final String s_motel = "motel".intern();
private static final String s_sand = "sand".intern();
private static final String s_fishmonger = "fishmonger".intern();
private static final String s_fountain = "fountain".intern();
private static final String s_playground = "playground".intern();
private static final String s_7 = "7".intern();
private static final String s_parking_aisle = "parking_aisle".intern();
private static final String s_protected_area = "protected_area".intern();
private static final String s_electronics = "electronics".intern();
private static final String s_Paved = "Paved".intern();
private static final String s_highway = "highway".intern();
private static final String s_fine_gravel = "fine_gravel".intern();
private static final String s_barrier = "barrier".intern();
private static final String s_hairdresser = "hairdresser".intern();
private static final String s_post_box = "post_box".intern();
private static final String s_pub = "pub".intern();
private static final String s_coastline = "coastline".intern();
private static final String s_marina = "marina".intern();
private static final String s_reedbed = "reedbed".intern();
private static final String s_biergarten = "biergarten".intern();
private static final String s_dismantled = "dismantled".intern();
private static final String s_farmland = "farmland".intern();
private static final String s_yard = "yard".intern();
private static final String s_route = "route".intern();
private static final String s_atm = "atm".intern();
private static final String s_place = "place".intern();
private static final String s_bus_station = "bus_station".intern();
private static final String s_retail = "retail".intern();
private static final String s_industrial = "industrial".intern();
private static final String s_municipality = "municipality".intern();
private static final String s_primary = "primary".intern();
private static final String s_nursing_home = "nursing_home".intern();
private static final String s_florist = "florist".intern();
private static final String s_ditch = "ditch".intern();
private static final String s_national_park = "national_park".intern();
private static final String s_city = "city".intern();
private static final String s_confectionery = "confectionery".intern();
private static final String s_service = "service".intern();
private static final String s_unknown = "unknown".intern();
private static final String s_cycle_barrier = "cycle_barrier".intern();
private static final String s_elevator = "elevator".intern();
private static final String s_2 = "2".intern();
private static final String s_car_rental = "car_rental".intern();
private static final String s_flagpole = "flagpole".intern();
private static final String s_cabin = "cabin".intern();
private static final String s_paved = "paved".intern();
private static final String s_guest_house = "guest_house".intern();
private static final String s_mobile_phone = "mobile_phone".intern();
private static final String s_lot = "lot".intern();
private static final String s_quarry = "quarry".intern();
private static final String s_train_station = "train_station".intern();
private static final String s_hotel = "hotel".intern();
private static final String s_park = "park".intern();
private static final String s_hut = "hut".intern();
private static final String s_dentist = "dentist".intern();
private static final String s_doctors = "doctors".intern();
private static final String s_greenhouse = "greenhouse".intern();
private static final String s_11 = "11".intern();
private static final String s_10 = "10".intern();
private static final String s_theme_park = "theme_park".intern();
private static final String s_tree = "tree".intern();
private static final String s_shower = "shower".intern();
private static final String s_siding = "siding".intern();
private static final String s_aeroway = "aeroway".intern();
private static final String s_emergency_access_point = "emergency_access_point"
.intern();
private static final String s_watermill = "watermill".intern();
private static final String s_college = "college".intern();
private static final String s_landuse = "landuse".intern();
private static final String s_tracktype = "tracktype".intern();
private static final String s_ferry = "ferry".intern();
private static final String s_bridge = "bridge".intern();
private static final String s_vacant = "vacant".intern();
private static final String s_cattle_grid = "cattle_grid".intern();
private static final String s_brownfield = "brownfield".intern();
private static final String s_allotments = "allotments".intern();
private static final String s_alley = "alley".intern();
private static final String s_pedestrian = "pedestrian".intern();
private static final String s_borough = "borough".intern();
private static final String s_bare_rock = "bare_rock".intern();
private static final String s_motorcycle = "motorcycle".intern();
private static final String s_bakery = "bakery".intern();
private static final String s_zoo = "zoo".intern();
private static final String s_scree = "scree".intern();
private static final String s_fire_station = "fire_station".intern();
private static final String s_theatre = "theatre".intern();
private static final String s_track = "track".intern();
private static final String s_reinforced_slope = "reinforced_slope".intern();
private static final String s_slipway = "slipway".intern();
private static final String s_mangrove = "mangrove".intern();
private static final String s_aerodrome = "aerodrome".intern();
private static final String s_byway = "byway".intern();
private static final String s_metal = "metal".intern();
private static final String s_swamp = "swamp".intern();
private static final String s_construction = "construction".intern();
private static final String s_grassland = "grassland".intern();
private static final String s_shop = "shop".intern();
private static final String s_soakhole = "soakhole".intern();
private static final String s_asphalt = "asphalt".intern();
private static final String s_social_facility = "social_facility".intern();
private static final String s_isolated_dwelling = "isolated_dwelling".intern();
private static final String s_hamlet = "hamlet".intern();
private static final String s_picnic_table = "picnic_table".intern();
private static final String s_artificial = "artificial".intern();
private static final String s_earth = "earth".intern();
private static final String s_grit_bin = "grit_bin".intern();
private static final String s_ground = "ground".intern();
private static final String s_groyne = "groyne".intern();
private static final String s_office = "office".intern();
private static final String s_state = "state".intern();
private static final String s_terminal = "terminal".intern();
private static final String s_wood = "wood".intern();
private static final String s_fuel = "fuel".intern();
private static final String s_8 = "8".intern();
private static final String s_garden_centre = "garden_centre".intern();
private static final String s_horse_riding = "horse_riding".intern();
private static final String s_viewpoint = "viewpoint".intern();
private static final String s_designated = "designated".intern();
private static final String s_leisure = "leisure".intern();
private static final String s_waste_basket = "waste_basket".intern();
private static final String s_hifi = "hifi".intern();
private static final String s_hedge = "hedge".intern();
private static final String s_spur = "spur".intern();
private static final String s_chimney = "chimney".intern();
private static final String s_secondary = "secondary".intern();
private static final String s_rest_area = "rest_area".intern();
private static final String s_bar = "bar".intern();
private static final String s_bay = "bay".intern();
private static final String s_common = "common".intern();
private static final String s_river = "river".intern();
private static final String s_ruins = "ruins".intern();
private static final String s_terrace = "terrace".intern();
private static final String s_art = "art".intern();
private static final String s_residental = "residental".intern();
private static final String s_newsagent = "newsagent".intern();
private static final String s_turntable = "turntable".intern();
private static final String s_computer = "computer".intern();
private static final String s_wetland = "wetland".intern();
private static final String s_driveway = "driveway".intern();
private static final String s_parking = "parking".intern();
private static final String s_compacted = "compacted".intern();
private static final String s_barn = "barn".intern();
private static final String s_alpine_hut = "alpine_hut".intern();
private static final String s_wire_fence = "wire_fence".intern();
private static final String s_unpaved = "unpaved".intern();
private static final String s_dormitory = "dormitory".intern();
private static final String s_mud = "mud".intern();
private static final String s_3 = "3".intern();
private static final String s_semi = "semi".intern();
private static final String s_boundary = "boundary".intern();
private static final String s_field_boundary = "field_boundary".intern();
private static final String s_beverages = "beverages".intern();
private static final String s_supermarket = "supermarket".intern();
private static final String s_store = "store".intern();
private static final String s_restaurant = "restaurant".intern();
private static final String s_region = "region".intern();
private static final String s_variety_store = "variety_store".intern();
private static final String s_saltmarsh = "saltmarsh".intern();
private static final String s_landform = "landform".intern();
private static final String s_helipad = "helipad".intern();
private static final String s_railway = "railway".intern();
private static final String s_greenhouse_horticulture = "greenhouse_horticulture"
.intern();
private static final String s_wall = "wall".intern();
private static final String s_recycling = "recycling".intern();
private static final String s_passing_place = "passing_place".intern();
private static final String s_church = "church".intern();
private static final String s_pharmacy = "pharmacy".intern();
private static final String s_lighthouse = "lighthouse".intern();
private static final String s_platform = "platform".intern();
private static final String s_cinema = "cinema".intern();
private static final String s_political = "political".intern();
private static final String s_stadium = "stadium".intern();
private static final String s_basin = "basin".intern();
private static final String s_gasometer = "gasometer".intern();
private static final String s_bicycle_parking = "bicycle_parking".intern();
private static final String s_bbq = "bbq".intern();
private static final String s_incline_steep = "incline_steep".intern();
private static final String s_drinking_water = "drinking_water".intern();
private static final String s_living_street = "living_street".intern();
private static final String s_chalet = "chalet".intern();
private static final String s_narrow_gauge = "narrow_gauge".intern();
private static final String s_prison = "prison".intern();
private static final String s_mine = "mine".intern();
private static final String s_level_crossing = "level_crossing".intern();
private static final String s_water_works = "water_works".intern();
private static final String s_street_lamp = "street_lamp".intern();
private static final String s_main = "main".intern();
private static final String s_tank = "tank".intern();
private static final String s_abandoned = "abandoned".intern();
private static final String s_ski = "ski".intern();
private static final String s_runway = "runway".intern();
private static final String s_parking_space = "parking_space".intern();
private static final String s_dirt_sand = "dirt/sand".intern();
private static final String s_salt_pond = "salt_pond".intern();
private static final String s_hedge_bank = "hedge_bank".intern();
private static final String s_amenity = "amenity".intern();
private static final String s_telephone = "telephone".intern();
private static final String s_surface = "surface".intern();
private static final String s_travel_agency = "travel_agency".intern();
private static final String s_hardware = "hardware".intern();
private static final String s_wastewater_plant = "wastewater_plant".intern();
private static final String s_waterway = "waterway".intern();
private static final String s_butcher = "butcher".intern();
private static final String s_surveillance = "surveillance".intern();
private static final String s_Dirt_Sand = "Dirt/Sand".intern();
private static final String s_9 = "9".intern();
private static final String s_windmill = "windmill".intern();
private static final String s_picnic_site = "picnic_site".intern();
private static final String s_rail = "rail".intern();
private static final String s_cement = "cement".intern();
private static final String s_sauna = "sauna".intern();
private static final String s_suburb = "suburb".intern();
private static final String s_waterfall = "waterfall".intern();
private static final String s_bunker = "bunker".intern();
private static final String s_ice_cream = "ice_cream".intern();
private static final String s_culvert = "culvert".intern();
private static final String s_drain = "drain".intern();
private static final String s_dock = "dock".intern();
private static final String s_glasshouse = "glasshouse".intern();
private static final String s_no = "no".intern();
private static final String s_well = "well".intern();
private static final String s_wet_meadow = "wet_meadow".intern();
private static final String s_concrete = "concrete".intern();
private static final String s_dismount = "dismount".intern();
private static final String s_vending_machine = "vending_machine".intern();
private static final String s_oneway = "oneway".intern();
private static final String s_taxi = "taxi".intern();
private static final String s_outdoor = "outdoor".intern();
private static final String s_proposed = "proposed".intern();
private static final String s_sally_port = "sally_port".intern();
private static final String s_photo = "photo".intern();
private static final String s_plant_nursery = "plant_nursery".intern();
private static final String s_clinic = "clinic".intern();
private static final String s_fishing = "fishing".intern();
private static final String s_yes = "yes".intern();
private static final String s_turning_circle = "turning_circle".intern();
private static final String s_toilets = "toilets".intern();
private static final String s_guard_rail = "guard_rail".intern();
private static final String s_townhall = "townhall".intern();
private static final String s_community_centre = "community_centre".intern();
private static final String s_residential = "residential".intern();
private static final String s_cemetery = "cemetery".intern();
private static final String s_survey_point = "survey_point".intern();
private static final String s_bench = "bench".intern();
private static final String s_4 = "4".intern();
private static final String s_bollard = "bollard".intern();
private static final String s_sports_centre = "sports_centre".intern();
private static final String s_paving_stones_30 = "paving_stones:30".intern();
private static final String s_administrative = "administrative".intern();
private static final String s_Building = "Building".intern();
private static final String s_customers = "customers".intern();
private static final String s_emergency = "emergency".intern();
private static final String s_motorway_junction = "motorway_junction".intern();
private static final String s_grade1 = "grade1".intern();
private static final String s_grade3 = "grade3".intern();
private static final String s_grade2 = "grade2".intern();
private static final String s_grade5 = "grade5".intern();
private static final String s_grade4 = "grade4".intern();
private static final String s_lock_gate = "lock_gate".intern();
private static final String s_furniture = "furniture".intern();
private static final String s_place_of_worship = "place_of_worship".intern();
private static final String s_optician = "optician".intern();
private static final String s_gift = "gift".intern();
private static final String s_parking_entrance = "parking_entrance".intern();
private static final String s_garage = "garage".intern();
private static final String s_tram_stop = "tram_stop".intern();
private static final String s_steps = "steps".intern();
private static final String s_tower = "tower".intern();
private static final String s_works = "works".intern();
private static final String s_shed = "shed".intern();
private static final String s_car_sharing = "car_sharing".intern();
private static final String s_apartments = "apartments".intern();
private static final String s_spring = "spring".intern();
private static final String s_village = "village".intern();
private static final String s_library = "library".intern();
private static final String s_emergency_access = "emergency_access".intern();
private static final String s_home = "home".intern();
private static final String s_farm_auxiliary = "farm_auxiliary".intern();
private static final String s_primary_link = "primary_link".intern();
private static final String s_toll_booth = "toll_booth".intern();
private static final String s_jewelry = "jewelry".intern();
private static final String s_pet = "pet".intern();
private static final String s_veterinary = "veterinary".intern();
private static final String s_man_made = "man_made".intern();
private static final String s_motorway_link = "motorway_link".intern();
private static final String s_offices = "offices".intern();
private static final String s_power = "power".intern();
private static final String s_weir = "weir".intern();
private static final String s_unsurfaced = "unsurfaced".intern();
private static final String s_tertiary_link = "tertiary_link".intern();
private static final String s_trunk_link = "trunk_link".intern();
private static final String s_tyres = "tyres".intern();
private static final String s_paving_stones = "paving_stones".intern();
private static final String s_pipeline = "pipeline".intern();
private static final String s_census = "census".intern();
private static final String s_incline = "incline".intern();
private static final String s_footway = "footway".intern();
private static final String s_drive_through = "drive-through".intern();
private static final String s_island = "island".intern();
private static final String s_monitoring_station = "monitoring_station".intern();
private static final String s_nightclub = "nightclub".intern();
private static final String s_unclassified = "unclassified".intern();
private static final String s_aquaculture = "aquaculture".intern();
private static final String s_mixed = "mixed".intern();
private static final String s_road = "road".intern();
private static final String s_greenfield = "greenfield".intern();
private static final String s_breakwater = "breakwater".intern();
private static final String s_services = "services".intern();
private static final String s_railway_crossing = "railway_crossing".intern();
private static final String s_residentiel1 = "residentiel1".intern();
private static final String s_canal = "canal".intern();
private static final String s__1 = "-1".intern();
private static final String s_ridge = "ridge".intern();
private static final String s_fabric = "fabric".intern();
private static final String s_museum = "museum".intern();
private static final String s_communications_tower = "communications_tower".intern();
private static final String s_semi_detached = "semi-detached".intern();
private static final String s_conservation = "conservation".intern();
private static final String s_way = "way".intern();
private static final String s_wood_fence = "wood_fence".intern();
private static final String s_manufacture = "manufacture".intern();
private static final String s_admin_level = "admin_level".intern();
private static final String s_building_concrete = "building_concrete".intern();
private static final String s_bus = "bus".intern();
private static final String s_collapsed = "collapsed".intern();
private static final String s_ford = "ford".intern();
private static final String s_delivery = "delivery".intern();
private static final String s_garages = "garages".intern();
private static final String s_funeral_directors = "funeral_directors".intern();
private static final String s_land = "land".intern();
private static final String s_interlock = "interlock".intern();
private static final String s_reef = "reef".intern();
private static final String s_crane = "crane".intern();
private static final String s_true = "true".intern();
private static final String s_storage_tank = "storage_tank".intern();
private static final String s_official = "official".intern();
private static final String s_subway_entrance = "subway_entrance".intern();
private static final String s_mtb = "mtb".intern();
private static final String s_grass = "grass".intern();
private static final String s_marketplace = "marketplace".intern();
private static final String s_rapids = "rapids".intern();
private static final String s_car_wash = "car_wash".intern();
private static final String s_general = "general".intern();
private static final String s_cafe = "cafe".intern();
private static final String s_locality = "locality".intern();
private static final String s_glacier = "glacier".intern();
private static final String s_storage = "storage".intern();
private static final String s_cycleway = "cycleway".intern();
private static final String s_forestry = "forestry".intern();
private static final String s_field = "field".intern();
private static final String s_5 = "5".intern();
private static final String s_arts_centre = "arts_centre".intern();
private static final String s_warehouse = "warehouse".intern();
private static final String s_chemist = "chemist".intern();
private static final String s_pier = "pier".intern();
private static final String s_scrub = "scrub".intern();
private static final String s_shelter = "shelter".intern();
private static final String s_emergency_phone = "emergency_phone".intern();
private static final String s_tidalflat = "tidalflat".intern();
private static final String s_cobblestone = "cobblestone".intern();
private static final String s_fell = "fell".intern();
private static final String s_peak = "peak".intern();
private static final String s_charging_station = "charging_station".intern();
private static final String s_cliff = "cliff".intern();
private static final String s_building = "building".intern();
private static final String s_fire_hydrant = "fire_hydrant".intern();
private static final String s_traffic_signals = "traffic_signals".intern();
private static final String s_heath = "heath".intern();
private static final String s_landfill = "landfill".intern();
private static final String s_mast = "mast".intern();
private static final String s_boutique = "boutique".intern();
private static final String s_boat_storage = "boat_storage".intern();
public static final Tag[] tags = {
new Tag(s_building, s_yes, false), new Tag(s_highway, s_residential, false),
new Tag(s_highway, s_service, false), new Tag(s_waterway, s_stream, false),
new Tag(s_highway, s_unclassified, false), new Tag(s_highway, s_track, false),
new Tag(s_oneway, s_yes, false), new Tag(s_natural, s_water, false),
new Tag(s_highway, s_footway, false), new Tag(s_access, s_private, false),
new Tag(s_highway, s_tertiary, false), new Tag(s_highway, s_path, false),
new Tag(s_highway, s_secondary, false), new Tag(s_landuse, s_forest, false),
new Tag(s_bridge, s_yes, false), new Tag(s_natural, s_tree, false),
new Tag(s_surface, s_paved, false), new Tag(s_natural, s_wood, false),
new Tag(s_highway, s_primary, false), new Tag(s_landuse, s_grass, false),
new Tag(s_landuse, s_residential, false), new Tag(s_surface, s_unpaved, false),
new Tag(s_highway, s_bus_stop, false), new Tag(s_surface, s_asphalt, false),
new Tag(s_bicycle, s_yes, false), new Tag(s_amenity, s_parking, false),
new Tag(s_place, s_locality, false), new Tag(s_railway, s_rail, false),
new Tag(s_service, s_parking_aisle, false),
new Tag(s_boundary, s_administrative, false),
new Tag(s_building, s_house, false), new Tag(s_place, s_village, false),
new Tag(s_natural, s_coastline, false), new Tag(s_tracktype, s_grade2, false),
new Tag(s_oneway, s_no, false), new Tag(s_service, s_driveway, false),
new Tag(s_highway, s_turning_circle, false), new Tag(s_place, s_hamlet, false),
new Tag(s_natural, s_wetland, false), new Tag(s_tracktype, s_grade3, false),
new Tag(s_waterway, s_river, false), new Tag(s_highway, s_cycleway, false),
new Tag(s_barrier, s_fence, false), new Tag(s_building, s_residential, false),
new Tag(s_amenity, s_school, false), new Tag(s_highway, s_crossing, false),
new Tag(s_admin_level, s_8, false), new Tag(s_highway, s_trunk, false),
new Tag(s_amenity, s_place_of_worship, false),
new Tag(s_landuse, s_farmland, false), new Tag(s_tracktype, s_grade1, false),
new Tag(s_highway, s_road, false), new Tag(s_landuse, s_farm, false),
new Tag(s_surface, s_gravel, false), new Tag(s_landuse, s_meadow, false),
new Tag(s_highway, s_motorway, false),
new Tag(s_highway, s_traffic_signals, false),
new Tag(s_building, s_hut, false), new Tag(s_highway, s_motorway_link, false),
new Tag(s_tracktype, s_grade4, false), new Tag(s_barrier, s_gate, false),
new Tag(s_highway, s_living_street, false), new Tag(s_bicycle, s_no, false),
new Tag(s_leisure, s_pitch, false), new Tag(s_tunnel, s_yes, false),
new Tag(s_surface, s_ground, false), new Tag(s_highway, s_steps, false),
new Tag(s_natural, s_land, false), new Tag(s_man_made, s_survey_point, false),
new Tag(s_tracktype, s_grade5, false), new Tag(s_waterway, s_ditch, false),
new Tag(s_leisure, s_park, false), new Tag(s_amenity, s_restaurant, false),
new Tag(s_barrier, s_wall, false), new Tag(s_waterway, s_riverbank, false),
new Tag(s_amenity, s_bench, false), new Tag(s_building, s_garage, false),
new Tag(s_natural, s_scrub, false), new Tag(s_highway, s_pedestrian, false),
new Tag(s_natural, s_peak, false), new Tag(s_building, s_entrance, false),
new Tag(s_landuse, s_reservoir, false), new Tag(s_access, s_yes, false),
new Tag(s_bicycle, s_designated, false),
new Tag(s_leisure, s_swimming_pool, false),
new Tag(s_landuse, s_farmyard, false),
new Tag(s_railway, s_level_crossing, false),
new Tag(s_building, s_apartments, false), new Tag(s_surface, s_grass, false),
new Tag(s_wheelchair, s_yes, false), new Tag(s_service, s_alley, false),
new Tag(s_landuse, s_industrial, false), new Tag(s_amenity, s_fuel, false),
new Tag(s_surface, s_dirt, false), new Tag(s_highway, s_trunk_link, false),
new Tag(s_waterway, s_drain, false), new Tag(s_barrier, s_hedge, false),
new Tag(s_amenity, s_grave_yard, false),
new Tag(s_tourism, s_information, false),
new Tag(s_shop, s_supermarket, false),
new Tag(s_highway, s_primary_link, false), new Tag(s_wood, s_deciduous, false),
new Tag(s_leisure, s_playground, false), new Tag(s_building, s_roof, false),
new Tag(s_building, s_industrial, false),
new Tag(s_amenity, s_post_box, false), new Tag(s_waterway, s_canal, false),
new Tag(s_barrier, s_bollard, false), new Tag(s_leisure, s_garden, false),
new Tag(s_wood, s_mixed, false), new Tag(s_landuse, s_cemetery, false),
new Tag(s_landuse, s_orchard, false), new Tag(s_shop, s_convenience, false),
new Tag(s_access, s_permissive, false), new Tag(s_surface, s_concrete, false),
new Tag(s_surface, s_paving_stones, false), new Tag(s_service, s_spur, false),
new Tag(s_building, s_garages, false), new Tag(s_amenity, s_bank, false),
new Tag(s_tourism, s_hotel, false), new Tag(s_access, s_no, false),
new Tag(s_amenity, s_fast_food, false), new Tag(s_man_made, s_pier, false),
new Tag(s_amenity, s_kindergarten, false),
new Tag(s_access, s_agricultural, false),
new Tag(s_surface, s_cobblestone, false), new Tag(s_wheelchair, s_no, false),
new Tag(s_amenity, s_cafe, false), new Tag(s_amenity, s_hospital, false),
new Tag(s_amenity, s_post_office, false),
new Tag(s_amenity, s_public_building, false),
new Tag(s_amenity, s_recycling, false),
new Tag(s_highway, s_street_lamp, false), new Tag(s_man_made, s_tower, false),
new Tag(s_waterway, s_dam, false), new Tag(s_amenity, s_pub, false),
new Tag(s_wood, s_coniferous, false), new Tag(s_access, s_destination, false),
new Tag(s_admin_level, s_6, false), new Tag(s_landuse, s_commercial, false),
new Tag(s_amenity, s_pharmacy, false), new Tag(s_railway, s_abandoned, false),
new Tag(s_service, s_yard, false), new Tag(s_place, s_island, false),
new Tag(s_oneway, s__1, false), new Tag(s_landuse, s_quarry, false),
new Tag(s_landuse, s_vineyard, false),
new Tag(s_highway, s_motorway_junction, false),
new Tag(s_railway, s_station, false), new Tag(s_landuse, s_allotments, false),
new Tag(s_barrier, s_lift_gate, false), new Tag(s_admin_level, s_10, false),
new Tag(s_amenity, s_telephone, false), new Tag(s_place, s_town, false),
new Tag(s_man_made, s_cutline, false), new Tag(s_place, s_suburb, false),
new Tag(s_aeroway, s_taxiway, false), new Tag(s_wheelchair, s_limited, false),
new Tag(s_highway, s_secondary_link, false),
new Tag(s_leisure, s_sports_centre, false),
new Tag(s_amenity, s_bicycle_parking, false),
new Tag(s_surface, s_sand, false), new Tag(s_highway, s_stop, false),
new Tag(s_man_made, s_works, false), new Tag(s_landuse, s_retail, false),
new Tag(s_amenity, s_fire_station, false), new Tag(s_service, s_siding, false),
new Tag(s_amenity, s_toilets, false), new Tag(s_bench, s_yes, false),
new Tag(s_oneway, s_1, false), new Tag(s_surface, s_compacted, false),
new Tag(s_landuse, s_basin, false), new Tag(s_amenity, s_police, false),
new Tag(s_railway, s_tram, false), new Tag(s_route, s_road, false),
new Tag(s_natural, s_cliff, false), new Tag(s_highway, s_construction, false),
new Tag(s_aeroway, s_aerodrome, false), new Tag(s_entrance, s_yes, false),
new Tag(s_man_made, s_storage_tank, false), new Tag(s_amenity, s_atm, false),
new Tag(s_tourism, s_attraction, false), new Tag(s_route, s_bus, false),
new Tag(s_shop, s_bakery, false), new Tag(s_tourism, s_viewpoint, false),
new Tag(s_amenity, s_swimming_pool, false), new Tag(s_natural, s_beach, false),
new Tag(s_tourism, s_picnic_site, false), new Tag(s_oneway, s_true, false),
new Tag(s_highway, s_bridleway, false), new Tag(s_tourism, s_camp_site, false),
new Tag(s_abutters, s_residential, false),
new Tag(s_leisure, s_nature_reserve, false),
new Tag(s_amenity, s_drinking_water, false), new Tag(s_shop, s_clothes, false),
new Tag(s_natural, s_heath, false),
new Tag(s_highway, s_mini_roundabout, false),
new Tag(s_landuse, s_construction, false),
new Tag(s_amenity, s_waste_basket, false),
new Tag(s_railway, s_platform, false), new Tag(s_amenity, s_townhall, false),
new Tag(s_shop, s_hairdresser, false), new Tag(s_amenity, s_shelter, false),
new Tag(s_admin_level, s_9, false),
new Tag(s_building, s_farm_auxiliary, false),
new Tag(s_amenity, s_library, false), new Tag(s_building, s_detached, false),
new Tag(s_admin_level, s_4, false), new Tag(s_landuse, s_village_green, false),
new Tag(s_barrier, s_stile, false), new Tag(s_landuse, s_garages, false),
new Tag(s_amenity, s_bar, false), new Tag(s_railway, s_buffer_stop, false),
new Tag(s_wetland, s_marsh, false), new Tag(s_tourism, s_museum, false),
new Tag(s_barrier, s_cycle_barrier, false), new Tag(s_route, s_bicycle, false),
new Tag(s_railway, s_tram_stop, false),
new Tag(s_amenity, s_parking_space, false),
new Tag(s_barrier, s_retaining_wall, false),
new Tag(s_landuse, s_recreation_ground, false),
new Tag(s_amenity, s_university, false),
new Tag(s_highway, s_tertiary_link, false),
new Tag(s_building, s_terrace, false), new Tag(s_shop, s_car_repair, false),
new Tag(s_amenity, s_hunting_stand, false),
new Tag(s_amenity, s_fountain, false), new Tag(s_man_made, s_pipeline, false),
new Tag(s_wetland, s_swamp, false), new Tag(s_shop, s_car, false),
new Tag(s_bench, s_no, false), new Tag(s_tunnel, s_culvert, false),
new Tag(s_building, s_school, false), new Tag(s_barrier, s_entrance, false),
new Tag(s_railway, s_disused, false), new Tag(s_railway, s_crossing, false),
new Tag(s_building, s_church, false),
new Tag(s_amenity, s_social_facility, false), new Tag(s_natural, s_bay, false),
new Tag(s_shop, s_kiosk, false), new Tag(s_amenity, s_vending_machine, false),
new Tag(s_route, s_hiking, false), new Tag(s_natural, s_spring, false),
new Tag(s_leisure, s_common, false), new Tag(s_railway, s_switch, false),
new Tag(s_waterway, s_rapids, false), new Tag(s_admin_level, s_7, false),
new Tag(s_leisure, s_stadium, false), new Tag(s_leisure, s_track, false),
new Tag(s_place, s_isolated_dwelling, false), new Tag(s_place, s_islet, false),
new Tag(s_waterway, s_weir, false), new Tag(s_amenity, s_doctors, false),
new Tag(s_access, s_designated, false),
new Tag(s_landuse, s_conservation, false),
new Tag(s_waterway, s_artificial, false),
new Tag(s_amenity, s_bus_station, false),
new Tag(s_leisure, s_golf_course, false),
new Tag(s_shop, s_doityourself, false), new Tag(s_building, s_service, false),
new Tag(s_tourism, s_guest_house, false), new Tag(s_aeroway, s_runway, false),
new Tag(s_place, s_city, false), new Tag(s_railway, s_subway, false),
new Tag(s_man_made, s_wastewater_plant, false),
new Tag(s_building, s_commercial, false), new Tag(s_railway, s_halt, false),
new Tag(s_amenity, s_emergency_phone, false),
new Tag(s_building, s_retail, false), new Tag(s_barrier, s_block, false),
new Tag(s_leisure, s_recreation_ground, false),
new Tag(s_access, s_forestry, false), new Tag(s_amenity, s_college, false),
new Tag(s_highway, s_platform, false), new Tag(s_access, s_unknown, false),
new Tag(s_man_made, s_water_tower, false),
new Tag(s_surface, s_pebblestone, false), new Tag(s_bridge, s_viaduct, false),
new Tag(s_shop, s_butcher, false), new Tag(s_shop, s_florist, false),
new Tag(s_boundary, s_landuse, false), new Tag(s_aeroway, s_helipad, false),
new Tag(s_building, s_hangar, false), new Tag(s_natural, s_glacier, false),
new Tag(s_highway, s_proposed, false), new Tag(s_shop, s_mall, false),
new Tag(s_barrier, s_toll_booth, false),
new Tag(s_amenity, s_fire_hydrant, false),
new Tag(s_building, s_manufacture, false), new Tag(s_building, s_farm, false),
new Tag(s_surface, s_wood, false), new Tag(s_amenity, s_car_wash, false),
new Tag(s_amenity, s_dentist, false), new Tag(s_natural, s_marsh, false),
new Tag(s_man_made, s_surveillance, false), new Tag(s_shop, s_bicycle, false),
new Tag(s_route, s_foot, false), new Tag(s_amenity, s_theatre, false),
new Tag(s_building, s_office, false), new Tag(s_railway, s_light_rail, false),
new Tag(s_man_made, s_petroleum_well, false),
new Tag(s_amenity, s_taxi, false), new Tag(s_building, s_greenhouse, false),
new Tag(s_landuse, s_brownfield, false),
new Tag(s_bicycle, s_permissive, false), new Tag(s_admin_level, s_2, false),
new Tag(s_aeroway, s_apron, false), new Tag(s_building, s_cabin, false),
new Tag(s_amenity, s_cinema, false), new Tag(s_access, s_customers, false),
new Tag(s_tourism, s_motel, false), new Tag(s_railway, s_narrow_gauge, false),
new Tag(s_amenity, s_marketplace, false), new Tag(s_shop, s_furniture, false),
new Tag(s_entrance, s_staircase, false), new Tag(s_tourism, s_artwork, false),
new Tag(s_natural, s_grassland, false), new Tag(s_shop, s_books, false),
new Tag(s_admin_level, s_5, false), new Tag(s_man_made, s_groyne, false),
new Tag(s_waterway, s_lock_gate, false),
new Tag(s_highway, s_emergency_access_point, false),
new Tag(s_natural, s_sand, false), new Tag(s_landuse, s_military, false),
new Tag(s_boundary, s_protected_area, false),
new Tag(s_amenity, s_community_centre, false),
new Tag(s_barrier, s_kissing_gate, false),
new Tag(s_highway, s_speed_camera, false),
new Tag(s_boundary, s_national_park, false),
new Tag(s_railway, s_subway_entrance, false),
new Tag(s_man_made, s_silo, false), new Tag(s_shop, s_alcohol, false),
new Tag(s_highway, s_give_way, false), new Tag(s_leisure, s_slipway, false),
new Tag(s_shop, s_electronics, false), new Tag(s_bicycle, s_dismount, false),
new Tag(s_leisure, s_marina, false), new Tag(s_entrance, s_main, false),
new Tag(s_boundary, s_postal_code, false),
new Tag(s_landuse, s_greenhouse_horticulture, false),
new Tag(s_highway, s_milestone, false),
new Tag(s_natural, s_cave_entrance, false),
new Tag(s_landuse, s_landfill, false), new Tag(s_shop, s_chemist, false),
new Tag(s_shop, s_shoes, false), new Tag(s_barrier, s_cattle_grid, false),
new Tag(s_landuse, s_railway, false), new Tag(s_tourism, s_hostel, false),
new Tag(s_tourism, s_chalet, false), new Tag(s_place, s_county, false),
new Tag(s_shop, s_department_store, false), new Tag(s_highway, s_ford, false),
new Tag(s_natural, s_scree, false), new Tag(s_landuse, s_greenfield, false),
new Tag(s_amenity, s_nursing_home, false),
new Tag(s_barrier, s_wire_fence, false),
new Tag(s_access, s_restricted, false),
new Tag(s_man_made, s_reservoir_covered, false),
new Tag(s_amenity, s_bicycle_rental, false), new Tag(s_man_made, s_MDF, false),
new Tag(s_man_made, s_water_well, false), new Tag(s_landuse, s_field, false),
new Tag(s_landuse, s_wood, false), new Tag(s_shop, s_hardware, false),
new Tag(s_tourism, s_alpine_hut, false), new Tag(s_natural, s_tree_row, false),
new Tag(s_tourism, s_caravan_site, false), new Tag(s_bridge, s_no, false),
new Tag(s_wetland, s_bog, false), new Tag(s_amenity, s_courthouse, false),
new Tag(s_route, s_ferry, false), new Tag(s_barrier, s_city_wall, false),
new Tag(s_amenity, s_veterinary, false), new Tag(s_shop, s_jewelry, false),
new Tag(s_building, s_transportation, false),
new Tag(s_amenity, s_arts_centre, false),
new Tag(s_bicycle, s_official, false), new Tag(s_shop, s_optician, false),
new Tag(s_shop, s_yes, false), new Tag(s_building, s_collapsed, false),
new Tag(s_shop, s_garden_centre, false), new Tag(s_man_made, s_chimney, false),
new Tag(s_man_made, s_mine, false), new Tag(s_bench, s_unknown, false),
new Tag(s_railway, s_preserved, false), new Tag(s_building, s_public, false),
new Tag(s_amenity, s_ferry_terminal, false),
new Tag(s_highway, s_raceway, false), new Tag(s_natural, s_rock, false),
new Tag(s_tunnel, s_no, false), new Tag(s_building, s_university, false),
new Tag(s_shop, s_beverages, false),
new Tag(s_amenity, s_waste_disposal, false),
new Tag(s_building, s_warehouse, false),
new Tag(s_leisure, s_water_park, false), new Tag(s_shop, s_gift, false),
new Tag(s_place, s_farm, false), new Tag(s_wetland, s_tidalflat, false),
new Tag(s_waterway, s_waterfall, false), new Tag(s_man_made, s_dolphin, false),
new Tag(s_service, s_drive_through, false),
new Tag(s_amenity, s_nightclub, false), new Tag(s_building, s_shed, false),
new Tag(s_shop, s_greengrocer, false), new Tag(s_natural, s_fell, false),
new Tag(s_wetland, s_wet_meadow, false), new Tag(s_aeroway, s_gate, false),
new Tag(s_shop, s_computer, false), new Tag(s_man_made, s_lighthouse, false),
new Tag(s_wetland, s_reedbed, false), new Tag(s_man_made, s_breakwater, false),
new Tag(s_surface, s_Dirt_Sand, false), new Tag(s_barrier, s_ditch, false),
new Tag(s_barrier, s_yes, false), new Tag(s_amenity, s_biergarten, false),
new Tag(s_shop, s_mobile_phone, false), new Tag(s_route, s_mtb, false),
new Tag(s_amenity, s_grit_bin, false), new Tag(s_amenity, s_bbq, false),
new Tag(s_shop, s_sports, false), new Tag(s_barrier, s_wood_fence, false),
new Tag(s_entrance, s_home, false), new Tag(s_shop, s_laundry, false),
new Tag(s_man_made, s_gasometer, false),
new Tag(s_barrier, s_embankment, false), new Tag(s_shop, s_toys, false),
new Tag(s_wetland, s_saltmarsh, false), new Tag(s_waterway, s_soakhole, false),
new Tag(s_shop, s_travel_agency, false),
new Tag(s_man_made, s_water_works, false), new Tag(s_route, s_railway, false),
new Tag(s_amenity, s_prison, false), new Tag(s_highway, s_rest_area, false),
new Tag(s_shop, s_stationery, false), new Tag(s_admin_level, s_11, false),
new Tag(s_building, s_train_station, false),
new Tag(s_building, s_storage_tank, false),
new Tag(s_man_made, s_windmill, false), new Tag(s_shop, s_beauty, false),
new Tag(s_building, s_semi, false), new Tag(s_highway, s_services, false),
new Tag(s_bicycle, s_private, false), new Tag(s_route, s_ski, false),
new Tag(s_service, s_emergency_access, false),
new Tag(s_building, s_factory, false),
new Tag(s_man_made, s_reinforced_slope, false),
new Tag(s_amenity, s_car_sharing, false), new Tag(s_surface, s_earth, false),
new Tag(s_shop, s_hifi, false), new Tag(s_amenity, s_car_rental, false),
new Tag(s_barrier, s_hedge_bank, false),
new Tag(s_shop, s_confectionery, false), new Tag(s_aeroway, s_terminal, false),
new Tag(s_highway, s_passing_place, false),
new Tag(s_building, s_building, false), new Tag(s_man_made, s_dyke, false),
new Tag(s_building, s_construction, false), new Tag(s_building, s_shop, false),
new Tag(s_natural, s_reef, false), new Tag(s_landuse, s_aquaculture, false),
new Tag(s_shop, s_dry_cleaning, false), new Tag(s_amenity, s_embassy, false),
new Tag(s_shop, s_newsagent, false), new Tag(s_landuse, s_salt_pond, false),
new Tag(s_railway, s_spur, false), new Tag(s_wheelchair, s_unknown, false),
new Tag(s_tourism, s_zoo, false), new Tag(s_man_made, s_waterway, false),
new Tag(s_surface, s_fine_gravel, false), new Tag(s_shop, s_motorcycle, false),
new Tag(s_building, s_Building, false),
new Tag(s_railway, s_construction, false),
new Tag(s_place, s_neighbourhood, false), new Tag(s_route, s_train, false),
new Tag(s_building, s_no, false), new Tag(s_natural, s_mud, false),
new Tag(s_place, s_region, false),
new Tag(s_landuse, s_reservoir_watershed, false),
new Tag(s_boundary, s_marker, false), new Tag(s_man_made, s_beacon, false),
new Tag(s_shop, s_outdoor, false), new Tag(s_access, s_public, false),
new Tag(s_abutters, s_industrial, false), new Tag(s_building, s_barn, false),
new Tag(s_leisure, s_picnic_table, false),
new Tag(s_building, s_hospital, false), new Tag(s_access, s_official, false),
new Tag(s_shop, s_variety_store, false), new Tag(s_man_made, s_crane, false),
new Tag(s_amenity, s_parking_fuel, false), new Tag(s_route, s_tram, false),
new Tag(s_tourism, s_theme_park, false), new Tag(s_shop, s_pet, false),
new Tag(s_building, s_kindergarten, false),
new Tag(s_man_made, s_storage, false), new Tag(s_man_made, s_mast, false),
new Tag(s_amenity, s_parking_entrance, false),
new Tag(s_amenity, s_clock, false),
new Tag(s_landuse, s_industrial_retail, false),
new Tag(s_shop, s_video, false), new Tag(s_access, s_delivery, false),
new Tag(s_amenity, s_driving_school, false), new Tag(s_service, s_yes, false),
new Tag(s_natural, s_bare_rock, false), new Tag(s_building, s_chapel, false),
new Tag(s_natural, s_volcano, false), new Tag(s_waterway, s_dock, false),
new Tag(s_building, s_dormitory, false),
new Tag(s_amenity, s_boat_storage, false), new Tag(s_man_made, s_tank, false),
new Tag(s_man_made, s_flagpole, false),
new Tag(s_surface, s_grass_paver, false), new Tag(s_shop, s_organic, false),
new Tag(s_natural, s_landform, false), new Tag(s_highway, s_unsurfaced, false),
new Tag(s_route, s_power, false), new Tag(s_surface, s_mud, false),
new Tag(s_building, s_building_concrete, false),
new Tag(s_abutters, s_retail, false), new Tag(s_building, s_store, false),
new Tag(s_shop, s_vacant, false), new Tag(s_leisure, s_miniature_golf, false),
new Tag(s_man_made, s_monitoring_station, false),
new Tag(s_natural, s_waterfall, false), new Tag(s_aeroway, s_hangar, false),
new Tag(s_shop, s_boutique, false), new Tag(s_route, s_detour, false),
new Tag(s_building, s_way, false), new Tag(s_railway, s_stop, false),
new Tag(s_amenity, s_ice_cream, false), new Tag(s_building, s_storage, false),
new Tag(s_shop, s_car_parts, false), new Tag(s_natural, s_ridge, false),
new Tag(s_shop, s_tyres, false), new Tag(s_railway, s_dismantled, false),
new Tag(s_amenity, s_shop, false), new Tag(s_landuse, s_plant_nursery, false),
new Tag(s_building, s_residentiel1, false),
new Tag(s_barrier, s_field_boundary, false),
new Tag(s_barrier, s_border_control, false),
new Tag(s_surface, s_Paved, false), new Tag(s_barrier, s_sally_port, false),
new Tag(s_amenity, s_bureau_de_change, false),
new Tag(s_leisure, s_fishing, false),
new Tag(s_amenity, s_charging_station, false),
new Tag(s_building, s_supermarket, false), new Tag(s_highway, s_stile, false),
new Tag(s_amenity, s_sauna, false), new Tag(s_place, s_municipality, false),
new Tag(s_building, s_hotel, false), new Tag(s_surface, s_metal, false),
new Tag(s_highway, s_incline_steep, false),
new Tag(s_shop, s_estate_agent, false), new Tag(s_natural, s_grass, false),
new Tag(s_shop, s_pharmacy, false),
new Tag(s_surface, s_concrete_plates, false),
new Tag(s_shop, s_copyshop, false),
new Tag(s_surface, s_paving_stones_30, false),
new Tag(s_surface, s_interlock, false), new Tag(s_access, s_hov, false),
new Tag(s_highway, s_elevator, false),
new Tag(s_boundary, s_local_authority, false),
new Tag(s_man_made, s_communications_tower, false),
new Tag(s_shop, s_deli, false), new Tag(s_barrier, s_turnstile, false),
new Tag(s_building, s_offices, false), new Tag(s_building, s_bunker, false),
new Tag(s_natural, s_stone, false),
new Tag(s_railway, s_railway_crossing, false),
new Tag(s_leisure, s_dog_park, false),
new Tag(s_building, s_semi_detached, false),
new Tag(s_man_made, s_watermill, false), new Tag(s_route, s_trolleybus, false),
new Tag(s_admin_level, s_3, false), new Tag(s_building, s_block, false),
new Tag(s_barrier, s_guard_rail, false), new Tag(s_bicycle, s_unknown, false),
new Tag(s_highway, s_abandoned, false), new Tag(s_surface, s_dirt_sand, false),
new Tag(s_barrier, s_chain, false), new Tag(s_barrier, s_bump_gate, false),
new Tag(s_building, s_residental, false), new Tag(s_surface, s_cement, false),
new Tag(s_man_made, s_embankment, false), new Tag(s_building, s_ruins, false),
new Tag(s_highway, s_incline, false), new Tag(s_abutters, s_commercial, false),
new Tag(s_barrier, s_hampshire_gate, false), new Tag(s_shop, s_music, false),
new Tag(s_shop, s_funeral_directors, false),
new Tag(s_wetland, s_mangrove, false), new Tag(s_place, s_borough, false),
new Tag(s_building, s_apartment, false), new Tag(s_boundary, s_census, false),
new Tag(s_barrier, s_kerb, false), new Tag(s_building, s_glasshouse, false),
new Tag(s_aeroway, s_holding_position, false),
new Tag(s_shop, s_general, false), new Tag(s_building, s_tank, false),
new Tag(s_railway, s_monorail, false), new Tag(s_service, s_parking, false),
new Tag(s_place, s_state, false), new Tag(s_railway, s_proposed, false),
new Tag(s_shop, s_art, false), new Tag(s_natural, s_hill, false),
new Tag(s_railway, s_turntable, false), new Tag(s_tourism, s_cabin, false),
new Tag(s_shop, s_photo, false), new Tag(s_boundary, s_lot, false),
new Tag(s_shop, s_fishmonger, false), new Tag(s_amenity, s_clinic, false),
new Tag(s_boundary, s_political, false), new Tag(s_man_made, s_well, false),
new Tag(s_highway, s_byway, false), new Tag(s_leisure, s_horse_riding, false),
new Tag(s_service, s_bus, false), new Tag(s_building, s_tower, false),
new Tag(s_entrance, s_service, false), new Tag(s_shop, s_fabric, false),
new Tag(s_railway, s_miniature, false), new Tag(s_abutters, s_mixed, false),
new Tag(s_surface, s_stone, false), new Tag(s_access, s_emergency, false),
new Tag(s_landuse, s_mine, false), new Tag(s_amenity, s_shower, false),
new Tag(s_waterway, s_lock, false)
};
}

View File

@@ -0,0 +1,449 @@
/*
* Copyright 2013
*
* 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.oscim.tilesource.oscimap;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import org.oscim.core.GeometryBuffer.GeometryType;
import org.oscim.core.MapElement;
import org.oscim.core.Tag;
import org.oscim.core.Tile;
import org.oscim.tilesource.common.PbfDecoder;
import org.oscim.tilesource.ITileDataSink;
import android.util.Log;
public class TileDecoder extends PbfDecoder {
private final static String TAG = TileDecoder.class.getName();
private final static float REF_TILE_SIZE = 4096.0f;
private static final int TAG_TILE_TAGS = 1;
private static final int TAG_TILE_WAYS = 2;
private static final int TAG_TILE_POLY = 3;
private static final int TAG_TILE_NODES = 4;
private static final int TAG_WAY_TAGS = 11;
private static final int TAG_WAY_INDEX = 12;
private static final int TAG_WAY_COORDS = 13;
private static final int TAG_WAY_LAYER = 21;
private static final int TAG_WAY_NUM_TAGS = 1;
private static final int TAG_WAY_NUM_INDICES = 2;
private static final int TAG_WAY_NUM_COORDS = 3;
private static final int TAG_NODE_TAGS = 11;
private static final int TAG_NODE_COORDS = 12;
private static final int TAG_NODE_LAYER = 21;
private static final int TAG_NODE_NUM_TAGS = 1;
private static final int TAG_NODE_NUM_COORDS = 2;
private int MAX_TILE_TAGS = 100;
private Tag[] curTags = new Tag[MAX_TILE_TAGS];
private int mCurTagCnt;
private ITileDataSink mSink;
private float mScale;
private Tile mTile;
private final MapElement mElem;
TileDecoder() {
mElem = new MapElement();
}
@Override
public boolean decode(Tile tile, ITileDataSink sink, InputStream is, int contentLength)
throws IOException {
setInputStream(is, contentLength);
mTile = tile;
mSink = sink;
mScale = REF_TILE_SIZE / Tile.SIZE;
return decode();
}
private static final int MAX_TAGS_CACHE = 100;
private static Map<String, Tag> tagHash = Collections
.synchronizedMap(new LinkedHashMap<String, Tag>(
MAX_TAGS_CACHE, 0.75f, true) {
private static final long serialVersionUID = 1L;
@Override
protected boolean removeEldestEntry(Entry<String, Tag> e) {
if (size() < MAX_TAGS_CACHE)
return false;
return true;
}
});
private boolean decode() throws IOException {
int val;
mCurTagCnt = 0;
while (hasData() && (val = decodeVarint32()) > 0) {
// read tag and wire type
int tag = (val >> 3);
switch (tag) {
case TAG_TILE_TAGS:
decodeTileTags();
break;
case TAG_TILE_WAYS:
decodeTileWays(false);
break;
case TAG_TILE_POLY:
decodeTileWays(true);
break;
case TAG_TILE_NODES:
decodeTileNodes();
break;
default:
Log.d(TAG, "invalid type for tile: " + tag);
return false;
}
}
return true;
}
private boolean decodeTileTags() throws IOException {
String tagString = decodeString();
if (tagString == null || tagString.length() == 0) {
curTags[mCurTagCnt++] = new Tag(Tag.TAG_KEY_NAME, "...");
return false;
}
Tag tag = tagHash.get(tagString);
if (tag == null) {
if (tagString.startsWith(Tag.TAG_KEY_NAME))
tag = new Tag(Tag.TAG_KEY_NAME, tagString.substring(5), false);
else
tag = new Tag(tagString);
tagHash.put(tagString, tag);
}
if (mCurTagCnt >= MAX_TILE_TAGS) {
MAX_TILE_TAGS = mCurTagCnt + 10;
Tag[] tmp = new Tag[MAX_TILE_TAGS];
System.arraycopy(curTags, 0, tmp, 0, mCurTagCnt);
curTags = tmp;
}
curTags[mCurTagCnt++] = tag;
return true;
}
private boolean decodeTileWays(boolean polygon) throws IOException {
int bytes = decodeVarint32();
int end = position() + bytes;
int indexCnt = 0;
int tagCnt = 0;
int coordCnt = 0;
int layer = 5;
Tag[] tags = null;
boolean fail = false;
while (position() < end) {
// read tag and wire type
int val = decodeVarint32();
if (val == 0)
break;
int tag = (val >> 3);
switch (tag) {
case TAG_WAY_TAGS:
tags = decodeWayTags(tagCnt);
break;
case TAG_WAY_INDEX:
//index =
decodeWayIndices(indexCnt);
break;
case TAG_WAY_COORDS:
if (coordCnt == 0) {
Log.d(TAG, mTile + " no coordinates");
}
mElem.ensurePointSize(coordCnt, false);
int cnt = decodeInterleavedPoints(mElem.points, mScale);
if (cnt != coordCnt) {
Log.d(TAG, mTile + " wrong number of coordintes "
+ coordCnt + "/" + cnt);
fail = true;
}
break;
case TAG_WAY_LAYER:
layer = decodeVarint32();
break;
case TAG_WAY_NUM_TAGS:
tagCnt = decodeVarint32();
break;
case TAG_WAY_NUM_INDICES:
indexCnt = decodeVarint32();
break;
case TAG_WAY_NUM_COORDS:
coordCnt = decodeVarint32();
break;
default:
Log.d(TAG, "X invalid type for way: " + tag);
}
}
if (fail || tags == null || indexCnt == 0 || tagCnt == 0) {
Log.d(TAG, "failed reading way: bytes:" + bytes + " index:"
+ (tags != null ? tags.toString() : "...") + " "
+ indexCnt + " " + coordCnt + " " + tagCnt);
return false;
}
// FIXME, remove all tiles from cache then remove this below
//if (layer == 0)
// layer = 5;
mElem.type = polygon ? GeometryType.POLY : GeometryType.LINE;
mElem.set(tags, layer);
mSink.process(mElem);
return true;
}
private boolean decodeTileNodes() throws IOException {
int bytes = decodeVarint32();
int end = position() + bytes;
int tagCnt = 0;
int coordCnt = 0;
byte layer = 0;
Tag[] tags = null;
while (position() < end) {
// read tag and wire type
int val = decodeVarint32();
if (val == 0)
break;
int tag = (val >> 3);
switch (tag) {
case TAG_NODE_TAGS:
tags = decodeWayTags(tagCnt);
break;
case TAG_NODE_COORDS:
int cnt = decodeNodeCoordinates(coordCnt, layer, tags);
if (cnt != coordCnt) {
Log.d(TAG, "X wrong number of coordintes");
return false;
}
break;
case TAG_NODE_LAYER:
layer = (byte) decodeVarint32();
break;
case TAG_NODE_NUM_TAGS:
tagCnt = decodeVarint32();
break;
case TAG_NODE_NUM_COORDS:
coordCnt = decodeVarint32();
break;
default:
Log.d(TAG, "X invalid type for node: " + tag);
}
}
return true;
}
private int decodeNodeCoordinates(int numNodes, byte layer, Tag[] tags)
throws IOException {
int bytes = decodeVarint32();
fillBuffer(bytes);
int cnt = 0;
int end = position() + bytes;
// read repeated sint32
int lastX = 0;
int lastY = 0;
float[] coords = mElem.ensurePointSize(numNodes, false);
while (position() < end && cnt < numNodes) {
int lon = deZigZag(decodeVarint32());
int lat = deZigZag(decodeVarint32());
lastX = lon + lastX;
lastY = lat + lastY;
coords[cnt++] = lastX / mScale;
coords[cnt++] = Tile.SIZE - lastY / mScale;
}
mElem.index[0] = (short) numNodes;
mElem.type = GeometryType.POINT;
mElem.set(tags, layer);
mSink.process(mElem);
return cnt;
}
private Tag[] decodeWayTags(int tagCnt) throws IOException {
int bytes = decodeVarint32();
Tag[] tags = new Tag[tagCnt];
int cnt = 0;
int end = position() + bytes;
int max = mCurTagCnt;
while (position() < end) {
int tagNum = decodeVarint32();
if (tagNum < 0 || cnt == tagCnt) {
Log.d(TAG, "NULL TAG: " + mTile + " invalid tag:" + tagNum
+ " " + tagCnt + "/" + cnt);
} else {
if (tagNum < Tags.MAX)
tags[cnt++] = Tags.tags[tagNum];
else {
tagNum -= Tags.LIMIT;
if (tagNum >= 0 && tagNum < max) {
// Log.d(TAG, "variable tag: " + curTags[tagNum]);
tags[cnt++] = curTags[tagNum];
} else {
Log.d(TAG, "NULL TAG: " + mTile + " could find tag:"
+ tagNum + " " + tagCnt + "/" + cnt);
}
}
}
}
if (tagCnt != cnt)
Log.d(TAG, "NULL TAG: " + mTile);
return tags;
}
private int decodeWayIndices(int indexCnt) throws IOException {
mElem.ensureIndexSize(indexCnt, false);
decodeVarintArray(indexCnt, mElem.index);
short[] index = mElem.index;
int coordCnt = 0;
for (int i = 0; i < indexCnt; i++) {
coordCnt += index[i];
index[i] *= 2;
}
// set end marker
if (indexCnt < index.length)
index[indexCnt] = -1;
return coordCnt;
}
@Override
protected int decodeInterleavedPoints(float[] coords, float scale)
throws IOException {
int bytes = decodeVarint32();
fillBuffer(bytes);
int cnt = 0;
int lastX = 0;
int lastY = 0;
boolean even = true;
byte[] buf = buffer;
int pos = bufferPos;
int end = pos + bytes;
int val;
while (pos < end) {
if (buf[pos] >= 0) {
val = buf[pos++];
} else if (buf[pos + 1] >= 0) {
val = (buf[pos++] & 0x7f)
| buf[pos++] << 7;
} else if (buf[pos + 2] >= 0) {
val = (buf[pos++] & 0x7f)
| (buf[pos++] & 0x7f) << 7
| (buf[pos++]) << 14;
} else if (buf[pos + 3] >= 0) {
val = (buf[pos++] & 0x7f)
| (buf[pos++] & 0x7f) << 7
| (buf[pos++] & 0x7f) << 14
| (buf[pos++]) << 21;
} else {
val = (buf[pos++] & 0x7f)
| (buf[pos++] & 0x7f) << 7
| (buf[pos++] & 0x7f) << 14
| (buf[pos++] & 0x7f) << 21
| (buf[pos]) << 28;
if (buf[pos++] < 0)
throw INVALID_VARINT;
}
// zigzag decoding
int s = ((val >>> 1) ^ -(val & 1));
if (even) {
lastX = lastX + s;
coords[cnt++] = lastX / scale;
even = false;
} else {
lastY = lastY + s;
coords[cnt++] = Tile.SIZE - lastY / scale;
even = true;
}
}
if (pos != bufferPos + bytes)
throw INVALID_PACKED_SIZE;
bufferPos = pos;
// return number of points read
return cnt;
}
}

View File

@@ -0,0 +1,343 @@
/*
* Copyright 2013 Hannes Janetzek
*
* 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.oscim.tilesource.oscimap2;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Arrays;
import org.oscim.core.GeometryBuffer.GeometryType;
import org.oscim.core.MapElement;
import org.oscim.core.Tag;
import org.oscim.core.Tile;
import org.oscim.tilesource.ITileDataSink;
import org.oscim.tilesource.ITileDataSource;
import org.oscim.tilesource.common.LwHttp;
import org.oscim.tilesource.common.PbfDecoder;
import org.oscim.tilesource.common.PbfTileDataSource;
import org.oscim.tilesource.common.UrlTileSource;
import android.util.Log;
public class OSciMap2TileSource extends UrlTileSource {
@Override
public ITileDataSource getDataSource() {
return new TileDataSource(mUrl);
}
class TileDataSource extends PbfTileDataSource {
public TileDataSource(URL url) {
super(new TileDecoder());
mConn = new LwHttp(url, "application/osmtile", "osmtile", false);
}
}
static class TileDecoder extends PbfDecoder {
private final static String TAG = TileDecoder.class.getName();
private static final int TAG_TILE_NUM_TAGS = 1;
private static final int TAG_TILE_TAG_KEYS = 2;
private static final int TAG_TILE_TAG_VALUES = 3;
private static final int TAG_TILE_LINE = 11;
private static final int TAG_TILE_POLY = 12;
private static final int TAG_TILE_POINT = 13;
// private static final int TAG_TILE_LABEL = 21;
// private static final int TAG_TILE_WATER = 31;
private static final int TAG_ELEM_NUM_INDICES = 1;
private static final int TAG_ELEM_TAGS = 11;
private static final int TAG_ELEM_INDEX = 12;
private static final int TAG_ELEM_COORDS = 13;
private static final int TAG_ELEM_LAYER = 21;
private static final int TAG_ELEM_HEIGHT = 31;
private static final int TAG_ELEM_MIN_HEIGHT = 32;
private static final int TAG_ELEM_PRIORITY = 41;
private short[] mSArray = new short[100];
private final Tag[] mTmpTags = new Tag[20];
private final Tag[][] mElementTags;
private final int MAX_TILE_TAGS = 100;
private Tag[] curTags = new Tag[MAX_TILE_TAGS];
private int mCurTagCnt;
// scale coordinates to tile size
private final static float REF_TILE_SIZE = 4096.0f;
private float mScale;
private Tile mTile;
private final MapElement mElem;
private ITileDataSink mMapDataSink;
TileDecoder() {
mElem = new MapElement();
// reusable tag set
Tag[][] tags = new Tag[10][];
for (int i = 0; i < 10; i++)
tags[i] = new Tag[i + 1];
mElementTags = tags;
}
@Override
public boolean decode(Tile tile, ITileDataSink sink, InputStream is, int contentLength)
throws IOException {
int byteCount = readUnsignedInt(is, buffer);
Log.d(TAG, tile + " contentLength:" + byteCount);
if (byteCount < 0) {
Log.d(TAG, "invalid contentLength: " + byteCount);
return false;
}
setInputStream(is, byteCount);
mTile = tile;
mMapDataSink = sink;
mScale = REF_TILE_SIZE / Tile.SIZE;
mCurTagCnt = 0;
int val;
int numTags = 0;
while (hasData() && (val = decodeVarint32()) > 0) {
// read tag and wire type
int tag = (val >> 3);
switch (tag) {
case TAG_TILE_NUM_TAGS:
numTags = decodeVarint32();
if (numTags > curTags.length)
curTags = new Tag[numTags];
break;
case TAG_TILE_TAG_KEYS:
int len = numTags;
if (mSArray.length < len)
mSArray = new short[len];
decodeVarintArray(numTags, mSArray);
break;
case TAG_TILE_TAG_VALUES:
// this wastes one byte, as there is no packed string...
decodeTileTags(mCurTagCnt++);
break;
case TAG_TILE_LINE:
case TAG_TILE_POLY:
case TAG_TILE_POINT:
decodeTileElement(tag);
break;
default:
Log.d(TAG, mTile + " invalid type for tile: " + tag);
return false;
}
}
return true;
}
private boolean decodeTileTags(int curTag) throws IOException {
String tagString = decodeString();
String key = Tags.keys[mSArray[curTag]];
Tag tag;
if (key == Tag.TAG_KEY_NAME)
tag = new Tag(key, tagString, false);
else
tag = new Tag(key, tagString, true);
if (debug)
Log.d(TAG, mTile + " add tag: " + curTag + " " + tag);
curTags[curTag] = tag;
return true;
}
private int decodeWayIndices(int indexCnt) throws IOException {
mElem.ensureIndexSize(indexCnt, false);
decodeVarintArray(indexCnt, mElem.index);
short[] index = mElem.index;
int coordCnt = 0;
for (int i = 0; i < indexCnt; i++) {
coordCnt += index[i];
index[i] *= 2;
}
// set end marker
if (indexCnt < index.length)
index[indexCnt] = -1;
return coordCnt;
}
private boolean decodeTileElement(int type) throws IOException {
int bytes = decodeVarint32();
Tag[] tags = null;
short[] index = null;
int end = position() + bytes;
int indexCnt = 1;
boolean fail = false;
int coordCnt = 0;
if (type == TAG_TILE_POINT) {
coordCnt = 1;
mElem.index[0] = 2;
}
mElem.layer = 5;
mElem.priority = 0;
mElem.height = 0;
mElem.minHeight = 0;
while (position() < end) {
// read tag and wire type
int val = decodeVarint32();
if (val == 0)
break;
int tag = (val >> 3);
switch (tag) {
case TAG_ELEM_TAGS:
tags = decodeElementTags();
break;
case TAG_ELEM_NUM_INDICES:
indexCnt = decodeVarint32();
break;
case TAG_ELEM_INDEX:
coordCnt = decodeWayIndices(indexCnt);
break;
case TAG_ELEM_COORDS:
if (coordCnt == 0) {
Log.d(TAG, mTile + " no coordinates");
}
mElem.ensurePointSize(coordCnt, false);
int cnt = decodeInterleavedPoints(mElem.points, mScale);
if (cnt != coordCnt) {
Log.d(TAG, mTile + " wrong number of coordintes");
fail = true;
}
break;
case TAG_ELEM_LAYER:
mElem.layer = decodeVarint32();
break;
case TAG_ELEM_HEIGHT:
mElem.height = decodeVarint32();
break;
case TAG_ELEM_MIN_HEIGHT:
mElem.minHeight = decodeVarint32();
break;
case TAG_ELEM_PRIORITY:
mElem.priority = decodeVarint32();
break;
default:
Log.d(TAG, mTile + " invalid type for way: " + tag);
}
}
if (fail || tags == null || indexCnt == 0) {
Log.d(TAG, mTile + " failed reading way: bytes:" + bytes + " index:"
+ (Arrays.toString(index)) + " tag:"
+ (tags != null ? Arrays.deepToString(tags) : "null") + " "
+ indexCnt + " " + coordCnt);
return false;
}
mElem.tags = tags;
switch (type) {
case TAG_TILE_LINE:
mElem.type = GeometryType.LINE;
break;
case TAG_TILE_POLY:
mElem.type = GeometryType.POLY;
break;
case TAG_TILE_POINT:
mElem.type = GeometryType.POINT;
break;
}
mMapDataSink.process(mElem);
return true;
}
private Tag[] decodeElementTags() throws IOException {
int bytes = decodeVarint32();
Tag[] tmp = mTmpTags;
int cnt = 0;
int end = position() + bytes;
int max = mCurTagCnt;
while (position() < end) {
int tagNum = decodeVarint32();
if (tagNum < 0) {
Log.d(TAG, "NULL TAG: " + mTile + " invalid tag:" + tagNum + " " + cnt);
} else if (tagNum < Tags.MAX) {
tmp[cnt++] = Tags.tags[tagNum];
} else {
tagNum -= Tags.LIMIT;
if (tagNum >= 0 && tagNum < max) {
// Log.d(TAG, "variable tag: " + curTags[tagNum]);
tmp[cnt++] = curTags[tagNum];
} else {
Log.d(TAG, "NULL TAG: " + mTile + " could not find tag:"
+ tagNum + " " + cnt);
}
}
}
if (cnt == 0) {
Log.d(TAG, "got no TAG!");
}
Tag[] tags;
if (cnt < 11)
tags = mElementTags[cnt - 1];
else
tags = new Tag[cnt];
for (int i = 0; i < cnt; i++)
tags[i] = tmp[i];
return tags;
}
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,46 @@
// Protocol Version 2
package org.oscim.database.oscimap;
//option java_package = "org.oscimap.database.pbmap";
//option optimize_for = LITE_RUNTIME;
//
message Data {
message Element {
optional uint32 num_indices = 1 [default = 1];
repeated uint32 tags = 11 [packed = true];
// minimum is 1, number of coordinates for each geometry
repeated uint32 indices = 12 [packed = true];
repeated sint32 coordinates = 13 [packed = true];
optional uint32 layer = 21;
// building height, precision 1/100m
optional int32 height = 31;
optional int32 min_height = 32;
// inteded for symbol and label placement, not used
optional uint32 priority = 41;
}
// tags
required uint32 num_tags = 1;
repeated uint32 keys = 2 [packed = true];
repeated string values = 3;
// non-closed linestring
repeated Element lines = 11;
// polygons are implicitly closed
repeated Element polygons = 12;
// POIs
repeated Element points = 13;
// prepared label placement, not used
repeated Element waylabel = 21;
// tile is completely water, not used
optional uint32 water = 31;
}

View File

@@ -0,0 +1,37 @@
/*
* Copyright 2013 Hannes Janetzek
*
* 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.oscim.tilesource.oscimap4;
import java.net.URL;
import org.oscim.tilesource.ITileDataSource;
import org.oscim.tilesource.common.LwHttp;
import org.oscim.tilesource.common.PbfTileDataSource;
import org.oscim.tilesource.common.UrlTileSource;
public class OSciMap4TileSource extends UrlTileSource {
@Override
public ITileDataSource getDataSource() {
return new TileDataSource(mUrl);
}
class TileDataSource extends PbfTileDataSource {
public TileDataSource(URL url) {
super(new TileDecoder());
mConn = new LwHttp(url, "application/x-protobuf", "vtm", false);
}
}
}

View File

@@ -0,0 +1,354 @@
/*
* Copyright 2012 Hannes Janetzek
*
* 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.oscim.tilesource.oscimap4;
public class Tags {
// TODO this should be retrieved from tile 0/0/0
public final static int ATTRIB_OFFSET = 256;
// the keys that were imported via osm2pgsql + some more
public final static String[] keys = {
"access",
"addr:housename",
"addr:housenumber",
"addr:interpolation",
"admin_level",
"aerialway",
"aeroway",
"amenity",
"area",
"barrier",
"bicycle",
"brand",
"bridge",
"boundary",
"building",
"construction",
"covered",
"culvert",
"cutting",
"denomination",
"disused",
"embankment",
"foot",
"generator:source",
"harbour",
"highway",
"historic",
"horse",
"intermittent",
"junction",
"landuse",
"layer",
"leisure",
"lock",
"man_made",
"military",
"motorcar",
"name",
"natural",
"oneway",
"operator",
"population",
"power",
"power_source",
"place",
"railway",
"ref",
"religion",
"route",
"service",
"shop",
"sport",
"surface",
"toll",
"tourism",
"tower:type",
"tracktype",
"tunnel",
"water",
"waterway",
"wetland",
"width",
"wood",
"height",
"min_height",
"scalerank"
};
public final static int MAX_KEY = keys.length - 1;
// most popular values for the selected key (created from taginfo db)
public final static String[] values = {
"yes",
"residential",
"service",
"unclassified",
"stream",
"track",
"water",
"footway",
"tertiary",
"private",
"tree",
"path",
"forest",
"secondary",
"house",
"no",
"asphalt",
"wood",
"grass",
"paved",
"primary",
"unpaved",
"bus_stop",
"parking",
"parking_aisle",
"rail",
"driveway",
"8",
"administrative",
"locality",
"turning_circle",
"crossing",
"village",
"fence",
"grade2",
"coastline",
"grade3",
"farmland",
"hamlet",
"hut",
"meadow",
"wetland",
"cycleway",
"river",
"school",
"trunk",
"gravel",
"place_of_worship",
"farm",
"grade1",
"traffic_signals",
"wall",
"garage",
"gate",
"motorway",
"living_street",
"pitch",
"grade4",
"industrial",
"road",
"ground",
"scrub",
"motorway_link",
"steps",
"ditch",
"swimming_pool",
"grade5",
"park",
"apartments",
"restaurant",
"designated",
"bench",
"survey_point",
"pedestrian",
"hedge",
"reservoir",
"riverbank",
"alley",
"farmyard",
"peak",
"level_crossing",
"roof",
"dirt",
"drain",
"garages",
"entrance",
"street_lamp",
"deciduous",
"fuel",
"trunk_link",
"information",
"playground",
"supermarket",
"primary_link",
"concrete",
"mixed",
"permissive",
"orchard",
"grave_yard",
"canal",
"garden",
"spur",
"paving_stones",
"rock",
"bollard",
"convenience",
"cemetery",
"post_box",
"commercial",
"pier",
"bank",
"hotel",
"cliff",
"retail",
"construction",
"-1",
"fast_food",
"coniferous",
"cafe",
"6",
"kindergarten",
"tower",
"hospital",
"yard",
"sand",
"public_building",
"cobblestone",
"destination",
"island",
"abandoned",
"vineyard",
"recycling",
"agricultural",
"isolated_dwelling",
"pharmacy",
"post_office",
"motorway_junction",
"pub",
"allotments",
"dam",
"secondary_link",
"lift_gate",
"siding",
"stop",
"main",
"farm_auxiliary",
"quarry",
"10",
"station",
"platform",
"taxiway",
"limited",
"sports_centre",
"cutline",
"detached",
"storage_tank",
"basin",
"bicycle_parking",
"telephone",
"terrace",
"town",
"suburb",
"bus",
"compacted",
"toilets",
"heath",
"works",
"tram",
"beach",
"culvert",
"fire_station",
"recreation_ground",
"bakery",
"police",
"atm",
"clothes",
"tertiary_link",
"waste_basket",
"attraction",
"viewpoint",
"bicycle",
"church",
"shelter",
"drinking_water",
"marsh",
"picnic_site",
"hairdresser",
"bridleway",
"retaining_wall",
"buffer_stop",
"nature_reserve",
"village_green",
"university",
"1",
"bar",
"townhall",
"mini_roundabout",
"camp_site",
"aerodrome",
"stile",
"9",
"car_repair",
"parking_space",
"library",
"pipeline",
"true",
"cycle_barrier",
"4",
"museum",
"spring",
"hunting_stand",
"disused",
"car",
"tram_stop",
"land",
"fountain",
"hiking",
"manufacture",
"vending_machine",
"kiosk",
"swamp",
"unknown",
"7",
"islet",
"shed",
"switch",
"rapids",
"office",
"bay",
"proposed",
"common",
"weir",
"grassland",
"customers",
"social_facility",
"hangar",
"doctors",
"stadium",
"give_way",
"greenhouse",
"guest_house",
"viaduct",
"doityourself",
"runway",
"bus_station",
"water_tower",
"golf_course",
"conservation",
"block",
"college",
"wastewater_plant",
"subway",
"halt",
"forestry",
"florist",
"butcher"
};
public final static int MAX_VALUE = values.length - 1;
}

View File

@@ -0,0 +1,91 @@
// Protocol Version 4
package org.oscim.database.oscimap4;
message Data {
message Element {
// number of geometry 'indices'
optional uint32 num_indices = 1 [default = 1];
// number of 'tags'
optional uint32 num_tags = 2 [default = 1];
// elevation per coordinate
// (pixel relative to ground meters)
// optional bool has_elevation = 3 [default = false];
// reference to tile.tags
repeated uint32 tags = 11 [packed = true];
// A list of number of coordinates for each geometry.
// - polygons are separated by one '0' index
// - for single points this can be omitted.
// e.g 2,2 for two lines with two points each, or
// 4,3,0,4,3 for two polygons with four points in
// the outer ring and 3 points in the inner.
repeated uint32 indices = 12 [packed = true];
// single delta encoded coordinate x,y pairs scaled
// to a tile size of 4096
// note: geometries start at x,y = tile size / 2
repeated sint32 coordinates = 13 [packed = true];
//---------------- optional items ---------------
// osm layer [-5 .. 5] -> [0 .. 10]
optional uint32 layer = 21 [default = 5];
// intended for symbol and label placement, not used
//optional uint32 rank = 32 [packed = true];
// elevation per coordinate
// (pixel relative to ground meters)
// repeated sint32 elevation = 33 [packed = true];
// building height, precision 1/10m
//repeated sint32 height = 34 [packed = true];
// building height, precision 1/10m
//repeated sint32 min_height = 35 [packed = true];
}
required uint32 version = 1;
// tile creation time
optional uint64 timestamp = 2;
// tile is completely water (not used yet)
optional bool water = 3;
// number of 'tags'
required uint32 num_tags = 11;
optional uint32 num_keys = 12 [default = 0];
optional uint32 num_vals = 13 [default = 0];
// strings referenced by tags
repeated string keys = 14;
// separate common attributes from label to
// allow
repeated string values = 15;
// (key[0xfffffffc] | type[0x03]), value pairs
// key: uint32 -> reference to key-strings
// type 0: attribute -> uint32 reference to value-strings
// type 1: string -> uint32 reference to label-strings
// type 2: sint32
// type 3: float
// value: uint32 interpreted according to 'type'
repeated uint32 tags = 16 [packed = true];
// linestring
repeated Element lines = 21;
// polygons (MUST be implicitly closed)
repeated Element polygons = 22;
// points (POIs)
repeated Element points = 23;
}

View File

@@ -0,0 +1,391 @@
/*
* Copyright 2013
*
* 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.oscim.tilesource.oscimap4;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import org.oscim.core.GeometryBuffer.GeometryType;
import org.oscim.core.MapElement;
import org.oscim.core.Tag;
import org.oscim.core.TagSet;
import org.oscim.core.Tile;
import org.oscim.tilesource.common.PbfDecoder;
import org.oscim.tilesource.ITileDataSink;
import android.util.Log;
public class TileDecoder extends PbfDecoder {
private final static String TAG = TileDecoder.class.getName();
private static final int TAG_TILE_VERSION = 1;
//private static final int TAG_TILE_TIMESTAMP = 2;
//private static final int TAG_TILE_ISWATER = 3;
private static final int TAG_TILE_NUM_TAGS = 11;
private static final int TAG_TILE_NUM_KEYS = 12;
private static final int TAG_TILE_NUM_VALUES = 13;
private static final int TAG_TILE_TAG_KEYS = 14;
private static final int TAG_TILE_TAG_VALUES = 15;
private static final int TAG_TILE_TAGS = 16;
private static final int TAG_TILE_LINE = 21;
private static final int TAG_TILE_POLY = 22;
private static final int TAG_TILE_POINT = 23;
private static final int TAG_ELEM_NUM_INDICES = 1;
private static final int TAG_ELEM_NUM_TAGS = 2;
//private static final int TAG_ELEM_HAS_ELEVATION = 3;
private static final int TAG_ELEM_TAGS = 11;
private static final int TAG_ELEM_INDEX = 12;
private static final int TAG_ELEM_COORDS = 13;
private static final int TAG_ELEM_LAYER = 21;
//private static final int TAG_ELEM_HEIGHT = 31;
//private static final int TAG_ELEM_MIN_HEIGHT = 32;
//private static final int TAG_ELEM_PRIORITY = 41;
private short[] mSArray = new short[100];
private Tile mTile;
private final MapElement mElem;
private final Tag[][] mElementTags;
private final TagSet curTags = new TagSet(100);
private ITileDataSink mMapDataSink;
// scale coordinates to tile size
private final static float REF_TILE_SIZE = 4096.0f;
private final float mScaleFactor = REF_TILE_SIZE / Tile.SIZE;
TileDecoder() {
mElem = new MapElement();
// reusable tag set
Tag[][] tags = new Tag[10][];
for (int i = 0; i < 10; i++)
tags[i] = new Tag[i + 1];
mElementTags = tags;
}
@Override
public boolean decode(Tile tile, ITileDataSink sink, InputStream is, int contentLength)
throws IOException {
int byteCount = readUnsignedInt(is, buffer);
Log.d(TAG, tile + " contentLength:" + byteCount);
if (byteCount < 0) {
Log.d(TAG, "invalid contentLength: " + byteCount);
return false;
}
setInputStream(is, byteCount);
mTile = tile;
mMapDataSink = sink;
curTags.clear(true);
int version = -1;
int val;
int numTags = 0;
int numKeys = -1;
int numValues = -1;
int curKey = 0;
int curValue = 0;
String[] keys = null;
String[] values = null;
while (hasData() && (val = decodeVarint32()) > 0) {
// read tag and wire type
int tag = (val >> 3);
//Log.d(TAG, "tag: " + tag);
switch (tag) {
case TAG_TILE_LINE:
case TAG_TILE_POLY:
case TAG_TILE_POINT:
decodeTileElement(tag);
break;
case TAG_TILE_TAG_KEYS:
if (keys == null || curKey >= numKeys) {
Log.d(TAG, mTile + " wrong number of keys " + numKeys);
return false;
}
keys[curKey++] = decodeString();
break;
case TAG_TILE_TAG_VALUES:
if (values == null || curValue >= numValues) {
Log.d(TAG, mTile + " wrong number of values " + numValues);
return false;
}
values[curValue++] = decodeString();
break;
case TAG_TILE_NUM_TAGS:
numTags = decodeVarint32();
//Log.d(TAG, "num tags " + numTags);
break;
case TAG_TILE_NUM_KEYS:
numKeys = decodeVarint32();
//Log.d(TAG, "num keys " + numKeys);
keys = new String[numKeys];
break;
case TAG_TILE_NUM_VALUES:
numValues = decodeVarint32();
//Log.d(TAG, "num values " + numValues);
values = new String[numValues];
break;
case TAG_TILE_TAGS:
int len = numTags * 2;
if (mSArray.length < len)
mSArray = new short[len];
decodeVarintArray(len, mSArray);
if (!decodeTileTags(numTags, mSArray, keys, values)) {
Log.d(TAG, mTile + " invalid tags");
return false;
}
break;
case TAG_TILE_VERSION:
version = decodeVarint32();
if (version != 4) {
Log.d(TAG, mTile + " invalid version " + version);
return false;
}
break;
default:
Log.d(TAG, mTile + " invalid type for tile: " + tag);
return false;
}
}
return true;
}
private boolean decodeTileTags(int numTags, short[] tagIdx, String[] keys, String[] vals) {
Tag tag;
for (int i = 0, n = (numTags << 1); i < n; i += 2) {
int k = tagIdx[i];
int v = tagIdx[i + 1];
String key, val;
if (k < Tags.ATTRIB_OFFSET) {
if (k > Tags.MAX_KEY)
return false;
key = Tags.keys[k];
} else {
k -= Tags.ATTRIB_OFFSET;
if (k >= keys.length)
return false;
key = keys[k];
}
if (v < Tags.ATTRIB_OFFSET) {
if (v > Tags.MAX_VALUE)
return false;
val = Tags.values[v];
} else {
v -= Tags.ATTRIB_OFFSET;
if (v >= vals.length)
return false;
val = vals[v];
}
// FIXME filter out all variable tags
// might depend on theme though
if (key == Tag.TAG_KEY_NAME || key == Tag.KEY_HEIGHT || key == Tag.KEY_MIN_HEIGHT)
tag = new Tag(key, val, false);
else
tag = new Tag(key, val, true);
curTags.add(tag);
}
return true;
}
private int decodeWayIndices(int indexCnt) throws IOException {
mElem.ensureIndexSize(indexCnt, false);
decodeVarintArray(indexCnt, mElem.index);
short[] index = mElem.index;
int coordCnt = 0;
for (int i = 0; i < indexCnt; i++) {
coordCnt += index[i];
index[i] *= 2;
}
// set end marker
if (indexCnt < index.length)
index[indexCnt] = -1;
return coordCnt;
}
private boolean decodeTileElement(int type) throws IOException {
int bytes = decodeVarint32();
Tag[] tags = null;
short[] index = null;
int end = position() + bytes;
int numIndices = 1;
int numTags = 1;
//boolean skip = false;
boolean fail = false;
int coordCnt = 0;
if (type == TAG_TILE_POINT) {
coordCnt = 1;
mElem.index[0] = 2;
}
mElem.layer = 5;
mElem.priority = 0;
mElem.height = 0;
mElem.minHeight = 0;
while (position() < end) {
// read tag and wire type
int val = decodeVarint32();
if (val == 0)
break;
int tag = (val >> 3);
switch (tag) {
case TAG_ELEM_TAGS:
tags = decodeElementTags(numTags);
break;
case TAG_ELEM_NUM_INDICES:
numIndices = decodeVarint32();
break;
case TAG_ELEM_NUM_TAGS:
numTags = decodeVarint32();
break;
case TAG_ELEM_INDEX:
coordCnt = decodeWayIndices(numIndices);
break;
case TAG_ELEM_COORDS:
if (coordCnt == 0) {
Log.d(TAG, mTile + " no coordinates");
}
mElem.ensurePointSize(coordCnt, false);
int cnt = decodeInterleavedPoints(mElem.points, mScaleFactor);
if (cnt != coordCnt) {
Log.d(TAG, mTile + " wrong number of coordintes "
+ coordCnt + "/" + cnt);
fail = true;
}
break;
case TAG_ELEM_LAYER:
mElem.layer = decodeVarint32();
break;
// case TAG_ELEM_HEIGHT:
// mElem.height = decodeVarint32();
// break;
//
// case TAG_ELEM_MIN_HEIGHT:
// mElem.minHeight = decodeVarint32();
// break;
//
// case TAG_ELEM_PRIORITY:
// mElem.priority = decodeVarint32();
// break;
default:
Log.d(TAG, mTile + " invalid type for way: " + tag);
}
}
if (fail || tags == null || numIndices == 0) {
Log.d(TAG, mTile + " failed reading way: bytes:" + bytes + " index:"
+ (Arrays.toString(index)) + " tag:"
+ (tags != null ? Arrays.deepToString(tags) : "null") + " "
+ numIndices + " " + coordCnt);
return false;
}
mElem.tags = tags;
switch (type) {
case TAG_TILE_LINE:
mElem.type = GeometryType.LINE;
break;
case TAG_TILE_POLY:
mElem.type = GeometryType.POLY;
break;
case TAG_TILE_POINT:
mElem.type = GeometryType.POINT;
break;
}
mMapDataSink.process(mElem);
return true;
}
private Tag[] decodeElementTags(int numTags) throws IOException {
if (mSArray.length < numTags)
mSArray = new short[numTags];
short[] tagIds = mSArray;
decodeVarintArray(numTags, tagIds);
Tag[] tags;
if (numTags < 11)
tags = mElementTags[numTags - 1];
else
tags = new Tag[numTags];
int max = curTags.numTags;
for (int i = 0; i < numTags; i++) {
int idx = tagIds[i];
if (idx < 0 || idx > max) {
Log.d(TAG, mTile + " invalid tag:" + idx + " " + i);
return null;
}
tags[i] = curTags.tags[idx];
}
return tags;
}
}

View File

@@ -0,0 +1,194 @@
/*
* Copyright 2012 Hannes Janetzek
*
* 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.oscim.tilesource.test;
import org.oscim.core.BoundingBox;
import org.oscim.core.MapElement;
import org.oscim.core.Tag;
import org.oscim.core.Tile;
import org.oscim.layers.tile.MapTile;
import org.oscim.tilesource.ITileDataSink;
import org.oscim.tilesource.ITileDataSource;
import org.oscim.tilesource.MapInfo;
/**
*
*
*/
public class TestTileSource implements ITileDataSource {
private final Tag[] mTags = {
new Tag("natural", "water")
};
private final Tag[] mTagsWay = {
new Tag("highway", "primary"),
new Tag("name", "Highway Rd")
};
private final Tag[] mTagsBoundary = {
new Tag("boundary", "administrative"),
new Tag("admin_level", "2")
};
private final Tag[] mTagsPlace = {
new Tag("place", "city"),
null
};
private final MapInfo mMapInfo =
new MapInfo(new BoundingBox(-180, -90, 180, 90),
new Byte((byte) 5), null, null, 0, 0, 0,
"", "", "", null);
private final boolean mOpenFile = false;
private final MapElement mElem;
public TestTileSource() {
mElem = new MapElement();
}
private final boolean renderWays = false;
private final boolean renderBoundary = false;
private final boolean renderPlace = false;
@Override
public QueryResult executeQuery(MapTile tile,
ITileDataSink mapDataSink) {
int size = Tile.SIZE;
MapElement e = mElem;
float x1 = -1;
float y1 = -1;
float x2 = size + 1;
float y2 = size + 1;
// always clear geometry before starting
// a different type.
e.clear();
e.startPolygon();
e.addPoint(x1, y1);
e.addPoint(x2, y1);
e.addPoint(x2, y2);
e.addPoint(x1, y2);
y1 = 5;
y2 = size - 5;
x1 = 5;
x2 = size - 5;
e.startHole();
e.addPoint(x1, y1);
e.addPoint(x2, y1);
e.addPoint(x2, y2);
e.addPoint(x1, y2);
e.set(mTags, 0);
mapDataSink.process(e);
if (renderWays) {
e.clear();
// middle horizontal
e.startLine();
e.addPoint(0, size / 2);
e.addPoint(size, size / 2);
// center up
e.startLine();
e.addPoint(size / 2, -size / 2);
e.addPoint(size / 2, size / 2);
// center down
e.startLine();
e.addPoint(size / 2, size / 2);
e.addPoint(size / 2, size / 2 + size);
e.set(mTagsWay, 0);
mapDataSink.process(e);
e.clear();
// left-top to center
e.startLine();
e.addPoint(size / 2, size / 2);
e.addPoint(10, 10);
e.startLine();
e.addPoint(0, 10);
e.addPoint(size, 10);
e.startLine();
e.addPoint(10, 0);
e.addPoint(10, size);
e.set(mTagsWay, 1);
mapDataSink.process(e);
}
if (renderBoundary) {
e.clear();
e.startPolygon();
float r = size / 2;
for (int i = 0; i < 360; i += 4) {
double d = Math.toRadians(i);
e.addPoint(r + (float) Math.cos(d) * (r - 40),
r + (float) Math.sin(d) * (r - 40));
}
e.set(mTagsBoundary, 1);
mapDataSink.process(e);
}
if (renderPlace) {
e.clear();
e.startPoints();
e.addPoint(size / 2, size / 2);
mTagsPlace[1] = new Tag("name", tile.toString());
e.set(mTagsPlace, 0);
mapDataSink.process(e);
}
return QueryResult.SUCCESS;
}
// @Override
// public boolean isOpen() {
// return mOpenFile;
// }
//
// @Override
// public OpenResult open(MapOptions options) {
// mOpenFile = true;
// return OpenResult.SUCCESS;
// }
//
// @Override
// public void close() {
// mOpenFile = false;
// }
//
// @Override
// public void cancel() {
// }
@Override
public void destroy() {
// TODO Auto-generated method stub
}
}