def _project(self, subset): proj_path = os.path.join(self.temp_dir, 'tiled_proj.tif') setattr(self, 'projection', proj_path) profile = copy.deepcopy(self.target_profile) profile['dtype'] = float32 bb = self.bbox.as_tuple() if self.src_bounds_wsen: bounds = self.src_bounds_wsen else: bounds = (bb[0], bb[1], bb[2], bb[3]) dst_affine, dst_width, dst_height = cdt( CRS({'init': 'epsg:4326'}), CRS({'init': 'epsg:4326'}), subset.shape[1], subset.shape[2], *bounds, ) profile.update({ 'crs': CRS({'init': 'epsg:4326'}), 'transform': dst_affine, 'width': dst_width, 'height': dst_height }) with rasopen(proj_path, 'w', **profile) as dst: dst.write(subset)
def _reproject(self): self.reprojection = os.path.join(self.temp_dir, 'cdl_reprojection.tif') with rasopen(self.original_tif, 'r') as src: src_profile = src.profile src_bounds = src.bounds src_array = src.read(1) dst_profile = copy.deepcopy(self.target_profile) dst_profile['dtype'] = float32 bounds = src_bounds dst_affine, dst_width, dst_height = cdt(src_profile['crs'], dst_profile['crs'], src_profile['width'], src_profile['height'], *bounds) dst_profile.update({'crs': dst_profile['crs'], 'transform': dst_affine, 'width': dst_width, 'height': dst_height}) with rasopen(self.reprojection, 'w', **dst_profile) as dst: dst_array = empty((1, dst_height, dst_width), dtype=float32) reproject(src_array, dst_array, src_transform=src_profile['transform'], src_crs=src_profile['crs'], dst_crs=self.target_profile['crs'], dst_transform=dst_affine, resampling=Resampling.nearest, num_threads=2) dst.write(dst_array.reshape(1, dst_array.shape[1], dst_array.shape[2]))
def reproject(src_file, dst_file, dst_crs, dst_nodata = None): """reproject a file file into a desired crs""" # use this trick to use ESRI:54009 until the csr is available if type(dst_crs) == str: dst_crs = pyproj.crs.CRS.from_string(dst_crs) # reproject with rio.open(src_file) as src: transform, width, height = cdt( src.crs, dst_crs, src.width, src.height, *src.bounds ) kwargs = src.meta.copy() kwargs.update({ 'crs': dst_crs, 'transform': transform, 'width': width, 'height': height }) if not dst_nodata: dst_nodata = kwargs['nodata'] with rio.open(dst_file, 'w', **kwargs) as dst: for i in range(1, src.count + 1): reproject( source=rio.band(src, i), destination=rio.band(dst, i), src_transform=src.transform, src_crs=src.crs, dst_transform=transform, dst_crs=dst_crs, dst_nodata=dst_nodata, resampling=Resampling.nearest ) return
def ConvertRaster2LatLong(InputRasterFile,OutputRasterFile): """ Convert a raster to lat long WGS1984 EPSG:4326 coordinates for global plotting MDH """ # import modules import rasterio from rasterio.warp import reproject, calculate_default_transform as cdt, Resampling # read the source raster with rasterio.open(InputRasterFile) as src: #get input coordinate system Input_CRS = src.crs # define the output coordinate system Output_CRS = {'init': "epsg:4326"} # set up the transform Affine, Width, Height = cdt(Input_CRS,Output_CRS,src.width,src.height,*src.bounds) kwargs = src.meta.copy() kwargs.update({ 'crs': Output_CRS, 'transform': Affine, 'affine': Affine, 'width': Width, 'height': Height }) with rasterio.open(OutputRasterFile, 'w', **kwargs) as dst: for i in range(1, src.count+1): reproject( source=rasterio.band(src, i), destination=rasterio.band(dst, i), src_transform=src.affine, src_crs=src.crs, dst_transform=Affine, dst_crs=Output_CRS, resampling=Resampling.bilinear)
def ConvertRaster2LatLong(InputRasterFile, OutputRasterFile): """ Convert a raster to lat long WGS1984 EPSG:4326 coordinates for global plotting MDH """ # import modules import rasterio from rasterio.warp import reproject, calculate_default_transform as cdt, Resampling # read the source raster with rasterio.open(InputRasterFile) as src: #get input coordinate system Input_CRS = src.crs # define the output coordinate system Output_CRS = {'init': "epsg:4326"} # set up the transform Affine, Width, Height = cdt(Input_CRS, Output_CRS, src.width, src.height, *src.bounds) kwargs = src.meta.copy() kwargs.update({ 'crs': Output_CRS, 'transform': Affine, 'affine': Affine, 'width': Width, 'height': Height }) with rasterio.open(OutputRasterFile, 'w', **kwargs) as dst: for i in range(1, src.count + 1): reproject(source=rasterio.band(src, i), destination=rasterio.band(dst, i), src_transform=src.affine, src_crs=src.crs, dst_transform=Affine, dst_crs=Output_CRS, resampling=Resampling.bilinear)