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,75 @@
/*
* Copyright 2010, 2011, 2012 mapsforge.org
*
* This program is free software: you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.mapsforge.database;
/**
* A FileOpenResult is a simple DTO which is returned by IMapDatabase#openFile(File).
*/
public class FileOpenResult {
/**
* Singleton for a FileOpenResult instance with {@code success=true}.
*/
public static final FileOpenResult SUCCESS = new FileOpenResult();
private final String errorMessage;
private final boolean success;
/**
* @param errorMessage
* a textual message describing the error, must not be null.
*/
public FileOpenResult(String errorMessage) {
if (errorMessage == null) {
throw new IllegalArgumentException("error message must not be null");
}
this.success = false;
this.errorMessage = errorMessage;
}
/**
*
*/
public FileOpenResult() {
this.success = true;
this.errorMessage = null;
}
/**
* @return a textual error description (might be null).
*/
public String getErrorMessage() {
return this.errorMessage;
}
/**
* @return true if the file could be opened successfully, false otherwise.
*/
public boolean isSuccess() {
return this.success;
}
@Override
public String toString() {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("FileOpenResult [success=");
stringBuilder.append(this.success);
stringBuilder.append(", errorMessage=");
stringBuilder.append(this.errorMessage);
stringBuilder.append("]");
return stringBuilder.toString();
}
}

View File

@@ -0,0 +1,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;
import java.io.File;
import org.mapsforge.core.Tile;
/**
*
*
*/
public interface IMapDatabase {
/**
* Closes the map file and destroys all internal caches. Has no effect if no map file is currently opened.
*/
public abstract void closeFile();
/**
* Starts a database query with the given parameters.
*
* @param tile
* the tile to read.
* @param mapDatabaseCallback
* the callback which handles the extracted map elements.
*/
public abstract void executeQuery(Tile tile, IMapDatabaseCallback mapDatabaseCallback);
/**
* @return the metadata for the current map file.
* @throws IllegalStateException
* if no map is currently opened.
*/
public abstract MapFileInfo getMapFileInfo();
/**
* @return true if a map file is currently opened, false otherwise.
*/
public abstract boolean hasOpenFile();
/**
* Opens the given map file, reads its header data and validates them.
*
* @param mapFile
* the map file.
* @return a FileOpenResult containing an error message in case of a failure.
* @throws IllegalArgumentException
* if the given map file is null.
*/
public abstract FileOpenResult openFile(File mapFile);
/**
* @param position
* ....
* @return ...
*/
public abstract String readString(int position);
}

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;
import org.mapsforge.core.Tag;
import org.mapsforge.database.mapfile.MapDatabase;
/**
* Callback methods which can be triggered from the {@link MapDatabase}.
*/
public interface IMapDatabaseCallback {
/**
* Renders a single point of interest node (POI).
*
* @param layer
* the layer of the node.
* @param latitude
* the latitude of the node.
* @param longitude
* the longitude of the node.
* @param tags
* the tags of the node.
*/
void renderPointOfInterest(byte layer, int latitude, int longitude, Tag[] tags);
/**
* Renders water background for the current tile.
*/
void renderWaterBackground();
/**
* Renders a single way or area (closed way).
*
* @param layer
* the layer of the way.
* @param tags
* the tags of the way.
* @param wayNodes
* the geographical coordinates of the way nodes in the order longitude/latitude.
* @param wayLength
* length of way data in wayNodes
* @param changed
* tags have changed since last call (just an optional hint)
*/
void renderWay(byte layer, Tag[] tags, float[] wayNodes, int[] wayLength, boolean changed);
}

View File

@@ -0,0 +1,120 @@
/*
* 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;
import org.mapsforge.core.BoundingBox;
import org.mapsforge.core.GeoPoint;
import org.mapsforge.database.mapfile.MapDatabase;
/**
* Contains the immutable metadata of a map file.
*
* @see MapDatabase#getMapFileInfo()
*/
public class MapFileInfo {
/**
* The bounding box of the map file.
*/
public final BoundingBox boundingBox;
/**
* The comment field of the map file (may be null).
*/
public final String comment;
/**
* The created by field of the map file (may be null).
*/
public final String createdBy;
/**
* The size of the map file, measured in bytes.
*/
public final long fileSize;
/**
* The file version number of the map file.
*/
public final int fileVersion;
/**
* The preferred language for names as defined in ISO 3166-1 (may be null).
*/
public final String languagePreference;
/**
* The center point of the map file.
*/
public final GeoPoint mapCenter;
/**
* The date of the map data in milliseconds since January 1, 1970.
*/
public final long mapDate;
/**
* The name of the projection used in the map file.
*/
public final String projectionName;
/**
* The map start position from the file header (may be null).
*/
public final GeoPoint startPosition;
/**
* The map start zoom level from the file header (may be null).
*/
public final Byte startZoomLevel;
/**
* @param bbox
* ...
* @param zoom
* ...
* @param start
* ...
* @param projection
* ...
* @param date
* ...
* @param size
* ...
* @param version
* ...
* @param language
* ...
* @param comment
* ...
* @param createdBy
* ...
*/
public MapFileInfo(BoundingBox bbox, Byte zoom, GeoPoint start, String projection,
long date, long size, int version, String language, String comment, String createdBy) {
this.startZoomLevel = zoom;
this.startPosition = start;
this.projectionName = projection;
this.mapDate = date;
this.boundingBox = bbox;
this.mapCenter = bbox.getCenterPoint();
this.languagePreference = language;
this.fileSize = size;
this.fileVersion = version;
this.comment = comment;
this.createdBy = createdBy;
}
}

