whitebox-tools icon indicating copy to clipboard operation
whitebox-tools copied to clipboard

raster_to_vector_polygons creating invalid polygons

Open aornugent opened this issue 4 years ago • 1 comments

Heyo -

I've run into an error with your new raster_to_vector_polygons function. It runs fast as hell, but R objects to the validity of the resulting polygons.

# polygonize
whitebox::wbt_raster_to_vector_polygons("S19_M3_MPI-ESM-LR_L_Car_1x_U_2050.tif", "luto2050_polgyon.shp")

# read into R
x <- st_read("luto2050_polgyon.shp")

# invalid geometry
st_is_valid(x$geometry[1], reason = T)
Error in CPL_geos_is_valid_reason(x) : 
  Evaluation error: IllegalArgumentException: Points of LinearRing do not form a closed linestring.

# example polygons
x$geometry[1][[1]]
MULTIPOLYGON (((145.005 -15.505, 145.015 -15.505, 145.015 -15.515, 145.045 -15.515, 145.045 -15.525, 145.035 -15.525, 145.035 -15.535, 145.025 -15.535, 145.025 -15.525, 145.015 -15.525, 145.015 -15.515, 145.005 -15.515, 145.005 -15.505, 145.015 -15.505)))

x$geometry[2][[1]]
MULTIPOLYGON (((145.235 -15.505, 145.245 -15.505, 145.245 -15.515, 145.235 -15.515, 145.235 -15.505, 145.245 -15.505)))

Best I can tell, it doesn't like that the first and last coordinates are not identical. Any suggestions?

aornugent avatar May 15 '20 06:05 aornugent

The technical description of ESRI Shapefile (page 13 of the PDF) calls for polygons being closed rings, so the first and last vertex must be the same.

In you case, your first geometry starts at 145.005 -15.505 and ends at 145.015 -15.505 which are obviously not the same coordinates. It seems however that there is a vertex in excess instead of one short. Your first two vertices (145.005 -15.505, 145.015 -15.505) are the same as the last two.

I just ran the tool and got the same issue, so there is a bug but I don't have the ability myself to find where.

jfbourdon avatar May 20 '20 18:05 jfbourdon