KipCrossing / geotiff

A noGDAL tool for reading and writing geotiff files
GNU Lesser General Public License v2.1
216 stars 23 forks source link

tif_bBox_converted does not return the actual bounding box #61

Open jarmniku opened 1 year ago

jarmniku commented 1 year ago

The bounding box cannot be determined using just two corners, but all the four corners should be taken into consideration. If using just two corners, it may happen that part of the tif data is not included. That is because although the tif data is perfectly rectangle, the converted coordinates may not produce rectangle, but instead a skewed area, due to spherical nature of the Earth. I am experimenting with the following code to fix that:

    @property
    def tif_bBox_converted(self) -> BBox:
        right_top = self._convert_coords(self.crs_code, self.as_crs, self.tif_bBox[0])
        left_bottom = self._convert_coords(self.crs_code, self.as_crs, self.tif_bBox[1])
        right_bottom = self._convert_coords(
            self.crs_code, self.as_crs, (self.tif_bBox[0][0], self.tif_bBox[1][1]))
        left_top = self._convert_coords(
            self.crs_code, self.as_crs, (self.tif_bBox[1][0], self.tif_bBox[0][1]))
        x_min = min(right_top[0], right_bottom[0])
        x_max = max(left_top[0], left_bottom[0])
        y_min = min(left_bottom[1], right_bottom[1])
        y_max = max(left_top[1], right_top[1])
        return (x_min, y_max), (x_max, y_min)

Even this might not solve all the coordinate conversion situations, since the widest and/or tallest points may be somewhere in the midle of the area.