switch package org.mapsforge -> org.oscim
This commit is contained in:
91
src/org/oscim/database/mapfile/Deserializer.java
Normal file
91
src/org/oscim/database/mapfile/Deserializer.java
Normal 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.oscim.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();
|
||||
}
|
||||
}
|
||||
119
src/org/oscim/database/mapfile/IndexCache.java
Normal file
119
src/org/oscim/database/mapfile/IndexCache.java
Normal 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.oscim.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.oscim.core.LRUCache;
|
||||
import org.oscim.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;
|
||||
}
|
||||
}
|
||||
}
|
||||
73
src/org/oscim/database/mapfile/IndexCacheEntryKey.java
Normal file
73
src/org/oscim/database/mapfile/IndexCacheEntryKey.java
Normal 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.oscim.database.mapfile;
|
||||
|
||||
import org.oscim.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;
|
||||
}
|
||||
}
|
||||
1003
src/org/oscim/database/mapfile/MapDatabase.java
Normal file
1003
src/org/oscim/database/mapfile/MapDatabase.java
Normal file
File diff suppressed because it is too large
Load Diff
167
src/org/oscim/database/mapfile/QueryCalculations.java
Normal file
167
src/org/oscim/database/mapfile/QueryCalculations.java
Normal 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.oscim.database.mapfile;
|
||||
|
||||
import org.oscim.core.Tile;
|
||||
import org.oscim.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();
|
||||
}
|
||||
}
|
||||
58
src/org/oscim/database/mapfile/QueryParameters.java
Normal file
58
src/org/oscim/database/mapfile/QueryParameters.java
Normal 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.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();
|
||||
}
|
||||
}
|
||||
535
src/org/oscim/database/mapfile/ReadBuffer.java
Normal file
535
src/org/oscim/database/mapfile/ReadBuffer.java
Normal 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.oscim.database.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;
|
||||
}
|
||||
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
251
src/org/oscim/database/mapfile/header/MapFileHeader.java
Normal file
251
src/org/oscim/database/mapfile/header/MapFileHeader.java
Normal 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.oscim.database.mapfile.header;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.oscim.database.OpenResult;
|
||||
import org.oscim.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 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]);
|
||||
}
|
||||
|
||||
// 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 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
72
src/org/oscim/database/mapfile/header/MapFileInfo.java
Normal file
72
src/org/oscim/database/mapfile/header/MapFileInfo.java
Normal 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.oscim.database.mapfile.header;
|
||||
|
||||
import org.oscim.core.Tag;
|
||||
import org.oscim.database.mapfile.MapDatabase;
|
||||
|
||||
/**
|
||||
* Contains the immutable metadata of a map file.
|
||||
*
|
||||
* @see MapDatabase#getMapInfo()
|
||||
*/
|
||||
public class MapFileInfo extends org.oscim.database.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);
|
||||
|
||||
debugFile = mapFileInfoBuilder.optionalFields.isDebugFile;
|
||||
|
||||
numberOfSubFiles = mapFileInfoBuilder.numberOfSubFiles;
|
||||
poiTags = mapFileInfoBuilder.poiTags;
|
||||
|
||||
tilePixelSize = mapFileInfoBuilder.tilePixelSize;
|
||||
wayTags = mapFileInfoBuilder.wayTags;
|
||||
}
|
||||
}
|
||||
@@ -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.oscim.database.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;
|
||||
|
||||
MapFileInfo build() {
|
||||
return new MapFileInfo(this);
|
||||
}
|
||||
}
|
||||
163
src/org/oscim/database/mapfile/header/OptionalFields.java
Normal file
163
src/org/oscim/database/mapfile/header/OptionalFields.java
Normal 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.oscim.database.mapfile.header;
|
||||
|
||||
import org.oscim.core.GeoPoint;
|
||||
import org.oscim.database.OpenResult;
|
||||
import org.oscim.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 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;
|
||||
}
|
||||
}
|
||||
235
src/org/oscim/database/mapfile/header/RequiredFields.java
Normal file
235
src/org/oscim/database/mapfile/header/RequiredFields.java
Normal 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.oscim.database.mapfile.header;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.oscim.core.BoundingBox;
|
||||
import org.oscim.core.Tag;
|
||||
import org.oscim.database.OpenResult;
|
||||
import org.oscim.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 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 != SUPPORTED_FILE_VERSION) {
|
||||
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.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();
|
||||
}
|
||||
}
|
||||
213
src/org/oscim/database/mapfile/header/SubFileParameter.java
Normal file
213
src/org/oscim/database/mapfile/header/SubFileParameter.java
Normal 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.oscim.database.mapfile.header;
|
||||
|
||||
import org.oscim.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;
|
||||
}
|
||||
}
|
||||
@@ -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.database.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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user