mirror of
https://github.com/tumic0/QtPBFImagePlugin.git
synced 2024-11-23 19:25:55 +01:00
Improved performance
This commit is contained in:
parent
2a89ef8dfd
commit
a7294b30f8
196
src/pbf.cpp
196
src/pbf.cpp
@ -1,22 +1,24 @@
|
||||
#include <QByteArray>
|
||||
#include <QImage>
|
||||
#include <QPainter>
|
||||
#include <QDebug>
|
||||
#include <QVariantHash>
|
||||
#include "vector_tile.pb.h"
|
||||
#include "style.h"
|
||||
#include "tile.h"
|
||||
#include "pbf.h"
|
||||
|
||||
|
||||
#define MOVE_TO 1
|
||||
#define LINE_TO 2
|
||||
#define CLOSE_PATH 7
|
||||
|
||||
#define POLYGON vector_tile::Tile_GeomType_POLYGON
|
||||
#define LINESTRING vector_tile::Tile_GeomType_LINESTRING
|
||||
#define POINT vector_tile::Tile_GeomType_POINT
|
||||
|
||||
#define MOVE_TO 1
|
||||
#define LINE_TO 2
|
||||
#define CLOSE_PATH 7
|
||||
|
||||
|
||||
static inline qint32 zigzag32decode(quint32 value)
|
||||
{
|
||||
return static_cast<qint32>((value >> 1u) ^ static_cast<quint32>(
|
||||
-static_cast<qint32>(value & 1u)));
|
||||
}
|
||||
|
||||
static inline QPoint parameters(quint32 v1, quint32 v2)
|
||||
{
|
||||
return QPoint(zigzag32decode(v1), zigzag32decode(v2));
|
||||
}
|
||||
|
||||
static QVariant value(const vector_tile::Tile_Value &val)
|
||||
{
|
||||
@ -38,100 +40,37 @@ static QVariant value(const vector_tile::Tile_Value &val)
|
||||
return QVariant();
|
||||
}
|
||||
|
||||
class Feature
|
||||
const QVariant *PBF::Feature::value(const QString &key) const
|
||||
{
|
||||
public:
|
||||
Feature() : _data(0) {}
|
||||
Feature(const vector_tile::Tile_Feature *data, const QVector<QString> *keys,
|
||||
const QVector<QVariant> *values) : _data(data)
|
||||
{
|
||||
for (int i = 0; i < data->tags_size(); i = i + 2)
|
||||
_tags.insert(keys->at(data->tags(i)), values->at(data->tags(i+1)));
|
||||
const KeyHash &keys(_layer->keys());
|
||||
KeyHash::const_iterator it(keys.find(key));
|
||||
if (it == keys.constEnd())
|
||||
return 0;
|
||||
|
||||
switch (data->type()) {
|
||||
case POLYGON:
|
||||
_tags.insert("$type", QVariant("Polygon"));
|
||||
break;
|
||||
case LINESTRING:
|
||||
_tags.insert("$type", QVariant("LineString"));
|
||||
break;
|
||||
case POINT:
|
||||
_tags.insert("$type", QVariant("Point"));
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
const google::protobuf::RepeatedField<google::protobuf::uint32>
|
||||
&tags(_data->tags());
|
||||
google::protobuf::uint32 index = *it;
|
||||
for (int i = 0; i < _data->tags_size(); i = i + 2)
|
||||
if (tags[i] == index)
|
||||
return &(_layer->values().at(tags[i+1]));
|
||||
|
||||
const QVariantHash &tags() const {return _tags;}
|
||||
const vector_tile::Tile_Feature *data() const {return _data;}
|
||||
|
||||
private:
|
||||
QVariantHash _tags;
|
||||
const vector_tile::Tile_Feature *_data;
|
||||
};
|
||||
|
||||
static bool cmp(const Feature &f1, const Feature &f2)
|
||||
{
|
||||
return f1.data()->id() < f2.data()->id();
|
||||
return 0;
|
||||
}
|
||||
|
||||
class Layer
|
||||
QPainterPath PBF::Feature::path(const QSizeF &factor) const
|
||||
{
|
||||
public:
|
||||
Layer(const vector_tile::Tile_Layer *data) : _data(data)
|
||||
{
|
||||
QVector<QString> keys;
|
||||
QVector<QVariant> values;
|
||||
|
||||
for (int i = 0; i < data->keys_size(); i++)
|
||||
keys.append(QString::fromStdString(data->keys(i)));
|
||||
for (int i = 0; i < data->values_size(); i++)
|
||||
values.append(value(data->values(i)));
|
||||
|
||||
_features.reserve(data->features_size());
|
||||
for (int i = 0; i < data->features_size(); i++)
|
||||
_features.append(Feature(&(data->features(i)), &keys, &values));
|
||||
qSort(_features.begin(), _features.end(), cmp);
|
||||
}
|
||||
|
||||
const QVector<Feature> &features() const {return _features;}
|
||||
const vector_tile::Tile_Layer *data() const {return _data;}
|
||||
|
||||
private:
|
||||
const vector_tile::Tile_Layer *_data;
|
||||
QVector<Feature> _features;
|
||||
};
|
||||
|
||||
static inline qint32 zigzag32decode(quint32 value)
|
||||
{
|
||||
return static_cast<qint32>((value >> 1u) ^ static_cast<quint32>(
|
||||
-static_cast<qint32>(value & 1u)));
|
||||
}
|
||||
|
||||
static inline QPoint parameters(quint32 v1, quint32 v2)
|
||||
{
|
||||
return QPoint(zigzag32decode(v1), zigzag32decode(v2));
|
||||
}
|
||||
|
||||
static void drawFeature(const Feature &feature, const Style *style,
|
||||
int styleLayer, const QSizeF &factor, Tile &tile)
|
||||
{
|
||||
if (!style->match(tile.zoom(), styleLayer, feature.tags()))
|
||||
return;
|
||||
|
||||
QPoint cursor;
|
||||
QPainterPath path;
|
||||
|
||||
for (int i = 0; i < feature.data()->geometry_size(); i++) {
|
||||
quint32 g = feature.data()->geometry(i);
|
||||
for (int i = 0; i < _data->geometry_size(); i++) {
|
||||
quint32 g = _data->geometry(i);
|
||||
unsigned cmdId = g & 0x7;
|
||||
unsigned cmdCount = g >> 3;
|
||||
|
||||
if (cmdId == MOVE_TO) {
|
||||
for (unsigned j = 0; j < cmdCount; j++) {
|
||||
QPoint offset = parameters(feature.data()->geometry(i+1),
|
||||
feature.data()->geometry(i+2));
|
||||
QPoint offset = parameters(_data->geometry(i+1),
|
||||
_data->geometry(i+2));
|
||||
i += 2;
|
||||
cursor += offset;
|
||||
path.moveTo(QPointF(cursor.x() * factor.width(),
|
||||
@ -139,8 +78,8 @@ static void drawFeature(const Feature &feature, const Style *style,
|
||||
}
|
||||
} else if (cmdId == LINE_TO) {
|
||||
for (unsigned j = 0; j < cmdCount; j++) {
|
||||
QPoint offset = parameters(feature.data()->geometry(i+1),
|
||||
feature.data()->geometry(i+2));
|
||||
QPoint offset = parameters(_data->geometry(i+1),
|
||||
_data->geometry(i+2));
|
||||
i += 2;
|
||||
cursor += offset;
|
||||
path.lineTo(QPointF(cursor.x() * factor.width(),
|
||||
@ -152,60 +91,35 @@ static void drawFeature(const Feature &feature, const Style *style,
|
||||
}
|
||||
}
|
||||
|
||||
if (path.elementCount())
|
||||
style->drawFeature(tile, styleLayer, path, feature.tags());
|
||||
return path;
|
||||
}
|
||||
|
||||
static void drawLayer(const Layer &layer, const Style *style, int styleLayer,
|
||||
Tile &tile)
|
||||
PBF::Layer::Layer(const vector_tile::Tile_Layer *data) : _data(data)
|
||||
{
|
||||
if (layer.data()->version() > 2)
|
||||
return;
|
||||
_keys.reserve(data->keys_size());
|
||||
for (int i = 0; i < data->keys_size(); i++)
|
||||
_keys.insert(QString::fromStdString(data->keys(i)), i);
|
||||
_values.reserve(data->values_size());
|
||||
for (int i = 0; i < data->values_size(); i++)
|
||||
_values.append(value(data->values(i)));
|
||||
|
||||
QSizeF factor(tile.size().width() / tile.scale().x() /
|
||||
(qreal)layer.data()->extent(), tile.size().height() / tile.scale().y()
|
||||
/ (qreal)layer.data()->extent());
|
||||
|
||||
tile.painter().save();
|
||||
style->setupLayer(tile, styleLayer);
|
||||
for (int i = 0; i < layer.features().size(); i++)
|
||||
drawFeature(layer.features().at(i), style, styleLayer, factor, tile);
|
||||
tile.painter().restore();
|
||||
_features.reserve(data->features_size());
|
||||
for (int i = 0; i < data->features_size(); i++)
|
||||
_features.append(Feature(&(data->features(i)), this));
|
||||
qSort(_features.begin(), _features.end());
|
||||
}
|
||||
|
||||
bool PBF::render(const QByteArray &data, int zoom, const Style *style,
|
||||
const QPointF &scale, QImage *image)
|
||||
PBF::PBF(const vector_tile::Tile &tile)
|
||||
{
|
||||
vector_tile::Tile tile;
|
||||
if (!tile.ParseFromArray(data.constData(), data.size())) {
|
||||
qCritical() << "Invalid tile protocol buffer data";
|
||||
return false;
|
||||
}
|
||||
|
||||
Tile t(image, zoom, scale);
|
||||
|
||||
style->drawBackground(t);
|
||||
|
||||
// Prepare source layers
|
||||
QMap<QString, Layer> layers;
|
||||
for (int i = 0; i < tile.layers_size(); i++) {
|
||||
const vector_tile::Tile_Layer &layer = tile.layers(i);
|
||||
QString name(QString::fromStdString(layer.name()));
|
||||
if (style->sourceLayers().contains(name))
|
||||
layers.insert(name, Layer(&layer));
|
||||
_layers.insert(QString::fromStdString(layer.name()), new Layer(&layer));
|
||||
}
|
||||
|
||||
// Process source layers in order of style layers
|
||||
for (int i = 0; i < style->sourceLayers().size(); i++) {
|
||||
QMap<QString, Layer>::const_iterator it = layers.find(
|
||||
style->sourceLayers().at(i));
|
||||
if (it == layers.constEnd())
|
||||
continue;
|
||||
|
||||
drawLayer(*it, style, i, t);
|
||||
}
|
||||
|
||||
t.text().render(&t.painter());
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
PBF::~PBF()
|
||||
{
|
||||
for (QHash<QString, Layer*>::iterator it = _layers.begin();
|
||||
it != _layers.end(); it++)
|
||||
delete *it;
|
||||
}
|
||||
|
68
src/pbf.h
68
src/pbf.h
@ -1,15 +1,67 @@
|
||||
#ifndef PBF_H
|
||||
#define PBF_H
|
||||
|
||||
class QByteArray;
|
||||
class QImage;
|
||||
class Style;
|
||||
class QPointF;
|
||||
#include <QVariant>
|
||||
#include <QVector>
|
||||
#include <QHash>
|
||||
#include <QPainterPath>
|
||||
#include "vector_tile.pb.h"
|
||||
|
||||
namespace PBF
|
||||
|
||||
typedef QHash<QString, google::protobuf::uint32> KeyHash;
|
||||
|
||||
class PBF
|
||||
{
|
||||
bool render(const QByteArray &data, int zoom, const Style *style,
|
||||
const QPointF &scale, QImage *render);
|
||||
}
|
||||
public:
|
||||
class Layer;
|
||||
|
||||
class Feature
|
||||
{
|
||||
public:
|
||||
Feature() : _data(0), _layer(0) {}
|
||||
Feature(const vector_tile::Tile_Feature *data, const Layer *layer)
|
||||
: _data(data), _layer(layer) {}
|
||||
|
||||
const QVariant *value(const QString &key) const;
|
||||
vector_tile::Tile_GeomType type() const {return _data->type();}
|
||||
QPainterPath path(const QSizeF &factor) const;
|
||||
|
||||
friend bool operator<(const Feature &f1, const Feature &f2);
|
||||
|
||||
private:
|
||||
const vector_tile::Tile_Feature *_data;
|
||||
const Layer *_layer;
|
||||
};
|
||||
|
||||
class Layer
|
||||
{
|
||||
public:
|
||||
|
||||
Layer(const vector_tile::Tile_Layer *data);
|
||||
|
||||
const QVector<Feature> &features() const {return _features;}
|
||||
const QVector<QVariant> &values() const {return _values;}
|
||||
const KeyHash &keys() const {return _keys;}
|
||||
const vector_tile::Tile_Layer *data() const {return _data;}
|
||||
|
||||
private:
|
||||
const vector_tile::Tile_Layer *_data;
|
||||
QVector<Feature> _features;
|
||||
QVector<QVariant> _values;
|
||||
KeyHash _keys;
|
||||
};
|
||||
|
||||
|
||||
PBF(const vector_tile::Tile &tile);
|
||||
~PBF();
|
||||
|
||||
const QHash<QString, Layer*> &layers() const {return _layers;}
|
||||
|
||||
private:
|
||||
QHash<QString, Layer*> _layers;
|
||||
};
|
||||
|
||||
inline bool operator<(const PBF::Feature &f1, const PBF::Feature &f2)
|
||||
{return f1._data->id() < f2._data->id();}
|
||||
|
||||
#endif // PBF_H
|
||||
|
@ -1,8 +1,10 @@
|
||||
#include <QImage>
|
||||
#include <QIODevice>
|
||||
#include <QtEndian>
|
||||
#include <QDebug>
|
||||
#include "gzip.h"
|
||||
#include "pbf.h"
|
||||
#include "tile.h"
|
||||
#include "style.h"
|
||||
#include "pbfhandler.h"
|
||||
|
||||
|
||||
@ -61,6 +63,11 @@ bool PBFHandler::read(QImage *image)
|
||||
ba = device()->readAll();
|
||||
if (ba.isNull())
|
||||
return false;
|
||||
vector_tile::Tile data;
|
||||
if (!data.ParseFromArray(ba.constData(), ba.size())) {
|
||||
qCritical() << "Invalid PBF data";
|
||||
return false;
|
||||
}
|
||||
|
||||
bool ok;
|
||||
int zoom = format().toInt(&ok);
|
||||
@ -71,8 +78,11 @@ bool PBFHandler::read(QImage *image)
|
||||
? QPointF(_scaledSize.width() / TILE_SIZE, _scaledSize.height() / TILE_SIZE)
|
||||
: QPointF(1.0, 1.0);
|
||||
*image = QImage(size, QImage::Format_ARGB32_Premultiplied);
|
||||
Tile tile(image, ok ? zoom : -1, scale);
|
||||
|
||||
return PBF::render(ba, ok ? zoom : -1, _style, scale, image);
|
||||
_style->render(data, tile);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool PBFHandler::supportsOption(ImageOption option) const
|
||||
|
169
src/style.cpp
169
src/style.cpp
@ -10,9 +10,22 @@
|
||||
#include "color.h"
|
||||
#include "font.h"
|
||||
#include "tile.h"
|
||||
#include "pbf.h"
|
||||
#include "style.h"
|
||||
|
||||
|
||||
static vector_tile::Tile_GeomType geometryType(const QString &str)
|
||||
{
|
||||
if (str == "Point")
|
||||
return vector_tile::Tile_GeomType_POINT;
|
||||
else if (str == "LineString")
|
||||
return vector_tile::Tile_GeomType_LINESTRING;
|
||||
else if (str == "Polygon")
|
||||
return vector_tile::Tile_GeomType_POLYGON;
|
||||
else
|
||||
return vector_tile::Tile_GeomType_UNKNOWN;
|
||||
}
|
||||
|
||||
Style::Layer::Filter::Filter(const QJsonArray &json)
|
||||
: _type(Unknown), _not(false)
|
||||
{
|
||||
@ -27,9 +40,15 @@ Style::Layer::Filter::Filter(const QJsonArray &json)
|
||||
if (type == "==") {
|
||||
if (json.size() != 3)
|
||||
INVALID_FILTER(json);
|
||||
_type = EQ;
|
||||
_kv = QPair<QString, QVariant>(json.at(1).toString(),
|
||||
json.at(2).toVariant());
|
||||
if (json.at(1).toString() == "$type") {
|
||||
_type = GeometryType;
|
||||
_kv = QPair<QString, QVariant>(QString(),
|
||||
QVariant(geometryType(json.at(2).toString())));
|
||||
} else {
|
||||
_type = EQ;
|
||||
_kv = QPair<QString, QVariant>(json.at(1).toString(),
|
||||
json.at(2).toVariant());
|
||||
}
|
||||
} else if (type == "!=") {
|
||||
if (json.size() != 3)
|
||||
INVALID_FILTER(json);
|
||||
@ -98,45 +117,68 @@ Style::Layer::Filter::Filter(const QJsonArray &json)
|
||||
INVALID_FILTER(json);
|
||||
}
|
||||
|
||||
bool Style::Layer::Filter::match(const QVariantHash &tags) const
|
||||
bool Style::Layer::Filter::match(const PBF::Feature &feature) const
|
||||
{
|
||||
const QVariant *v;
|
||||
|
||||
switch (_type) {
|
||||
case None:
|
||||
return true;
|
||||
case EQ:
|
||||
return tags.value(_kv.first) == _kv.second;
|
||||
if (!(v = feature.value(_kv.first)))
|
||||
return false;
|
||||
else
|
||||
return *v == _kv.second;
|
||||
case NE:
|
||||
return tags.value(_kv.first) != _kv.second;
|
||||
if (!(v = feature.value(_kv.first)))
|
||||
return true;
|
||||
else
|
||||
return *v != _kv.second;
|
||||
case GT:
|
||||
return tags.value(_kv.first) > _kv.second;
|
||||
if (!(v = feature.value(_kv.first)))
|
||||
return false;
|
||||
else
|
||||
return *v > _kv.second;
|
||||
case GE:
|
||||
return tags.value(_kv.first) >= _kv.second;
|
||||
if (!(v = feature.value(_kv.first)))
|
||||
return false;
|
||||
else
|
||||
return *v >= _kv.second;
|
||||
case LT:
|
||||
return tags.value(_kv.first) < _kv.second;
|
||||
if (!(v = feature.value(_kv.first)))
|
||||
return false;
|
||||
else
|
||||
return *v < _kv.second;
|
||||
case LE:
|
||||
return tags.value(_kv.first) <= _kv.second;
|
||||
if (!(v = feature.value(_kv.first)))
|
||||
return false;
|
||||
else
|
||||
return *v <= _kv.second;
|
||||
case In:
|
||||
return _set.contains(tags.value(_kv.first).toString()) ^ _not;
|
||||
if (!(v = feature.value(_kv.first)))
|
||||
return _not;
|
||||
else
|
||||
return _set.contains((*v).toString()) ^ _not;
|
||||
case Has:
|
||||
return tags.contains(_kv.first) ^ _not;
|
||||
return (feature.value(_kv.first) ? true : false) ^ _not;
|
||||
case All:
|
||||
for (int i = 0; i < _filters.size(); i++) {
|
||||
if (!_filters.at(i).match(tags))
|
||||
for (int i = 0; i < _filters.size(); i++)
|
||||
if (!_filters.at(i).match(feature))
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
case Any:
|
||||
for (int i = 0; i < _filters.size(); i++) {
|
||||
if (_filters.at(i).match(tags))
|
||||
for (int i = 0; i < _filters.size(); i++)
|
||||
if (_filters.at(i).match(feature))
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
case GeometryType:
|
||||
return feature.type() == _kv.second.toUInt();
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
QString Style::Layer::Template::value(int zoom, const QVariantHash &tags) const
|
||||
QString Style::Layer::Template::value(int zoom, const PBF::Feature &feature) const
|
||||
{
|
||||
QRegExp rx = QRegExp("\\{[^\\}]*\\}");
|
||||
QString text(_field.value(zoom));
|
||||
@ -150,8 +192,8 @@ QString Style::Layer::Template::value(int zoom, const QVariantHash &tags) const
|
||||
}
|
||||
for (int i = 0; i < keys.size(); i++) {
|
||||
const QString &key = keys.at(i);
|
||||
const QVariant val = tags.value(key);
|
||||
text.replace(QString("{%1}").arg(key), val.toString());
|
||||
const QVariant *val = feature.value(key);
|
||||
text.replace(QString("{%1}").arg(key), val ? val->toString() : "");
|
||||
}
|
||||
|
||||
return text;
|
||||
@ -428,7 +470,7 @@ Style::Layer::Layer(const QJsonObject &json)
|
||||
_paint = Paint(json["paint"].toObject());
|
||||
}
|
||||
|
||||
bool Style::Layer::match(int zoom, const QVariantHash &tags) const
|
||||
bool Style::Layer::match(int zoom, const PBF::Feature &feature) const
|
||||
{
|
||||
if (zoom >= 0) {
|
||||
if (_minZoom > 0 && zoom < _minZoom)
|
||||
@ -437,7 +479,7 @@ bool Style::Layer::match(int zoom, const QVariantHash &tags) const
|
||||
return false;
|
||||
}
|
||||
|
||||
return _filter.match(tags);
|
||||
return _filter.match(feature);
|
||||
}
|
||||
|
||||
void Style::Layer::setPathPainter(Tile &tile, const Sprites &sprites) const
|
||||
@ -472,13 +514,13 @@ void Style::Layer::setTextProperties(Tile &tile) const
|
||||
}
|
||||
|
||||
void Style::Layer::addSymbol(Tile &tile, const QPainterPath &path,
|
||||
const QVariantHash &tags, const Sprites &sprites) const
|
||||
const PBF::Feature &feature, const Sprites &sprites) const
|
||||
{
|
||||
QString text = _layout.text(tile.zoom(), tags);
|
||||
QString text = _layout.text(tile.zoom(), feature);
|
||||
if (text.isEmpty())
|
||||
return;
|
||||
|
||||
QString icon = _layout.icon(tile.zoom(), tags);
|
||||
QString icon = _layout.icon(tile.zoom(), feature);
|
||||
tile.text().addLabel(text, sprites.icon(icon), path);
|
||||
}
|
||||
|
||||
@ -525,9 +567,6 @@ bool Style::load(const QString &fileName)
|
||||
_layers.append(Layer(layers[i].toObject()));
|
||||
}
|
||||
|
||||
for (int i = 0; i < _layers.size(); i++)
|
||||
_sourceLayers.append(_layers.at(i).sourceLayer());
|
||||
|
||||
QDir styleDir = QFileInfo(fileName).absoluteDir();
|
||||
loadSprites(styleDir, "sprite.json", "sprite.png", _sprites);
|
||||
#ifdef ENABLE_HIDPI
|
||||
@ -548,25 +587,12 @@ const Sprites &Style::sprites(const QPointF &scale) const
|
||||
#endif // ENABLE_HIDPI
|
||||
}
|
||||
|
||||
void Style::setupLayer(Tile &tile, int layer) const
|
||||
void Style::setupLayer(Tile &tile, const Layer &layer) const
|
||||
{
|
||||
const Layer &sl = _layers.at(layer);
|
||||
|
||||
if (sl.isSymbol())
|
||||
sl.setTextProperties(tile);
|
||||
else if (sl.isPath())
|
||||
sl.setPathPainter(tile, sprites(tile.scale()));
|
||||
}
|
||||
|
||||
void Style::drawFeature(Tile &tile, int layer, const QPainterPath &path,
|
||||
const QVariantHash &tags) const
|
||||
{
|
||||
const Layer &sl = _layers.at(layer);
|
||||
|
||||
if (sl.isPath())
|
||||
tile.painter().drawPath(path);
|
||||
else if (sl.isSymbol())
|
||||
sl.addSymbol(tile, path, tags, sprites(tile.scale()));
|
||||
if (layer.isSymbol())
|
||||
layer.setTextProperties(tile);
|
||||
else if (layer.isPath())
|
||||
layer.setPathPainter(tile, sprites(tile.scale()));
|
||||
}
|
||||
|
||||
void Style::drawBackground(Tile &tile) const
|
||||
@ -587,3 +613,52 @@ void Style::drawBackground(Tile &tile) const
|
||||
//tile.painter().setPen(Qt::red);
|
||||
//tile.painter().drawRect(rect);
|
||||
}
|
||||
|
||||
void Style::drawFeature(const PBF::Feature &feature, const Layer &layer,
|
||||
Tile &tile, const QSizeF &factor) const
|
||||
{
|
||||
if (!layer.match(tile.zoom(), feature))
|
||||
return;
|
||||
|
||||
QPainterPath path(feature.path(factor));
|
||||
if (!path.elementCount())
|
||||
return;
|
||||
|
||||
if (layer.isPath())
|
||||
tile.painter().drawPath(path);
|
||||
else if (layer.isSymbol())
|
||||
layer.addSymbol(tile, path, feature, sprites(tile.scale()));
|
||||
}
|
||||
|
||||
void Style::drawLayer(const PBF::Layer &pbfLayer, const Layer &styleLayer,
|
||||
Tile &tile) const
|
||||
{
|
||||
if (pbfLayer.data()->version() > 2)
|
||||
return;
|
||||
|
||||
QSizeF factor(tile.size().width() / tile.scale().x() /
|
||||
(qreal)pbfLayer.data()->extent(), tile.size().height() / tile.scale().y()
|
||||
/ (qreal)pbfLayer.data()->extent());
|
||||
|
||||
tile.painter().save();
|
||||
setupLayer(tile, styleLayer);
|
||||
for (int i = 0; i < pbfLayer.features().size(); i++)
|
||||
drawFeature(pbfLayer.features().at(i), styleLayer, tile, factor);
|
||||
tile.painter().restore();
|
||||
}
|
||||
|
||||
void Style::render(const PBF &data, Tile &tile) const
|
||||
{
|
||||
drawBackground(tile);
|
||||
|
||||
for (int i = 0; i < _layers.size(); i++) {
|
||||
QHash<QString, PBF::Layer*>::const_iterator it = data.layers().find(
|
||||
_layers.at(i).sourceLayer());
|
||||
if (it == data.layers().constEnd())
|
||||
continue;
|
||||
|
||||
drawLayer(**it, _layers.at(i), tile);
|
||||
}
|
||||
|
||||
tile.text().render(&tile.painter());
|
||||
}
|
||||
|
38
src/style.h
38
src/style.h
@ -11,6 +11,7 @@
|
||||
#include <QPen>
|
||||
#include <QBrush>
|
||||
#include <QFont>
|
||||
#include "pbf.h"
|
||||
#include "config.h"
|
||||
#include "text.h"
|
||||
#include "function.h"
|
||||
@ -29,16 +30,7 @@ public:
|
||||
Style(QObject *parent = 0) : QObject(parent) {}
|
||||
|
||||
bool load(const QString &fileName);
|
||||
|
||||
const QStringList &sourceLayers() const
|
||||
{return _sourceLayers;}
|
||||
|
||||
bool match(int zoom, int layer, const QVariantHash &tags) const
|
||||
{return _layers.at(layer).match(zoom, tags);}
|
||||
void drawBackground(Tile &tile) const;
|
||||
void setupLayer(Tile &tile, int layer) const;
|
||||
void drawFeature(Tile &tile, int layer, const QPainterPath &path,
|
||||
const QVariantHash &tags) const;
|
||||
void render(const PBF &data, Tile &tile) const;
|
||||
|
||||
private:
|
||||
class Layer {
|
||||
@ -51,11 +43,11 @@ private:
|
||||
bool isBackground() const {return (_type == Background);}
|
||||
bool isSymbol() const {return (_type == Symbol);}
|
||||
|
||||
bool match(int zoom, const QVariantHash &tags) const;
|
||||
bool match(int zoom, const PBF::Feature &feature) const;
|
||||
void setPathPainter(Tile &tile, const Sprites &sprites) const;
|
||||
void setTextProperties(Tile &tile) const;
|
||||
void addSymbol(Tile &tile, const QPainterPath &path,
|
||||
const QVariantHash &tags, const Sprites &sprites) const;
|
||||
const PBF::Feature &feature, const Sprites &sprites) const;
|
||||
|
||||
private:
|
||||
enum Type {
|
||||
@ -71,13 +63,13 @@ private:
|
||||
Filter() : _type(None) {}
|
||||
Filter(const QJsonArray &json);
|
||||
|
||||
bool match(const QVariantHash &tags) const;
|
||||
bool match(const PBF::Feature &feature) const;
|
||||
private:
|
||||
enum Type {
|
||||
None, Unknown,
|
||||
EQ, NE, GE, GT, LE, LT,
|
||||
All, Any,
|
||||
In, Has
|
||||
In, Has, GeometryType
|
||||
};
|
||||
|
||||
Type _type;
|
||||
@ -91,7 +83,7 @@ private:
|
||||
public:
|
||||
Template() {}
|
||||
Template(const FunctionS &str) : _field(str) {}
|
||||
QString value(int zoom, const QVariantHash &tags) const;
|
||||
QString value(int zoom, const PBF::Feature &feature) const;
|
||||
|
||||
private:
|
||||
FunctionS _field;
|
||||
@ -107,10 +99,10 @@ private:
|
||||
{return _textMaxWidth.value(zoom);}
|
||||
qreal maxTextAngle(int zoom) const
|
||||
{return _textMaxAngle.value(zoom);}
|
||||
QString text(int zoom, const QVariantHash &tags) const
|
||||
{return _text.value(zoom, tags).trimmed();}
|
||||
QString icon(int zoom, const QVariantHash &tags) const
|
||||
{return _icon.value(zoom, tags);}
|
||||
QString text(int zoom, const PBF::Feature &feature) const
|
||||
{return _text.value(zoom, feature).trimmed();}
|
||||
QString icon(int zoom, const PBF::Feature &feature) const
|
||||
{return _icon.value(zoom, feature);}
|
||||
QFont font(int zoom) const;
|
||||
Qt::PenCapStyle lineCap(int zoom) const;
|
||||
Qt::PenJoinStyle lineJoin(int zoom) const;
|
||||
@ -170,8 +162,14 @@ private:
|
||||
|
||||
const Sprites &sprites(const QPointF &scale) const;
|
||||
|
||||
void drawBackground(Tile &tile) const;
|
||||
void setupLayer(Tile &tile, const Layer &layer) const;
|
||||
void drawFeature(const PBF::Feature &feature, const Layer &layer,
|
||||
Tile &tile, const QSizeF &factor) const;
|
||||
void drawLayer(const PBF::Layer &pbfLayer, const Layer &styleLayer,
|
||||
Tile &tile) const;
|
||||
|
||||
QVector<Layer> _layers;
|
||||
QStringList _sourceLayers;
|
||||
Sprites _sprites;
|
||||
#ifdef ENABLE_HIDPI
|
||||
Sprites _sprites2x;
|
||||
|
Loading…
Reference in New Issue
Block a user