switch package org.mapsforge -> org.oscim
This commit is contained in:
251
src/org/oscim/database/mapfile/header/MapFileHeader.java
Normal file
251
src/org/oscim/database/mapfile/header/MapFileHeader.java
Normal file
@@ -0,0 +1,251 @@
|
||||
/*
|
||||
* Copyright 2010, 2011, 2012 mapsforge.org
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it under the
|
||||
* terms of the GNU Lesser General Public License as published by the Free Software
|
||||
* Foundation, either version 3 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
|
||||
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
|
||||
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License along with
|
||||
* this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package org.oscim.database.mapfile.header;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.oscim.database.OpenResult;
|
||||
import org.oscim.database.mapfile.ReadBuffer;
|
||||
|
||||
/**
|
||||
* Reads and validates the header data from a binary map file.
|
||||
*/
|
||||
public class MapFileHeader {
|
||||
/**
|
||||
* Maximum valid base zoom level of a sub-file.
|
||||
*/
|
||||
private static final int BASE_ZOOM_LEVEL_MAX = 20;
|
||||
|
||||
/**
|
||||
* Minimum size of the file header in bytes.
|
||||
*/
|
||||
private static final int HEADER_SIZE_MIN = 70;
|
||||
|
||||
/**
|
||||
* Length of the debug signature at the beginning of the index.
|
||||
*/
|
||||
private static final byte SIGNATURE_LENGTH_INDEX = 16;
|
||||
|
||||
/**
|
||||
* A single whitespace character.
|
||||
*/
|
||||
private static final char SPACE = ' ';
|
||||
|
||||
private MapFileInfo mapFileInfo;
|
||||
private SubFileParameter[] subFileParameters;
|
||||
private byte zoomLevelMaximum;
|
||||
private byte zoomLevelMinimum;
|
||||
|
||||
/**
|
||||
* @return a MapFileInfo containing the header data.
|
||||
*/
|
||||
public MapFileInfo getMapFileInfo() {
|
||||
return this.mapFileInfo;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param zoomLevel
|
||||
* the originally requested zoom level.
|
||||
* @return the closest possible zoom level which is covered by a sub-file.
|
||||
*/
|
||||
public byte getQueryZoomLevel(byte zoomLevel) {
|
||||
if (zoomLevel > this.zoomLevelMaximum) {
|
||||
return this.zoomLevelMaximum;
|
||||
} else if (zoomLevel < this.zoomLevelMinimum) {
|
||||
return this.zoomLevelMinimum;
|
||||
}
|
||||
return zoomLevel;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param queryZoomLevel
|
||||
* the zoom level for which the sub-file parameters are needed.
|
||||
* @return the sub-file parameters for the given zoom level.
|
||||
*/
|
||||
public SubFileParameter getSubFileParameter(int queryZoomLevel) {
|
||||
return this.subFileParameters[queryZoomLevel];
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads and validates the header block from the map file.
|
||||
*
|
||||
* @param readBuffer
|
||||
* the ReadBuffer for the file data.
|
||||
* @param fileSize
|
||||
* the size of the map file in bytes.
|
||||
* @return a FileOpenResult containing an error message in case of a failure.
|
||||
* @throws IOException
|
||||
* if an error occurs while reading the file.
|
||||
*/
|
||||
public OpenResult readHeader(ReadBuffer readBuffer, long fileSize) throws IOException {
|
||||
OpenResult openResult = RequiredFields.readMagicByte(readBuffer);
|
||||
if (!openResult.isSuccess()) {
|
||||
return openResult;
|
||||
}
|
||||
|
||||
openResult = RequiredFields.readRemainingHeader(readBuffer);
|
||||
if (!openResult.isSuccess()) {
|
||||
return openResult;
|
||||
}
|
||||
|
||||
MapFileInfoBuilder mapFileInfoBuilder = new MapFileInfoBuilder();
|
||||
|
||||
openResult = RequiredFields.readFileVersion(readBuffer, mapFileInfoBuilder);
|
||||
if (!openResult.isSuccess()) {
|
||||
return openResult;
|
||||
}
|
||||
|
||||
openResult = RequiredFields.readFileSize(readBuffer, fileSize, mapFileInfoBuilder);
|
||||
if (!openResult.isSuccess()) {
|
||||
return openResult;
|
||||
}
|
||||
|
||||
openResult = RequiredFields.readMapDate(readBuffer, mapFileInfoBuilder);
|
||||
if (!openResult.isSuccess()) {
|
||||
return openResult;
|
||||
}
|
||||
|
||||
openResult = RequiredFields.readBoundingBox(readBuffer, mapFileInfoBuilder);
|
||||
if (!openResult.isSuccess()) {
|
||||
return openResult;
|
||||
}
|
||||
|
||||
openResult = RequiredFields.readTilePixelSize(readBuffer, mapFileInfoBuilder);
|
||||
if (!openResult.isSuccess()) {
|
||||
return openResult;
|
||||
}
|
||||
|
||||
openResult = RequiredFields.readProjectionName(readBuffer, mapFileInfoBuilder);
|
||||
if (!openResult.isSuccess()) {
|
||||
return openResult;
|
||||
}
|
||||
|
||||
openResult = OptionalFields.readOptionalFields(readBuffer, mapFileInfoBuilder);
|
||||
if (!openResult.isSuccess()) {
|
||||
return openResult;
|
||||
}
|
||||
|
||||
openResult = RequiredFields.readPoiTags(readBuffer, mapFileInfoBuilder);
|
||||
if (!openResult.isSuccess()) {
|
||||
return openResult;
|
||||
}
|
||||
|
||||
openResult = RequiredFields.readWayTags(readBuffer, mapFileInfoBuilder);
|
||||
if (!openResult.isSuccess()) {
|
||||
return openResult;
|
||||
}
|
||||
|
||||
openResult = readSubFileParameters(readBuffer, fileSize, mapFileInfoBuilder);
|
||||
if (!openResult.isSuccess()) {
|
||||
return openResult;
|
||||
}
|
||||
|
||||
this.mapFileInfo = mapFileInfoBuilder.build();
|
||||
return OpenResult.SUCCESS;
|
||||
}
|
||||
|
||||
private OpenResult readSubFileParameters(ReadBuffer readBuffer, long fileSize,
|
||||
MapFileInfoBuilder mapFileInfoBuilder) {
|
||||
// get and check the number of sub-files (1 byte)
|
||||
byte numberOfSubFiles = readBuffer.readByte();
|
||||
if (numberOfSubFiles < 1) {
|
||||
return new OpenResult("invalid number of sub-files: " + numberOfSubFiles);
|
||||
}
|
||||
mapFileInfoBuilder.numberOfSubFiles = numberOfSubFiles;
|
||||
|
||||
SubFileParameter[] tempSubFileParameters = new SubFileParameter[numberOfSubFiles];
|
||||
this.zoomLevelMinimum = Byte.MAX_VALUE;
|
||||
this.zoomLevelMaximum = Byte.MIN_VALUE;
|
||||
|
||||
// get and check the information for each sub-file
|
||||
for (byte currentSubFile = 0; currentSubFile < numberOfSubFiles; ++currentSubFile) {
|
||||
SubFileParameterBuilder subFileParameterBuilder = new SubFileParameterBuilder();
|
||||
|
||||
// get and check the base zoom level (1 byte)
|
||||
byte baseZoomLevel = readBuffer.readByte();
|
||||
if (baseZoomLevel < 0 || baseZoomLevel > BASE_ZOOM_LEVEL_MAX) {
|
||||
return new OpenResult("invalid base zooom level: " + baseZoomLevel);
|
||||
}
|
||||
subFileParameterBuilder.baseZoomLevel = baseZoomLevel;
|
||||
|
||||
// get and check the minimum zoom level (1 byte)
|
||||
byte zoomLevelMin = readBuffer.readByte();
|
||||
if (zoomLevelMin < 0 || zoomLevelMin > 22) {
|
||||
return new OpenResult("invalid minimum zoom level: " + zoomLevelMin);
|
||||
}
|
||||
subFileParameterBuilder.zoomLevelMin = zoomLevelMin;
|
||||
|
||||
// get and check the maximum zoom level (1 byte)
|
||||
byte zoomLevelMax = readBuffer.readByte();
|
||||
if (zoomLevelMax < 0 || zoomLevelMax > 22) {
|
||||
return new OpenResult("invalid maximum zoom level: " + zoomLevelMax);
|
||||
}
|
||||
subFileParameterBuilder.zoomLevelMax = zoomLevelMax;
|
||||
|
||||
// check for valid zoom level range
|
||||
if (zoomLevelMin > zoomLevelMax) {
|
||||
return new OpenResult("invalid zoom level range: " + zoomLevelMin + SPACE + zoomLevelMax);
|
||||
}
|
||||
|
||||
// get and check the start address of the sub-file (8 bytes)
|
||||
long startAddress = readBuffer.readLong();
|
||||
if (startAddress < HEADER_SIZE_MIN || startAddress >= fileSize) {
|
||||
return new OpenResult("invalid start address: " + startAddress);
|
||||
}
|
||||
subFileParameterBuilder.startAddress = startAddress;
|
||||
|
||||
long indexStartAddress = startAddress;
|
||||
if (mapFileInfoBuilder.optionalFields.isDebugFile) {
|
||||
// the sub-file has an index signature before the index
|
||||
indexStartAddress += SIGNATURE_LENGTH_INDEX;
|
||||
}
|
||||
subFileParameterBuilder.indexStartAddress = indexStartAddress;
|
||||
|
||||
// get and check the size of the sub-file (8 bytes)
|
||||
long subFileSize = readBuffer.readLong();
|
||||
if (subFileSize < 1) {
|
||||
return new OpenResult("invalid sub-file size: " + subFileSize);
|
||||
}
|
||||
subFileParameterBuilder.subFileSize = subFileSize;
|
||||
|
||||
subFileParameterBuilder.boundingBox = mapFileInfoBuilder.boundingBox;
|
||||
|
||||
// add the current sub-file to the list of sub-files
|
||||
tempSubFileParameters[currentSubFile] = subFileParameterBuilder.build();
|
||||
|
||||
updateZoomLevelInformation(tempSubFileParameters[currentSubFile]);
|
||||
}
|
||||
|
||||
// create and fill the lookup table for the sub-files
|
||||
this.subFileParameters = new SubFileParameter[this.zoomLevelMaximum + 1];
|
||||
for (int currentMapFile = 0; currentMapFile < numberOfSubFiles; ++currentMapFile) {
|
||||
SubFileParameter subFileParameter = tempSubFileParameters[currentMapFile];
|
||||
for (byte zoomLevel = subFileParameter.zoomLevelMin; zoomLevel <= subFileParameter.zoomLevelMax; ++zoomLevel) {
|
||||
this.subFileParameters[zoomLevel] = subFileParameter;
|
||||
}
|
||||
}
|
||||
return OpenResult.SUCCESS;
|
||||
}
|
||||
|
||||
private void updateZoomLevelInformation(SubFileParameter subFileParameter) {
|
||||
// update the global minimum and maximum zoom level information
|
||||
if (this.zoomLevelMinimum > subFileParameter.zoomLevelMin) {
|
||||
this.zoomLevelMinimum = subFileParameter.zoomLevelMin;
|
||||
}
|
||||
if (this.zoomLevelMaximum < subFileParameter.zoomLevelMax) {
|
||||
this.zoomLevelMaximum = subFileParameter.zoomLevelMax;
|
||||
}
|
||||
}
|
||||
}
|
||||
72
src/org/oscim/database/mapfile/header/MapFileInfo.java
Normal file
72
src/org/oscim/database/mapfile/header/MapFileInfo.java
Normal file
@@ -0,0 +1,72 @@
|
||||
/*
|
||||
* Copyright 2010, 2011, 2012 mapsforge.org
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it under the
|
||||
* terms of the GNU Lesser General Public License as published by the Free Software
|
||||
* Foundation, either version 3 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
|
||||
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
|
||||
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License along with
|
||||
* this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package org.oscim.database.mapfile.header;
|
||||
|
||||
import org.oscim.core.Tag;
|
||||
import org.oscim.database.mapfile.MapDatabase;
|
||||
|
||||
/**
|
||||
* Contains the immutable metadata of a map file.
|
||||
*
|
||||
* @see MapDatabase#getMapInfo()
|
||||
*/
|
||||
public class MapFileInfo extends org.oscim.database.MapInfo {
|
||||
|
||||
/**
|
||||
* True if the map file includes debug information, false otherwise.
|
||||
*/
|
||||
public final boolean debugFile;
|
||||
|
||||
/**
|
||||
* The number of sub-files in the map file.
|
||||
*/
|
||||
public final byte numberOfSubFiles;
|
||||
|
||||
/**
|
||||
* The POI tags.
|
||||
*/
|
||||
public final Tag[] poiTags;
|
||||
|
||||
/**
|
||||
* The way tags.
|
||||
*/
|
||||
public final Tag[] wayTags;
|
||||
|
||||
/**
|
||||
* The size of the tiles in pixels.
|
||||
*/
|
||||
public final int tilePixelSize;
|
||||
|
||||
MapFileInfo(MapFileInfoBuilder mapFileInfoBuilder) {
|
||||
super(mapFileInfoBuilder.boundingBox,
|
||||
mapFileInfoBuilder.optionalFields.startZoomLevel,
|
||||
mapFileInfoBuilder.optionalFields.startPosition,
|
||||
mapFileInfoBuilder.projectionName,
|
||||
mapFileInfoBuilder.mapDate,
|
||||
mapFileInfoBuilder.fileSize,
|
||||
mapFileInfoBuilder.fileVersion,
|
||||
mapFileInfoBuilder.optionalFields.languagePreference,
|
||||
mapFileInfoBuilder.optionalFields.comment,
|
||||
mapFileInfoBuilder.optionalFields.createdBy);
|
||||
|
||||
debugFile = mapFileInfoBuilder.optionalFields.isDebugFile;
|
||||
|
||||
numberOfSubFiles = mapFileInfoBuilder.numberOfSubFiles;
|
||||
poiTags = mapFileInfoBuilder.poiTags;
|
||||
|
||||
tilePixelSize = mapFileInfoBuilder.tilePixelSize;
|
||||
wayTags = mapFileInfoBuilder.wayTags;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
* Copyright 2010, 2011, 2012 mapsforge.org
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it under the
|
||||
* terms of the GNU Lesser General Public License as published by the Free Software
|
||||
* Foundation, either version 3 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
|
||||
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
|
||||
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License along with
|
||||
* this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package org.oscim.database.mapfile.header;
|
||||
|
||||
import org.oscim.core.BoundingBox;
|
||||
import org.oscim.core.Tag;
|
||||
|
||||
class MapFileInfoBuilder {
|
||||
BoundingBox boundingBox;
|
||||
long fileSize;
|
||||
int fileVersion;
|
||||
long mapDate;
|
||||
byte numberOfSubFiles;
|
||||
OptionalFields optionalFields;
|
||||
Tag[] poiTags;
|
||||
String projectionName;
|
||||
int tilePixelSize;
|
||||
Tag[] wayTags;
|
||||
|
||||
MapFileInfo build() {
|
||||
return new MapFileInfo(this);
|
||||
}
|
||||
}
|
||||
163
src/org/oscim/database/mapfile/header/OptionalFields.java
Normal file
163
src/org/oscim/database/mapfile/header/OptionalFields.java
Normal file
@@ -0,0 +1,163 @@
|
||||
/*
|
||||
* Copyright 2010, 2011, 2012 mapsforge.org
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it under the
|
||||
* terms of the GNU Lesser General Public License as published by the Free Software
|
||||
* Foundation, either version 3 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
|
||||
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
|
||||
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License along with
|
||||
* this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package org.oscim.database.mapfile.header;
|
||||
|
||||
import org.oscim.core.GeoPoint;
|
||||
import org.oscim.database.OpenResult;
|
||||
import org.oscim.database.mapfile.ReadBuffer;
|
||||
|
||||
final class OptionalFields {
|
||||
/**
|
||||
* Bitmask for the comment field in the file header.
|
||||
*/
|
||||
private static final int HEADER_BITMASK_COMMENT = 0x08;
|
||||
|
||||
/**
|
||||
* Bitmask for the created by field in the file header.
|
||||
*/
|
||||
private static final int HEADER_BITMASK_CREATED_BY = 0x04;
|
||||
|
||||
/**
|
||||
* Bitmask for the debug flag in the file header.
|
||||
*/
|
||||
private static final int HEADER_BITMASK_DEBUG = 0x80;
|
||||
|
||||
/**
|
||||
* Bitmask for the language preference field in the file header.
|
||||
*/
|
||||
private static final int HEADER_BITMASK_LANGUAGE_PREFERENCE = 0x10;
|
||||
|
||||
/**
|
||||
* Bitmask for the start position field in the file header.
|
||||
*/
|
||||
private static final int HEADER_BITMASK_START_POSITION = 0x40;
|
||||
|
||||
/**
|
||||
* Bitmask for the start zoom level field in the file header.
|
||||
*/
|
||||
private static final int HEADER_BITMASK_START_ZOOM_LEVEL = 0x20;
|
||||
|
||||
/**
|
||||
* The length of the language preference string.
|
||||
*/
|
||||
private static final int LANGUAGE_PREFERENCE_LENGTH = 2;
|
||||
|
||||
/**
|
||||
* Maximum valid start zoom level.
|
||||
*/
|
||||
private static final int START_ZOOM_LEVEL_MAX = 22;
|
||||
|
||||
static OpenResult readOptionalFields(ReadBuffer readBuffer, MapFileInfoBuilder mapFileInfoBuilder) {
|
||||
OptionalFields optionalFields = new OptionalFields(readBuffer.readByte());
|
||||
mapFileInfoBuilder.optionalFields = optionalFields;
|
||||
|
||||
OpenResult openResult = optionalFields.readOptionalFields(readBuffer);
|
||||
if (!openResult.isSuccess()) {
|
||||
return openResult;
|
||||
}
|
||||
return OpenResult.SUCCESS;
|
||||
}
|
||||
|
||||
String comment;
|
||||
String createdBy;
|
||||
final boolean hasComment;
|
||||
final boolean hasCreatedBy;
|
||||
final boolean hasLanguagePreference;
|
||||
final boolean hasStartPosition;
|
||||
final boolean hasStartZoomLevel;
|
||||
final boolean isDebugFile;
|
||||
String languagePreference;
|
||||
GeoPoint startPosition;
|
||||
Byte startZoomLevel;
|
||||
|
||||
private OptionalFields(byte flags) {
|
||||
this.isDebugFile = (flags & HEADER_BITMASK_DEBUG) != 0;
|
||||
this.hasStartPosition = (flags & HEADER_BITMASK_START_POSITION) != 0;
|
||||
this.hasStartZoomLevel = (flags & HEADER_BITMASK_START_ZOOM_LEVEL) != 0;
|
||||
this.hasLanguagePreference = (flags & HEADER_BITMASK_LANGUAGE_PREFERENCE) != 0;
|
||||
this.hasComment = (flags & HEADER_BITMASK_COMMENT) != 0;
|
||||
this.hasCreatedBy = (flags & HEADER_BITMASK_CREATED_BY) != 0;
|
||||
}
|
||||
|
||||
private OpenResult readLanguagePreference(ReadBuffer readBuffer) {
|
||||
if (this.hasLanguagePreference) {
|
||||
String countryCode = readBuffer.readUTF8EncodedString();
|
||||
if (countryCode.length() != LANGUAGE_PREFERENCE_LENGTH) {
|
||||
return new OpenResult("invalid language preference: " + countryCode);
|
||||
}
|
||||
this.languagePreference = countryCode;
|
||||
}
|
||||
return OpenResult.SUCCESS;
|
||||
}
|
||||
|
||||
private OpenResult readMapStartPosition(ReadBuffer readBuffer) {
|
||||
if (this.hasStartPosition) {
|
||||
// get and check the start position latitude (4 byte)
|
||||
int mapStartLatitude = readBuffer.readInt();
|
||||
if (mapStartLatitude < RequiredFields.LATITUDE_MIN || mapStartLatitude > RequiredFields.LATITUDE_MAX) {
|
||||
return new OpenResult("invalid map start latitude: " + mapStartLatitude);
|
||||
}
|
||||
|
||||
// get and check the start position longitude (4 byte)
|
||||
int mapStartLongitude = readBuffer.readInt();
|
||||
if (mapStartLongitude < RequiredFields.LONGITUDE_MIN || mapStartLongitude > RequiredFields.LONGITUDE_MAX) {
|
||||
return new OpenResult("invalid map start longitude: " + mapStartLongitude);
|
||||
}
|
||||
|
||||
this.startPosition = new GeoPoint(mapStartLatitude, mapStartLongitude);
|
||||
}
|
||||
return OpenResult.SUCCESS;
|
||||
}
|
||||
|
||||
private OpenResult readMapStartZoomLevel(ReadBuffer readBuffer) {
|
||||
if (this.hasStartZoomLevel) {
|
||||
// get and check the start zoom level (1 byte)
|
||||
byte mapStartZoomLevel = readBuffer.readByte();
|
||||
if (mapStartZoomLevel < 0 || mapStartZoomLevel > START_ZOOM_LEVEL_MAX) {
|
||||
return new OpenResult("invalid map start zoom level: " + mapStartZoomLevel);
|
||||
}
|
||||
|
||||
this.startZoomLevel = Byte.valueOf(mapStartZoomLevel);
|
||||
}
|
||||
return OpenResult.SUCCESS;
|
||||
}
|
||||
|
||||
private OpenResult readOptionalFields(ReadBuffer readBuffer) {
|
||||
OpenResult openResult = readMapStartPosition(readBuffer);
|
||||
if (!openResult.isSuccess()) {
|
||||
return openResult;
|
||||
}
|
||||
|
||||
openResult = readMapStartZoomLevel(readBuffer);
|
||||
if (!openResult.isSuccess()) {
|
||||
return openResult;
|
||||
}
|
||||
|
||||
openResult = readLanguagePreference(readBuffer);
|
||||
if (!openResult.isSuccess()) {
|
||||
return openResult;
|
||||
}
|
||||
|
||||
if (this.hasComment) {
|
||||
this.comment = readBuffer.readUTF8EncodedString();
|
||||
}
|
||||
|
||||
if (this.hasCreatedBy) {
|
||||
this.createdBy = readBuffer.readUTF8EncodedString();
|
||||
}
|
||||
|
||||
return OpenResult.SUCCESS;
|
||||
}
|
||||
}
|
||||
235
src/org/oscim/database/mapfile/header/RequiredFields.java
Normal file
235
src/org/oscim/database/mapfile/header/RequiredFields.java
Normal file
@@ -0,0 +1,235 @@
|
||||
/*
|
||||
* Copyright 2010, 2011, 2012 mapsforge.org
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it under the
|
||||
* terms of the GNU Lesser General Public License as published by the Free Software
|
||||
* Foundation, either version 3 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
|
||||
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
|
||||
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License along with
|
||||
* this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package org.oscim.database.mapfile.header;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.oscim.core.BoundingBox;
|
||||
import org.oscim.core.Tag;
|
||||
import org.oscim.database.OpenResult;
|
||||
import org.oscim.database.mapfile.ReadBuffer;
|
||||
|
||||
final class RequiredFields {
|
||||
/**
|
||||
* Magic byte at the beginning of a valid binary map file.
|
||||
*/
|
||||
private static final String BINARY_OSM_MAGIC_BYTE = "mapsforge binary OSM";
|
||||
|
||||
/**
|
||||
* Maximum size of the file header in bytes.
|
||||
*/
|
||||
private static final int HEADER_SIZE_MAX = 1000000;
|
||||
|
||||
/**
|
||||
* Minimum size of the file header in bytes.
|
||||
*/
|
||||
private static final int HEADER_SIZE_MIN = 70;
|
||||
|
||||
/**
|
||||
* The name of the Mercator projection as stored in the file header.
|
||||
*/
|
||||
private static final String MERCATOR = "Mercator";
|
||||
|
||||
/**
|
||||
* A single whitespace character.
|
||||
*/
|
||||
private static final char SPACE = ' ';
|
||||
|
||||
/**
|
||||
* Version of the map file format which is supported by this implementation.
|
||||
*/
|
||||
private static final int SUPPORTED_FILE_VERSION = 4;
|
||||
|
||||
/**
|
||||
* The maximum latitude values in microdegrees.
|
||||
*/
|
||||
static final int LATITUDE_MAX = 90000000;
|
||||
|
||||
/**
|
||||
* The minimum latitude values in microdegrees.
|
||||
*/
|
||||
static final int LATITUDE_MIN = -90000000;
|
||||
|
||||
/**
|
||||
* The maximum longitude values in microdegrees.
|
||||
*/
|
||||
static final int LONGITUDE_MAX = 180000000;
|
||||
|
||||
/**
|
||||
* The minimum longitude values in microdegrees.
|
||||
*/
|
||||
static final int LONGITUDE_MIN = -180000000;
|
||||
|
||||
static OpenResult readBoundingBox(ReadBuffer readBuffer, MapFileInfoBuilder mapFileInfoBuilder) {
|
||||
// get and check the minimum latitude (4 bytes)
|
||||
int minLatitude = readBuffer.readInt();
|
||||
if (minLatitude < LATITUDE_MIN || minLatitude > LATITUDE_MAX) {
|
||||
return new OpenResult("invalid minimum latitude: " + minLatitude);
|
||||
}
|
||||
|
||||
// get and check the minimum longitude (4 bytes)
|
||||
int minLongitude = readBuffer.readInt();
|
||||
if (minLongitude < LONGITUDE_MIN || minLongitude > LONGITUDE_MAX) {
|
||||
return new OpenResult("invalid minimum longitude: " + minLongitude);
|
||||
}
|
||||
|
||||
// get and check the maximum latitude (4 bytes)
|
||||
int maxLatitude = readBuffer.readInt();
|
||||
if (maxLatitude < LATITUDE_MIN || maxLatitude > LATITUDE_MAX) {
|
||||
return new OpenResult("invalid maximum latitude: " + maxLatitude);
|
||||
}
|
||||
|
||||
// get and check the maximum longitude (4 bytes)
|
||||
int maxLongitude = readBuffer.readInt();
|
||||
if (maxLongitude < LONGITUDE_MIN || maxLongitude > LONGITUDE_MAX) {
|
||||
return new OpenResult("invalid maximum longitude: " + maxLongitude);
|
||||
}
|
||||
|
||||
// check latitude and longitude range
|
||||
if (minLatitude > maxLatitude) {
|
||||
return new OpenResult("invalid latitude range: " + minLatitude + SPACE + maxLatitude);
|
||||
} else if (minLongitude > maxLongitude) {
|
||||
return new OpenResult("invalid longitude range: " + minLongitude + SPACE + maxLongitude);
|
||||
}
|
||||
|
||||
mapFileInfoBuilder.boundingBox = new BoundingBox(minLatitude, minLongitude, maxLatitude, maxLongitude);
|
||||
return OpenResult.SUCCESS;
|
||||
}
|
||||
|
||||
static OpenResult readFileSize(ReadBuffer readBuffer, long fileSize, MapFileInfoBuilder mapFileInfoBuilder) {
|
||||
// get and check the file size (8 bytes)
|
||||
long headerFileSize = readBuffer.readLong();
|
||||
if (headerFileSize != fileSize) {
|
||||
return new OpenResult("invalid file size: " + headerFileSize);
|
||||
}
|
||||
mapFileInfoBuilder.fileSize = fileSize;
|
||||
return OpenResult.SUCCESS;
|
||||
}
|
||||
|
||||
static OpenResult readFileVersion(ReadBuffer readBuffer, MapFileInfoBuilder mapFileInfoBuilder) {
|
||||
// get and check the file version (4 bytes)
|
||||
int fileVersion = readBuffer.readInt();
|
||||
if (fileVersion != SUPPORTED_FILE_VERSION) {
|
||||
return new OpenResult("unsupported file version: " + fileVersion);
|
||||
}
|
||||
mapFileInfoBuilder.fileVersion = fileVersion;
|
||||
return OpenResult.SUCCESS;
|
||||
}
|
||||
|
||||
static OpenResult readMagicByte(ReadBuffer readBuffer) throws IOException {
|
||||
// read the the magic byte and the file header size into the buffer
|
||||
int magicByteLength = BINARY_OSM_MAGIC_BYTE.length();
|
||||
if (!readBuffer.readFromFile(magicByteLength + 4)) {
|
||||
return new OpenResult("reading magic byte has failed");
|
||||
}
|
||||
|
||||
// get and check the magic byte
|
||||
String magicByte = readBuffer.readUTF8EncodedString(magicByteLength);
|
||||
if (!BINARY_OSM_MAGIC_BYTE.equals(magicByte)) {
|
||||
return new OpenResult("invalid magic byte: " + magicByte);
|
||||
}
|
||||
return OpenResult.SUCCESS;
|
||||
}
|
||||
|
||||
static OpenResult readMapDate(ReadBuffer readBuffer, MapFileInfoBuilder mapFileInfoBuilder) {
|
||||
// get and check the the map date (8 bytes)
|
||||
long mapDate = readBuffer.readLong();
|
||||
// is the map date before 2010-01-10 ?
|
||||
if (mapDate < 1200000000000L) {
|
||||
return new OpenResult("invalid map date: " + mapDate);
|
||||
}
|
||||
mapFileInfoBuilder.mapDate = mapDate;
|
||||
return OpenResult.SUCCESS;
|
||||
}
|
||||
|
||||
static OpenResult readPoiTags(ReadBuffer readBuffer, MapFileInfoBuilder mapFileInfoBuilder) {
|
||||
// get and check the number of POI tags (2 bytes)
|
||||
int numberOfPoiTags = readBuffer.readShort();
|
||||
if (numberOfPoiTags < 0) {
|
||||
return new OpenResult("invalid number of POI tags: " + numberOfPoiTags);
|
||||
}
|
||||
|
||||
Tag[] poiTags = new Tag[numberOfPoiTags];
|
||||
for (int currentTagId = 0; currentTagId < numberOfPoiTags; ++currentTagId) {
|
||||
// get and check the POI tag
|
||||
String tag = readBuffer.readUTF8EncodedString();
|
||||
if (tag == null) {
|
||||
return new OpenResult("POI tag must not be null: " + currentTagId);
|
||||
}
|
||||
poiTags[currentTagId] = new Tag(tag);
|
||||
}
|
||||
mapFileInfoBuilder.poiTags = poiTags;
|
||||
return OpenResult.SUCCESS;
|
||||
}
|
||||
|
||||
static OpenResult readProjectionName(ReadBuffer readBuffer, MapFileInfoBuilder mapFileInfoBuilder) {
|
||||
// get and check the projection name
|
||||
String projectionName = readBuffer.readUTF8EncodedString();
|
||||
if (!MERCATOR.equals(projectionName)) {
|
||||
return new OpenResult("unsupported projection: " + projectionName);
|
||||
}
|
||||
mapFileInfoBuilder.projectionName = projectionName;
|
||||
return OpenResult.SUCCESS;
|
||||
}
|
||||
|
||||
static OpenResult readRemainingHeader(ReadBuffer readBuffer) throws IOException {
|
||||
// get and check the size of the remaining file header (4 bytes)
|
||||
int remainingHeaderSize = readBuffer.readInt();
|
||||
if (remainingHeaderSize < HEADER_SIZE_MIN || remainingHeaderSize > HEADER_SIZE_MAX) {
|
||||
return new OpenResult("invalid remaining header size: " + remainingHeaderSize);
|
||||
}
|
||||
|
||||
// read the header data into the buffer
|
||||
if (!readBuffer.readFromFile(remainingHeaderSize)) {
|
||||
return new OpenResult("reading header data has failed: " + remainingHeaderSize);
|
||||
}
|
||||
return OpenResult.SUCCESS;
|
||||
}
|
||||
|
||||
static OpenResult readTilePixelSize(ReadBuffer readBuffer, MapFileInfoBuilder mapFileInfoBuilder) {
|
||||
// get and check the tile pixel size (2 bytes)
|
||||
int tilePixelSize = readBuffer.readShort();
|
||||
// if (tilePixelSize != Tile.TILE_SIZE) {
|
||||
// return new FileOpenResult("unsupported tile pixel size: " + tilePixelSize);
|
||||
// }
|
||||
mapFileInfoBuilder.tilePixelSize = tilePixelSize;
|
||||
return OpenResult.SUCCESS;
|
||||
}
|
||||
|
||||
static OpenResult readWayTags(ReadBuffer readBuffer, MapFileInfoBuilder mapFileInfoBuilder) {
|
||||
// get and check the number of way tags (2 bytes)
|
||||
int numberOfWayTags = readBuffer.readShort();
|
||||
if (numberOfWayTags < 0) {
|
||||
return new OpenResult("invalid number of way tags: " + numberOfWayTags);
|
||||
}
|
||||
|
||||
Tag[] wayTags = new Tag[numberOfWayTags];
|
||||
|
||||
for (int currentTagId = 0; currentTagId < numberOfWayTags; ++currentTagId) {
|
||||
// get and check the way tag
|
||||
String tag = readBuffer.readUTF8EncodedString();
|
||||
if (tag == null) {
|
||||
return new OpenResult("way tag must not be null: " + currentTagId);
|
||||
}
|
||||
wayTags[currentTagId] = new Tag(tag);
|
||||
}
|
||||
mapFileInfoBuilder.wayTags = wayTags;
|
||||
return OpenResult.SUCCESS;
|
||||
}
|
||||
|
||||
private RequiredFields() {
|
||||
throw new IllegalStateException();
|
||||
}
|
||||
}
|
||||
213
src/org/oscim/database/mapfile/header/SubFileParameter.java
Normal file
213
src/org/oscim/database/mapfile/header/SubFileParameter.java
Normal file
@@ -0,0 +1,213 @@
|
||||
/*
|
||||
* Copyright 2010, 2011, 2012 mapsforge.org
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it under the
|
||||
* terms of the GNU Lesser General Public License as published by the Free Software
|
||||
* Foundation, either version 3 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
|
||||
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
|
||||
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License along with
|
||||
* this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package org.oscim.database.mapfile.header;
|
||||
|
||||
import org.oscim.core.MercatorProjection;
|
||||
|
||||
/**
|
||||
* Holds all parameters of a sub-file.
|
||||
*/
|
||||
public class SubFileParameter {
|
||||
/**
|
||||
* Number of bytes a single index entry consists of.
|
||||
*/
|
||||
public static final byte BYTES_PER_INDEX_ENTRY = 5;
|
||||
|
||||
/**
|
||||
* Divisor for converting coordinates stored as integers to double values.
|
||||
*/
|
||||
private static final double COORDINATES_DIVISOR = 1000000d;
|
||||
|
||||
/**
|
||||
* Base zoom level of the sub-file, which equals to one block.
|
||||
*/
|
||||
public final byte baseZoomLevel;
|
||||
|
||||
/**
|
||||
* Size of the entries table at the beginning of each block in bytes.
|
||||
*/
|
||||
public final int blockEntriesTableSize;
|
||||
|
||||
/**
|
||||
* Vertical amount of blocks in the grid.
|
||||
*/
|
||||
public final long blocksHeight;
|
||||
|
||||
/**
|
||||
* Horizontal amount of blocks in the grid.
|
||||
*/
|
||||
public final long blocksWidth;
|
||||
|
||||
/**
|
||||
* Y number of the tile at the bottom boundary in the grid.
|
||||
*/
|
||||
public final long boundaryTileBottom;
|
||||
|
||||
/**
|
||||
* X number of the tile at the left boundary in the grid.
|
||||
*/
|
||||
public final long boundaryTileLeft;
|
||||
|
||||
/**
|
||||
* X number of the tile at the right boundary in the grid.
|
||||
*/
|
||||
public final long boundaryTileRight;
|
||||
|
||||
/**
|
||||
* Y number of the tile at the top boundary in the grid.
|
||||
*/
|
||||
public final long boundaryTileTop;
|
||||
|
||||
/**
|
||||
* Absolute end address of the index in the enclosing file.
|
||||
*/
|
||||
public final long indexEndAddress;
|
||||
|
||||
/**
|
||||
* Absolute start address of the index in the enclosing file.
|
||||
*/
|
||||
public final long indexStartAddress;
|
||||
|
||||
/**
|
||||
* Total number of blocks in the grid.
|
||||
*/
|
||||
public final long numberOfBlocks;
|
||||
|
||||
/**
|
||||
* Absolute start address of the sub-file in the enclosing file.
|
||||
*/
|
||||
public final long startAddress;
|
||||
|
||||
/**
|
||||
* Size of the sub-file in bytes.
|
||||
*/
|
||||
public final long subFileSize;
|
||||
|
||||
/**
|
||||
* Maximum zoom level for which the block entries tables are made.
|
||||
*/
|
||||
public final byte zoomLevelMax;
|
||||
|
||||
/**
|
||||
* Minimum zoom level for which the block entries tables are made.
|
||||
*/
|
||||
public final byte zoomLevelMin;
|
||||
|
||||
/**
|
||||
* Stores the hash code of this object.
|
||||
*/
|
||||
private final int hashCodeValue;
|
||||
|
||||
SubFileParameter(SubFileParameterBuilder subFileParameterBuilder) {
|
||||
this.startAddress = subFileParameterBuilder.startAddress;
|
||||
this.indexStartAddress = subFileParameterBuilder.indexStartAddress;
|
||||
this.subFileSize = subFileParameterBuilder.subFileSize;
|
||||
this.baseZoomLevel = subFileParameterBuilder.baseZoomLevel;
|
||||
this.zoomLevelMin = subFileParameterBuilder.zoomLevelMin;
|
||||
this.zoomLevelMax = subFileParameterBuilder.zoomLevelMax;
|
||||
this.hashCodeValue = calculateHashCode();
|
||||
|
||||
// calculate the XY numbers of the boundary tiles in this sub-file
|
||||
this.boundaryTileBottom = MercatorProjection.latitudeToTileY(subFileParameterBuilder.boundingBox.minLatitudeE6
|
||||
/ COORDINATES_DIVISOR, this.baseZoomLevel);
|
||||
this.boundaryTileLeft = MercatorProjection.longitudeToTileX(subFileParameterBuilder.boundingBox.minLongitudeE6
|
||||
/ COORDINATES_DIVISOR, this.baseZoomLevel);
|
||||
this.boundaryTileTop = MercatorProjection.latitudeToTileY(subFileParameterBuilder.boundingBox.maxLatitudeE6
|
||||
/ COORDINATES_DIVISOR, this.baseZoomLevel);
|
||||
this.boundaryTileRight = MercatorProjection.longitudeToTileX(subFileParameterBuilder.boundingBox.maxLongitudeE6
|
||||
/ COORDINATES_DIVISOR, this.baseZoomLevel);
|
||||
|
||||
// calculate the horizontal and vertical amount of blocks in this sub-file
|
||||
this.blocksWidth = this.boundaryTileRight - this.boundaryTileLeft + 1;
|
||||
this.blocksHeight = this.boundaryTileBottom - this.boundaryTileTop + 1;
|
||||
|
||||
// calculate the total amount of blocks in this sub-file
|
||||
this.numberOfBlocks = this.blocksWidth * this.blocksHeight;
|
||||
|
||||
this.indexEndAddress = this.indexStartAddress + this.numberOfBlocks * BYTES_PER_INDEX_ENTRY;
|
||||
|
||||
// calculate the size of the tile entries table
|
||||
this.blockEntriesTableSize = 2 * (this.zoomLevelMax - this.zoomLevelMin + 1) * 2;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj) {
|
||||
return true;
|
||||
} else if (!(obj instanceof SubFileParameter)) {
|
||||
return false;
|
||||
}
|
||||
SubFileParameter other = (SubFileParameter) obj;
|
||||
if (this.startAddress != other.startAddress) {
|
||||
return false;
|
||||
} else if (this.subFileSize != other.subFileSize) {
|
||||
return false;
|
||||
} else if (this.baseZoomLevel != other.baseZoomLevel) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return this.hashCodeValue;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
stringBuilder.append("SubFileParameter [baseZoomLevel=");
|
||||
stringBuilder.append(this.baseZoomLevel);
|
||||
stringBuilder.append(", blockEntriesTableSize=");
|
||||
stringBuilder.append(this.blockEntriesTableSize);
|
||||
stringBuilder.append(", blocksHeight=");
|
||||
stringBuilder.append(this.blocksHeight);
|
||||
stringBuilder.append(", blocksWidth=");
|
||||
stringBuilder.append(this.blocksWidth);
|
||||
stringBuilder.append(", boundaryTileBottom=");
|
||||
stringBuilder.append(this.boundaryTileBottom);
|
||||
stringBuilder.append(", boundaryTileLeft=");
|
||||
stringBuilder.append(this.boundaryTileLeft);
|
||||
stringBuilder.append(", boundaryTileRight=");
|
||||
stringBuilder.append(this.boundaryTileRight);
|
||||
stringBuilder.append(", boundaryTileTop=");
|
||||
stringBuilder.append(this.boundaryTileTop);
|
||||
stringBuilder.append(", indexStartAddress=");
|
||||
stringBuilder.append(this.indexStartAddress);
|
||||
stringBuilder.append(", numberOfBlocks=");
|
||||
stringBuilder.append(this.numberOfBlocks);
|
||||
stringBuilder.append(", startAddress=");
|
||||
stringBuilder.append(this.startAddress);
|
||||
stringBuilder.append(", subFileSize=");
|
||||
stringBuilder.append(this.subFileSize);
|
||||
stringBuilder.append(", zoomLevelMax=");
|
||||
stringBuilder.append(this.zoomLevelMax);
|
||||
stringBuilder.append(", zoomLevelMin=");
|
||||
stringBuilder.append(this.zoomLevelMin);
|
||||
stringBuilder.append("]");
|
||||
return stringBuilder.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the hash code of this object.
|
||||
*/
|
||||
private int calculateHashCode() {
|
||||
int result = 7;
|
||||
result = 31 * result + (int) (this.startAddress ^ (this.startAddress >>> 32));
|
||||
result = 31 * result + (int) (this.subFileSize ^ (this.subFileSize >>> 32));
|
||||
result = 31 * result + this.baseZoomLevel;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* Copyright 2010, 2011, 2012 mapsforge.org
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it under the
|
||||
* terms of the GNU Lesser General Public License as published by the Free Software
|
||||
* Foundation, either version 3 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
|
||||
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
|
||||
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License along with
|
||||
* this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package org.oscim.database.mapfile.header;
|
||||
|
||||
import org.oscim.core.BoundingBox;
|
||||
|
||||
class SubFileParameterBuilder {
|
||||
byte baseZoomLevel;
|
||||
BoundingBox boundingBox;
|
||||
long indexStartAddress;
|
||||
long startAddress;
|
||||
long subFileSize;
|
||||
byte zoomLevelMax;
|
||||
byte zoomLevelMin;
|
||||
|
||||
SubFileParameter build() {
|
||||
return new SubFileParameter(this);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user