emlab-ucsb / oceandatr

Offshore data preparation for prioritization package
https://emlab-ucsb.github.io/oceandatr/
GNU General Public License v3.0
4 stars 0 forks source link

Fatal errors for some functions when using `planning_grid` that crosses the antimeridian. #37

Closed echelleburns closed 6 months ago

echelleburns commented 7 months ago

Fatal errors for the following functions occur when using a planning_grid that crosses the antimeridian, like Kiribati.

Below is an example:

planning_grid <- get_planning_grid(area_polygon = get_area(area_name = "KIR", mregions_column = "iso_ter1"), 
                                   projection_crs = '+proj=laea +lon_0=-159.609375 +lat_0=0 +datum=WGS84 +units=m +no_defs', resolution = 5000)

get_knolls(planning_grid = planning_grid, antimeridian = TRUE)

The resulting messages, warnings, and errors include:

although coordinates are longitude/latitude, st_intersection assumes that they are planar
Error in (function (msg)  : 
  TopologyException: Input geom 0 is invalid: Self-intersection at 359.77776958277866 -2.9910478352132555
In addition: Warning messages:
1: In st_is_longlat(x) :
  bounding box has potentially an invalid value range for longlat data
2: In st_is_longlat(x) :
  bounding box has potentially an invalid value range for longlat data

The planning_grid itself seems sound:

terra::plot(planning_grid)

image

And this same error does not occur with the following functions:

For example:

bathymetry <- get_bathymetry(planning_grid = planning_grid, antimeridian = TRUE)

Has the expected result (a SpatRaster of depths), but the resulting file looks like it only has data on one part of the area of interest.

terra::plot(bathymetry

image

jflowernet commented 7 months ago

Commit 3f97000d1527eaba73d5b0963807fa31d5148e55 should have resolved most of these errors, but there is still a problem with get_knolls because the knolls dataset extends beyond -180 to 180 extent which sf::st_break_antimeridian doesn't like. My solution for this will be to crop it to a -180 to 180 extent, but currently having issues getting a valid output due to the polygons not splitting properly at the antimeridian. Working code for this is in data-raw/knolls_base_area_crop.R.

jflowernet commented 7 months ago

Also, I get_enviro_regions for Kiribati in tests is consuming all the memory then failing: will need to check that.

jflowernet commented 6 months ago

Commit 5a4518f8390884ae3fd0dd7ee62ea7931d706a4f has solved problems with the knolls by cropping as I suggested. But still have errors in Kiribati bathymetry in raster planning units

jflowernet commented 6 months ago

Ok, I've resolved all the errors and issues for Kiribati. @echelleburns can you check that Kiribati features are all coming up ok for you? There is a Kiribati section of code in the test_code.R file in root. You just need to run devtools::load_all() before it.

echelleburns commented 6 months ago

Works well for me, and all tests passed! Well done!