132 const int numTiles =
static_cast<int>( pow( 2, mTileID.zoomLevel() ) );
134 const double z0Width = rootMatrix.
extent().
width();
139 const double tileDX = z0Width / numTiles;
140 const double tileDY = z0Height / numTiles;
141 const double tileXMin = z0xMinimum + mTileID.column() * tileDX;
142 const double tileYMax = z0yMaximum - mTileID.row() * tileDY;
144 QMap<QString, vector_tile::Tile>::const_iterator it = tiles.constBegin();
145 for ( ; it != tiles.constEnd(); ++it )
147 vector_tile::Tile tile = it.value();
149 for (
int layerNum = 0; layerNum < tile.layers_size(); layerNum++ )
151 const ::vector_tile::Tile_Layer &layer = tile.layers( layerNum );
153 const QString layerName = layer.name().c_str();
154 if ( layerSubset && !layerSubset->contains( QString() ) && !layerSubset->contains( layerName ) )
158 QgsFields layerFields = perLayerFields[layerName];
160 const auto allLayerFields = perLayerFields.find( QString() );
161 if ( allLayerFields != perLayerFields.end() )
164 for (
const QgsField &field : allLayerFields.value() )
166 if ( layerFields.
lookupField( field.name() ) == -1 )
168 layerFields.
append( field );
174 QHash<int, int> tagKeyIndexToFieldIndex;
175 for (
int i = 0; i < layer.keys_size(); ++i )
177 const int fieldIndex = layerFields.
indexOf( layer.keys( i ).c_str() );
178 if ( fieldIndex != -1 )
179 tagKeyIndexToFieldIndex.insert( i, fieldIndex );
183 for (
int featureNum = 0; featureNum < layer.features_size(); featureNum++ )
185 const ::vector_tile::Tile_Feature &feature = layer.features( featureNum );
193 fid |= ( layerNum & 0xff ) << 24;
194 fid |= (
static_cast<QgsFeatureId>( mTileID.row() ) & 0xff ) << 32;
195 fid |= (
static_cast<QgsFeatureId>( mTileID.column() ) & 0xff ) << 40;
204 for (
int tagNum = 0; tagNum + 1 < feature.tags_size(); tagNum += 2 )
206 const int keyIndex =
static_cast<int>( feature.tags( tagNum ) );
207 const int fieldIndex = tagKeyIndexToFieldIndex.value( keyIndex, -1 );
208 if ( fieldIndex == -1 )
211 const int valueIndex =
static_cast<int>( feature.tags( tagNum + 1 ) );
212 if ( valueIndex >= layer.values_size() )
217 const ::vector_tile::Tile_Value &value = layer.values( valueIndex );
219 if ( value.has_string_value() )
220 f.
setAttribute( fieldIndex, QString::fromStdString( value.string_value() ) );
221 else if ( value.has_float_value() )
222 f.
setAttribute( fieldIndex,
static_cast<double>( value.float_value() ) );
223 else if ( value.has_double_value() )
225 else if ( value.has_int_value() )
226 f.
setAttribute( fieldIndex,
static_cast<long long>( value.int_value() ) );
227 else if ( value.has_uint_value() )
228 f.
setAttribute( fieldIndex,
static_cast<long long>( value.uint_value() ) );
229 else if ( value.has_sint_value() )
230 f.
setAttribute( fieldIndex,
static_cast<long long>( value.sint_value() ) );
231 else if ( value.has_bool_value() )
232 f.
setAttribute( fieldIndex,
static_cast<bool>( value.bool_value() ) );
243 const int extent =
static_cast<int>( layer.extent() );
244 int cursorx = 0, cursory = 0;
246 QVector<QgsPoint *> outputPoints;
247 QVector<QgsLineString *> outputLinestrings;
248 QVector<QgsPolygon *> outputPolygons;
249 QVector<QgsPoint> tmpPoints;
251 for (
int i = 0; i < feature.geometry_size(); i ++ )
253 const unsigned g = feature.geometry( i );
254 const unsigned cmdId = g & 0x7;
255 const int cmdCount =
static_cast<int>( g >> 3 );
258 if ( i +
static_cast<int>( cmdCount ) * 2 >= feature.geometry_size() )
264 if ( feature.type() == vector_tile::Tile_GeomType_POINT )
265 outputPoints.reserve(
static_cast<int>( outputPoints.size() ) + cmdCount );
267 tmpPoints.reserve(
static_cast<int>( tmpPoints.size() ) + cmdCount );
269 for (
int j = 0; j < cmdCount; j++ )
271 const int v =
static_cast<int>( feature.geometry( i + 1 ) );
272 const int w =
static_cast<int>( feature.geometry( i + 2 ) );
273 const int dx = ( ( v >> 1 ) ^ ( -( v & 1 ) ) );
274 const int dy = ( ( w >> 1 ) ^ ( -( w & 1 ) ) );
277 const double px = tileXMin + tileDX * double( cursorx ) / double( extent );
278 const double py = tileYMax - tileDY * double( cursory ) / double( extent );
280 if ( feature.type() == vector_tile::Tile_GeomType_POINT )
282 outputPoints.append(
new QgsPoint( px, py ) );
284 else if ( feature.type() == vector_tile::Tile_GeomType_LINESTRING )
286 if ( tmpPoints.size() > 0 )
291 tmpPoints.append(
QgsPoint( px, py ) );
293 else if ( feature.type() == vector_tile::Tile_GeomType_POLYGON )
295 tmpPoints.append(
QgsPoint( px, py ) );
300 else if ( cmdId == 2 )
302 if ( i +
static_cast<int>( cmdCount ) * 2 >= feature.geometry_size() )
307 tmpPoints.reserve( tmpPoints.size() + cmdCount );
308 for (
int j = 0; j < cmdCount; j++ )
310 const int v =
static_cast<int>( feature.geometry( i + 1 ) );
311 const int w =
static_cast<int>( feature.geometry( i + 2 ) );
312 const int dx = ( v >> 1 ) ^ ( -( v & 1 ) );
313 const int dy = ( w >> 1 ) ^ ( -( w & 1 ) );
316 const double px = tileXMin + tileDX * double( cursorx ) / double( extent );
317 const double py = tileYMax - tileDY * double( cursory ) / double( extent );
319 tmpPoints.push_back(
QgsPoint( px, py ) );
323 else if ( cmdId == 7 )
325 if ( feature.type() == vector_tile::Tile_GeomType_POLYGON )
327 tmpPoints.append( tmpPoints.first() );
329 auto ring = std::make_unique<QgsLineString>( tmpPoints );
337 outputPolygons.append( p );
342 if ( outputPolygons.count() != 0 )
344 outputPolygons[outputPolygons.count() - 1]->addInteriorRing( ring.release() );
348 QgsDebugError( u
"Malformed geometry: first ring of a polygon is interior ring"_s );
356 QgsDebugError( u
"Unexpected command ID: %1"_s.arg( cmdId ) );
361 if ( feature.type() == vector_tile::Tile_GeomType_POINT )
363 geomType = u
"Point"_s;
364 if ( outputPoints.count() == 1 )
369 mp->
reserve( outputPoints.count() );
370 for (
int k = 0; k < outputPoints.count(); ++k )
375 else if ( feature.type() == vector_tile::Tile_GeomType_LINESTRING )
377 geomType = u
"LineString"_s;
382 if ( outputLinestrings.count() == 1 )
387 mls->
reserve( outputLinestrings.size() );
388 for (
int k = 0; k < outputLinestrings.count(); ++k )
393 else if ( feature.type() == vector_tile::Tile_GeomType_POLYGON )
395 geomType = u
"Polygon"_s;
397 if ( outputPolygons.count() == 1 )
402 mpl->
reserve( outputPolygons.size() );
403 for (
int k = 0; k < outputPolygons.count(); ++k )