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,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);
}
}