Exemplo n.º 1
0
    def get_geo_attribute(self, return_geo_transform=False, crs=None):
        """Get geo_attributes (idx, idx_h, idx_w, geo_transform, geometry) 
        of all splitted images.

        Parameters
        ----------
        return_geo_transform: bool, optional, default: False
            Return gdal geo_transform for each geometry in the output GeoDataFrame.
        crs: str, optional
            The crs for the output GeoDataFrame e.g. 'epsg:4326'.

        Returns
        -------
        df_attribute: gpd.GeoDataFrame
            The geo_attributes of all splitted images, which can be output as a shapefile.
        """
        rows = []
        for i in range(self.n_splitted_images):
            idx_h , idx_w = self.convert_order_to_location_index(i)

            h_start_inner, h_stop_inner = self.__convert_to_inner_index_h(idx_h, idx_h)
            w_start_inner, w_stop_inner = self.__convert_to_inner_index_w(idx_w, idx_w)

            left_top_coord = tgp.npidxs_to_coords([(h_start_inner, w_start_inner)], self.src_gt)[0]
            left_buttom_coord = tgp.npidxs_to_coords([(h_start_inner, w_stop_inner)], self.src_gt)[0]
            right_buttom_coord = tgp.npidxs_to_coords([(h_stop_inner, w_stop_inner)], self.src_gt)[0]
            right_top_coord = tgp.npidxs_to_coords([(h_stop_inner, w_start_inner)], self.src_gt)[0]

            x_min, y_max = left_top_coord
            row = {
                "idx":i,
                "idx_h":idx_h,
                "idx_w":idx_w,
                "geo_transform":(x_min, self.src_gt[1], self.src_gt[2], y_max, self.src_gt[4], self.src_gt[5]),
                "geometry": Polygon([left_top_coord, 
                                    left_buttom_coord, 
                                    right_buttom_coord, 
                                    right_top_coord, 
                                    left_top_coord]),
            }
            rows.append(row)
        df_attribute = gpd.GeoDataFrame(rows, geometry='geometry')

        if crs is not None:
            df_attribute.crs = crs
        elif self.proj is not None: 
            try:
                df_attribute.crs = 'init:' + str(tgp.wkt_to_epsg(self.proj))
            except:
                df_attribute.crs = self.proj

        if not return_geo_transform:
            df_attribute.drop('geo_transform', axis=1, inplace=True)

        return df_attribute
Exemplo n.º 2
0
    def reproject(self, dst_crs='EPSG:4326', src_crs=None):
        """Reproject the raster data.

        Parameters
        ----------
        dst_crs: str, optional, default: EPSG:4326
            The target crs to transform the raster to.
        src_crs: str, optional
            The source crs to transform the raster from. If None, 
            get the projection from src_raster.

        Returns
        -------
        dst_raster: Raster
            Reprojected result.

        Examples
        -------- 
        >>> import TronGisPy as tgp
        >>> src_raster_fp = tgp.get_testing_fp()
        >>> src_raster = tgp.read_raster(src_raster_fp)
        >>> print("project(before)", src_raster.projection)
        >>> dst_raster = src_raster.reproject(dst_crs='EPSG:4326', src_crs=None)
        >>> print("project(after)", tgp.wkt_to_epsg(dst_raster.projection))
        """
        src_ds = self.to_gdal_ds()
        if src_crs:
            dst_ds = gdal.Warp('',
                               src_ds,
                               srcSRS=src_crs,
                               dstSRS=dst_crs,
                               format='MEM')
        else:
            dst_ds = gdal.Warp('', src_ds, dstSRS=dst_crs, format='MEM')
            if dst_ds is None:
                try:
                    src_crs = "EPSG:" + str(tgp.wkt_to_epsg(self.projection))
                    dst_ds = gdal.Warp('',
                                       src_ds,
                                       srcSRS=src_crs,
                                       dstSRS=dst_crs,
                                       format='MEM')
                    assert dst_ds is not None, "Please provide src_crs since the raster does not contain valid crs information."
                    print("Please provide src_crs to accelerate the process.")
                except:
                    assert False, "Please provide src_crs since the raster does not contain crs information."

        dst_raster = tgp.read_gdal_ds(dst_ds)
        return dst_raster