View File

@@ -0,0 +1,145 @@
/*
* Copyright 2012 Hannes Janetzek
*
* This program is free software: you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.mapsforge.database.json;
import java.io.File;
import org.mapsforge.core.BoundingBox;
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.MapFileInfo;
/**
*
*
*/
public class MapDatabase implements IMapDatabase {
private float[] mCoords = new float[20];
private int[] mIndex = new int[1];
// private Tag[] mTags = { new Tag("boundary", "administrative"), new Tag("admin_level", "2") };
private Tag[] mTags = { new Tag("building", "yes") };
private final MapFileInfo mMapInfo =
new MapFileInfo(new BoundingBox(-180, -90, 180, 90),
new Byte((byte) 0), null, "Mercator", 0, 0, 0, "de", "yo!", "by me");
private boolean mOpenFile = false;
private static double radius = 6378137;
private static double D2R = Math.PI / 180;
// private static double HALF_PI = Math.PI / 2;
@Override
public void executeQuery(Tile tile, IMapDatabaseCallback mapDatabaseCallback) {
long cx = tile.pixelX + (Tile.TILE_SIZE >> 1);
long cy = tile.pixelY + (Tile.TILE_SIZE >> 1);
// float lon1 = (float) MercatorProjection.pixelXToLongitude(cx - 100, tile.zoomLevel) * 1000000;
// float lon2 = (float) MercatorProjection.pixelXToLongitude(cx + 100, tile.zoomLevel) * 1000000;
// float lat1 = (float) MercatorProjection.pixelYToLatitude(cy - 100, tile.zoomLevel) * 1000000;
// float lat2 = (float) MercatorProjection.pixelYToLatitude(cy + 100, tile.zoomLevel) * 1000000;
float lon1 = (float) MercatorProjection.pixelXToLongitude(cx - 100, tile.zoomLevel);
float lon2 = (float) MercatorProjection.pixelXToLongitude(cx + 100, tile.zoomLevel);
float lat1 = (float) MercatorProjection.pixelYToLatitude(cy - 100, tile.zoomLevel);
float lat2 = (float) MercatorProjection.pixelYToLatitude(cy + 100, tile.zoomLevel);
double lonRadians = (D2R * lon1);
double latRadians = (D2R * lat1);
// spherical mercator projection
lon1 = (float) (radius * lonRadians);
lat1 = (float) (radius * Math.log(Math.tan(Math.PI * 0.25 + latRadians * 0.5)));
lonRadians = (D2R * lon2);
latRadians = (D2R * lat2);
lon2 = (float) (radius * lonRadians);
lat2 = (float) (radius * Math.log(Math.tan(Math.PI * 0.25 + latRadians * 0.5)));
mCoords[0] = lon1;
mCoords[1] = lat1;
mCoords[2] = lon2;
mCoords[3] = lat1;
mCoords[4] = lon2;
mCoords[5] = lat2;
mCoords[6] = lon1;
mCoords[7] = lat2;
mCoords[8] = lon1;
mCoords[9] = lat1;
mIndex[0] = 10;
// lon1 = (float) MercatorProjection.pixelXToLongitude(cx - 80, tile.zoomLevel) * 1000000;
// lon2 = (float) MercatorProjection.pixelXToLongitude(cx + 80, tile.zoomLevel) * 1000000;
// lat1 = (float) MercatorProjection.pixelYToLatitude(cy - 80, tile.zoomLevel) * 1000000;
// lat2 = (float) MercatorProjection.pixelYToLatitude(cy + 80, tile.zoomLevel) * 1000000;
//
// mCoords[10] = lon1;
// mCoords[11] = lat1;
//
// mCoords[12] = lon2;
// mCoords[13] = lat1;
//
// mCoords[14] = lon2;
// mCoords[15] = lat2;
//
// mCoords[16] = lon1;
// mCoords[17] = lat2;
//
// mCoords[18] = lon1;
// mCoords[19] = lat1;
//
// mIndex[1] = 10;
mapDatabaseCallback.renderWay((byte) 0, mTags, mCoords, mIndex, true);
}
@Override
public MapFileInfo getMapFileInfo() {
return mMapInfo;
}
@Override
public boolean hasOpenFile() {
return mOpenFile;
}
@Override
public FileOpenResult openFile(File mapFile) {
mOpenFile = true;
return new FileOpenResult();
}
@Override
public void closeFile() {
mOpenFile = false;
}
@Override
public String readString(int position) {
return null;
}
}

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

View File

