satellogic / telluric

telluric is a Python library to manage vector and raster geospatial data in an interactive and easy way
MIT License
87 stars 18 forks source link

Make MutableGeoraster implement limit_to_bands() #297

Open marcronq opened 3 years ago

marcronq commented 3 years ago

The GeoRaster2.limit_to_bands method behaves differently depending on whether _image has been already read or not:

    def limit_to_bands(self, bands):
        if self._image is not None:
            bands_data = self.bands_data(bands)
            return self.copy_with(image=bands_data, band_names=bands)
        else:
            indices = self.bands_indices(bands)
            bidxs = map(lambda idx: idx + 1, indices)
            doc = boundless_vrt_doc(self._raster_opener(self.source_file), bands=bidxs)
            filename = self._save_to_destination_file(doc.tostring())
            return self.__class__(filename=filename, band_names=bands)

When dealing with a MutableGeoraster instance this provokes that depending on this condition the returned type is MutableGeoRaster or GeoRaster2:

In [2]: img = GeoRaster2.open("whatever.tif", lazy_load=False, mutable=True)

In [5]: band = img.limit_to_bands(["raw"])

In [6]: type(band)
Out[6]: telluric.georaster.GeoRaster2

In [7]: img = GeoRaster2.open("whatever.tif", mutable=True)

In [8]: band = img.limit_to_bands(["raw"])

In [9]: type(band)
Out[9]: telluric.georaster.MutableGeoRaster

I made MutableGeoRaster implement the limit_to_bands method. This could also be accomplished modifying the base method but I don't know if Georaster2 class should know about its childs. What do you think?