【其他算法】卫星影像处理方法

class Imagery:
    def __init__(self):
        """
        本类为影像存储相关
        1.影像是以256的小瓦片进行存储,且存储方式是有规律的
        2.存储方式:把地球通过web墨卡托坐标转换展开成一张矩形平面图,以平面图的某个角为像素原点,则平面中的每个点都有对应的像素坐标,每256*256的像素块是一个瓦片
        3.只要实现 大地经纬度坐标-矩形像素坐标-瓦片id间的互相转换即可实现影像的处理
        """
        pass


    def _getXY(self, coord):
        """
        大地经纬度坐标转web墨卡托坐标
        """
        lon, lat = coord
        earth_radius = 6378137.0
        x = lon * math.pi / 180.0 * earth_radius
        y = lat * math.pi / 180.0
        y = earth_radius / 2.0 * math.log((1.0 + math.sin(y)) / (1.0 - math.sin(y)))
        return [x, y]

    def _get_coord_from_tile(self, tile_x, tile_y, level):
        lon = (tile_x / 2 ** level) * 360 - 180
        lat = -math.atan(math.sinh(math.pi - 2 * math.pi * (tile_y / 2 ** level))) * 180 / math.pi
        return [lon, lat]

    def get_pix_from_coord(self, coord, level):
        """
        从经纬度坐标获得图像坐标, 输入为坐标点和影像级别
        """
        start_point = self._getXY(coord)
        bl = self._getXY((-180, -85.05112877980659))
        tl = self._getXY((-180, 85.05112877980659))
        br = self._getXY((180, -85.05112877980659))
        grid_w = (br[0] - bl[0]) / math.pow(2, level)
        grid_h = (tl[1] - bl[1]) / math.pow(2, level)
        col = (start_point[0] - bl[0]) / grid_w
        row = (start_point[1] - bl[1]) / grid_h
        row = math.pow(2, level) - row
        return np.array([round(col*256), round(row*256)])

    def get_coord_from_pix(self, pix, level):
        """
        从图像坐标获得经纬度坐标, 输入为图像点和影像级别
        """
        log, lat = self._get_coord_from_tile(pix[0] / 256, pix[1] / 256, level)
        return [log, -lat]

    def get_pixregion_from_tile(self, tile_id):
        """
        根据瓦片id获得左上角和右下角图像绝对坐标
        """
        col, row = tile_id
        left_top = [col * 256, row * 256]
        right_bottom = [(col + 1) * 256, (row + 1) * 256]
        return left_top, right_bottom

    def get_tile_from_pix(self, pix):
        """
        根据图像绝对坐标获得瓦片id
        """
        col, row = pix
        col, row = col // 256, row // 256
        return np.array([col, row])

 

posted @ 2022-12-07 15:17  我若成风者  阅读(53)  评论(0编辑  收藏  举报