paleolimbot / wk

Lightweight Well-Known Geometry Parsing

Home Page:https://paleolimbot.github.io/wk

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Transformers are using meta flags inconsistently when operating on coords

anthonynorth opened this issue · comments

Transformers are using meta flags inconsistently when operating on coords.

wk/src/transform.c

Lines 152 to 164 in 67f7013

if (meta->flags & WK_FLAG_HAS_Z && meta->flags & WK_FLAG_HAS_M) {
trans_filter->xyzm_in[2] = coord[2];
trans_filter->xyzm_in[3] = coord[3];
} else if (meta->flags & WK_FLAG_HAS_Z) {
trans_filter->xyzm_in[2] = coord[2];
trans_filter->xyzm_in[3] = R_NaN;
} else if (new_meta->flags & WK_FLAG_HAS_M) {
trans_filter->xyzm_in[2] = R_NaN;
trans_filter->xyzm_in[3] = coord[2];
} else {
trans_filter->xyzm_in[2] = R_NaN;
trans_filter->xyzm_in[3] = R_NaN;
}

I think the bug is line 158, which tests for the m dimension being in the output. This results in garbage output in m dimension, when use_m = TRUE and input dimensions are xy. The below reprex shows what I mean.

If my understanding is correct, fix is a 1-liner.

wk::wk_transform(
  wk::xy(1,1),
  wk::wk_trans_set(wk::xy(1, 1), use_m = TRUE)
)
#> <wk_xym[1]>
#> [1] M (1 1 4.526213e-315)

wk::wk_transform(
  wk::xy(1,1),
  wk::wk_trans_set(wk::xy(1, 1), use_z = TRUE)
)
#> <wk_xyz[1]>
#> [1] Z (1 1 NaN)

wk::wk_transform(
  wk::xy(1,1),
  wk::wk_trans_set(wk::xy(1, 1), use_m = TRUE, use_z = TRUE)
)
#> <wk_xyzm[1]>
#> [1] ZM (1 1 NaN 2.359942e-312)

wk::wk_transform(
  wk::xy(1, 1),
  PROJ::proj_trans_create("OGC:CRS84", "EPSG:3857", use_m = TRUE)
)
#> <wk_xym[1]>
#> [1] M (111319.5 111325.1 2.359942e-312)

wk::wk_transform(
  wk::xy(1, 1),
  PROJ::proj_trans_create("OGC:CRS84", "EPSG:3857", use_z = TRUE)
)
#> <wk_xyz[1]>
#> [1] Z (111319.5 111325.1 NaN)

wk::wk_transform(
  wk::xy(1, 1),
  PROJ::proj_trans_create("OGC:CRS84", "EPSG:3857", use_z = TRUE, use_m = TRUE)
)
#> <wk_xyzm[1]>
#> [1] ZM (111319.5 111325.1 NaN 2.359942e-312)

Created on 2024-03-06 with reprex v2.0.2

Thank you for tracking this down!