129 const int numTiles =
static_cast<int>( pow( 2, mTileID.zoomLevel() ) );
131 const double z0Width = rootMatrix.
extent().
width();
136 const double tileDX = z0Width / numTiles;
137 const double tileDY = z0Height / numTiles;
138 const double tileXMin = z0xMinimum + mTileID.column() * tileDX;
139 const double tileYMax = z0yMaximum - mTileID.row() * tileDY;
141 QMap<QString, vector_tile::Tile>::const_iterator it = tiles.constBegin();
142 for ( ; it != tiles.constEnd(); ++it )
144 vector_tile::Tile tile = it.value();
146 for (
int layerNum = 0; layerNum < tile.layers_size(); layerNum++ )
148 const ::vector_tile::Tile_Layer &layer = tile.layers( layerNum );
150 const QString layerName = layer.name().c_str();
151 if ( layerSubset && !layerSubset->contains( QString() ) && !layerSubset->contains( layerName ) )
155 QgsFields layerFields = perLayerFields[layerName];
157 const auto allLayerFields = perLayerFields.find( QString() );
158 if ( allLayerFields != perLayerFields.end() )
161 for (
const QgsField &field : allLayerFields.value() )
163 if ( layerFields.
lookupField( field.name() ) == -1 )
165 layerFields.
append( field );
171 QHash<int, int> tagKeyIndexToFieldIndex;
172 for (
int i = 0; i < layer.keys_size(); ++i )
174 const int fieldIndex = layerFields.
indexOf( layer.keys( i ).c_str() );
175 if ( fieldIndex != -1 )
176 tagKeyIndexToFieldIndex.insert( i, fieldIndex );
180 for (
int featureNum = 0; featureNum < layer.features_size(); featureNum++ )
182 const ::vector_tile::Tile_Feature &feature = layer.features( featureNum );
190 fid |= ( layerNum & 0xff ) << 24;
191 fid |= (
static_cast<QgsFeatureId>( mTileID.row() ) & 0xff ) << 32;
192 fid |= (
static_cast<QgsFeatureId>( mTileID.column() ) & 0xff ) << 40;
201 for (
int tagNum = 0; tagNum + 1 < feature.tags_size(); tagNum += 2 )
203 const int keyIndex =
static_cast<int>( feature.tags( tagNum ) );
204 const int fieldIndex = tagKeyIndexToFieldIndex.value( keyIndex, -1 );
205 if ( fieldIndex == -1 )
208 const int valueIndex =
static_cast<int>( feature.tags( tagNum + 1 ) );
209 if ( valueIndex >= layer.values_size() )
214 const ::vector_tile::Tile_Value &value = layer.values( valueIndex );
216 if ( value.has_string_value() )
217 f.
setAttribute( fieldIndex, QString::fromStdString( value.string_value() ) );
218 else if ( value.has_float_value() )
219 f.
setAttribute( fieldIndex,
static_cast<double>( value.float_value() ) );
220 else if ( value.has_double_value() )
222 else if ( value.has_int_value() )
223 f.
setAttribute( fieldIndex,
static_cast<long long>( value.int_value() ) );
224 else if ( value.has_uint_value() )
225 f.
setAttribute( fieldIndex,
static_cast<long long>( value.uint_value() ) );
226 else if ( value.has_sint_value() )
227 f.
setAttribute( fieldIndex,
static_cast<long long>( value.sint_value() ) );
228 else if ( value.has_bool_value() )
229 f.
setAttribute( fieldIndex,
static_cast<bool>( value.bool_value() ) );
240 const int extent =
static_cast<int>( layer.extent() );
241 int cursorx = 0, cursory = 0;
243 QVector<QgsPoint *> outputPoints;
244 QVector<QgsLineString *> outputLinestrings;
245 QVector<QgsPolygon *> outputPolygons;
246 QVector<QgsPoint> tmpPoints;
248 for (
int i = 0; i < feature.geometry_size(); i ++ )
250 const unsigned g = feature.geometry( i );
251 const unsigned cmdId = g & 0x7;
252 const int cmdCount =
static_cast<int>( g >> 3 );
255 if ( i +
static_cast<int>( cmdCount ) * 2 >= feature.geometry_size() )
261 if ( feature.type() == vector_tile::Tile_GeomType_POINT )
262 outputPoints.reserve(
static_cast<int>( outputPoints.size() ) + cmdCount );
264 tmpPoints.reserve(
static_cast<int>( tmpPoints.size() ) + cmdCount );
266 for (
int j = 0; j < cmdCount; j++ )
268 const int v =
static_cast<int>( feature.geometry( i + 1 ) );
269 const int w =
static_cast<int>( feature.geometry( i + 2 ) );
270 const int dx = ( ( v >> 1 ) ^ ( -( v & 1 ) ) );
271 const int dy = ( ( w >> 1 ) ^ ( -( w & 1 ) ) );
274 const double px = tileXMin + tileDX * double( cursorx ) / double( extent );
275 const double py = tileYMax - tileDY * double( cursory ) / double( extent );
277 if ( feature.type() == vector_tile::Tile_GeomType_POINT )
279 outputPoints.append(
new QgsPoint( px, py ) );
281 else if ( feature.type() == vector_tile::Tile_GeomType_LINESTRING )
283 if ( tmpPoints.size() > 0 )
288 tmpPoints.append(
QgsPoint( px, py ) );
290 else if ( feature.type() == vector_tile::Tile_GeomType_POLYGON )
292 tmpPoints.append(
QgsPoint( px, py ) );
297 else if ( cmdId == 2 )
299 if ( i +
static_cast<int>( cmdCount ) * 2 >= feature.geometry_size() )
304 tmpPoints.reserve( tmpPoints.size() + cmdCount );
305 for (
int j = 0; j < cmdCount; j++ )
307 const int v =
static_cast<int>( feature.geometry( i + 1 ) );
308 const int w =
static_cast<int>( feature.geometry( i + 2 ) );
309 const int dx = ( v >> 1 ) ^ ( -( v & 1 ) );
310 const int dy = ( w >> 1 ) ^ ( -( w & 1 ) );
313 const double px = tileXMin + tileDX * double( cursorx ) / double( extent );
314 const double py = tileYMax - tileDY * double( cursory ) / double( extent );
316 tmpPoints.push_back(
QgsPoint( px, py ) );
320 else if ( cmdId == 7 )
322 if ( feature.type() == vector_tile::Tile_GeomType_POLYGON )
324 tmpPoints.append( tmpPoints.first() );
326 auto ring = std::make_unique<QgsLineString>( tmpPoints );
334 outputPolygons.append( p );
339 if ( outputPolygons.count() != 0 )
341 outputPolygons[outputPolygons.count() - 1]->addInteriorRing( ring.release() );
345 QgsDebugError( u
"Malformed geometry: first ring of a polygon is interior ring"_s );
353 QgsDebugError( u
"Unexpected command ID: %1"_s.arg( cmdId ) );
358 if ( feature.type() == vector_tile::Tile_GeomType_POINT )
360 geomType = u
"Point"_s;
361 if ( outputPoints.count() == 1 )
366 mp->
reserve( outputPoints.count() );
367 for (
int k = 0; k < outputPoints.count(); ++k )
372 else if ( feature.type() == vector_tile::Tile_GeomType_LINESTRING )
374 geomType = u
"LineString"_s;
379 if ( outputLinestrings.count() == 1 )
384 mls->
reserve( outputLinestrings.size() );
385 for (
int k = 0; k < outputLinestrings.count(); ++k )
390 else if ( feature.type() == vector_tile::Tile_GeomType_POLYGON )
392 geomType = u
"Polygon"_s;
394 if ( outputPolygons.count() == 1 )
399 mpl->
reserve( outputPolygons.size() );
400 for (
int k = 0; k < outputPolygons.count(); ++k )