@@ -164,6 +164,89 @@ struct MLTRoundtripGeometryTests {
164164 #expect ( feat . geometry is MultiPoint )
165165 }
166166
167+ // Regression: a MultiPolygon with a single polygon containing many rings
168+ // and vertices that fall OUTSIDE the tile extent (negative coords, as
169+ // happens with buffered PostGIS tiles). The C++ MLT encoder must preserve
170+ // the ring→vertex mapping so that decoding returns each ring's original
171+ // vertices in order. This validates the workaround that disables the
172+ // Morton/Hilbert dictionary vertex encoding in Bridge.cpp.
173+ @ Test
174+ func multiPolygonManyRingsWithNegativeCoordsRoundtrip ( ) throws {
175+ // One polygon with 67 rings; ring0 has 5 vertices starting at (-405, 2730).
176+ // Holes use smaller rings. Coordinates deliberately include negative
177+ // values (buffer zone) to exercise the space-filling-curve path.
178+ var rings : [ [ Coordinate3D ] ] = [ ]
179+ // Ring 0 (exterior): a large rectangle spanning the buffer + tile.
180+ // Winding is chosen so that `normalizePolygonTilespace` does NOT reverse
181+ // it (negative tile-space area), so expectedRing0 == ring0.
182+ let ring0 = [
183+ Coordinate3D ( x : - 405.0 , y : - 400.0 , projection : . noSRID ) ,
184+ Coordinate3D ( x : 4502.0 , y : - 400.0 , projection : . noSRID ) ,
185+ Coordinate3D ( x : 4502.0 , y : 2730.0 , projection : . noSRID ) ,
186+ Coordinate3D ( x : - 405.0 , y : 2730.0 , projection : . noSRID ) ,
187+ Coordinate3D ( x : - 405.0 , y : - 400.0 , projection : . noSRID ) ,
188+ ]
189+ rings . append ( ring0 )
190+ // 66 holes, each a small triangle in-tile to vary winding/offsets.
191+ for i in 0 ..< 66 {
192+ let base = Double ( i * 60 ) + 10
193+ rings . append ( [
194+ Coordinate3D ( x : base , y : base , projection : . noSRID ) ,
195+ Coordinate3D ( x : base + 50 , y : base , projection : . noSRID ) ,
196+ Coordinate3D ( x : base + 25 , y : base + 50 , projection : . noSRID ) ,
197+ Coordinate3D ( x : base , y : base , projection : . noSRID ) ,
198+ ] )
199+ }
200+ let poly = try #require ( Polygon ( rings ) )
201+ let mpoly = try #require ( MultiPolygon ( [ poly ] ) )
202+ // Add MANY features with VARIED geometries in the same layer, so the
203+ // Morton/Hilbert dictionary is built across heterogeneous vertices
204+ // (this mirrors real tiles like `oceans` where features have different
205+ // ring counts/sizes and is what triggers the reorder bug).
206+ var features : [ Feature ] = [ ]
207+ features . append ( Feature ( mpoly , id : . int ( 0 ) ) )
208+ // Add small varied polygons with different vertex ranges.
209+ for i in 1 ..< 25 {
210+ let off = Double ( i ) * 100.0
211+ let small = try #require ( Polygon ( [ [
212+ Coordinate3D ( x : off , y : off , projection : . noSRID ) ,
213+ Coordinate3D ( x : off + 200 , y : off , projection : . noSRID ) ,
214+ Coordinate3D ( x : off + 100 , y : off + 200 , projection : . noSRID ) ,
215+ Coordinate3D ( x : off , y : off , projection : . noSRID ) ,
216+ ] ] ) )
217+ features . append ( Feature ( try #require ( MultiPolygon ( [ small ] ) ) , id : . int ( i ) ) )
218+ }
219+
220+ guard let data = MLTEncoder . encode (
221+ layers : [ " test " : VectorTile . LayerContainer ( features : features , boundingBox : nil ) ] ,
222+ x : 0 , y : 0 , z : 0 , projection : . noSRID )
223+ else { return }
224+ let decoded = try MLTDecoder . decode ( from : data , x : 0 , y : 0 , z : 0 , projection : . noSRID )
225+ let layer = try #require ( decoded [ " test " ] )
226+ #expect ( layer . features . count == 25 , " all 25 features decoded " )
227+ // Find the feature whose ring0 has 67 rings (the big mpoly). Its ring0
228+ // must come back with its original first vertex.
229+ let big = try #require ( layer . features . first ( where : {
230+ ( $0 . geometry as? MultiPolygon ) ? . coordinates . first ? . count == 67
231+ } ) )
232+ let result = try #require ( big . geometry as? MultiPolygon )
233+ #expect ( result . coordinates . count == 1 , " one polygon " )
234+ #expect ( result . coordinates [ 0 ] . count == 67 , " 67 rings " )
235+ // Ring 0 must come back with its original first vertex (allowing Int rounding).
236+ // normalizePolygonTilespace keeps ring0 as-is because its tile-space area
237+ // is negative (exterior), so expected == original.
238+ let expectedRing0 = ring0
239+ let decodedRing0 = result . coordinates [ 0 ] [ 0 ]
240+ #expect ( decodedRing0 . count == expectedRing0 . count )
241+ #expect ( abs ( decodedRing0 [ 0 ] . x - expectedRing0 [ 0 ] . x ) < 1.0 , " ring0 first x preserved (got \( decodedRing0 [ 0 ] . x ) , want \( expectedRing0 [ 0 ] . x ) ) " )
242+ #expect ( abs ( decodedRing0 [ 0 ] . y - expectedRing0 [ 0 ] . y ) < 1.0 , " ring0 first y preserved (got \( decodedRing0 [ 0 ] . y ) , want \( expectedRing0 [ 0 ] . y ) ) " )
243+ // Diagnostic: dump decoded ring0 vs expected to spot any misalignment.
244+ if abs ( decodedRing0 [ 0 ] . y - expectedRing0 [ 0 ] . y ) >= 1.0 {
245+ print ( " Expected ring0: \( expectedRing0 . map { " ( \( $0 . x ) , \( $0 . y ) ) " } ) " )
246+ print ( " Decoded ring0: \( decodedRing0 . map { " ( \( $0 . x ) , \( $0 . y ) ) " } ) " )
247+ }
248+ }
249+
167250 @ Test
168251 func altitudeIsDropped ( ) throws {
169252 let coord = Coordinate3D ( latitude : 10.0 , longitude : 20.0 , altitude : 500.0 , m : 123.0 )
0 commit comments