1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2025-01-18 11:52:08 +01:00

Reduce the number of file opens for IMG files

This commit is contained in:
Martin Tůma 2024-11-30 00:13:35 +01:00
parent 9ac10e2909
commit f620bbc383
16 changed files with 166 additions and 140 deletions

View File

@ -92,7 +92,7 @@ bool GMAPData::loadTile(const QDir &dir)
} }
} }
if (!tile->init()) { if (!tile->init(0)) {
qWarning("%s: Invalid map tile", qPrintable(dir.path())); qWarning("%s: Invalid map tile", qPrintable(dir.path()));
delete tile; delete tile;
return false; return false;

View File

@ -29,11 +29,11 @@ static SubFile::Type tileType(const char str[3])
return SubFile::Unknown; return SubFile::Unknown;
} }
bool IMGData::readSubFileBlocks(QFile &file, quint64 offset, SubFile *subFile) bool IMGData::readSubFileBlocks(QFile *file, quint64 offset, SubFile *subFile)
{ {
quint16 block; quint16 block;
if (!file.seek(offset + 0x20)) if (!file->seek(offset + 0x20))
return false; return false;
for (int i = 0; i < 240; i++) { for (int i = 0; i < 240; i++) {
if (!readValue(file, block)) if (!readValue(file, block))
@ -46,11 +46,11 @@ bool IMGData::readSubFileBlocks(QFile &file, quint64 offset, SubFile *subFile)
return true; return true;
} }
bool IMGData::readIMGHeader(QFile &file) bool IMGData::readIMGHeader(QFile *file)
{ {
char signature[7], identifier[7]; char signature[7], identifier[7];
if (!(file.read((char*)&_key, 1) && file.seek(0x10) if (!(file->read((char*)&_key, 1) && file->seek(0x10)
&& read(file, signature, sizeof(signature)) && file.seek(0x41) && read(file, signature, sizeof(signature)) && file->seek(0x41)
&& read(file, identifier, sizeof(identifier))) && read(file, identifier, sizeof(identifier)))
|| memcmp(signature, "DSKIMG", sizeof(signature)) || memcmp(signature, "DSKIMG", sizeof(signature))
|| memcmp(identifier, "GARMIN", sizeof(identifier))) { || memcmp(identifier, "GARMIN", sizeof(identifier))) {
@ -60,8 +60,8 @@ bool IMGData::readIMGHeader(QFile &file)
char d1[20], d2[31]; char d1[20], d2[31];
quint8 e1, e2; quint8 e1, e2;
if (!(file.seek(0x49) && read(file, d1, sizeof(d1)) && file.seek(0x61) if (!(file->seek(0x49) && read(file, d1, sizeof(d1)) && file->seek(0x61)
&& readValue(file, e1) && readValue(file, e2) && file.seek(0x65) && readValue(file, e1) && readValue(file, e2) && file->seek(0x65)
&& read(file, d2, sizeof(d2)))) { && read(file, d2, sizeof(d2)))) {
_errorString = "Error reading IMG header"; _errorString = "Error reading IMG header";
return false; return false;
@ -74,7 +74,7 @@ bool IMGData::readIMGHeader(QFile &file)
return true; return true;
} }
bool IMGData::readFAT(QFile &file, TileMap &tileMap) bool IMGData::readFAT(QFile *file, TileMap &tileMap)
{ {
QByteArray typFile; QByteArray typFile;
quint8 flag; quint8 flag;
@ -82,7 +82,7 @@ bool IMGData::readFAT(QFile &file, TileMap &tileMap)
// Skip unused FAT blocks if any // Skip unused FAT blocks if any
while (true) { while (true) {
if (!(file.seek(offset) && readValue(file, flag))) if (!(file->seek(offset) && readValue(file, flag)))
return false; return false;
if (flag) if (flag)
break; break;
@ -93,14 +93,14 @@ bool IMGData::readFAT(QFile &file, TileMap &tileMap)
char name[8], type[3]; char name[8], type[3];
quint32 size; quint32 size;
quint16 part; quint16 part;
if (!(file.seek(offset + 12) && readValue(file, size))) if (!(file->seek(offset + 12) && readValue(file, size)))
return false; return false;
offset += 512; offset += 512;
int cnt = (size - offset) / 512; int cnt = (size - offset) / 512;
// Read FAT blocks describing the IMG sub-files // Read FAT blocks describing the IMG sub-files
for (int i = 0; i < cnt; i++) { for (int i = 0; i < cnt; i++) {
if (!(file.seek(offset) && readValue(file, flag) if (!(file->seek(offset) && readValue(file, flag)
&& read(file, name, sizeof(name)) && read(file, name, sizeof(name))
&& read(file, type, sizeof(type)) && readValue(file, size) && read(file, type, sizeof(type)) && readValue(file, size)
&& readValue(file, part))) && readValue(file, part)))
@ -140,13 +140,13 @@ bool IMGData::readFAT(QFile &file, TileMap &tileMap)
return true; return true;
} }
bool IMGData::createTileTree(const TileMap &tileMap) bool IMGData::createTileTree(QFile *file, const TileMap &tileMap)
{ {
for (TileMap::const_iterator it = tileMap.constBegin(); for (TileMap::const_iterator it = tileMap.constBegin();
it != tileMap.constEnd(); ++it) { it != tileMap.constEnd(); ++it) {
VectorTile *tile = it.value(); VectorTile *tile = it.value();
if (!tile->init()) { if (!tile->init(file)) {
qWarning("%s: %s: Invalid map tile", qPrintable(_fileName), qWarning("%s: %s: Invalid map tile", qPrintable(_fileName),
qPrintable(it.key())); qPrintable(it.key()));
delete tile; delete tile;
@ -177,14 +177,14 @@ IMGData::IMGData(const QString &fileName) : MapData(fileName)
return; return;
} }
if (!readIMGHeader(file)) if (!readIMGHeader(&file))
return; return;
if (!readFAT(file, tileMap)) { if (!readFAT(&file, tileMap)) {
_errorString = "Error reading FAT data"; _errorString = "Error reading FAT data";
qDeleteAll(tileMap); qDeleteAll(tileMap);
return; return;
} }
if (!createTileTree(tileMap)) { if (!createTileTree(&file, tileMap)) {
_errorString = "No usable map tile found"; _errorString = "No usable map tile found";
return; return;
} }
@ -194,16 +194,16 @@ IMGData::IMGData(const QString &fileName) : MapData(fileName)
_valid = true; _valid = true;
} }
qint64 IMGData::read(QFile &file, char *data, qint64 maxSize) const qint64 IMGData::read(QFile *file, char *data, qint64 maxSize) const
{ {
qint64 ret = file.read(data, maxSize); qint64 ret = file->read(data, maxSize);
if (_key) if (_key)
for (int i = 0; i < ret; i++) for (int i = 0; i < ret; i++)
data[i] ^= _key; data[i] ^= _key;
return ret; return ret;
} }
template<class T> bool IMGData::readValue(QFile &file, T &val) const template<class T> bool IMGData::readValue(QFile *file, T &val) const
{ {
T data; T data;
@ -215,9 +215,9 @@ template<class T> bool IMGData::readValue(QFile &file, T &val) const
return true; return true;
} }
bool IMGData::readBlock(QFile &file, int blockNum, char *data) const bool IMGData::readBlock(QFile *file, int blockNum, char *data) const
{ {
if (!file.seek((quint64)blockNum << _blockBits)) if (!file->seek((quint64)blockNum << _blockBits))
return false; return false;
if (read(file, data, 1ULL<<_blockBits) < (qint64)(1ULL<<_blockBits)) if (read(file, data, 1ULL<<_blockBits) < (qint64)(1ULL<<_blockBits))
return false; return false;

View File

@ -13,17 +13,17 @@ public:
IMGData(const QString &fileName); IMGData(const QString &fileName);
unsigned blockBits() const {return _blockBits;} unsigned blockBits() const {return _blockBits;}
bool readBlock(QFile &file, int blockNum, char *data) const; bool readBlock(QFile *file, int blockNum, char *data) const;
private: private:
typedef QMap<QByteArray, VectorTile*> TileMap; typedef QMap<QByteArray, VectorTile*> TileMap;
qint64 read(QFile &file, char *data, qint64 maxSize) const; qint64 read(QFile *file, char *data, qint64 maxSize) const;
template<class T> bool readValue(QFile &file, T &val) const; template<class T> bool readValue(QFile *file, T &val) const;
bool readSubFileBlocks(QFile &file, quint64 offset, SubFile *subFile); bool readSubFileBlocks(QFile *file, quint64 offset, SubFile *subFile);
bool readFAT(QFile &file, TileMap &tileMap); bool readFAT(QFile *file, TileMap &tileMap);
bool readIMGHeader(QFile &file); bool readIMGHeader(QFile *file);
bool createTileTree(const TileMap &tileMap); bool createTileTree(QFile *file, const TileMap &tileMap);
quint8 _key; quint8 _key;
unsigned _blockBits; unsigned _blockBits;

View File

@ -13,7 +13,7 @@ using namespace IMG;
bool MapData::polyCb(VectorTile *tile, void *context) bool MapData::polyCb(VectorTile *tile, void *context)
{ {
PolyCTX *ctx = (PolyCTX*)context; PolyCTX *ctx = (PolyCTX*)context;
tile->polys(ctx->rect, ctx->zoom, ctx->polygons, ctx->lines, tile->polys(ctx->file, ctx->rect, ctx->zoom, ctx->polygons, ctx->lines,
ctx->cache, ctx->lock); ctx->cache, ctx->lock);
return true; return true;
} }
@ -21,14 +21,16 @@ bool MapData::polyCb(VectorTile *tile, void *context)
bool MapData::pointCb(VectorTile *tile, void *context) bool MapData::pointCb(VectorTile *tile, void *context)
{ {
PointCTX *ctx = (PointCTX*)context; PointCTX *ctx = (PointCTX*)context;
tile->points(ctx->rect, ctx->zoom, ctx->points, ctx->cache, ctx->lock); tile->points(ctx->file, ctx->rect, ctx->zoom, ctx->points, ctx->cache,
ctx->lock);
return true; return true;
} }
bool MapData::elevationCb(VectorTile *tile, void *context) bool MapData::elevationCb(VectorTile *tile, void *context)
{ {
ElevationCTX *ctx = (ElevationCTX*)context; ElevationCTX *ctx = (ElevationCTX*)context;
tile->elevations(ctx->rect, ctx->zoom, ctx->elevations, ctx->cache, ctx->lock); tile->elevations(ctx->file, ctx->rect, ctx->zoom, ctx->elevations,
ctx->cache, ctx->lock);
return true; return true;
} }
@ -50,10 +52,10 @@ MapData::~MapData()
delete _style; delete _style;
} }
void MapData::polys(const RectC &rect, int bits, QList<Poly> *polygons, void MapData::polys(QFile *file, const RectC &rect, int bits,
QList<Poly> *lines) QList<Poly> *polygons, QList<Poly> *lines)
{ {
PolyCTX ctx(rect, zoom(bits), polygons, lines, &_polyCache, &_lock); PolyCTX ctx(file, rect, zoom(bits), polygons, lines, &_polyCache, &_lock);
double min[2], max[2]; double min[2], max[2];
min[0] = rect.left(); min[0] = rect.left();
@ -64,9 +66,10 @@ void MapData::polys(const RectC &rect, int bits, QList<Poly> *polygons,
_tileTree.Search(min, max, polyCb, &ctx); _tileTree.Search(min, max, polyCb, &ctx);
} }
void MapData::points(const RectC &rect, int bits, QList<Point> *points) void MapData::points(QFile *file, const RectC &rect, int bits,
QList<Point> *points)
{ {
PointCTX ctx(rect, zoom(bits), points, &_pointCache, &_lock); PointCTX ctx(file, rect, zoom(bits), points, &_pointCache, &_lock);
double min[2], max[2]; double min[2], max[2];
min[0] = rect.left(); min[0] = rect.left();
@ -77,9 +80,10 @@ void MapData::points(const RectC &rect, int bits, QList<Point> *points)
_tileTree.Search(min, max, pointCb, &ctx); _tileTree.Search(min, max, pointCb, &ctx);
} }
void MapData::elevations(const RectC &rect, int bits, QList<Elevation> *elevations) void MapData::elevations(QFile *file, const RectC &rect, int bits,
QList<Elevation> *elevations)
{ {
ElevationCTX ctx(rect, zoom(bits), elevations, &_demCache, &_demLock); ElevationCTX ctx(file, rect, zoom(bits), elevations, &_demCache, &_demLock);
double min[2], max[2]; double min[2], max[2];
min[0] = rect.left(); min[0] = rect.left();
@ -95,14 +99,14 @@ void MapData::load(qreal ratio)
Q_ASSERT(!_style); Q_ASSERT(!_style);
if (_typ) if (_typ)
_style = new Style(ratio, _typ); _style = new Style(0, ratio, _typ);
else { else {
QString typFile(ProgramPaths::typFile()); QString typFile(ProgramPaths::typFile());
if (QFileInfo::exists(typFile)) { if (QFileInfo::exists(typFile)) {
SubFile typ(&typFile); SubFile typ(&typFile);
_style = new Style(ratio, &typ); _style = new Style(0, ratio, &typ);
} else } else
_style = new Style(ratio); _style = new Style(0, ratio);
} }
} }

View File

@ -5,6 +5,7 @@
#include <QPointF> #include <QPointF>
#include <QCache> #include <QCache>
#include <QMutex> #include <QMutex>
#include <QFile>
#include <QDebug> #include <QDebug>
#include "common/rectc.h" #include "common/rectc.h"
#include "common/rtree.h" #include "common/rtree.h"
@ -77,10 +78,11 @@ public:
const RectC &bounds() const {return _bounds;} const RectC &bounds() const {return _bounds;}
const Range &zooms() const {return _zoomLevels;} const Range &zooms() const {return _zoomLevels;}
const Style *style() const {return _style;} const Style *style() const {return _style;}
void polys(const RectC &rect, int bits, QList<Poly> *polygons, void polys(QFile *file, const RectC &rect, int bits, QList<Poly> *polygons,
QList<Poly> *lines); QList<Poly> *lines);
void points(const RectC &rect, int bits, QList<Point> *points); void points(QFile *file, const RectC &rect, int bits, QList<Point> *points);
void elevations(const RectC &rect, int bits, QList<Elevation> *elevations); void elevations(QFile *file, const RectC &rect, int bits,
QList<Elevation> *elevations);
void load(qreal ratio); void load(qreal ratio);
void clear(); void clear();
@ -122,12 +124,13 @@ private:
struct PolyCTX struct PolyCTX
{ {
PolyCTX(const RectC &rect, const Zoom &zoom, PolyCTX(QFile *file, const RectC &rect, const Zoom &zoom,
QList<MapData::Poly> *polygons, QList<MapData::Poly> *lines, QList<MapData::Poly> *polygons, QList<MapData::Poly> *lines,
PolyCache *cache, QMutex *lock) PolyCache *cache, QMutex *lock)
: rect(rect), zoom(zoom), polygons(polygons), lines(lines), : file(file), rect(rect), zoom(zoom), polygons(polygons),
cache(cache), lock(lock) {} lines(lines), cache(cache), lock(lock) {}
QFile *file;
const RectC &rect; const RectC &rect;
const Zoom &zoom; const Zoom &zoom;
QList<MapData::Poly> *polygons; QList<MapData::Poly> *polygons;
@ -138,10 +141,12 @@ private:
struct PointCTX struct PointCTX
{ {
PointCTX(const RectC &rect, const Zoom &zoom, PointCTX(QFile *file, const RectC &rect, const Zoom &zoom,
QList<MapData::Point> *points, PointCache *cache, QMutex *lock) QList<MapData::Point> *points, PointCache *cache, QMutex *lock)
: rect(rect), zoom(zoom), points(points), cache(cache), lock(lock) {} : file(file), rect(rect), zoom(zoom), points(points), cache(cache),
lock(lock) {}
QFile *file;
const RectC &rect; const RectC &rect;
const Zoom &zoom; const Zoom &zoom;
QList<MapData::Point> *points; QList<MapData::Point> *points;
@ -151,11 +156,12 @@ private:
struct ElevationCTX struct ElevationCTX
{ {
ElevationCTX(const RectC &rect, const Zoom &zoom, ElevationCTX(QFile *file, const RectC &rect, const Zoom &zoom,
QList<Elevation> *elevations, ElevationCache *cache, QMutex *lock) QList<Elevation> *elevations, ElevationCache *cache, QMutex *lock)
: rect(rect), zoom(zoom), elevations(elevations), cache(cache), : file(file), rect(rect), zoom(zoom), elevations(elevations),
lock(lock) {} cache(cache), lock(lock) {}
QFile *file;
const RectC &rect; const RectC &rect;
const Zoom &zoom; const Zoom &zoom;
QList<Elevation> *elevations; QList<Elevation> *elevations;

View File

@ -152,7 +152,7 @@ void RasterTile::drawPolygons(QPainter *painter,
bool insert = false; bool insert = false;
SubFile::Handle *hdl = hc.object(poly.raster.lbl()); SubFile::Handle *hdl = hc.object(poly.raster.lbl());
if (!hdl) { if (!hdl) {
hdl = new SubFile::Handle(poly.raster.lbl()); hdl = new SubFile::Handle(_file, poly.raster.lbl());
insert = true; insert = true;
} }
QPixmap pm(poly.raster.lbl()->image(*hdl, poly.raster.id())); QPixmap pm(poly.raster.lbl()->image(*hdl, poly.raster.id()));
@ -443,11 +443,16 @@ void RasterTile::fetchData(QList<MapData::Poly> &polygons,
{ {
QPoint ttl(_rect.topLeft()); QPoint ttl(_rect.topLeft());
if (dynamic_cast<IMGData*>(_data)) {
_file = new QFile(_data->fileName());
_file->open(QIODevice::ReadOnly | QIODevice::Unbuffered);
}
QRectF polyRect(ttl, QPointF(ttl.x() + _rect.width(), ttl.y() QRectF polyRect(ttl, QPointF(ttl.x() + _rect.width(), ttl.y()
+ _rect.height())); + _rect.height()));
RectD polyRectD(_transform.img2proj(polyRect.topLeft()), RectD polyRectD(_transform.img2proj(polyRect.topLeft()),
_transform.img2proj(polyRect.bottomRight())); _transform.img2proj(polyRect.bottomRight()));
_data->polys(polyRectD.toRectC(_proj, 20), _zoom, _data->polys(_file, polyRectD.toRectC(_proj, 20), _zoom,
&polygons, _vectors ? &lines : 0); &polygons, _vectors ? &lines : 0);
if (_vectors) { if (_vectors) {
@ -456,7 +461,7 @@ void RasterTile::fetchData(QList<MapData::Poly> &polygons,
+ TEXT_EXTENT)); + TEXT_EXTENT));
RectD pointRectD(_transform.img2proj(pointRect.topLeft()), RectD pointRectD(_transform.img2proj(pointRect.topLeft()),
_transform.img2proj(pointRect.bottomRight())); _transform.img2proj(pointRect.bottomRight()));
_data->points(pointRectD.toRectC(_proj, 20), _zoom, &points); _data->points(_file, pointRectD.toRectC(_proj, 20), _zoom, &points);
} }
} }
@ -481,7 +486,7 @@ MatrixD RasterTile::elevation(int extend) const
// Extra margin for always including the next DEM tile on the map tile // Extra margin for always including the next DEM tile on the map tile
// edges (the DEM tile resolution is usally 0.5-15% of the map tile) // edges (the DEM tile resolution is usally 0.5-15% of the map tile)
double factor = 6 - (_zoom - 24) * 1.7; double factor = 6 - (_zoom - 24) * 1.7;
_data->elevations(rect.adjusted(0, 0, rect.width() / factor, _data->elevations(_file, rect.adjusted(0, 0, rect.width() / factor,
-rect.height() / factor), _zoom, &tiles); -rect.height() / factor), _zoom, &tiles);
DEMTree tree(tiles); DEMTree tree(tiles);

View File

@ -22,7 +22,8 @@ public:
bool hillShading, bool rasters, bool vectors) bool hillShading, bool rasters, bool vectors)
: _proj(proj), _transform(transform), _data(data), _zoom(zoom), : _proj(proj), _transform(transform), _data(data), _zoom(zoom),
_rect(rect), _ratio(ratio), _key(key), _hillShading(hillShading), _rect(rect), _ratio(ratio), _key(key), _hillShading(hillShading),
_rasters(rasters), _vectors(vectors) {} _rasters(rasters), _vectors(vectors), _file(0) {}
~RasterTile() {delete _file;}
const QString &key() const {return _key;} const QString &key() const {return _key;}
QPoint xy() const {return _rect.topLeft();} QPoint xy() const {return _rect.topLeft();}
@ -88,6 +89,7 @@ private:
QPixmap _pixmap; QPixmap _pixmap;
bool _hillShading; bool _hillShading;
bool _rasters, _vectors; bool _rasters, _vectors;
QFile *_file;
}; };
} }

View File

@ -1268,50 +1268,50 @@ bool Style::parseDrawOrder(SubFile *file, SubFile::Handle &hdl,
return true; return true;
} }
bool Style::parseTYPFile(SubFile *file) bool Style::parseTYPFile(QFile *file, SubFile *typ)
{ {
SubFile::Handle hdl(file); SubFile::Handle hdl(file, typ);
Section points, lines, polygons, order; Section points, lines, polygons, order;
quint16 tmp16, codepage; quint16 tmp16, codepage;
if (!(file->seek(hdl, 0x15) && file->readUInt16(hdl, codepage) if (!(typ->seek(hdl, 0x15) && typ->readUInt16(hdl, codepage)
&& file->readUInt32(hdl, points.offset) && typ->readUInt32(hdl, points.offset)
&& file->readUInt32(hdl, points.size) && typ->readUInt32(hdl, points.size)
&& file->readUInt32(hdl, lines.offset) && typ->readUInt32(hdl, lines.offset)
&& file->readUInt32(hdl, lines.size) && typ->readUInt32(hdl, lines.size)
&& file->readUInt32(hdl, polygons.offset) && typ->readUInt32(hdl, polygons.offset)
&& file->readUInt32(hdl, polygons.size))) && typ->readUInt32(hdl, polygons.size)))
return false; return false;
if (!(file->readUInt16(hdl, tmp16) && file->readUInt16(hdl, tmp16))) if (!(typ->readUInt16(hdl, tmp16) && typ->readUInt16(hdl, tmp16)))
return false; return false;
if (!(file->readUInt32(hdl, points.arrayOffset) if (!(typ->readUInt32(hdl, points.arrayOffset)
&& file->readUInt16(hdl, points.arrayItemSize) && typ->readUInt16(hdl, points.arrayItemSize)
&& file->readUInt32(hdl, points.arraySize) && typ->readUInt32(hdl, points.arraySize)
&& file->readUInt32(hdl, lines.arrayOffset) && typ->readUInt32(hdl, lines.arrayOffset)
&& file->readUInt16(hdl, lines.arrayItemSize) && typ->readUInt16(hdl, lines.arrayItemSize)
&& file->readUInt32(hdl, lines.arraySize) && typ->readUInt32(hdl, lines.arraySize)
&& file->readUInt32(hdl, polygons.arrayOffset) && typ->readUInt32(hdl, polygons.arrayOffset)
&& file->readUInt16(hdl, polygons.arrayItemSize) && typ->readUInt16(hdl, polygons.arrayItemSize)
&& file->readUInt32(hdl, polygons.arraySize) && typ->readUInt32(hdl, polygons.arraySize)
&& file->readUInt32(hdl, order.arrayOffset) && typ->readUInt32(hdl, order.arrayOffset)
&& file->readUInt16(hdl, order.arrayItemSize) && typ->readUInt16(hdl, order.arrayItemSize)
&& file->readUInt32(hdl, order.arraySize))) && typ->readUInt32(hdl, order.arraySize)))
return false; return false;
if (!(parsePoints(file, hdl, points) && parseLines(file, hdl, lines) if (!(parsePoints(typ, hdl, points) && parseLines(typ, hdl, lines)
&& parsePolygons(file, hdl, polygons) && parsePolygons(typ, hdl, polygons)
&& parseDrawOrder(file, hdl, order))) { && parseDrawOrder(typ, hdl, order))) {
qWarning("%s: Invalid TYP file, using default style", qWarning("%s: Invalid TYP file, using default style",
qPrintable(file->fileName())); qPrintable(typ->fileName()));
return false; return false;
} }
return true; return true;
} }
Style::Style(qreal ratio, SubFile *typ) Style::Style(QFile *file, qreal ratio, SubFile *typ)
{ {
_large = pixelSizeFont(16); _large = pixelSizeFont(16);
_normal = pixelSizeFont(14); _normal = pixelSizeFont(14);
@ -1326,7 +1326,7 @@ Style::Style(qreal ratio, SubFile *typ)
defaultPointStyle(ratio); defaultPointStyle(ratio);
if (typ) if (typ)
parseTYPFile(typ); parseTYPFile(file, typ);
} }
const Style::Line &Style::line(quint32 type) const const Style::Line &Style::line(quint32 type) const

View File

@ -104,7 +104,7 @@ public:
}; };
Style(qreal ratio, SubFile *typ = 0); Style(QFile *file, qreal ratio, SubFile *typ = 0);
const Line &line(quint32 type) const; const Line &line(quint32 type) const;
const Polygon &polygon(quint32 type) const; const Polygon &polygon(quint32 type) const;
@ -171,7 +171,7 @@ private:
bool extended; bool extended;
}; };
bool parseTYPFile(SubFile *file); bool parseTYPFile(QFile *file, SubFile *typ);
bool parsePoints(SubFile *file, SubFile::Handle &hdl, bool parsePoints(SubFile *file, SubFile::Handle &hdl,
const Section &section); const Section &section);
bool parsePoint(SubFile *file, SubFile::Handle &hdl, bool parsePoint(SubFile *file, SubFile::Handle &hdl,

View File

@ -27,9 +27,9 @@ bool SubFile::seek(Handle &handle, quint32 pos) const
int blockNum = pos >> BLOCK_BITS; int blockNum = pos >> BLOCK_BITS;
if (handle._blockNum != blockNum) { if (handle._blockNum != blockNum) {
if (!handle._file.seek((quint64)blockNum << BLOCK_BITS)) if (!handle._file->seek((quint64)blockNum << BLOCK_BITS))
return false; return false;
if (handle._file.read(handle._data.data(), (1<<BLOCK_BITS)) < 0) if (handle._file->read(handle._data.data(), (1<<BLOCK_BITS)) < 0)
return false; return false;
handle._blockNum = blockNum; handle._blockNum = blockNum;
} }

View File

@ -18,25 +18,34 @@ public:
class Handle class Handle
{ {
public: public:
Handle(const SubFile *subFile) Handle(QFile *file, const SubFile *subFile)
: _blockNum(-1), _blockPos(-1), _pos(-1) : _file(file), _blockNum(-1), _blockPos(-1), _pos(-1), _delete(false)
{ {
if (!subFile) if (!subFile)
return; return;
if (!_file) {
_file = new QFile(subFile->fileName());
_file->open(QIODevice::ReadOnly | QIODevice::Unbuffered);
_delete = true;
}
_data.resize(subFile->blockSize()); _data.resize(subFile->blockSize());
_file.setFileName(subFile->fileName()); }
_file.open(QIODevice::ReadOnly | QIODevice::Unbuffered); ~Handle()
{
if (_delete)
delete _file;
} }
private: private:
friend class SubFile; friend class SubFile;
QFile _file; QFile *_file;
QByteArray _data; QByteArray _data;
int _blockNum; int _blockNum;
int _blockPos; int _blockPos;
int _pos; int _pos;
bool _delete;
}; };
SubFile(const IMGData *img) SubFile(const IMGData *img)

View File

@ -49,9 +49,9 @@ TREFile::~TREFile()
clear(); clear();
} }
bool TREFile::init() bool TREFile::init(QFile *file)
{ {
Handle hdl(this); Handle hdl(file, this);
quint8 locked, levels[64]; quint8 locked, levels[64];
quint16 hdrLen; quint16 hdrLen;
qint32 north, east, south, west; qint32 north, east, south, west;
@ -154,9 +154,9 @@ int TREFile::readExtEntry(Handle &hdl, quint32 &polygons, quint32 &lines,
return rb; return rb;
} }
bool TREFile::load(int idx) bool TREFile::load(QFile *file, int idx)
{ {
Handle hdl(this); Handle hdl(file, this);
QList<SubDiv*> sl; QList<SubDiv*> sl;
SubDiv *s = 0; SubDiv *s = 0;
SubDivTree *tree = new SubDivTree(); SubDivTree *tree = new SubDivTree();
@ -282,7 +282,7 @@ void TREFile::clear()
_subdivs.clear(); _subdivs.clear();
} }
const TREFile::SubDivTree *TREFile::subdivs(const Zoom &zoom) const TREFile::SubDivTree *TREFile::subdivs(QFile *file, const Zoom &zoom)
{ {
int idx = -1; int idx = -1;
@ -296,7 +296,7 @@ const TREFile::SubDivTree *TREFile::subdivs(const Zoom &zoom)
if (idx < 0) if (idx < 0)
return 0; return 0;
if (!_subdivs.contains(_levels.at(idx).level) && !load(idx)) if (!_subdivs.contains(_levels.at(idx).level) && !load(file, idx))
return 0; return 0;
return _subdivs.value(_levels.at(idx).level); return _subdivs.value(_levels.at(idx).level);
@ -309,10 +309,10 @@ static bool cb(SubDiv *subdiv, void *context)
return true; return true;
} }
QList<SubDiv*> TREFile::subdivs(const RectC &rect, const Zoom &zoom) QList<SubDiv*> TREFile::subdivs(QFile *file, const RectC &rect, const Zoom &zoom)
{ {
QList<SubDiv*> list; QList<SubDiv*> list;
const SubDivTree *tree = subdivs(zoom); const SubDivTree *tree = subdivs(file, zoom);
double min[2], max[2]; double min[2], max[2];
min[0] = rect.left(); min[0] = rect.left();

View File

@ -24,11 +24,11 @@ public:
: SubFile(gmp, offset), _flags(0), _extItemSize(0) {} : SubFile(gmp, offset), _flags(0), _extItemSize(0) {}
~TREFile(); ~TREFile();
bool init(); bool init(QFile *file);
void clear(); void clear();
const RectC &bounds() const {return _bounds;} const RectC &bounds() const {return _bounds;}
QList<SubDiv*> subdivs(const RectC &rect, const Zoom &zoom); QList<SubDiv*> subdivs(QFile *file, const RectC &rect, const Zoom &zoom);
quint32 shift(quint8 bits) const quint32 shift(quint8 bits) const
{return (bits == _levels.last().bits) ? (_flags >> 0xb) & 7 : 0;} {return (bits == _levels.last().bits) ? (_flags >> 0xb) & 7 : 0;}
QVector<Zoom> zooms() const; QVector<Zoom> zooms() const;
@ -41,8 +41,8 @@ private:
}; };
typedef RTree<SubDiv*, double, 2> SubDivTree; typedef RTree<SubDiv*, double, 2> SubDivTree;
bool load(int idx); bool load(QFile *file, int idx);
const SubDivTree *subdivs(const Zoom &zoom); const SubDivTree *subdivs(QFile *file, const Zoom &zoom);
int readExtEntry(Handle &hdl, quint32 &polygons, quint32 &lines, int readExtEntry(Handle &hdl, quint32 &polygons, quint32 &lines,
quint32 &points); quint32 &points);

View File

@ -40,20 +40,20 @@ SubFile *VectorTile::file(SubFile::Type type)
} }
} }
bool VectorTile::init() bool VectorTile::init(QFile *file)
{ {
if (_gmp && !initGMP()) if (_gmp && !initGMP(file))
return false; return false;
if (!(_tre && _tre->init() && _rgn)) if (!(_tre && _tre->init(file) && _rgn))
return false; return false;
return true; return true;
} }
bool VectorTile::initGMP() bool VectorTile::initGMP(QFile *file)
{ {
SubFile::Handle hdl(_gmp); SubFile::Handle hdl(file, _gmp);
quint32 tre, rgn, lbl, net, nod, dem; quint32 tre, rgn, lbl, net, nod, dem;
if (!(_gmp->seek(hdl, 0x19) && _gmp->readUInt32(hdl, tre) if (!(_gmp->seek(hdl, 0x19) && _gmp->readUInt32(hdl, tre)
@ -118,7 +118,7 @@ void VectorTile::clear()
_demLoaded = 0; _demLoaded = 0;
} }
void VectorTile::polys(const RectC &rect, const Zoom &zoom, void VectorTile::polys(QFile *file, const RectC &rect, const Zoom &zoom,
QList<MapData::Poly> *polygons, QList<MapData::Poly> *lines, QList<MapData::Poly> *polygons, QList<MapData::Poly> *lines,
MapData::PolyCache *cache, QMutex *lock) MapData::PolyCache *cache, QMutex *lock)
{ {
@ -133,10 +133,10 @@ void VectorTile::polys(const RectC &rect, const Zoom &zoom,
} }
if (!_loaded) { if (!_loaded) {
rgnHdl = new SubFile::Handle(_rgn); rgnHdl = new SubFile::Handle(file, _rgn);
lblHdl = new SubFile::Handle(_lbl); lblHdl = new SubFile::Handle(file, _lbl);
netHdl = new SubFile::Handle(_net); netHdl = new SubFile::Handle(file, _net);
nodHdl = new SubFile::Handle(_nod); nodHdl = new SubFile::Handle(file, _nod);
if (!load(*rgnHdl, *lblHdl, *netHdl, *nodHdl)) { if (!load(*rgnHdl, *lblHdl, *netHdl, *nodHdl)) {
lock->unlock(); lock->unlock();
@ -145,7 +145,7 @@ void VectorTile::polys(const RectC &rect, const Zoom &zoom,
} }
} }
QList<SubDiv*> subdivs = _tre->subdivs(rect, zoom); QList<SubDiv*> subdivs = _tre->subdivs(file, rect, zoom);
for (int i = 0; i < subdivs.size(); i++) { for (int i = 0; i < subdivs.size(); i++) {
SubDiv *subdiv = subdivs.at(i); SubDiv *subdiv = subdivs.at(i);
@ -154,9 +154,9 @@ void VectorTile::polys(const RectC &rect, const Zoom &zoom,
quint32 shift = _tre->shift(subdiv->bits()); quint32 shift = _tre->shift(subdiv->bits());
if (!rgnHdl) { if (!rgnHdl) {
rgnHdl = new SubFile::Handle(_rgn); rgnHdl = new SubFile::Handle(file, _rgn);
lblHdl = new SubFile::Handle(_lbl); lblHdl = new SubFile::Handle(file, _lbl);
netHdl = new SubFile::Handle(_net); netHdl = new SubFile::Handle(file, _net);
} }
if (!subdiv->initialized() && !_rgn->subdivInit(*rgnHdl, subdiv)) if (!subdiv->initialized() && !_rgn->subdivInit(*rgnHdl, subdiv))
@ -175,9 +175,9 @@ void VectorTile::polys(const RectC &rect, const Zoom &zoom,
if (_net && _net->hasLinks()) { if (_net && _net->hasLinks()) {
if (!nodHdl) if (!nodHdl)
nodHdl = new SubFile::Handle(_nod); nodHdl = new SubFile::Handle(file, _nod);
if (!nodHdl2) if (!nodHdl2)
nodHdl2 = new SubFile::Handle(_nod); nodHdl2 = new SubFile::Handle(file, _nod);
_rgn->links(*rgnHdl, subdiv, shift, _net, *netHdl, _nod, *nodHdl, _rgn->links(*rgnHdl, subdiv, shift, _net, *netHdl, _nod, *nodHdl,
*nodHdl2, _lbl, *lblHdl, &polys->lines); *nodHdl2, _lbl, *lblHdl, &polys->lines);
} }
@ -199,7 +199,7 @@ void VectorTile::polys(const RectC &rect, const Zoom &zoom,
delete rgnHdl; delete lblHdl; delete netHdl; delete nodHdl; delete nodHdl2; delete rgnHdl; delete lblHdl; delete netHdl; delete nodHdl; delete nodHdl2;
} }
void VectorTile::points(const RectC &rect, const Zoom &zoom, void VectorTile::points(QFile *file, const RectC &rect, const Zoom &zoom,
QList<MapData::Point> *points, MapData::PointCache *cache, QMutex *lock) QList<MapData::Point> *points, MapData::PointCache *cache, QMutex *lock)
{ {
SubFile::Handle *rgnHdl = 0, *lblHdl = 0; SubFile::Handle *rgnHdl = 0, *lblHdl = 0;
@ -212,10 +212,10 @@ void VectorTile::points(const RectC &rect, const Zoom &zoom,
} }
if (!_loaded) { if (!_loaded) {
rgnHdl = new SubFile::Handle(_rgn); rgnHdl = new SubFile::Handle(file, _rgn);
lblHdl = new SubFile::Handle(_lbl); lblHdl = new SubFile::Handle(file, _lbl);
SubFile::Handle nodHdl(_nod); SubFile::Handle nodHdl(file, _nod);
SubFile::Handle netHdl(_net); SubFile::Handle netHdl(file, _net);
if (!load(*rgnHdl, *lblHdl, netHdl, nodHdl)) { if (!load(*rgnHdl, *lblHdl, netHdl, nodHdl)) {
lock->unlock(); lock->unlock();
@ -224,15 +224,15 @@ void VectorTile::points(const RectC &rect, const Zoom &zoom,
} }
} }
QList<SubDiv*> subdivs = _tre->subdivs(rect, zoom); QList<SubDiv*> subdivs = _tre->subdivs(file, rect, zoom);
for (int i = 0; i < subdivs.size(); i++) { for (int i = 0; i < subdivs.size(); i++) {
SubDiv *subdiv = subdivs.at(i); SubDiv *subdiv = subdivs.at(i);
QList<MapData::Point> *pl = cache->object(subdiv); QList<MapData::Point> *pl = cache->object(subdiv);
if (!pl) { if (!pl) {
if (!rgnHdl) { if (!rgnHdl) {
rgnHdl = new SubFile::Handle(_rgn); rgnHdl = new SubFile::Handle(file, _rgn);
lblHdl = new SubFile::Handle(_lbl); lblHdl = new SubFile::Handle(file, _lbl);
} }
if (!subdiv->initialized() && !_rgn->subdivInit(*rgnHdl, subdiv)) if (!subdiv->initialized() && !_rgn->subdivInit(*rgnHdl, subdiv))
@ -258,7 +258,7 @@ void VectorTile::points(const RectC &rect, const Zoom &zoom,
delete rgnHdl; delete lblHdl; delete rgnHdl; delete lblHdl;
} }
void VectorTile::elevations(const RectC &rect, const Zoom &zoom, void VectorTile::elevations(QFile *file, const RectC &rect, const Zoom &zoom,
QList<MapData::Elevation> *elevations, MapData::ElevationCache *cache, QList<MapData::Elevation> *elevations, MapData::ElevationCache *cache,
QMutex *lock) QMutex *lock)
{ {
@ -272,7 +272,7 @@ void VectorTile::elevations(const RectC &rect, const Zoom &zoom,
} }
if (!_demLoaded) { if (!_demLoaded) {
hdl = new SubFile::Handle(_dem); hdl = new SubFile::Handle(file, _dem);
if (!loadDem(*hdl)) { if (!loadDem(*hdl)) {
lock->unlock(); lock->unlock();
@ -293,7 +293,7 @@ void VectorTile::elevations(const RectC &rect, const Zoom &zoom,
if (!el) { if (!el) {
if (!hdl) if (!hdl)
hdl = new SubFile::Handle(_dem); hdl = new SubFile::Handle(file, _dem);
el = _dem->elevations(*hdl, level, tile); el = _dem->elevations(*hdl, level, tile);
if (!el->m.isNull()) if (!el->m.isNull())

View File

@ -21,7 +21,7 @@ public:
delete _dem; delete _gmp; delete _dem; delete _gmp;
} }
bool init(); bool init(QFile *file);
void clear(); void clear();
const RectC &bounds() const {return _tre->bounds();} const RectC &bounds() const {return _tre->bounds();}
@ -30,12 +30,12 @@ public:
SubFile *file(SubFile::Type type); SubFile *file(SubFile::Type type);
void polys(const RectC &rect, const Zoom &zoom, void polys(QFile *file, const RectC &rect, const Zoom &zoom,
QList<MapData::Poly> *polygons, QList<MapData::Poly> *lines, QList<MapData::Poly> *polygons, QList<MapData::Poly> *lines,
MapData::PolyCache *cache, QMutex *lock); MapData::PolyCache *cache, QMutex *lock);
void points(const RectC &rect, const Zoom &zoom, void points(QFile *file, const RectC &rect, const Zoom &zoom,
QList<MapData::Point> *points, MapData::PointCache *cache, QMutex *lock); QList<MapData::Point> *points, MapData::PointCache *cache, QMutex *lock);
void elevations(const RectC &rect, const Zoom &zoom, void elevations(QFile *file, const RectC &rect, const Zoom &zoom,
QList<MapData::Elevation> *elevations, MapData::ElevationCache *cache, QList<MapData::Elevation> *elevations, MapData::ElevationCache *cache,
QMutex *lock); QMutex *lock);
@ -78,7 +78,7 @@ public:
} }
private: private:
bool initGMP(); bool initGMP(QFile *file);
bool load(SubFile::Handle &rgnHdl, SubFile::Handle &lblHdl, bool load(SubFile::Handle &rgnHdl, SubFile::Handle &lblHdl,
SubFile::Handle &netHdl, SubFile::Handle &nodHdl); SubFile::Handle &netHdl, SubFile::Handle &nodHdl);
bool loadDem(SubFile::Handle &demHdl); bool loadDem(SubFile::Handle &demHdl);

View File

@ -276,7 +276,7 @@ double IMGMap::elevation(const Coordinates &c)
if (d->hasDEM()) { if (d->hasDEM()) {
QList<MapData::Elevation> tiles; QList<MapData::Elevation> tiles;
d->elevations(RectC(c, Coordinates(c.lon() + DELTA, c.lat() - DELTA)), d->elevations(0, RectC(c, Coordinates(c.lon() + DELTA, c.lat() - DELTA)),
d->zooms().max(), &tiles); d->zooms().max(), &tiles);
DEMTree tree(tiles); DEMTree tree(tiles);