def _reproject(self, eopatch, src_raster): """ Reprojects the raster data from Geopedia's CRS (POP_WEB) to EOPatch's CRS. """ height, width = src_raster.shape dst_raster = np.ones((height, width), dtype=self.raster_dtype) src_bbox = transform_bbox(eopatch.bbox, CRS.POP_WEB) src_transform = rasterio.transform.from_bounds(*src_bbox, width=width, height=height) dst_bbox = eopatch.bbox dst_transform = rasterio.transform.from_bounds(*dst_bbox, width=width, height=height) rasterio.warp.reproject( src_raster, dst_raster, src_transform=src_transform, src_crs={'init': CRS.ogc_string(CRS.POP_WEB)}, src_nodata=0, dst_transform=dst_transform, dst_crs={'init': CRS.ogc_string(eopatch.bbox.crs)}, dst_nodata=self.no_data_val) return dst_raster
def execute(self, eopatch, *, filename=None): """ Execute method :param eopatch: input EOPatch :type eopatch: EOPatch :param filename: filename of tiff file or None if entire path has already been specified in `folder` parameter of task initialization. :type filename: str or None :return: Unchanged input EOPatch :rtype: EOPatch """ feature_type, feature_name = next(self.feature(eopatch)) array = eopatch[feature_type][feature_name] array_sub = self._get_bands_subset(array) if feature_type.is_time_dependent(): array_sub = self._get_dates_subset(array_sub, eopatch.timestamp) else: # add temporal dimension array_sub = np.expand_dims(array_sub, axis=0) if not feature_type.is_spatial(): # add height and width dimensions array_sub = np.expand_dims(np.expand_dims(array_sub, axis=1), axis=1) time_dim, height, width, band_dim = array_sub.shape index = time_dim * band_dim dst_transform = rasterio.transform.from_bounds(*eopatch.bbox, width=width, height=height) dst_crs = {'init': CRS.ogc_string(eopatch.bbox.crs)} image_dtype = array_sub.dtype if self.image_dtype is None else self.image_dtype if image_dtype == np.int64: image_dtype = np.int32 warnings.warn( 'Data from feature {} cannot be exported to tiff with dtype numpy.int64. Will export as ' 'numpy.int32 instead'.format((feature_type, feature_name))) # Write it out to a file with rasterio.open(self._get_file_path(filename), 'w', driver='GTiff', width=width, height=height, count=index, dtype=image_dtype, nodata=self.no_data_value, transform=dst_transform, crs=dst_crs) as dst: output_array = array_sub.astype(image_dtype) output_array = np.moveaxis(output_array, -1, 1).reshape(index, height, width) dst.write(output_array) return eopatch
def generate_slo_shapefile(path): DATA_FOLDER = os.path.join('data') area = gpd.read_file(os.path.join(DATA_FOLDER, 'svn_buffered.geojson')) # area = gpd.read_file(os.path.join(DATA_FOLDER, 'austria.geojson')) # Convert CRS to UTM_33N country_crs = CRS.UTM_33N area = area.to_crs(crs={'init': CRS.ogc_string(country_crs)}) # Get the country's shape in polygon format country_shape = area.geometry.values.tolist()[-1] # Plot country area.plot() plt.axis('off') # Create the splitter to obtain a list of bboxes bbox_splitter = BBoxSplitter([country_shape], country_crs, (25 * 3, 17 * 3)) bbox_list = np.array(bbox_splitter.get_bbox_list()) info_list = np.array(bbox_splitter.get_info_list()) path_out = path + '/shapefiles' if not os.path.isdir(path_out): os.makedirs(path_out) geometry = [Polygon(bbox.get_polygon()) for bbox in bbox_list] idxs_x = [info['index_x'] for info in info_list] idxs_y = [info['index_y'] for info in info_list] gdf = gpd.GeoDataFrame({ 'index_x': idxs_x, 'index_y': idxs_y }, crs={'init': CRS.ogc_string(country_crs)}, geometry=geometry) shapefile_name = path_out + '/slovenia.shp' gdf.to_file(shapefile_name) return gdf, bbox_list
def test_ogc_string(self): crs_values = ((CRS.POP_WEB, 'EPSG:3857'), (CRS.WGS84, 'EPSG:4326'), (CRS.UTM_33N, 'EPSG:32633'), (CRS.UTM_33S, 'EPSG:32733')) for crs, epsg in crs_values: with self.subTest(msg=epsg): ogc_str = CRS.ogc_string(crs) self.assertEqual(epsg, ogc_str, msg="Expected {}, got {}".format( epsg, ogc_str))
def execute(self, eopatch, *, filename): feature_type, feature_name = next(self.feature(eopatch)) array = eopatch[feature_type][feature_name] array_sub = self._get_bands_subset(array) if feature_type in [ FeatureType.DATA, FeatureType.MASK, FeatureType.SCALAR ]: array_sub = self._get_dates_subset(array_sub, eopatch.timestamp) if feature_type is FeatureType.SCALAR: # add height and width dimensions array_sub = np.expand_dims(np.expand_dims(array_sub, axis=1), axis=1) else: # add temporal dimension array_sub = np.expand_dims(array_sub, axis=0) if feature_type is FeatureType.SCALAR_TIMELESS: # add height and width dimensions array_sub = np.expand_dims(np.expand_dims(array_sub, axis=1), axis=1) time_dim, height, width, band_dim = array_sub.shape index = time_dim * band_dim dst_transform = rasterio.transform.from_bounds(*eopatch.bbox, width=width, height=height) dst_crs = {'init': CRS.ogc_string(eopatch.bbox.crs)} # Write it out to a file. with rasterio.open(os.path.join(self.folder, filename), 'w', driver='GTiff', width=width, height=height, count=index, dtype=self.image_dtype, nodata=self.no_data_value, transform=dst_transform, crs=dst_crs) as dst: output_array = array_sub.astype(self.image_dtype) output_array = np.moveaxis(output_array, -1, 1).reshape(index, height, width) dst.write(output_array) return eopatch
def execute(self, eopatch, *, filename): array = eopatch.get_feature(self.feature_type, self.feature_name) if self.band_count == 1: array = array[..., 0] dst_shape = array.shape dst_transform = rasterio.transform.from_bounds(*eopatch.bbox, width=dst_shape[1], height=dst_shape[0]) dst_crs = {'init': CRS.ogc_string(eopatch.bbox.crs)} # Write it out to a file. with rasterio.open(filename, 'w', driver='GTiff', width=dst_shape[1], height=dst_shape[0], count=self.band_count, dtype=self.image_dtype, nodata=self.no_data_value, transform=dst_transform, crs=dst_crs) as dst: dst.write(array.astype(self.image_dtype), indexes=self.band_count) return eopatch
test_patchIDs.append(patchIDs[i]) test_patchIDs = np.int64(test_patchIDs) # Is this even necessary?? #TIME INTERVAL time_interval = ['2017-01-01', '2017-12-31'] # time interval for the sentinelhub request #%% Section 2 #Defining AOI and making Bboxes country = gpd.read_file('./DK.geojson') # Convert CRS to UTM_32N country_crs = CRS.UTM_32N country = country.to_crs(crs={'init': CRS.ogc_string(country_crs)}) # Get the country's shape in polygon format country_shape = country.geometry.values.tolist()[-1] #country.plot() #plt.axis('off'); # Create the splitter to obtain a list of bboxes bbox_splitter_large = BBoxSplitter([country_shape], country_crs, (45, 35)) bbox_splitter_small = BBoxSplitter([country_shape], country_crs, (45 * 3, 35 * 3)) bbox_splitter = bbox_splitter_large bbox_list = np.array(bbox_splitter.get_bbox_list()) info_list = np.array(bbox_splitter.get_info_list())
def execute(self, eopatch, *, filename=None): """ Execute method :param eopatch: input EOPatch :type eopatch: EOPatch :param filename: filename of tiff file or None if entire path has already been specified in `folder` parameter of task initialization. :type filename: str or None :return: Unchanged input EOPatch :rtype: EOPatch """ try: feature = next(self.feature(eopatch)) except ValueError as error: LOGGER.warning(error) if self.fail_on_missing: raise ValueError(error) return eopatch image_array = self._prepare_image_array(eopatch, feature) channel_count, height, width = image_array.shape src_crs = {'init': CRS.ogc_string(eopatch.bbox.crs)} src_transform = rasterio.transform.from_bounds(*eopatch.bbox, width=width, height=height) if self.crs: dst_crs = {'init': CRS.ogc_string(self.crs)} dst_transform, dst_width, dst_height = rasterio.warp.calculate_default_transform( src_crs, dst_crs, width, height, *eopatch.bbox) else: dst_crs = src_crs dst_transform = src_transform dst_width, dst_height = width, height with rasterio.open(self._get_file_path(filename, create_dir=True), 'w', driver='GTiff', width=dst_width, height=dst_height, count=channel_count, dtype=image_array.dtype, nodata=self.no_data_value, transform=dst_transform, crs=dst_crs) as dst: if dst_crs == src_crs: dst.write(image_array) else: for idx in range(channel_count): rasterio.warp.reproject( source=image_array[idx, ...], destination=rasterio.band(dst, idx + 1), src_transform=src_transform, src_crs=src_crs, dst_transform=dst_transform, dst_crs=dst_crs, resampling=rasterio.warp.Resampling.nearest) return eopatch