refactor VectorTileMap

This commit is contained in:
Hannes Janetzek
2012-06-19 04:45:43 +02:00
parent 40cadbe805
commit e7c5831302
45 changed files with 178 additions and 161 deletions

View File

@@ -0,0 +1,91 @@
/*
* 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.mapsforge.database.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,119 @@
/*
* 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.mapsforge.database.mapfile;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.mapsforge.core.LRUCache;
import org.mapsforge.database.mapfile.header.SubFileParameter;
/**
* 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 = 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.
*/
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,73 @@
/*
* 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.mapsforge.database.mapfile;
import org.mapsforge.database.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,952 @@
/*
* 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.mapsforge.database.mapfile;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.mapsforge.core.MercatorProjection;
import org.mapsforge.core.Tag;
import org.mapsforge.core.Tile;
import org.mapsforge.database.FileOpenResult;
import org.mapsforge.database.IMapDatabase;
import org.mapsforge.database.IMapDatabaseCallback;
import org.mapsforge.database.mapfile.header.MapFileHeader;
import org.mapsforge.database.mapfile.header.MapFileInfo;
import org.mapsforge.database.mapfile.header.SubFileParameter;
/**
* A class for reading binary map files.
* <p>
* This class is not thread-safe. Each thread should use its own instance.
*
* @see <a href="http://code.google.com/p/mapsforge/wiki/SpecificationBinaryMapFile">Specification</a>
*/
public class MapDatabase implements IMapDatabase {
/**
* 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: ";
/**
* Amount of cache blocks that the index cache should store.
*/
private static final int INDEX_CACHE_SIZE = 64;
/**
* Error message for an invalid first way offset.
*/
private static final String INVALID_FIRST_WAY_OFFSET = "invalid first way offset: ";
private static final Logger LOG = Logger.getLogger(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;
private static final String READ_ONLY_MODE = "r";
/**
* 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 IndexCache mDatabaseIndexCache;
private long mFileSize;
private boolean mDebugFile;
private RandomAccessFile mInputFile;
private MapFileHeader mMapFileHeader;
private ReadBuffer mReadBuffer;
private String mSignatureBlock;
private String mSignaturePoi;
private String mSignatureWay;
private int mTileLatitude;
private int mTileLongitude;
private int[] mIntBuffer;
private float[] mWayNodes = new float[100000];
private int mWayNodePosition;
/*
* (non-Javadoc)
* @see org.mapsforge.map.reader.IMapDatabase#closeFile()
*/
@Override
public void closeFile() {
try {
mMapFileHeader = null;
if (mDatabaseIndexCache != null) {
mDatabaseIndexCache.destroy();
mDatabaseIndexCache = null;
}
if (mInputFile != null) {
mInputFile.close();
mInputFile = null;
}
mReadBuffer = null;
} catch (IOException e) {
LOG.log(Level.SEVERE, null, e);
}
}
private int minLat, minLon;
/*
* (non-Javadoc)
* @see org.mapsforge.map.reader.IMapDatabase#executeQuery(org.mapsforge.core.Tile,
* org.mapsforge.map.reader.MapDatabaseCallback)
*/
@Override
public void executeQuery(Tile tile, IMapDatabaseCallback mapDatabaseCallback) {
if (mMapFileHeader == null)
return;
if (mIntBuffer == null)
mIntBuffer = new int[MAXIMUM_WAY_NODES_SEQUENCE_LENGTH * 2];
mWayNodePosition = 0;
// if (tile.zoomLevel < 10) {
// // reduce small nodes with distance smaller min pixel
// int min = 1;
// long cx = tile.getPixelX() + (Tile.TILE_SIZE >> 1);
// long cy = tile.getPixelY() + (Tile.TILE_SIZE >> 1);
// double l1 = MercatorProjection.pixelXToLongitude(cx, tile.zoomLevel);
// double l2 = MercatorProjection.pixelXToLongitude(cx + min, tile.zoomLevel);
// minLon = (int) Math.abs((l1 * 1000000.0) - (l2 * 1000000.0));
// l1 = MercatorProjection.pixelYToLatitude(cy, tile.zoomLevel);
// l2 = MercatorProjection.pixelYToLatitude(cy + min, tile.zoomLevel);
// minLat = (int) Math.abs((l1 * 1000000.0) - (l2 * 1000000.0));
// } else {
minLat = 0;
minLon = 0;
// }
try {
prepareExecution();
QueryParameters queryParameters = new QueryParameters();
queryParameters.queryZoomLevel = mMapFileHeader.getQueryZoomLevel(tile.zoomLevel);
// get and check the sub-file for the query zoom level
SubFileParameter subFileParameter = mMapFileHeader.getSubFileParameter(queryParameters.queryZoomLevel);
if (subFileParameter == null) {
LOG.warning("no sub-file for zoom level: " + queryParameters.queryZoomLevel);
return;
}
QueryCalculations.calculateBaseTiles(queryParameters, tile, subFileParameter);
QueryCalculations.calculateBlocks(queryParameters, subFileParameter);
processBlocks(mapDatabaseCallback, queryParameters, subFileParameter);
} catch (IOException e) {
LOG.log(Level.SEVERE, null, e);
}
}
/*
* (non-Javadoc)
* @see org.mapsforge.map.reader.IMapDatabase#getMapFileInfo()
*/
@Override
public MapFileInfo getMapFileInfo() {
if (mMapFileHeader == null) {
throw new IllegalStateException("no map file is currently opened");
}
return mMapFileHeader.getMapFileInfo();
}
/*
* (non-Javadoc)
* @see org.mapsforge.map.reader.IMapDatabase#hasOpenFile()
*/
@Override
public boolean hasOpenFile() {
return mInputFile != null;
}
/*
* (non-Javadoc)
* @see org.mapsforge.map.reader.IMapDatabase#openFile(java.io.File)
*/
@Override
public FileOpenResult openFile(File mapFile) {
try {
if (mapFile == null) {
throw new IllegalArgumentException("mapFile must not be null");
}
// make sure to close any previously opened file first
closeFile();
// check if the file exists and is readable
if (!mapFile.exists()) {
return new FileOpenResult("file does not exist: " + mapFile);
} else if (!mapFile.isFile()) {
return new FileOpenResult("not a file: " + mapFile);
} else if (!mapFile.canRead()) {
return new FileOpenResult("cannot read file: " + mapFile);
}
// open the file in read only mode
mInputFile = new RandomAccessFile(mapFile, READ_ONLY_MODE);
mFileSize = mInputFile.length();
mReadBuffer = new ReadBuffer(mInputFile);
mMapFileHeader = new MapFileHeader();
FileOpenResult fileOpenResult = mMapFileHeader.readHeader(mReadBuffer, mFileSize);
if (!fileOpenResult.isSuccess()) {
closeFile();
return fileOpenResult;
}
return FileOpenResult.SUCCESS;
} catch (IOException e) {
LOG.log(Level.SEVERE, null, e);
// make sure that the file is closed
closeFile();
return new FileOpenResult(e.getMessage());
}
}
/**
* Logs the debug signatures of the current way and block.
*/
private void logDebugSignatures() {
if (mDebugFile) {
LOG.warning(DEBUG_SIGNATURE_WAY + mSignatureWay);
LOG.warning(DEBUG_SIGNATURE_BLOCK + mSignatureBlock);
}
}
private void prepareExecution() {
if (mDatabaseIndexCache == null) {
mDatabaseIndexCache = new IndexCache(mInputFile, INDEX_CACHE_SIZE);
}
}
/**
* 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 mapDatabaseCallback
* the callback which handles the extracted map elements.
*/
private void processBlock(QueryParameters queryParameters, SubFileParameter subFileParameter,
IMapDatabaseCallback mapDatabaseCallback) {
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.warning(INVALID_FIRST_WAY_OFFSET + firstWayOffset);
if (mDebugFile) {
LOG.warning(DEBUG_SIGNATURE_BLOCK + mSignatureBlock);
}
return;
}
// add the current buffer position to the relative first way offset
firstWayOffset += mReadBuffer.getBufferPosition();
if (firstWayOffset > mReadBuffer.getBufferSize()) {
LOG.warning(INVALID_FIRST_WAY_OFFSET + firstWayOffset);
if (mDebugFile) {
LOG.warning(DEBUG_SIGNATURE_BLOCK + mSignatureBlock);
}
return;
}
if (!processPOIs(mapDatabaseCallback, poisOnQueryZoomLevel)) {
return;
}
// finished reading POIs, check if the current buffer position is valid
if (mReadBuffer.getBufferPosition() > firstWayOffset) {
LOG.warning("invalid buffer position: " + mReadBuffer.getBufferPosition());
if (mDebugFile) {
LOG.warning(DEBUG_SIGNATURE_BLOCK + mSignatureBlock);
}
return;
}
// move the pointer to the first way
mReadBuffer.setBufferPosition(firstWayOffset);
if (!processWays(queryParameters, mapDatabaseCallback, waysOnQueryZoomLevel)) {
return;
}
}
private void processBlocks(IMapDatabaseCallback mapDatabaseCallback, 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 = mDatabaseIndexCache.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.warning("invalid current block pointer: " + currentBlockPointer);
LOG.warning("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 = mDatabaseIndexCache.getIndexEntry(subFileParameter, blockNumber + 1)
& BITMASK_INDEX_OFFSET;
if (nextBlockPointer < 1 || nextBlockPointer > subFileParameter.subFileSize) {
LOG.warning("invalid next block pointer: " + nextBlockPointer);
LOG.warning("sub-file size: " + subFileParameter.subFileSize);
return;
}
}
// calculate the size of the current block
int currentBlockSize = (int) (nextBlockPointer - currentBlockPointer);
if (currentBlockSize < 0) {
LOG.warning("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.warning("current block size too large: " + currentBlockSize);
continue;
} else if (currentBlockPointer + currentBlockSize > mFileSize) {
LOG.warning("current block largher 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.warning("reading current block has failed: " + currentBlockSize);
return;
}
// calculate the top-left coordinates of the underlying tile
double tileLatitudeDeg = MercatorProjection.tileYToLatitude(subFileParameter.boundaryTileTop + row,
subFileParameter.baseZoomLevel);
double tileLongitudeDeg = MercatorProjection.tileXToLongitude(subFileParameter.boundaryTileLeft
+ column, subFileParameter.baseZoomLevel);
mTileLatitude = (int) (tileLatitudeDeg * 1000000);
mTileLongitude = (int) (tileLongitudeDeg * 1000000);
try {
processBlock(queryParameters, subFileParameter, mapDatabaseCallback);
} catch (ArrayIndexOutOfBoundsException e) {
LOG.log(Level.SEVERE, null, e);
}
}
}
// 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.warning("invalid block signature: " + mSignatureBlock);
return false;
}
}
return true;
}
/**
* Processes the given number of POIs.
*
* @param mapDatabaseCallback
* 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(IMapDatabaseCallback mapDatabaseCallback, int numberOfPois) {
// List<Tag> tags = new ArrayList<Tag>();
Tag[] poiTags = mMapFileHeader.getMapFileInfo().poiTags;
Tag[] tags = null;
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.warning("invalid POI signature: " + mSignaturePoi);
LOG.warning(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);
// boolean changed = false;
if (numberOfTags != 0) {
tags = mReadBuffer.readTags(poiTags, numberOfTags);
// changed = true;
}
if (tags == null)
return false;
// get the feature bitmask (1 byte)
byte featureByte = mReadBuffer.readByte();
// bit 1-3 enable optional features
boolean featureName = (featureByte & POI_FEATURE_NAME) != 0;
boolean featureHouseNumber = (featureByte & POI_FEATURE_HOUSE_NUMBER) != 0;
boolean featureElevation = (featureByte & POI_FEATURE_ELEVATION) != 0;
// check if the POI has a name
if (featureName) {
mReadBuffer.getPositionAndSkip();
}
// check if the POI has a house number
if (featureHouseNumber) {
mReadBuffer.getPositionAndSkip();
}
// check if the POI has an elevation
if (featureElevation) {
mReadBuffer.readSignedInt();
// mReadBuffer.getPositionAndSkip();// tags.add(new Tag(Tag.TAG_KEY_ELE,
// Integer.toString(mReadBuffer.readSignedInt())));
}
mapDatabaseCallback.renderPointOfInterest(layer, latitude, longitude, tags);
}
return true;
}
private int[] 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.warning("invalid number of way coordinate blocks: " + numBlocks);
return null;
}
int[] wayLengths = new int[numBlocks];
mWayNodePosition = 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.warning("invalid number of way nodes: " + numWayNodes);
logDebugSignatures();
return null;
}
// each way node consists of latitude and longitude
int len = numWayNodes * 2;
if (doubleDeltaEncoding) {
len = decodeWayNodesDoubleDelta(len);
} else {
len = decodeWayNodesSingleDelta(len);
}
wayLengths[coordinateBlock] = len;
}
return wayLengths;
}
private int decodeWayNodesDoubleDelta(int length) {
int[] buffer = mIntBuffer;
float[] outBuffer = mWayNodes;
mReadBuffer.readSignedInt(buffer, length);
int floatPos = mWayNodePosition;
// 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[floatPos++] = wayNodeLongitude;
outBuffer[floatPos++] = 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[floatPos++] = nLon;
outBuffer[floatPos++] = nLat;
cnt += 2;
}
}
mWayNodePosition = floatPos;
return cnt;
}
private int decodeWayNodesSingleDelta(int length) {
int[] buffer = mIntBuffer;
float[] outBuffer = mWayNodes;
mReadBuffer.readSignedInt(buffer, length);
int floatPos = mWayNodePosition;
// 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[floatPos++] = wayNodeLongitude;
outBuffer[floatPos++] = 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[floatPos++] = nLon;
outBuffer[floatPos++] = nLat;
cnt += 2;
}
}
mWayNodePosition = floatPos;
return cnt;
}
private int stringOffset = -1;
/*
* (non-Javadoc)
* @see org.mapsforge.map.reader.IMapDatabase#readString(int)
*/
@Override
public String readString(int position) {
return mReadBuffer.readUTF8EncodedStringAt(stringOffset + position);
}
/**
* Processes the given number of ways.
*
* @param queryParameters
* the parameters of the current query.
* @param mapDatabaseCallback
* 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, IMapDatabaseCallback mapDatabaseCallback,
int numberOfWays) {
Tag[] tags = null;
Tag[] wayTags = mMapFileHeader.getMapFileInfo().wayTags;
int[] textPos = new int[3];
// float[] labelPosition;
boolean skippedWays = false;
int wayDataBlocks;
// skip string block
int 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.warning("invalid way signature: " + mSignatureWay);
LOG.warning(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 (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;
skippedWays = true;
mReadBuffer.setBufferPosition(pos);
}
} else {
int wayDataSize = mReadBuffer.readUnsignedInt();
if (wayDataSize < 0) {
LOG.warning("invalid way data size: " + wayDataSize);
if (mDebugFile) {
LOG.warning(DEBUG_SIGNATURE_BLOCK + mSignatureBlock);
}
LOG.warning("EEEEEK 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);
boolean changed = skippedWays;
skippedWays = false;
if (numberOfTags != 0) {
tags = mReadBuffer.readTags(wayTags, numberOfTags);
changed = true;
}
if (tags == null)
return false;
// 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;
// check if the way has a name
if ((featureByte & WAY_FEATURE_NAME) != 0)
textPos[0] = mReadBuffer.readUnsignedInt();
else
textPos[0] = -1;
// check if the way has a house number
if ((featureByte & WAY_FEATURE_HOUSE_NUMBER) != 0)
textPos[1] = mReadBuffer.readUnsignedInt();
else
textPos[1] = -1;
// check if the way has a reference
if ((featureByte & WAY_FEATURE_REF) != 0)
textPos[2] = mReadBuffer.readUnsignedInt();
else
textPos[2] = -1;
if ((featureByte & WAY_FEATURE_LABEL_POSITION) != 0)
// labelPosition =
readOptionalLabelPosition();
// else
// labelPosition = null;
if ((featureByte & WAY_FEATURE_DATA_BLOCKS_BYTE) != 0) {
wayDataBlocks = mReadBuffer.readUnsignedInt();
if (wayDataBlocks < 1) {
LOG.warning("invalid number of way data blocks: " + wayDataBlocks);
logDebugSignatures();
return false;
}
} else {
wayDataBlocks = 1;
}
for (int wayDataBlock = 0; wayDataBlock < wayDataBlocks; ++wayDataBlock) {
int[] wayLengths = processWayDataBlock(featureWayDoubleDeltaEncoding);
if (wayLengths == null)
return false;
// wayDataContainer.textPos = textPos;
mapDatabaseCallback.renderWay(layer, tags, mWayNodes, wayLengths, changed);
}
}
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.warning("invalid cumulated number of POIs in row " + row + ' ' + cumulatedNumberOfPois);
if (mDebugFile) {
LOG.warning(DEBUG_SIGNATURE_BLOCK + mSignatureBlock);
}
return null;
} else if (cumulatedNumberOfWays < 0 || cumulatedNumberOfWays > MAXIMUM_ZOOM_TABLE_OBJECTS) {
LOG.warning("invalid cumulated number of ways in row " + row + ' ' + cumulatedNumberOfWays);
if (mMapFileHeader.getMapFileInfo().debugFile) {
LOG.warning(DEBUG_SIGNATURE_BLOCK + mSignatureBlock);
}
return null;
}
zoomTable[row][0] = cumulatedNumberOfPois;
zoomTable[row][1] = cumulatedNumberOfWays;
}
return zoomTable;
}
}

View File

@@ -0,0 +1,167 @@
/*
* 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.mapsforge.database.mapfile;
import org.mapsforge.core.Tile;
import org.mapsforge.database.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 % 2 == 1 && tile.tileY % 2 == 0) {
// upper right quadrant
return 0x3300;
} else if (tile.tileX % 2 == 0 && tile.tileY % 2 == 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.mapsforge.database.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,535 @@
/*
* 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.mapsforge.database.mapfile;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.io.UnsupportedEncodingException;
import java.util.logging.Logger;
import org.mapsforge.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;
}
// public void readSignedInt(int[] values, int length) {
// int pos = mBufferPosition;
// byte[] data = mBufferData;
//
// for (int i = 0; i < length; i++) {
//
// if ((data[pos] & 0x80) == 0) {
// if ((data[pos] & 0x40) != 0)
// values[i] = -(data[pos] & 0x3f);
// else
// values[i] = (data[pos] & 0x3f);
// pos += 1;
// } else if ((data[pos + 1] & 0x80) == 0) {
// if ((data[pos + 1] & 0x40) != 0)
// values[i] = -((data[pos] & 0x7f)
// | ((data[pos + 1] & 0x3f) << 7));
// else
// values[i] = (data[pos] & 0x7f)
// | ((data[pos + 1] & 0x3f) << 7);
// pos += 2;
// } else if ((data[pos + 2] & 0x80) == 0) {
// if ((data[pos + 2] & 0x40) != 0)
// values[i] = -((data[pos] & 0x7f)
// | ((data[pos + 1] & 0x7f) << 7)
// | ((data[pos + 2] & 0x3f) << 14));
// else
// values[i] = (data[pos] & 0x7f)
// | ((data[pos + 1] & 0x7f) << 7)
// | ((data[pos + 2] & 0x3f) << 14);
// pos += 3;
// } else if ((data[pos + 3] & 0x80) == 0) {
// if ((data[pos + 3] & 0x40) != 0)
// values[i] = -((data[pos] & 0x7f)
// | ((data[pos + 1] & 0x7f) << 7)
// | ((data[pos + 2] & 0x7f) << 14)
// | ((data[pos + 3] & 0x3f) << 21));
// else
// values[i] = (data[pos] & 0x7f)
// | ((data[pos + 1] & 0x7f) << 7)
// | ((data[pos + 2] & 0x7f) << 14)
// | ((data[pos + 3] & 0x3f) << 21);
// pos += 4;
// } else {
// if ((data[pos + 4] & 0x40) != 0)
// 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));
// else
// 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));
// 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,251 @@
/*
* 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.mapsforge.database.mapfile.header;
import java.io.IOException;
import org.mapsforge.database.FileOpenResult;
import org.mapsforge.database.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 FileOpenResult readHeader(ReadBuffer readBuffer, long fileSize) throws IOException {
FileOpenResult fileOpenResult = RequiredFields.readMagicByte(readBuffer);
if (!fileOpenResult.isSuccess()) {
return fileOpenResult;
}
fileOpenResult = RequiredFields.readRemainingHeader(readBuffer);
if (!fileOpenResult.isSuccess()) {
return fileOpenResult;
}
MapFileInfoBuilder mapFileInfoBuilder = new MapFileInfoBuilder();
fileOpenResult = RequiredFields.readFileVersion(readBuffer, mapFileInfoBuilder);
if (!fileOpenResult.isSuccess()) {
return fileOpenResult;
}
fileOpenResult = RequiredFields.readFileSize(readBuffer, fileSize, mapFileInfoBuilder);
if (!fileOpenResult.isSuccess()) {
return fileOpenResult;
}
fileOpenResult = RequiredFields.readMapDate(readBuffer, mapFileInfoBuilder);
if (!fileOpenResult.isSuccess()) {
return fileOpenResult;
}
fileOpenResult = RequiredFields.readBoundingBox(readBuffer, mapFileInfoBuilder);
if (!fileOpenResult.isSuccess()) {
return fileOpenResult;
}
fileOpenResult = RequiredFields.readTilePixelSize(readBuffer, mapFileInfoBuilder);
if (!fileOpenResult.isSuccess()) {
return fileOpenResult;
}
fileOpenResult = RequiredFields.readProjectionName(readBuffer, mapFileInfoBuilder);
if (!fileOpenResult.isSuccess()) {
return fileOpenResult;
}
fileOpenResult = OptionalFields.readOptionalFields(readBuffer, mapFileInfoBuilder);
if (!fileOpenResult.isSuccess()) {
return fileOpenResult;
}
fileOpenResult = RequiredFields.readPoiTags(readBuffer, mapFileInfoBuilder);
if (!fileOpenResult.isSuccess()) {
return fileOpenResult;
}
fileOpenResult = RequiredFields.readWayTags(readBuffer, mapFileInfoBuilder);
if (!fileOpenResult.isSuccess()) {
return fileOpenResult;
}
fileOpenResult = readSubFileParameters(readBuffer, fileSize, mapFileInfoBuilder);
if (!fileOpenResult.isSuccess()) {
return fileOpenResult;
}
this.mapFileInfo = mapFileInfoBuilder.build();
return FileOpenResult.SUCCESS;
}
private FileOpenResult 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 FileOpenResult("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 FileOpenResult("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 FileOpenResult("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 FileOpenResult("invalid maximum zoom level: " + zoomLevelMax);
}
subFileParameterBuilder.zoomLevelMax = zoomLevelMax;
// check for valid zoom level range
if (zoomLevelMin > zoomLevelMax) {
return new FileOpenResult("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 FileOpenResult("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 FileOpenResult("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]);
}
// 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];
for (byte zoomLevel = subFileParameter.zoomLevelMin; zoomLevel <= subFileParameter.zoomLevelMax; ++zoomLevel) {
this.subFileParameters[zoomLevel] = subFileParameter;
}
}
return FileOpenResult.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,72 @@
/*
* 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.mapsforge.database.mapfile.header;
import org.mapsforge.core.Tag;
import org.mapsforge.database.mapfile.MapDatabase;
/**
* Contains the immutable metadata of a map file.
*
* @see MapDatabase#getMapFileInfo()
*/
public class MapFileInfo extends org.mapsforge.database.MapFileInfo {
/**
* 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);
debugFile = mapFileInfoBuilder.optionalFields.isDebugFile;
numberOfSubFiles = mapFileInfoBuilder.numberOfSubFiles;
poiTags = mapFileInfoBuilder.poiTags;
tilePixelSize = mapFileInfoBuilder.tilePixelSize;
wayTags = mapFileInfoBuilder.wayTags;
}
}

View File

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

View File

@@ -0,0 +1,163 @@
/*
* 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.mapsforge.database.mapfile.header;
import org.mapsforge.core.GeoPoint;
import org.mapsforge.database.FileOpenResult;
import org.mapsforge.database.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 FileOpenResult readOptionalFields(ReadBuffer readBuffer, MapFileInfoBuilder mapFileInfoBuilder) {
OptionalFields optionalFields = new OptionalFields(readBuffer.readByte());
mapFileInfoBuilder.optionalFields = optionalFields;
FileOpenResult fileOpenResult = optionalFields.readOptionalFields(readBuffer);
if (!fileOpenResult.isSuccess()) {
return fileOpenResult;
}
return FileOpenResult.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 FileOpenResult readLanguagePreference(ReadBuffer readBuffer) {
if (this.hasLanguagePreference) {
String countryCode = readBuffer.readUTF8EncodedString();
if (countryCode.length() != LANGUAGE_PREFERENCE_LENGTH) {
return new FileOpenResult("invalid language preference: " + countryCode);
}
this.languagePreference = countryCode;
}
return FileOpenResult.SUCCESS;
}
private FileOpenResult 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 FileOpenResult("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 FileOpenResult("invalid map start longitude: " + mapStartLongitude);
}
this.startPosition = new GeoPoint(mapStartLatitude, mapStartLongitude);
}
return FileOpenResult.SUCCESS;
}
private FileOpenResult 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 FileOpenResult("invalid map start zoom level: " + mapStartZoomLevel);
}
this.startZoomLevel = Byte.valueOf(mapStartZoomLevel);
}
return FileOpenResult.SUCCESS;
}
private FileOpenResult readOptionalFields(ReadBuffer readBuffer) {
FileOpenResult fileOpenResult = readMapStartPosition(readBuffer);
if (!fileOpenResult.isSuccess()) {
return fileOpenResult;
}
fileOpenResult = readMapStartZoomLevel(readBuffer);
if (!fileOpenResult.isSuccess()) {
return fileOpenResult;
}
fileOpenResult = readLanguagePreference(readBuffer);
if (!fileOpenResult.isSuccess()) {
return fileOpenResult;
}
if (this.hasComment) {
this.comment = readBuffer.readUTF8EncodedString();
}
if (this.hasCreatedBy) {
this.createdBy = readBuffer.readUTF8EncodedString();
}
return FileOpenResult.SUCCESS;
}
}

View File

@@ -0,0 +1,235 @@
/*
* 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.mapsforge.database.mapfile.header;
import java.io.IOException;
import org.mapsforge.core.BoundingBox;
import org.mapsforge.core.Tag;
import org.mapsforge.database.FileOpenResult;
import org.mapsforge.database.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 SUPPORTED_FILE_VERSION = 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 FileOpenResult 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 FileOpenResult("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 FileOpenResult("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 FileOpenResult("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 FileOpenResult("invalid maximum longitude: " + maxLongitude);
}
// check latitude and longitude range
if (minLatitude > maxLatitude) {
return new FileOpenResult("invalid latitude range: " + minLatitude + SPACE + maxLatitude);
} else if (minLongitude > maxLongitude) {
return new FileOpenResult("invalid longitude range: " + minLongitude + SPACE + maxLongitude);
}
mapFileInfoBuilder.boundingBox = new BoundingBox(minLatitude, minLongitude, maxLatitude, maxLongitude);
return FileOpenResult.SUCCESS;
}
static FileOpenResult readFileSize(ReadBuffer readBuffer, long fileSize, MapFileInfoBuilder mapFileInfoBuilder) {
// get and check the file size (8 bytes)
long headerFileSize = readBuffer.readLong();
if (headerFileSize != fileSize) {
return new FileOpenResult("invalid file size: " + headerFileSize);
}
mapFileInfoBuilder.fileSize = fileSize;
return FileOpenResult.SUCCESS;
}
static FileOpenResult readFileVersion(ReadBuffer readBuffer, MapFileInfoBuilder mapFileInfoBuilder) {
// get and check the file version (4 bytes)
int fileVersion = readBuffer.readInt();
if (fileVersion != SUPPORTED_FILE_VERSION) {
return new FileOpenResult("unsupported file version: " + fileVersion);
}
mapFileInfoBuilder.fileVersion = fileVersion;
return FileOpenResult.SUCCESS;
}
static FileOpenResult 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 FileOpenResult("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 FileOpenResult("invalid magic byte: " + magicByte);
}
return FileOpenResult.SUCCESS;
}
static FileOpenResult 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 FileOpenResult("invalid map date: " + mapDate);
}
mapFileInfoBuilder.mapDate = mapDate;
return FileOpenResult.SUCCESS;
}
static FileOpenResult readPoiTags(ReadBuffer readBuffer, MapFileInfoBuilder mapFileInfoBuilder) {
// get and check the number of POI tags (2 bytes)
int numberOfPoiTags = readBuffer.readShort();
if (numberOfPoiTags < 0) {
return new FileOpenResult("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 FileOpenResult("POI tag must not be null: " + currentTagId);
}
poiTags[currentTagId] = new Tag(tag);
}
mapFileInfoBuilder.poiTags = poiTags;
return FileOpenResult.SUCCESS;
}
static FileOpenResult readProjectionName(ReadBuffer readBuffer, MapFileInfoBuilder mapFileInfoBuilder) {
// get and check the projection name
String projectionName = readBuffer.readUTF8EncodedString();
if (!MERCATOR.equals(projectionName)) {
return new FileOpenResult("unsupported projection: " + projectionName);
}
mapFileInfoBuilder.projectionName = projectionName;
return FileOpenResult.SUCCESS;
}
static FileOpenResult 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 FileOpenResult("invalid remaining header size: " + remainingHeaderSize);
}
// read the header data into the buffer
if (!readBuffer.readFromFile(remainingHeaderSize)) {
return new FileOpenResult("reading header data has failed: " + remainingHeaderSize);
}
return FileOpenResult.SUCCESS;
}
static FileOpenResult readTilePixelSize(ReadBuffer readBuffer, MapFileInfoBuilder mapFileInfoBuilder) {
// get and check the tile pixel size (2 bytes)
int tilePixelSize = readBuffer.readShort();
// if (tilePixelSize != Tile.TILE_SIZE) {
// return new FileOpenResult("unsupported tile pixel size: " + tilePixelSize);
// }
mapFileInfoBuilder.tilePixelSize = tilePixelSize;
return FileOpenResult.SUCCESS;
}
static FileOpenResult readWayTags(ReadBuffer readBuffer, MapFileInfoBuilder mapFileInfoBuilder) {
// get and check the number of way tags (2 bytes)
int numberOfWayTags = readBuffer.readShort();
if (numberOfWayTags < 0) {
return new FileOpenResult("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 FileOpenResult("way tag must not be null: " + currentTagId);
}
wayTags[currentTagId] = new Tag(tag);
}
mapFileInfoBuilder.wayTags = wayTags;
return FileOpenResult.SUCCESS;
}
private RequiredFields() {
throw new IllegalStateException();
}
}

View File

@@ -0,0 +1,213 @@
/*
* 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.mapsforge.database.mapfile.header;
import org.mapsforge.core.MercatorProjection;
/**
* 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 = MercatorProjection.latitudeToTileY(subFileParameterBuilder.boundingBox.minLatitudeE6
/ COORDINATES_DIVISOR, this.baseZoomLevel);
this.boundaryTileLeft = MercatorProjection.longitudeToTileX(subFileParameterBuilder.boundingBox.minLongitudeE6
/ COORDINATES_DIVISOR, this.baseZoomLevel);
this.boundaryTileTop = MercatorProjection.latitudeToTileY(subFileParameterBuilder.boundingBox.maxLatitudeE6
/ COORDINATES_DIVISOR, this.baseZoomLevel);
this.boundaryTileRight = MercatorProjection.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.mapsforge.database.mapfile.header;
import org.mapsforge.core.BoundingBox;
class SubFileParameterBuilder {
byte baseZoomLevel;
BoundingBox boundingBox;
long indexStartAddress;
long startAddress;
long subFileSize;
byte zoomLevelMax;
byte zoomLevelMin;
SubFileParameter build() {
return new SubFileParameter(this);
}
}