tobac
tobac copied to clipboard
How to link segmentations based on lon and lat coordinates?
I see @JuliaKukulies has added the lon/lat support (#65), is it possible to link the 2D segmentations with different shapes? Let's assume we have two segmentations: seg1 (y:200, x: 100) and seg2 (y: 190, x: 120). Both of them have 2D lon and lat coordinates. If we can utilize the lon/lat coordinates with segmentation info together. that would be awesome!
We have some code here that we've been working on to do basically that. It's close but not yet ready for a PR. Hopefully, we can get this in for v1.4 or v1.5.
Transferring detected features and segmentations across different grids is definitely a feature on the radar for tobac v2.0. This sounds like a subset of that situation, where it's only applied to two lat/lon grids (rather than grids on different coordinates). A couple of questions to see how complex this is:
- Do the lat/lon grids share the same points and grid spacing as each other. I.e. are the offset from each other but overlapping?
- Do they occupy the same spatial extent, but the grid spacing is different?
There are two approaches to this I've considered. The first, more direct approach is simply to regrid from one grid to the other. This is pretty straightforward, but more computationally intensive and slightly less satisfactory, particularly if you want to preserve resolution. The second approach is to turn each segmentation mask into a set of geometric objects that contain each of the segmented features, which could then be mapped to any other grid without having the regrid the data on the second grid itself
@w-k-jones Thanks for the idea. For polar-orbiting satellite data, the lon/lat, spacing, and spatial extent are all different. Here's an example. I guess I can use geopandas to convert segmentations into geometric objects, but how to map it to another grid?
Here's a quick mock-up of what I'm thinking:

The idea is that it's much easier to map a geopandas/shapely boundary formed from a set of points from one coordinate system to a different coordinate system than it is an entire grid (this isn't even necessary if they're both in lat/lon coords). Then you just need to check which pixels on the new grid are within the boundary of segmentation on the old grid, giving you the mask in the new grid.
This is fairly straightforward for 2d, but I'm not sure if there are any libraries which could do the same for 3d data
Ah- I see what the two of you are thinking. I misunderstood the original question. We don't have code that does that yet here. What we have is code that allows one to take feature detection output from one grid (with lat/lons) and use that to seed watershedding on a different grid (with lats/lons).
I think what the two of you are discussing is very interesting and I agree with @w-k-jones that it would be great to get something like that into v2.x.
@w-k-jones Thanks for the detailed explanation. I make a simple draft to accomplish your idea by pyresample:
The orange grid contains the mask and I get the mask in another grid (blue one) by resampling the blue grid to the mask points (lon/lat) of the orange grid.
Core code:
from pyresample import kd_tree
from pyresample.geometry import SwathDefinition
# orange grid
swath_to_map = SwathDefinition(lons=lon_to_map, lats=lat_to_map)
# blue grid
swath = SwathDefinition(lons=swath_lon, lats=swath_lat)
result = kd_tree.resample_nearest(swath_to_map,
np.full((len(lon_to_map)), 1), # assign mask to 1
swath,
radius_of_influence=5000,
epsilon=0.5)
Note that pyresample can deal with both swath and grid data. It means that the model data, GEO, and polar-orbiting satellite observations can be all supported! The radius_of_influence
can be the input from users or the auto-calculated space which has been added by @JuliaKukulies.
I suppose this solution can't deal with 3d data ....