113 const int numTiles =
static_cast<int>( pow( 2, mTileID.zoomLevel() ) );
115 const double z0Width = rootMatrix.
extent().
width();
120 const double tileDX = z0Width / numTiles;
121 const double tileDY = z0Height / numTiles;
122 const double tileXMin = z0xMinimum + mTileID.column() * tileDX;
123 const double tileYMax = z0yMaximum - mTileID.row() * tileDY;
125 QMap<QString, vector_tile::Tile>::const_iterator it = tiles.constBegin();
126 for ( ; it != tiles.constEnd(); ++it )
128 vector_tile::Tile tile = it.value();
130 for (
int layerNum = 0; layerNum < tile.layers_size(); layerNum++ )
132 const ::vector_tile::Tile_Layer &layer = tile.layers( layerNum );
134 const QString layerName = layer.name().c_str();
135 if ( layerSubset && !layerSubset->contains( QString() ) && !layerSubset->contains( layerName ) )
139 QgsFields layerFields = perLayerFields[layerName];
141 const auto allLayerFields = perLayerFields.find( QString() );
142 if ( allLayerFields != perLayerFields.end() )
145 for (
const QgsField &field : allLayerFields.value() )
147 if ( layerFields.
lookupField( field.name() ) == -1 )
149 layerFields.
append( field );
155 QHash<int, int> tagKeyIndexToFieldIndex;
156 for (
int i = 0; i < layer.keys_size(); ++i )
158 const int fieldIndex = layerFields.
indexOf( layer.keys( i ).c_str() );
159 if ( fieldIndex != -1 )
160 tagKeyIndexToFieldIndex.insert( i, fieldIndex );
164 for (
int featureNum = 0; featureNum < layer.features_size(); featureNum++ )
166 const ::vector_tile::Tile_Feature &feature = layer.features( featureNum );
174 fid |= ( layerNum & 0xff ) << 24;
175 fid |= (
static_cast<QgsFeatureId>( mTileID.row() ) & 0xff ) << 32;
176 fid |= (
static_cast<QgsFeatureId>( mTileID.column() ) & 0xff ) << 40;
185 for (
int tagNum = 0; tagNum + 1 < feature.tags_size(); tagNum += 2 )
187 const int keyIndex =
static_cast<int>( feature.tags( tagNum ) );
188 const int fieldIndex = tagKeyIndexToFieldIndex.value( keyIndex, -1 );
189 if ( fieldIndex == -1 )
192 const int valueIndex =
static_cast<int>( feature.tags( tagNum + 1 ) );
193 if ( valueIndex >= layer.values_size() )
195 QgsDebugError( QStringLiteral(
"Invalid value index for attribute" ) );
198 const ::vector_tile::Tile_Value &value = layer.values( valueIndex );
200 if ( value.has_string_value() )
201 f.
setAttribute( fieldIndex, QString::fromStdString( value.string_value() ) );
202 else if ( value.has_float_value() )
203 f.
setAttribute( fieldIndex,
static_cast<double>( value.float_value() ) );
204 else if ( value.has_double_value() )
206 else if ( value.has_int_value() )
207 f.
setAttribute( fieldIndex,
static_cast<long long>( value.int_value() ) );
208 else if ( value.has_uint_value() )
209 f.
setAttribute( fieldIndex,
static_cast<long long>( value.uint_value() ) );
210 else if ( value.has_sint_value() )
211 f.
setAttribute( fieldIndex,
static_cast<long long>( value.sint_value() ) );
212 else if ( value.has_bool_value() )
213 f.
setAttribute( fieldIndex,
static_cast<bool>( value.bool_value() ) );
216 QgsDebugError( QStringLiteral(
"Unexpected attribute value" ) );
224 const int extent =
static_cast<int>( layer.extent() );
225 int cursorx = 0, cursory = 0;
227 QVector<QgsPoint *> outputPoints;
228 QVector<QgsLineString *> outputLinestrings;
229 QVector<QgsPolygon *> outputPolygons;
230 QVector<QgsPoint> tmpPoints;
232 for (
int i = 0; i < feature.geometry_size(); i ++ )
234 const unsigned g = feature.geometry( i );
235 const unsigned cmdId = g & 0x7;
236 const int cmdCount =
static_cast<int>( g >> 3 );
239 if ( i +
static_cast<int>( cmdCount ) * 2 >= feature.geometry_size() )
241 QgsDebugError( QStringLiteral(
"Malformed geometry: invalid cmdCount" ) );
245 if ( feature.type() == vector_tile::Tile_GeomType_POINT )
246 outputPoints.reserve(
static_cast<int>( outputPoints.size() ) + cmdCount );
248 tmpPoints.reserve(
static_cast<int>( tmpPoints.size() ) + cmdCount );
250 for (
int j = 0; j < cmdCount; j++ )
252 const int v =
static_cast<int>( feature.geometry( i + 1 ) );
253 const int w =
static_cast<int>( feature.geometry( i + 2 ) );
254 const int dx = ( ( v >> 1 ) ^ ( -( v & 1 ) ) );
255 const int dy = ( ( w >> 1 ) ^ ( -( w & 1 ) ) );
258 const double px = tileXMin + tileDX * double( cursorx ) / double( extent );
259 const double py = tileYMax - tileDY * double( cursory ) / double( extent );
261 if ( feature.type() == vector_tile::Tile_GeomType_POINT )
263 outputPoints.append(
new QgsPoint( px, py ) );
265 else if ( feature.type() == vector_tile::Tile_GeomType_LINESTRING )
267 if ( tmpPoints.size() > 0 )
272 tmpPoints.append(
QgsPoint( px, py ) );
274 else if ( feature.type() == vector_tile::Tile_GeomType_POLYGON )
276 tmpPoints.append(
QgsPoint( px, py ) );
281 else if ( cmdId == 2 )
283 if ( i +
static_cast<int>( cmdCount ) * 2 >= feature.geometry_size() )
285 QgsDebugError( QStringLiteral(
"Malformed geometry: invalid cmdCount" ) );
288 tmpPoints.reserve( tmpPoints.size() + cmdCount );
289 for (
int j = 0; j < cmdCount; j++ )
291 const int v =
static_cast<int>( feature.geometry( i + 1 ) );
292 const int w =
static_cast<int>( feature.geometry( i + 2 ) );
293 const int dx = ( v >> 1 ) ^ ( -( v & 1 ) );
294 const int dy = ( w >> 1 ) ^ ( -( w & 1 ) );
297 const double px = tileXMin + tileDX * double( cursorx ) / double( extent );
298 const double py = tileYMax - tileDY * double( cursory ) / double( extent );
300 tmpPoints.push_back(
QgsPoint( px, py ) );
304 else if ( cmdId == 7 )
306 if ( feature.type() == vector_tile::Tile_GeomType_POLYGON )
308 tmpPoints.append( tmpPoints.first() );
310 auto ring = std::make_unique<QgsLineString>( tmpPoints );
318 outputPolygons.append( p );
323 if ( outputPolygons.count() != 0 )
325 outputPolygons[outputPolygons.count() - 1]->addInteriorRing( ring.release() );
329 QgsDebugError( QStringLiteral(
"Malformed geometry: first ring of a polygon is interior ring" ) );
337 QgsDebugError( QStringLiteral(
"Unexpected command ID: %1" ).arg( cmdId ) );
342 if ( feature.type() == vector_tile::Tile_GeomType_POINT )
344 geomType = QStringLiteral(
"Point" );
345 if ( outputPoints.count() == 1 )
350 mp->
reserve( outputPoints.count() );
351 for (
int k = 0; k < outputPoints.count(); ++k )
356 else if ( feature.type() == vector_tile::Tile_GeomType_LINESTRING )
358 geomType = QStringLiteral(
"LineString" );
363 if ( outputLinestrings.count() == 1 )
368 mls->
reserve( outputLinestrings.size() );
369 for (
int k = 0; k < outputLinestrings.count(); ++k )
374 else if ( feature.type() == vector_tile::Tile_GeomType_POLYGON )
376 geomType = QStringLiteral(
"Polygon" );
378 if ( outputPolygons.count() == 1 )
383 mpl->
reserve( outputPolygons.size() );
384 for (
int k = 0; k < outputPolygons.count(); ++k )
390 f.
setAttribute( QStringLiteral(
"_geom_type" ), geomType );