@@ -0,0 +1,372 @@
/*
* Geometry.java
*
* PostGIS extension for PostgreSQL JDBC driver - geometry model
*
* (C) 2004 Paul Ramsey, pramsey@refractions.net
*
* (C) 2005 Markus Schaber, markus.schaber@logix-tt.com
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General License as published by the Free
* Software Foundation, either version 2.1 of the License.
*
* This library 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 License for more
* details.
*
* You should have received a copy of the GNU Lesser General License
* along with this library; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA or visit the web at
* http://www.gnu.org.
*
* $Id: Geometry.java 9324 2012-02-27 22:08:12Z pramsey $
*/
package org.mapsforge.database.postgis;
import java.io.Serializable;
/** The base class of all geometries */
abstract class Geometry implements Serializable {
/* JDK 1.5 Serialization */
private static final long serialVersionUID = 0x100;
// OpenGIS Geometry types as defined in the OGC WKB Spec
// (May we replace this with an ENUM as soon as JDK 1.5
// has gained widespread usage?)
/** Fake type for linear ring */
static final int LINEARRING = 0;
/**
* The OGIS geometry type number for points.
*/
static final int POINT = 1;
/**
* The OGIS geometry type number for lines.
*/
static final int LINESTRING = 2;
/**
* The OGIS geometry type number for polygons.
*/
static final int POLYGON = 3;
/**
* The OGIS geometry type number for aggregate points.
*/
static final int MULTIPOINT = 4;
/**
* The OGIS geometry type number for aggregate lines.
*/
static final int MULTILINESTRING = 5;
/**
* The OGIS geometry type number for aggregate polygons.
*/
static final int MULTIPOLYGON = 6;
/**
* The OGIS geometry type number for feature collections.
*/
static final int GEOMETRYCOLLECTION = 7;
static final String[] ALLTYPES = new String[] {
"", // internally used LinearRing does not have any text in front of
// it
"POINT", "LINESTRING", "POLYGON", "MULTIPOINT", "MULTILINESTRING",
"MULTIPOLYGON", "GEOMETRYCOLLECTION" };
/**
* The Text representations of the geometry types
*
* @param type
* ...
* @return ...
*/
static String getTypeString(int type) {
if (type >= 0 && type <= 7)
return ALLTYPES[type];
throw new IllegalArgumentException("Unknown Geometry type" + type);
}
// Properties common to all geometries
/**
* The dimensionality of this feature (2,3)
*/
int dimension;
/**
* Do we have a measure (4th dimension)
*/
boolean haveMeasure = false;
/**
* The OGIS geometry type of this feature. this is final as it never changes, it is bound to the subclass of the
* instance.
*/
final int type;
/**
* Official UNKNOWN srid value
*/
final static int UNKNOWN_SRID = 0;
/**
* The spacial reference system id of this geometry, default is no srid
*/
int srid = UNKNOWN_SRID;
/**
* Parse a SRID value, anything <= 0 is unknown
*
* @param srid
* ...
* @return ...
*/
static int parseSRID(int srid) {
if (srid < 0) {
/* TODO: raise a warning ? */
return 0;
}
return srid;
}
/**
* Constructor for subclasses
*
* @param type
* has to be given by all subclasses.
*/
protected Geometry(int type) {
this.type = type;
}
/**
* java.lang.Object hashCode implementation
*/
@Override
public int hashCode() {
return dimension | (type * 4) | (srid * 32);
}
/**
* java.lang.Object equals implementation
*/
@Override
public boolean equals(Object other) {
return (other != null) && (other instanceof Geometry)
&& equals((Geometry) other);
}
/**
* geometry specific equals implementation - only defined for non-null values
*
* @param other
* ...
* @return ...
*/
public boolean equals(Geometry other) {
return (other != null) && (this.dimension == other.dimension)
&& (this.type == other.type) && (this.srid == other.srid)
&& (this.haveMeasure == other.haveMeasure)
&& other.getClass().equals(this.getClass())
&& this.equalsintern(other);
}
/**
* Whether test coordinates for geometry - subclass specific code Implementors can assume that dimensin, type, srid
* and haveMeasure are equal, other != null and other is the same subclass.
*
* @param other
* ...
* @return ...
*/
protected abstract boolean equalsintern(Geometry other);
/**
* Return the number of Points of the geometry
*
* @return ...
*/
abstract int numPoints();
/**
* Get the nth Point of the geometry
*
* @param n
* the index of the point, from 0 to numPoints()-1;
* @throws ArrayIndexOutOfBoundsException
* in case of an emtpy geometry or bad index.
*/
// abstract Point getPoint(int n);
//
// /**
// * Same as getPoint(0);
// */
// abstract Point getFirstPoint();
//
// /**
// * Same as getPoint(numPoints()-1);
// */
// abstract Point getLastPoint();
/**
* The OGIS geometry type number of this geometry.
*
* @return ...
*/
int getType() {
return this.type;
}
/**
* Return the Type as String
*
* @return ...
*/
String getTypeString() {
return getTypeString(this.type);
}
/**
* Returns whether we have a measure
*
* @return ....
*/
boolean isMeasured() {
return haveMeasure;
}
/**
* Queries the number of geometric dimensions of this geometry. This does not include measures, as opposed to the
* server.
*
* @return The dimensionality (eg, 2D or 3D) of this geometry.
*/
int getDimension() {
return this.dimension;
}
/**
* The OGIS geometry type number of this geometry.
*
* @return ...
*/
int getSrid() {
return this.srid;
}
/**
* Recursively sets the srid on this geometry and all contained subgeometries
*
* @param srid
* ...
*/
void setSrid(int srid) {
this.srid = srid;
}
@Override
public String toString() {
StringBuffer sb = new StringBuffer();
if (srid != UNKNOWN_SRID) {
sb.append("SRID=");
sb.append(srid);
sb.append(';');
}
outerWKT(sb, true);
return sb.toString();
}
/**
* Render the WKT version of this Geometry (without SRID) into the given StringBuffer.
*
* @param sb
* ...
* @param putM
* ...
*/
void outerWKT(StringBuffer sb, boolean putM) {
sb.append(getTypeString());
if (putM && haveMeasure && dimension == 2) {
sb.append('M');
}
mediumWKT(sb);
}
final void outerWKT(StringBuffer sb) {
outerWKT(sb, true);
}
/**
* Render the WKT without the type name, but including the brackets into the StringBuffer
*
* @param sb
* ...
*/
protected void mediumWKT(StringBuffer sb) {
sb.append('(');
innerWKT(sb);
sb.append(')');
}
/**
* Render the "inner" part of the WKT (inside the brackets) into the StringBuffer.
*
* @param SB
* ...
*/
protected abstract void innerWKT(StringBuffer SB);
/**
* backwards compatibility method
*
* @return ...
*/
String getValue() {
StringBuffer sb = new StringBuffer();
mediumWKT(sb);
return sb.toString();
}
/**
* Do some internal consistency checks on the geometry. Currently, all Geometries must have a valid dimension (2 or
* 3) and a valid type. 2-dimensional Points must have Z=0.0, as well as non-measured Points must have m=0.0.
* Composed geometries must have all equal SRID, dimensionality and measures, as well as that they do not contain
* NULL or inconsistent subgeometries. BinaryParser and WKTParser should only generate consistent geometries.
* BinaryWriter may produce invalid results on inconsistent geometries.
*
* @return true if all checks are passed.
*/
boolean checkConsistency() {
return (dimension >= 2 && dimension <= 3) && (type >= 0 && type <= 7);
}
/**
* Splits the SRID=4711; part of a EWKT rep if present and sets the srid.
*
* @param value
* ...
* @return value without the SRID=4711; part
*/
protected String initSRID(String value) {
String v = value.trim();
if (v.startsWith("SRID=")) {
int index = v.indexOf(';', 5); // sridprefix length is 5
if (index == -1) {
throw new IllegalArgumentException(
"Error parsing Geometry - SRID not delimited with ';' ");
}
this.srid = Integer.parseInt(v.substring(5, index));
return v.substring(index + 1).trim();
}
return v;
}
}

