microsoft / torchgeo

TorchGeo: datasets, samplers, transforms, and pre-trained models for geospatial data
https://www.osgeo.org/projects/torchgeo/
MIT License
2.71k stars 334 forks source link

support for non bbox roi in sampler #1190

Open asaph-marvin opened 1 year ago

asaph-marvin commented 1 year ago

Summary

Add option to pass Polygon, MultiPolygon or oriented bounding box as the roi param to the geosampler. (and perhaps support other geometry types?) The sampler would then sample bounding boxes that have their center fall somewhere inside the polygon / multipolygon, or in the case of the oriented bounding box, it would sample from patches from the OBB and use the rotation matrix to sample the actual image data.

Rationale

I'm using a global coastal dataset. It is therefor made up of very narrow strips, oriented in many directions spread out over the entire globe. The minimum bounding box for this dataset is basically the whole world, but the actual coastline makes up a tiny fragment of that bounding box. So the chance that the geo sampler will sample a "valid" image for me is basically 0.

ATM I have to manually split up my dataset into many small bounding boxes and create a dataloader for each one. This is labour intensive and suboptimal because there isn't one clear way how to cut the coastal region up into bboxes.

Instead it would be very convenient if I could simply pass the multi polygon to the geo sampler. I don't mind if the sampled images will have some pixels outside the roi as long as the center of the image is contained inside the roi.

Implementation

Use the bounding boxes of the polygons to sample points and then perform isContained() test for the points to see if they fall within the polygons. For the oriented bounding box, we can sample patches from it as if it's axis-aligned and then use the rotation matrix to sample the pixel values using backward mapping. (http://what-when-how.com/introduction-to-video-and-image-processing/geometric-transformations-introduction-to-video-and-image-processing-part-2/)

Alternatives

Would be happy to hear other suggestions :)

Additional information

No response

adamjstewart commented 1 year ago

@calebrob6 had some working code for this.

calebrob6 commented 1 month ago

Looking back at issues assigned to me -- this wouldn't be hard to hack, but I'm not sure the proper way to implement it. Our BoundingBox has temporal dimensions too, so we'd need some BoundingShape wrapper around shapely Polygon | MultiPolygon that appended a temporal dimension and worked with BoundingBox.