@@ -120,3 +120,99 @@ test_that("inspire_grid_from_ids_internal handles empty input", {
120120 " Input 'inspire' cannot be an empty vector."
121121 )
122122})
123+
124+ test_that(" Round-trip: grid from extent -> short IDs -> grid from IDs matches original" , {
125+ # This test verifies that a grid can be correctly reconstructed from short IDs
126+ # when CRS is provided, fixing the coordinate scaling and CRS propagation bugs.
127+
128+ # Step 1: Create a small extent for testing
129+ test_extent <- st_bbox(
130+ c(xmin = 1000000 , ymin = 1000000 , xmax = 1030000 , ymax = 1030000 ),
131+ crs = st_crs(TARGET_CRS )
132+ )
133+
134+ # Step 2: Generate the original grid from extent with short IDs
135+ grid_original <- inspire_grid_from_extent(
136+ grid_extent = test_extent ,
137+ cellsize_m = CELLSIZE ,
138+ crs = TARGET_CRS ,
139+ output_type = " sf_polygons" ,
140+ id_format = " short"
141+ )
142+
143+ expect_s3_class(grid_original , " sf" )
144+ expect_gt(nrow(grid_original ), 0 )
145+ expect_true(" GRD_ID" %in% names(grid_original ))
146+
147+ # Step 3: Extract the short IDs
148+ short_ids <- grid_original $ GRD_ID
149+
150+ # Verify they are indeed short format (no "CRS" prefix)
151+ expect_true(all(! startsWith(short_ids , " CRS" )))
152+
153+ # Step 4: Regenerate grid from short IDs WITH crs parameter
154+ # This should NOT produce a warning and should use the correct CRS
155+ grid_regenerated <- inspire_grid_from_ids_internal(
156+ short_ids ,
157+ output_type = " sf_polygons" ,
158+ crs = TARGET_CRS
159+ )
160+
161+ # Step 5: Compare the two grids
162+ expect_s3_class(grid_regenerated , " sf" )
163+ expect_equal(nrow(grid_regenerated ), nrow(grid_original ))
164+ expect_equal(st_crs(grid_regenerated ), st_crs(grid_original ))
165+
166+ # The regenerated grid should have the same cell positions
167+ # Sort both by short ID to ensure proper comparison
168+ grid_original_sorted <- grid_original [order(grid_original $ GRD_ID ), ]
169+ grid_regenerated_sorted <- grid_regenerated [order(grid_regenerated $ id ), ]
170+
171+ # Compare LLC coordinates (these should match exactly)
172+ expect_equal(
173+ grid_original_sorted $ X_LLC ,
174+ grid_regenerated_sorted $ X_LLC ,
175+ tolerance = 1e-6
176+ )
177+ expect_equal(
178+ grid_original_sorted $ Y_LLC ,
179+ grid_regenerated_sorted $ Y_LLC ,
180+ tolerance = 1e-6
181+ )
182+
183+ # Compare geometries (should be identical)
184+ geom_original <- st_geometry(grid_original_sorted )
185+ geom_regenerated <- st_geometry(grid_regenerated_sorted )
186+
187+ # Extract centroids and compare
188+ centroid_original <- st_coordinates(st_centroid(geom_original ))
189+ centroid_regenerated <- st_coordinates(st_centroid(geom_regenerated ))
190+
191+ expect_equal(
192+ centroid_original [, " X" ],
193+ centroid_regenerated [, " X" ],
194+ tolerance = 1e-6
195+ )
196+ expect_equal(
197+ centroid_original [, " Y" ],
198+ centroid_regenerated [, " Y" ],
199+ tolerance = 1e-6
200+ )
201+ })
202+
203+ test_that(" Short IDs with explicit CRS parameter do not produce warnings" , {
204+ # This test verifies that the CRS propagation fix works correctly
205+ short_ids <- c(" 10kmN100E100" , " 10kmN101E100" )
206+
207+ # With CRS provided, there should be NO warning
208+ expect_no_warning(
209+ grid_with_crs <- inspire_grid_from_ids_internal(
210+ short_ids ,
211+ output_type = " sf_polygons" ,
212+ crs = TARGET_CRS
213+ )
214+ )
215+
216+ expect_s3_class(grid_with_crs , " sf" )
217+ expect_equal(st_crs(grid_with_crs ), st_crs(TARGET_CRS ))
218+ })
0 commit comments