View File

@@ -0,0 +1,442 @@
/*
* Copyright 2012 Hannes Janetzek
*
* This program is free software: you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.mapsforge.database.postgis;
import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Properties;
import org.mapsforge.core.BoundingBox;
import org.mapsforge.core.GeoPoint;
import org.mapsforge.core.SphericalMercator;
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.MapFileInfo;
import org.postgresql.PGConnection;
/**
*
*
*/
public class MapDatabase implements IMapDatabase {
private static final String QUERY = "SELECT * FROM __get_tile(?,?,?)";
private final float mScale = 1; // 1000000.0f;
private int mCoordPos = 0;
private int mIndexPos = 0;
private float[] mCoords = new float[100000];
private int[] mIndex = new int[10000];
private Tag[] mTags;
private final MapFileInfo mMapInfo =
new MapFileInfo(new BoundingBox(-180, -85, 180, 85),
new Byte((byte) 14), new GeoPoint(53.11, 8.85), SphericalMercator.NAME,
0, 0, 0, "de", "yo!", "hannes");
// new MapFileInfo(new BoundingBox(-180, -90, 180, 90),
// new Byte((byte) 0), null, "Mercator",
// 0, 0, 0, "de", "yo!", "by me");
private boolean mOpenFile = false;
private Connection connection = null;
private static HashMap<Entry<String, String>, Tag> tagHash = new HashMap<Entry<String, String>, Tag>(100);
private PreparedStatement prepQuery = null;
private boolean connect() {
Connection conn = null;
// &socketTimeout=15&tcpKeepAlive=true
String dburl = "jdbc:postgresql://city.informatik.uni-bremen.de:5432/gis";
// String dburl = "jdbc:postgresql://city.informatik.uni-bremen.de:5432/planet-2.0";
// String dburl = "jdbc:postgresql://127.0.0.1:5432/bremen";
// String dburl = "jdbc:postgresql://127.0.0.1:5431/planet-2.0";
Properties dbOpts = new Properties();
dbOpts.setProperty("user", "osm");
dbOpts.setProperty("password", "osm");
dbOpts.setProperty("socketTimeout", "15000");
dbOpts.setProperty("tcpKeepAlive", "true");
try {
DriverManager.setLoginTimeout(20);
System.out.println("Creating JDBC connection...");
Class.forName("org.postgresql.Driver");
conn = DriverManager.getConnection(dburl, dbOpts);
connection = conn;
prepQuery = conn.prepareStatement(QUERY);
PGConnection pgconn = (PGConnection) conn;
pgconn.addDataType("hstore", PGHStore.class);
} catch (Exception e) {
System.err.println("Aborted due to error:");
e.printStackTrace();
return false;
}
return true;
}
@Override
public void executeQuery(Tile tile, IMapDatabaseCallback mapDatabaseCallback) {
if (connection == null) {
if (!connect())
return;
}
ResultSet r;
try {
prepQuery.setLong(1, tile.pixelX);
prepQuery.setLong(2, tile.pixelY);
prepQuery.setInt(3, tile.zoomLevel);
prepQuery.execute();
r = prepQuery.getResultSet();
} catch (SQLException e) {
e.printStackTrace();
connection = null;
return;
}
byte[] b = null;
PGHStore h = null;
// long id;
try {
while (r != null && r.next()) {
mIndexPos = 0;
mCoordPos = 0;
try {
// id = r.getLong(1);
Object obj = r.getObject(2);
h = null;
if (obj instanceof PGHStore)
h = (PGHStore) obj;
else
continue;
b = r.getBytes(3);
} catch (SQLException e) {
e.printStackTrace();
continue;
}
if (b == null)
continue;
mTags = new Tag[h.size()];
int i = 0;
for (Entry<String, String> t : h.entrySet()) {
if (t.getKey() == null) {
System.out.println("no KEY !!! ");
break;
}
Tag tag = tagHash.get(t);
if (tag == null) {
tag = new Tag(t.getKey(), t.getValue());
tagHash.put(t, tag);
}
mTags[i++] = tag;
}
if (i < mTags.length)
continue;
parse(b);
if (mIndexPos == 0)
continue;
int[] idx = new int[mIndexPos];
System.arraycopy(mIndex, 0, idx, 0, mIndexPos);
mapDatabaseCallback.renderWay((byte) 0, mTags, mCoords, idx, true);
}
} catch (SQLException e) {
e.printStackTrace();
try {
connection.close();
} catch (SQLException e1) {
e1.printStackTrace();
} finally {
connection = null;
}
}
}
@Override
public MapFileInfo getMapFileInfo() {
return mMapInfo;
}
@Override
public boolean hasOpenFile() {
return mOpenFile;
}
@Override
public FileOpenResult openFile(File mapFile) {
mOpenFile = true;
return new FileOpenResult();
}
@Override
public void closeFile() {
if (connection != null) {
try {
connection.close();
} catch (SQLException e1) {
e1.printStackTrace();
} finally {
connection = null;
}
}
mOpenFile = false;
}
@Override
public String readString(int position) {
return null;
}
// taken from postgis-java
private static ValueGetter valueGetterForEndian(byte[] bytes) {
if (bytes[0] == ValueGetter.XDR.NUMBER) { // XDR
return new ValueGetter.XDR(bytes);
} else if (bytes[0] == ValueGetter.NDR.NUMBER) {
return new ValueGetter.NDR(bytes);
} else {
throw new IllegalArgumentException("Unknown Endian type:" + bytes[0]);
}
}
/**
* Parse a binary encoded geometry. Is synchronized to protect offset counter. (Unfortunately, Java does not have
* neither call by reference nor multiple return values.)
*
* @param value
* ...
*/
private void parse(byte[] value) {
parseGeometry(valueGetterForEndian(value));
}
private void parseGeometry(ValueGetter data) {
byte endian = data.getByte(); // skip and test endian flag
if (endian != data.endian) {
throw new IllegalArgumentException("Endian inconsistency!");
}
int typeword = data.getInt();
int realtype = typeword & 0x1FFFFFFF; // cut off high flag bits
boolean haveZ = (typeword & 0x80000000) != 0;
boolean haveM = (typeword & 0x40000000) != 0;
boolean haveS = (typeword & 0x20000000) != 0;
// int srid = Geometry.UNKNOWN_SRID;
if (haveS) {
// srid = Geometry.parseSRID(data.getInt());
data.getInt();
}
switch (realtype) {
case Geometry.POINT:
parsePoint(data, haveZ, haveM);
break;
case Geometry.LINESTRING:
parseLineString(data, haveZ, haveM);
break;
case Geometry.POLYGON:
parsePolygon(data, haveZ, haveM);
break;
case Geometry.MULTIPOINT:
parseMultiPoint(data);
break;
case Geometry.MULTILINESTRING:
parseMultiLineString(data);
break;
case Geometry.MULTIPOLYGON:
parseMultiPolygon(data);
break;
case Geometry.GEOMETRYCOLLECTION:
parseCollection(data);
break;
default:
throw new IllegalArgumentException("Unknown Geometry Type: " + realtype);
}
// if (srid != Geometry.UNKNOWN_SRID) {
// result.setSrid(srid);
// }
}
private static void parsePoint(ValueGetter data, boolean haveZ, boolean haveM) {
// double X = data.getDouble();
// double Y = data.getDouble();
data.getDouble();
data.getDouble();
if (haveZ)
data.getDouble();
if (haveM)
data.getDouble();
}
/**
* Parse an Array of "full" Geometries
*
* @param data
* ...
* @param count
* ...
*/
private void parseGeometryArray(ValueGetter data, int count) {
for (int i = 0; i < count; i++) {
parseGeometry(data);
}
}
//
// private void parsePointArray(ValueGetter data, boolean haveZ, boolean haveM) {
// int count = data.getInt();
// for (int i = 0; i < count; i++) {
// parsePoint(data, haveZ, haveM);
// }
// }
private void parseMultiPoint(ValueGetter data) {
parseGeometryArray(data, data.getInt());
}
private void parseLineString(ValueGetter data, boolean haveZ, boolean haveM) {
int count = data.getInt();
for (int i = 0; i < count; i++) {
mCoords[mCoordPos++] = (float) (data.getDouble()) * mScale;
mCoords[mCoordPos++] = (float) (data.getDouble()) * mScale;
if (haveZ)
data.getDouble();
if (haveM)
data.getDouble();
}
mIndex[mIndexPos++] = count * 2;
}
private void parsePolygon(ValueGetter data, boolean haveZ, boolean haveM) {
int count = data.getInt();
for (int i = 0; i < count; i++) {
parseLineString(data, haveZ, haveM);
}
}
private void parseMultiLineString(ValueGetter data) {
int count = data.getInt();
parseGeometryArray(data, count);
}
private void parseMultiPolygon(ValueGetter data) {
int count = data.getInt();
parseGeometryArray(data, count);
}
private void parseCollection(ValueGetter data) {
int count = data.getInt();
parseGeometryArray(data, count);
}
// taken from mapsforge GeoUtils
// private static final double EQUATORIAL_RADIUS = 6378137.0;
// private static final double[] EPSILON_ZERO = new double[] { 0, 0 };
//
// static double latitudeDistance(int meters) {
// return (meters * 360) / (2 * Math.PI * EQUATORIAL_RADIUS);
// }
//
// static double longitudeDistance(int meters, double latitude) {
// return (meters * 360) / (2 * Math.PI * EQUATORIAL_RADIUS * Math.cos(Math.toRadians(latitude)));
// }
//
// private static double[] bufferInDegrees(long tileY, byte zoom, int enlargementInMeter) {
// if (enlargementInMeter == 0) {
// return EPSILON_ZERO;
// }
//
// double[] epsilons = new double[2];
// double lat = MercatorProjection.tileYToLatitude(tileY, zoom);
// epsilons[0] = latitudeDistance(enlargementInMeter);
// epsilons[1] = longitudeDistance(enlargementInMeter, lat);
//
// return epsilons;
// }
//
// static String tileToBOX3D(Tile tile, int pixel) {
// double minLat = MercatorProjection.pixelYToLatitude(tile.getPixelY() + Tile.TILE_SIZE + pixel, tile.zoomLevel);
// double maxLat = MercatorProjection.pixelYToLatitude(tile.getPixelY() - pixel, tile.zoomLevel);
//
// double minLon = MercatorProjection.pixelXToLongitude(tile.getPixelX() - pixel, tile.zoomLevel);
// double maxLon = MercatorProjection.pixelXToLongitude(tile.getPixelX() + Tile.TILE_SIZE + pixel, tile.zoomLevel);
//
// return "ST_SetSRID('BOX3D(" + minLon + " " + minLat + ", " + " " + maxLon + " " + maxLat + ")'::box3d ,4326)";
// }
//
// static String tileToBOX3D(Tile tile, int pixel, int projection) {
//
// double minLat = MercatorProjection.pixelYToLatitude(tile.getPixelY() + Tile.TILE_SIZE + pixel, tile.zoomLevel);
// double maxLat = MercatorProjection.pixelYToLatitude(tile.getPixelY() - pixel, tile.zoomLevel);
//
// double minLon = MercatorProjection.pixelXToLongitude(tile.getPixelX() - pixel, tile.zoomLevel);
// double maxLon = MercatorProjection.pixelXToLongitude(tile.getPixelX() + Tile.TILE_SIZE + pixel, tile.zoomLevel);
//
// return String
// .format("ST_Transform(ST_SetSRID('BOX3D(%f %f, %f %f)'::box3d , 4326), %d)",
// new Double(minLon), new Double(minLat), new Double(maxLon), new Double(maxLat), new Integer(
// projection));
// }
//
// static String tileToBOX3D(long tileX, long tileY, byte zoom, int enlargementInMeter) {
// double minLat = MercatorProjection.tileYToLatitude(tileY + 1, zoom);
// double maxLat = MercatorProjection.tileYToLatitude(tileY, zoom);
// double minLon = MercatorProjection.tileXToLongitude(tileX, zoom);
// double maxLon = MercatorProjection.tileXToLongitude(tileX + 1, zoom);
//
// double[] epsilons = bufferInDegrees(tileY, zoom, enlargementInMeter);
//
// minLon -= epsilons[1];
// minLat -= epsilons[0];
// maxLon += epsilons[1];
// maxLat += epsilons[0];
//
// return "ST_SetSRID('BOX3D(" + minLon + " " + minLat + ", " + " " + maxLon + " " + maxLat + ")'::box3d ,4326)";
// }
}

