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

External tests for functions with added sf capabilities #23

Closed echelleburns closed 1 year ago

echelleburns commented 1 year ago

Please test the following functions to ensure they are working with the new sf capabilities.

Functions:

Local tests using the following code yielded appropriate results on my end.

devtools::load_all()

# Housekeeping
area <- get_area(area_name = "Bermuda")
projection <- 'PROJCS["ProjWiz_Custom_Lambert_Azimuthal", GEOGCS["GCS_WGS_1984", DATUM["D_WGS_1984", SPHEROID["WGS_1984",6378137.0,298.257223563]], PRIMEM["Greenwich",0.0], UNIT["Degree",0.0174532925199433]], PROJECTION["Lambert_Azimuthal_Equal_Area"], PARAMETER["False_Easting",0.0], PARAMETER["False_Northing",0.0], PARAMETER["Central_Meridian",-64.5], PARAMETER["Latitude_Of_Origin",32], UNIT["Meter",1.0]]'
bathymetry <- get_bathymetry(area_polygon = area)

# Create planning area options
planning_rast <- get_planning_grid(area, projection)
planning_square <- get_planning_grid(area, projection, option = "sf_square")
planning_hex <- get_planning_grid(area, projection, option = "sf_hex")

# Bathymetry
depth_zones_rast <- classify_depths(bathymetry, planning_grid = planning_rast)
depth_zones_square <- classify_depths(bathymetry, planning_grid = planning_square)
depth_zones_hex <- classify_depths(bathymetry, planning_grid = planning_hex)

# Coral habitats
corals_rast <- get_coral_habitat(area_polygon = area, planning_grid = planning_rast)
corals_square <- get_coral_habitat(area_polygon = area, planning_grid = planning_square)
corals_hex <- get_coral_habitat(area_polygon = area, planning_grid = planning_hex)

# Enviro regions
enviro_rast <- get_enviro_regions(area_polygon = area, planning_grid = planning_rast)
enviro_square <- get_enviro_regions(area_polygon = area, planning_grid = planning_square)
enviro_hex <- get_enviro_regions(area_polygon = area, planning_grid = planning_hex)

# Geomorphology
geomorph_rast <- get_geomorphology(area_polygon = area, planning_grid = planning_rast)
geomorph_square <- get_geomorphology(area_polygon = area, planning_grid = planning_square)
geomorph_hex <- get_geomorphology(area_polygon = area, planning_grid = planning_hex)

# Knolls
knolls_rast <- get_knolls(area_polygon = area, planning_grid = planning_rast)
knolls_square <- get_knolls(area_polygon = area, planning_grid = planning_square)
knolls_hex <- get_knolls(area_polygon = area, planning_grid = planning_hex)

# Buffered seamounts 
seamounts_rast <- get_seamounts_buffered(area_polygon = area, planning_grid = planning_rast)
seamounts_square <- get_seamounts_buffered(area_polygon = area, planning_grid = planning_square)
seamounts_hex <- get_seamounts_buffered(area_polygon = area, planning_grid = planning_hex)
jflowernet commented 1 year ago

All seems good except:

echelleburns commented 1 year ago

I was able to update the classify_bathymetry() and get_enviro_regions() scripts to have geometry as the last column (this is because I used terra::separate() to create a stack)

For the warnings/errors issue, I was able to remove the following errors/messages:

There are additional errors/warnings that were popping up that I'm not sure there's a way around, so I've suppressed them for now, but we can revisit if needed.

jflowernet commented 1 year ago

Nice work on this. And I like your method for getting round the empty first element of the raster stack - I get this warning all the time but have never bothered figuring out a fix!

For the remaining messages: I think suppressing them is fine. As you said, we need the data in WGS84 and the function works ok. And I think the -181 to 181 is just some kind of quirk of the outputs that we can live with and won't cause issues as far as I can see.

Unfortunately I did realise there is a problem with the classify_depth and get_enviro_regions sf outputs: each depth or environmental regions should not overlap with another, but they do, e.g.

`depth_zones_square_sum <- dplyr::mutate(depth_zones_square, depth_zones_sum = rowSums(dplyr::pick(1:4), na.rm = TRUE))

plot(depth_zones_square_sum["depth_zones_sum"], border=FALSE)`

image

`enviro_square <- get_enviro_regions(area_polygon = area, planning_grid = planning_square, num_clusters = 3)

enviro_square_sum <- dplyr::mutate(enviro_square, enviro_sum = rowSums(dplyr::pick(1:3), na.rm = TRUE))

plot(enviro_square_sum["enviro_sum"], border = FALSE)`

image

Looks like there should be some kind of threshold of how much raster cell intersects the planning units??

echelleburns commented 1 year ago

Ok, just pushed an update.

Here's what the outputs should look like now:

devtools::load_all()

# Housekeeping
area <- get_area(area_name = "Bermuda")
projection <- 'PROJCS["ProjWiz_Custom_Lambert_Azimuthal", GEOGCS["GCS_WGS_1984", DATUM["D_WGS_1984", SPHEROID["WGS_1984",6378137.0,298.257223563]], PRIMEM["Greenwich",0.0], UNIT["Degree",0.0174532925199433]], PROJECTION["Lambert_Azimuthal_Equal_Area"], PARAMETER["False_Easting",0.0], PARAMETER["False_Northing",0.0], PARAMETER["Central_Meridian",-64.5], PARAMETER["Latitude_Of_Origin",32], UNIT["Meter",1.0]]'
bathymetry <- get_bathymetry(area_polygon = area)

# Create planning area options
planning_square <- get_planning_grid(area, projection, option = "sf_square")

# Depth zones
depth_zones_square <- classify_depths(bathymetry, planning_grid = planning_square)
depth_zones_square_sum <- dplyr::mutate(depth_zones_square, depth_zones_sum = rowSums(dplyr::pick(1:4), na.rm = TRUE))
plot(depth_zones_square_sum["depth_zones_sum"], border=FALSE)

image

# Environmental zones 
enviro_square <- get_enviro_regions(area_polygon = area, planning_grid = planning_square, num_clusters = 3)
enviro_square_sum <- dplyr::mutate(enviro_square, enviro_sum = rowSums(dplyr::pick(1:3), na.rm = TRUE))
plot(enviro_square_sum["enviro_sum"], border = FALSE)

image

jflowernet commented 1 year ago

Nice work! Your code works fine for me. I had to fix classify_depths for raster because the masking moved and the depth zone naming was giving errors; I've pushed the fix.

Couple of extra to dos/ points:

echelleburns commented 1 year ago

Whoops, thanks for catching/fixing the raster error. I had meant to check that it still worked but it slipped my mind.

Just pushed changes for the order of depth and enviro regions.

Can confirm that I am classifying first, then masking (for rasters)/using exact_extract() (for sf)

jflowernet commented 1 year ago

Reordering works fine for me. I'm going to close this and start another issue for another sf related problem....