AustralianAntarcticDivision / SOmap

Southern Ocean round maps
https://australianantarcticdivision.github.io/SOmap/
24 stars 6 forks source link

Give SOmanagement projection options #31

Closed Maschette closed 5 years ago

Maschette commented 5 years ago

No that we have SOproj(), we should be able to give SOmanagment()options for projection or SOmap objects and have it reproject.

This will be more useful for SOauto_map() than it will for SOmap() So we can add management boundaries to smaller areas.

Probably not as easy as I like to think it is.

Maschette commented 5 years ago

The projection part is quite simple using SOproj, the cropping part is ugly but @mdsumner does this already for the coastline in SOauto_map so we can bastardise that code. Another hard bit is that SOmanagment turns on multiple layers at once so finding a nice way to reproject each of them may be a pain. A working example is:

a<-SOauto_map(c(0, 50), c(-70, -50))

x<-SOproj(SOmap_data$CCAMLR_SSRU, target = a$projection)

coastline <- try(as(sf::st_crop(sf::st_buffer(sf::st_transform(sf::st_as_sf(x), a$projection), 0), xmin = raster::xmin(a$bathy), xmax = raster::xmax(a$bathy), ymin = raster::ymin(a$bathy), ymax = raster::ymax(a$bathy)), "Spatial"), silent = TRUE)

plot(coastline, border=2, add=TRUE)

image

Maschette commented 5 years ago

So I have turned the SOproj + try combo into SOauto_crop()

I am wondering what the easiest way to apply it to all the true layers in SOmanagement. @raymondben Do you think it would be easier to reproject the objects in 'out' as the final step?

Maschette commented 5 years ago

Something like this but not a for loop for obvious reasons....

if (!missing(x)){ for(i in names(out)[3:length(names(out))]) out[[i]]$plotargs$x<-SOauto_crop(layer = out[[i]]$plotargs$x, x=x) }

Maschette commented 5 years ago

Something like: lapply(names(out)[3:length(out)], function(x) SOauto_crop(out[[x]]$plotargs[['x']], a))

but working out how to do the reassignment is proving the be a pain in the ass. #lapplySucks

mdsumner commented 5 years ago

You need eval().

Actually I am joking, a for loop is fine here

Maschette commented 5 years ago

The lapply works I just cant work out to assign it back but I am sooooo out of my depth in the apply family.

raymondben commented 5 years ago

Few things here:

Maschette commented 5 years ago

Cheers Ben, The for loop almost works for some reason when I use the for loop the structure(out, class = "SOmap_management") Doesn't?

Ordering changes? :P I know its not the best I always forget setdiff exists and not sure how to use it in this context.

I just pushed it if your interested.

Maschette commented 5 years ago

a<-SOauto_map(c(0, 50), c(-70, -50)) SOmanagement(CCAMLR=TRUE, SSRU=TRUE, basemap=a)

basemap was what I went with but it should probably be something else.

raymondben commented 5 years ago

possibly working now, at least for your example. It won't work for SOmanagement(IWC = TRUE) because the IWC data is just a matrix, and the new auto-reproj doesn't know how to handle it yet

Maschette commented 5 years ago

The IWC thing is easier to reproject, for it and the labels I have made out$projection based on an if function where if basemap is present it becomes the projection of basemap. But to avoid the for loop trying to assess it I assume I will need to add IWC to the setdiff?

raymondben commented 5 years ago

Would it be better to make the object's IWC data into an sp/sf object, so that the existing reproj code just knows what to do with it? (WIll need the current IWC plotting code to be adjusted, presumably). I think (could be wrong) that overall this will make things generally more consistent and simplify the code?

Maschette commented 5 years ago

Urgh the IWC thing was easy but I just realised for all the labels it pulls out the coordinates based on the "standard" projection so we would have to reproject all the labels as well.

But yes turning the IWC layer into a sp/sf object will probably be easier, their should be a shapefile available for it somewhere I just couldn't find it from memory last time. I will check again. With the labels thing I was hoping the SOauto_crop would work as well but alas it appears not to.

mdsumner commented 5 years ago

All the objects in management should be formal, pulled out into a new data object, and we ensure that reproj()ection and crop methods work for them. How about that as a first step, pull out the code for each layer and turn it into sp in polar stereo?

Maschette commented 5 years ago

They already are sp objects in polar stereo within SOmap_data

raymondben commented 5 years ago

Not all of them, e.g. IWC is constructed on the fly in SOmanagement