View File

@@ -0,0 +1,416 @@
/*
* This file has been copied from the following location:
* http://archives.postgresql.org/pgsql-jdbc/2009-12/msg00037.php
*
* PostgreSQL code is typically under a BSD licence.
* http://jdbc.postgresql.org/license.html
*/
/*-------------------------------------------------------------------------
*
* A preliminary version of a custom type wrapper for hstore data.
* Once it gets some testing and cleanups it will go into the official
* PG JDBC driver, but stick it here for now because we need it sooner.
*
* Copyright (c) 2009, PostgreSQL Global Development Group
*
* IDENTIFICATION
* $PostgreSQL$
*
*-------------------------------------------------------------------------
*/
package org.mapsforge.database.postgis;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.postgresql.util.PGobject;
/**
* This implements a class that handles the PostgreSQL contrib/hstore type
*/
public class PGHStore extends PGobject implements Map<String, String>
{
private final static long serialVersionUID = 1;
private Map<String, String> _map;
/**
* required by the driver
*/
public PGHStore()
{
setType("hstore");
_map = new HashMap<String, String>();
}
/**
* Initialize a hstore with a given string representation
*
* @param value
* String representated hstore
* @throws SQLException
* Is thrown if the string representation has an unknown format
* @see #setValue(String)
*/
public PGHStore(String value)
throws SQLException
{
this();
setValue(value);
}
/**
* @param map
* ...
*/
public PGHStore(Map<String, String> map)
{
this();
setValue(map);
}
/**
* @param map
* ...
*/
public void setValue(Map<String, String> map)
{
_map = map;
}
/**
*/
@Override
public void setValue(String value)
throws SQLException
{
Parser p = new Parser();
_map = p.parse(value);
}
/**
* Returns the stored information as a string
*
* @return String represented hstore
*/
@Override
public String getValue()
{
StringBuffer buf = new StringBuffer();
Iterator<String> i = _map.keySet().iterator();
boolean first = true;
while (i.hasNext()) {
Object key = i.next();
Object val = _map.get(key);
if (first) {
first = false;
} else {
buf.append(',');
}
writeValue(buf, key);
buf.append("=>");
writeValue(buf, val);
}
return buf.toString();
}
private static void writeValue(StringBuffer buf, Object o) {
if (o == null) {
buf.append("NULL");
return;
}
String s = o.toString();
buf.append('"');
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c == '"' || c == '\\') {
buf.append('\\');
}
buf.append(c);
}
buf.append('"');
}
/**
* Returns whether an object is equal to this one or not
*
* @param obj
* Object to compare with
* @return true if the two hstores are identical
*/
@Override
public boolean equals(Object obj)
{
if (obj == null)
return false;
if (obj == this)
return true;
if (!(obj instanceof PGHStore))
return false;
return _map.equals(((PGHStore) obj)._map);
}
private static class Parser {
private String value;
private int ptr;
private StringBuffer cur;
private boolean escaped;
private List<String> keys;
private List<String> values;
private final static int GV_WAITVAL = 0;
private final static int GV_INVAL = 1;
private final static int GV_INESCVAL = 2;
private final static int GV_WAITESCIN = 3;
private final static int GV_WAITESCESCIN = 4;
private final static int WKEY = 0;
private final static int WVAL = 1;
private final static int WEQ = 2;
private final static int WGT = 3;
private final static int WDEL = 4;
public Parser() {
}
Map<String, String> parse(String val) throws SQLException {
this.value = val;
ptr = 0;
keys = new ArrayList<String>();
values = new ArrayList<String>();
parseHStore();
Map<String, String> map = new HashMap<String, String>();
for (int i = 0; i < keys.size(); i++) {
map.put(keys.get(i), values.get(i));
}
return map;
}
private boolean getValue(boolean ignoreEqual) throws SQLException {
int state = GV_WAITVAL;
cur = new StringBuffer();
escaped = false;
while (true) {
boolean atEnd = (value.length() == ptr);
char c = '\0';
if (!atEnd) {
c = value.charAt(ptr);
}
if (state == GV_WAITVAL) {
if (c == '"') {
escaped = true;
state = GV_INESCVAL;
} else if (c == '\0') {
return false;
} else if (c == '=' && !ignoreEqual) {
throw new SQLException("KJJ");
} else if (c == '\\') {
state = GV_WAITESCIN;
} else if (!Character.isWhitespace(c)) {
cur.append(c);
state = GV_INVAL;
}
} else if (state == GV_INVAL) {
if (c == '\\') {
state = GV_WAITESCIN;
} else if (c == '=' && !ignoreEqual) {
ptr--;
return true;
} else if (c == ',' && ignoreEqual) {
ptr--;
return true;
} else if (Character.isWhitespace(c)) {
return true;
} else if (c == '\0') {
ptr--;
return true;
} else {
cur.append(c);
}
} else if (state == GV_INESCVAL) {
if (c == '\\') {
state = GV_WAITESCESCIN;
} else if (c == '"') {
return true;
} else if (c == '\0') {
throw new SQLException("KJJ, unexpected end of string");
} else {
cur.append(c);
}
} else if (state == GV_WAITESCIN) {
if (c == '\0') {
throw new SQLException("KJJ, unexpected end of string");
}
cur.append(c);
state = GV_INVAL;
} else if (state == GV_WAITESCESCIN) {
if (c == '\0') {
throw new SQLException("KJJ, unexpected end of string");
}
cur.append(c);
state = GV_INESCVAL;
} else {
throw new SQLException("KJJ");
}
ptr++;
}
}
private void parseHStore() throws SQLException {
int state = WKEY;
escaped = false;
while (true) {
char c = '\0';
if (ptr < value.length()) {
c = value.charAt(ptr);
}
if (state == WKEY) {
if (!getValue(false))
return;
keys.add(cur.toString());
cur = null;
state = WEQ;
} else if (state == WEQ) {
if (c == '=') {
state = WGT;
} else if (state == '\0') {
throw new SQLException("KJJ, unexpected end of string");
} else if (!Character.isWhitespace(c)) {
throw new SQLException("KJJ, syntax err");
}
} else if (state == WGT) {
if (c == '>') {
state = WVAL;
} else if (c == '\0') {
throw new SQLException("KJJ, unexpected end of string");
} else {
throw new SQLException("KJJ, syntax err [" + c + "] at " + ptr);
}
} else if (state == WVAL) {
if (!getValue(true)) {
throw new SQLException("KJJ, unexpected end of string");
}
String val = cur.toString();
cur = null;
if (!escaped && "null".equalsIgnoreCase(val)) {
val = null;
}
values.add(val);
state = WDEL;
} else if (state == WDEL) {
if (c == ',')
{
state = WKEY;
} else if (c == '\0') {
return;
} else if (!Character.isWhitespace(c)) {
throw new SQLException("KJJ, syntax err");
}
} else {
throw new SQLException("KJJ unknown state");
}
ptr++;
}
}
}
// Farm out all the work to the real underlying map.
@Override
public void clear() {
_map.clear();
}
@Override
public boolean containsKey(Object key) {
return _map.containsKey(key);
}
@Override
public boolean containsValue(Object val) {
return _map.containsValue(val);
}
@Override
public Set<Map.Entry<String, String>> entrySet() {
return _map.entrySet();
}
@Override
public String get(Object key) {
return _map.get(key);
}
@Override
public int hashCode() {
return _map.hashCode();
}
@Override
public boolean isEmpty() {
return _map.isEmpty();
}
@Override
public Set<String> keySet() {
return _map.keySet();
}
@Override
public String put(String key, String val) {
return _map.put(key, val);
}
@Override
public void putAll(Map<? extends String, ? extends String> m) {
_map.putAll(m);
}
@Override
public String remove(Object key) {
return _map.remove(key);
}
@Override
public int size() {
return _map.size();
}
@Override
public Collection<String> values() {
return _map.values();
}
}

