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
def __getitem__(self, slice_value): if type(slice_value) in [int, slice]: h_start, h_stop = self.__process_slice_value(slice_value) w_start, w_stop = 0, self.n_steps_w elif type(slice_value) == tuple: h_start, h_stop = self.__process_slice_value(slice_value[0]) w_start, w_stop = self.__process_slice_value(slice_value[1]) h_start_inner, h_stop_inner = self.__convert_to_inner_index_h(h_start, h_stop) w_start_inner, w_stop_inner = self.__convert_to_inner_index_w(w_start, w_stop) data = self.src_image[h_start_inner:h_stop_inner, w_start_inner:w_stop_inner] gt = np.array(self.src_gt).copy() gt[[0, 3]] = tgp.npidxs_to_coords([(h_start_inner, w_start_inner)], self.src_gt)[0] raster = tgp.Raster(data, gt, self.proj, self.gdaldtype, self.no_data_value) return raster
def clip_raster_with_polygon(src_raster, src_poly, all_touched=False, no_data_value=0): """Clip raster with polygon. Parameters ---------- src_raster: Raster Which raster data to be clipped. src_poly: Geopandas.GeoDataFrame The clipper(clipping boundary). Returns ------- dst_raster: Raster Clipped result. Examples -------- >>> import geopandas as gpd >>> import TronGisPy as tgp >>> from TronGisPy import ShapeGrid >>> from matplotlib import pyplot as plt >>> src_raster_fp = tgp.get_testing_fp('satellite_tif') >>> src_poly_fp = tgp.get_testing_fp('satellite_tif_clipper') >>> src_raster = tgp.read_raster(src_raster_fp) >>> src_poly = gpd.read_file(src_poly_fp) >>> dst_raster = ShapeGrid.clip_raster_with_polygon(src_raster, src_poly) >>> fig, (ax1, ax2) = plt.subplots(1, 2) # plot the result >>> src_raster.plot(ax=ax1) >>> src_poly.boundary.plot(ax=ax1) >>> ax1.set_title('original image and clipper') >>> dst_raster.plot(ax=ax2) >>> ax2.set_title('clipped image') >>> plt.show() """ assert src_raster.geo_transform is not None, "src_raster.geo_transform should not be None" src_poly_copy = src_poly.copy() src_poly_copy['value'] = 1 src_poly_raster = rasterize_layer_by_ref_raster(src_poly_copy, src_raster, use_attribute='value', all_touched=all_touched, no_data_value=0) dst_raster = src_raster.copy() dst_raster.data[~(src_poly_raster.data[:, :, 0].astype(bool))] = no_data_value row_idxs, col_idxs, bands_idxs = np.where(src_poly_raster.data != 0) rmin, rmax, cmin, cmax = np.min(row_idxs), np.max(row_idxs), np.min( col_idxs), np.max(col_idxs) dst_raster.data = dst_raster.data[rmin:rmax + 1, cmin:cmax + 1] coords = tgp.npidxs_to_coords([(rmin, cmin)], src_raster.geo_transform)[0] geo_transform = np.array(dst_raster.geo_transform) geo_transform[[0, 3]] = coords dst_raster.geo_transform = geo_transform # src_ds = src_raster.to_gdal_ds() # temp_dir = tgp.create_temp_dir_when_not_exists() # src_shp_fp = os.path.join(temp_dir, 'src_poly.shp') # src_poly.to_file(src_shp_fp) # dst_ds = gdal.Warp('', src_ds, format= 'MEM', cutlineDSName=src_shp_fp, cropToCutline=True) # dst_raster = tgp.read_gdal_ds(dst_ds) return dst_raster