38 if ( !tile.ParseFromArray( rawTileData.constData(), rawTileData.count() ) )
43 mLayerNameToIndex.clear();
44 for (
int layerNum = 0; layerNum < tile.layers_size(); layerNum++ )
46 const ::vector_tile::Tile_Layer &layer = tile.layers( layerNum );
47 QString layerName = layer.name().c_str();
48 mLayerNameToIndex[layerName] = layerNum;
55 QStringList layerNames;
56 for (
int layerNum = 0; layerNum < tile.layers_size(); layerNum++ )
58 const ::vector_tile::Tile_Layer &layer = tile.layers( layerNum );
59 QString layerName = layer.name().c_str();
60 layerNames << layerName;
67 if ( !mLayerNameToIndex.contains( layerName ) )
70 const ::vector_tile::Tile_Layer &layer = tile.layers( mLayerNameToIndex[layerName] );
71 QStringList fieldNames;
72 for (
int i = 0; i < layer.keys_size(); ++i )
74 QString fieldName = layer.keys( i ).c_str();
75 fieldNames << fieldName;
84 int numTiles =
static_cast<int>( pow( 2, mTileID.
zoomLevel() ) );
85 double z0xMin = -20037508.3427892, z0yMin = -20037508.3427892;
86 double z0xMax = 20037508.3427892, z0yMax = 20037508.3427892;
87 double tileDX = ( z0xMax - z0xMin ) / numTiles;
88 double tileDY = ( z0yMax - z0yMin ) / numTiles;
89 double tileXMin = z0xMin + mTileID.
column() * tileDX;
90 double tileYMax = z0yMax - mTileID.
row() * tileDY;
92 for (
int layerNum = 0; layerNum < tile.layers_size(); layerNum++ )
94 const ::vector_tile::Tile_Layer &layer = tile.layers( layerNum );
96 QString layerName = layer.name().c_str();
98 QgsFields layerFields = perLayerFields[layerName];
101 QHash<int, int> tagKeyIndexToFieldIndex;
102 for (
int i = 0; i < layer.keys_size(); ++i )
104 int fieldIndex = layerFields.
indexOf( layer.keys( i ).c_str() );
105 if ( fieldIndex != -1 )
106 tagKeyIndexToFieldIndex.insert( i, fieldIndex );
110 for (
int featureNum = 0; featureNum < layer.features_size(); featureNum++ )
112 const ::vector_tile::Tile_Feature &feature = layer.features( featureNum );
115 if ( feature.has_id() )
123 fid |= ( layerNum & 0xff ) << 24;
134 for (
int tagNum = 0; tagNum + 1 < feature.tags_size(); tagNum += 2 )
136 int keyIndex =
static_cast<int>( feature.tags( tagNum ) );
137 int fieldIndex = tagKeyIndexToFieldIndex.value( keyIndex, -1 );
138 if ( fieldIndex == -1 )
141 int valueIndex =
static_cast<int>( feature.tags( tagNum + 1 ) );
142 if ( valueIndex >= layer.values_size() )
144 QgsDebugMsg( QStringLiteral(
"Invalid value index for attribute" ) );
147 const ::vector_tile::Tile_Value &value = layer.values( valueIndex );
149 if ( value.has_string_value() )
150 f.
setAttribute( fieldIndex, QString::fromStdString( value.string_value() ) );
151 else if ( value.has_float_value() )
152 f.
setAttribute( fieldIndex,
static_cast<double>( value.float_value() ) );
153 else if ( value.has_double_value() )
155 else if ( value.has_int_value() )
156 f.
setAttribute( fieldIndex,
static_cast<int>( value.int_value() ) );
157 else if ( value.has_uint_value() )
158 f.
setAttribute( fieldIndex,
static_cast<int>( value.uint_value() ) );
159 else if ( value.has_sint_value() )
160 f.
setAttribute( fieldIndex,
static_cast<int>( value.sint_value() ) );
161 else if ( value.has_bool_value() )
162 f.
setAttribute( fieldIndex,
static_cast<bool>( value.bool_value() ) );
165 QgsDebugMsg( QStringLiteral(
"Unexpected attribute value" ) );
173 int extent =
static_cast<int>( layer.extent() );
174 int cursorx = 0, cursory = 0;
176 QVector<QgsPoint *> outputPoints;
177 QVector<QgsLineString *> outputLinestrings;
178 QVector<QgsPolygon *> outputPolygons;
179 QVector<QgsPoint> tmpPoints;
181 for (
int i = 0; i < feature.geometry_size(); i ++ )
183 unsigned g = feature.geometry( i );
184 unsigned cmdId = g & 0x7;
185 unsigned cmdCount = g >> 3;
188 if ( i +
static_cast<int>( cmdCount ) * 2 >= feature.geometry_size() )
190 QgsDebugMsg( QStringLiteral(
"Malformed geometry: invalid cmdCount" ) );
193 for (
unsigned j = 0; j < cmdCount; j++ )
195 unsigned v = feature.geometry( i + 1 );
196 unsigned w = feature.geometry( i + 2 );
197 int dx = ( ( v >> 1 ) ^ ( -( v & 1 ) ) );
198 int dy = ( ( w >> 1 ) ^ ( -( w & 1 ) ) );
201 double px = tileXMin + tileDX * double( cursorx ) / double( extent );
202 double py = tileYMax - tileDY * double( cursory ) / double( extent );
204 if ( feature.type() == vector_tile::Tile_GeomType_POINT )
206 outputPoints.append(
new QgsPoint( px, py ) );
208 else if ( feature.type() == vector_tile::Tile_GeomType_LINESTRING )
210 if ( tmpPoints.size() > 0 )
215 tmpPoints.append(
QgsPoint( px, py ) );
217 else if ( feature.type() == vector_tile::Tile_GeomType_POLYGON )
219 tmpPoints.append(
QgsPoint( px, py ) );
224 else if ( cmdId == 2 )
226 if ( i +
static_cast<int>( cmdCount ) * 2 >= feature.geometry_size() )
228 QgsDebugMsg( QStringLiteral(
"Malformed geometry: invalid cmdCount" ) );
231 for (
unsigned j = 0; j < cmdCount; j++ )
233 unsigned v = feature.geometry( i + 1 );
234 unsigned w = feature.geometry( i + 2 );
235 int dx = ( ( v >> 1 ) ^ ( -( v & 1 ) ) );
236 int dy = ( ( w >> 1 ) ^ ( -( w & 1 ) ) );
239 double px = tileXMin + tileDX * double( cursorx ) / double( extent );
240 double py = tileYMax - tileDY * double( cursory ) / double( extent );
242 tmpPoints.push_back(
QgsPoint( px, py ) );
246 else if ( cmdId == 7 )
248 if ( feature.type() == vector_tile::Tile_GeomType_POLYGON )
250 tmpPoints.append( tmpPoints.first() );
252 std::unique_ptr<QgsLineString> ring(
new QgsLineString( tmpPoints ) );
260 outputPolygons.append( p );
265 if ( outputPolygons.count() != 0 )
267 outputPolygons[outputPolygons.count() - 1]->addInteriorRing( ring.release() );
271 QgsDebugMsg( QStringLiteral(
"Malformed geometry: first ring of a polygon is interior ring" ) );
279 QgsDebugMsg( QStringLiteral(
"Unexpected command ID: %1" ).arg( cmdId ) );
284 if ( feature.type() == vector_tile::Tile_GeomType_POINT )
286 geomType = QStringLiteral(
"Point" );
287 if ( outputPoints.count() == 1 )
292 for (
int k = 0; k < outputPoints.count(); ++k )
297 else if ( feature.type() == vector_tile::Tile_GeomType_LINESTRING )
299 geomType = QStringLiteral(
"LineString" );
304 if ( outputLinestrings.count() == 1 )
309 for (
int k = 0; k < outputLinestrings.count(); ++k )
314 else if ( feature.type() == vector_tile::Tile_GeomType_POLYGON )
316 geomType = QStringLiteral(
"Polygon" );
318 if ( outputPolygons.count() == 1 )
323 for (
int k = 0; k < outputPolygons.count(); ++k )
329 f.
setAttribute( QStringLiteral(
"_geom_type" ), geomType );