View File

@@ -0,0 +1,132 @@
/*
* ValueGetter.java
*
* PostGIS extension for PostgreSQL JDBC driver - Binary Parser
*
* (C) 2005 Markus Schaber, markus.schaber@logix-tt.com
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General License as published by the Free
* Software Foundation, either version 2.1 of the License.
*
* This library 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 License for more
* details.
*
* You should have received a copy of the GNU Lesser General License
* along with this library; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA or visit the web at
* http://www.gnu.org.
*
* $Id: ValueGetter.java 9324 2012-02-27 22:08:12Z pramsey $
*/
package org.mapsforge.database.postgis;
abstract class ValueGetter {
byte[] data;
int position;
final byte endian;
ValueGetter(byte[] data, byte endian) {
this.data = data;
this.endian = endian;
}
/**
* Get a byte, should be equal for all endians
*
* @return ...
*/
byte getByte() {
return data[position++];
}
int getInt() {
int res = getInt(position);
position += 4;
return res;
}
long getLong() {
long res = getLong(position);
position += 8;
return res;
}
/**
* Get a 32-Bit integer
*
* @param index
* ...
* @return ...
*/
protected abstract int getInt(int index);
/**
* Get a long value. This is not needed directly, but as a nice side-effect from GetDouble.
*
* @param index
* ...
* @return ...
*/
protected abstract long getLong(int index);
/**
* Get a double.
*
* @return ...
*/
double getDouble() {
long bitrep = getLong();
return Double.longBitsToDouble(bitrep);
}
static class XDR extends ValueGetter {
static final byte NUMBER = 0;
XDR(byte[] data) {
super(data, NUMBER);
}
@Override
protected int getInt(int index) {
return ((data[index] & 0xFF) << 24) + ((data[index + 1] & 0xFF) << 16)
+ ((data[index + 2] & 0xFF) << 8) + (data[index + 3] & 0xFF);
}
@Override
protected long getLong(int index) {
return ((long) (data[index] & 0xFF) << 56) | ((long) (data[index + 1] & 0xFF) << 48)
| ((long) (data[index + 2] & 0xFF) << 40) | ((long) (data[index + 3] & 0xFF) << 32)
| ((long) (data[index + 4] & 0xFF) << 24) | ((long) (data[index + 5] & 0xFF) << 16)
| ((long) (data[index + 6] & 0xFF) << 8) | ((long) (data[index + 7] & 0xFF) << 0);
}
}
static class NDR extends ValueGetter {
static final byte NUMBER = 1;
NDR(byte[] data) {
super(data, NUMBER);
}
@Override
protected int getInt(int index) {
return ((data[index + 3] & 0xFF) << 24) + ((data[index + 2] & 0xFF) << 16)
+ ((data[index + 1] & 0xFF) << 8) + (data[index] & 0xFF);
}
@Override
protected long getLong(int index) {
return ((long) (data[index + 7] & 0xFF) << 56) | ((long) (data[index + 6] & 0xFF) << 48)
| ((long) (data[index + 5] & 0xFF) << 40) | ((long) (data[index + 4] & 0xFF) << 32)
| ((long) (data[index + 3] & 0xFF) << 24) | ((long) (data[index + 2] & 0xFF) << 16)
| ((long) (data[index + 1] & 0xFF) << 8) | ((long) (data[index] & 0xFF) << 0);
}
}
}