def Mask_Transform(input_shp_path, out_path, tmp_path): # 裁剪转投影 Prjfl_path = "/Users/shuailee/Documents/ShareDisk/2017.11.10.Restart.Bigdata/Py/py_postgres/test/prj.prj" tmp_path_file_aaa = os.path.join(tmp_path, "aaa_test.tif") # tmp_path_file_trans = os.path.join(tmp_path, "aaa_transform.tif") cutrst = gdal.Warp(out_path, tmp_path_file_aaa, cutlineDSName=input_shp_path, srcNodata=-2, dstNodata=-2, cropToCutline=True, dstSRS="WGS84") cutrst.FlushCache() cutrst = None print("Mask OK!") return
def main(argv): src_dss = [SRC + f for f in os.listdir(SRC) if ".zip" not in f] src_dss.sort() for idx, f in enumerate(tqdm(src_dss)): for f1 in os.listdir(f): if ".xml" not in f1: gdal.Warp(DST_FOLDER + str(idx) + 'clipped_' + f1.split(".")[0] + ".tif", f + "/" + f1, dstSRS="EPSG:32629", resampleAlg="near", format="GTiff", xRes=10, yRes=10, cutlineDSName=MASK, cropToCutline=1)
def make_vrt(data_list, wkt_dst_srs, outputDir, output_vrt_file, outputBounds=None, resolution='average', resampling=gdal.GRA_Bilinear, srcNodata=0, error_threshold=0.125): if len(data_list) == 0: return 0 output_tmp_file_list=[os.path.join(outputDir, os.path.basename(file)+".VRT") for file in data_list] for file, dst_file in zip(data_list, output_tmp_file_list): # Open source dataset and read source SRS gdal_data = gdal.Open(file) data_proj = gdal_data.GetProjection() if data_proj == '': d=dict(gdal.Info(file,format='json')) in_epsg=int(d['metadata']['GEOLOCATION']['SRS'].rsplit('"EPSG","')[-1].split('"')[0]) src_srs = osr.SpatialReference() src_srs.ImportFromEPSG(in_epsg) src_srs = src_srs.ExportToWkt() else: src_srs = None ## Call AutoCreateWarpedVRT() #tmp_ds = gdal.AutoCreateWarpedVRT(gdal_data, src_srs, wkt_dst_srs, resampling, error_threshold) ## Create the final warped raster #dst_ds = gdal.GetDriverByName('VRT').CreateCopy(dst_file, tmp_ds) # Warp to dst_srs dst_ds = gdal.Warp(dst_file, gdal_data, resampleAlg=resampling, srcNodata=srcNodata, dstNodata=srcNodata, srcSRS=src_srs, dstSRS=wkt_dst_srs, errorThreshold=error_threshold) del dst_ds print(os.path.join(outputDir, output_vrt_file)) if outputBounds: vrt_data=gdal.BuildVRT(output_vrt_file, output_tmp_file_list, separate=True, outputBounds=outputBounds, resolution=resolution, srcNodata=srcNodata) else: vrt_data=gdal.BuildVRT(output_vrt_file, output_tmp_file_list, separate=True, resolution=resolution, srcNodata=srcNodata) bands = vrt_data.RasterCount # to force gdal to write file del vrt_data return bands
def get_modis_annual(ifiles, sds=None, warp_args={}): # loop over SDS sets and read into dictionary mfiles = {'bandnames': ifiles['bandnames']} del ifiles['bandnames'] # useful sds default if sds == None: sds = ifiles.keys() for s in sds: # do this in case we dont need to cut if warp_args != {}: g = gdal.Warp("", ifiles[s], **warp_args) mfiles[s] = g.ReadAsArray() else: mfiles[s] = ifiles[s] return mfiles
def clip_ortho2shp_array(input_file, clip_shp, nodata=None, out=''): """Converts clip_shape to GDAL object.""" output_file = out shape_path = clip_shp shape_name = os.path.basename(clip_shp)[:-4] input_object = gdal.Open(input_file) ds = gdal.Warp(output_file, input_object, format='VRT', cutlineDSName=shape_path, cutlineLayer=shape_name, warpOptions=['NUM_THREADS=ALL_CPUS'], multithread=True, warpMemoryLimit=3000, dstNodata=nodata, transformerOptions=['NUM_THREADS=ALL_CPUS']) return ds
def main(location, product, layers, username, password): if username == "": raise ValueError("No NASA username set!") if password == "": raise ValueError("No NASA username set!") layers = layers.split(",") today = dt.datetime.now() LOG.info(f"Started running. Current DoY {today.strftime('%Y-%j')}") current_year = today.year # Copy any files already available in JASMIN link_neodc_files(current_year) # Scan local files to see what's the latest we've processed last_time = scan_current_files(PROCESS_LOCATION, current_year) LOG.info(f"Last DoY: {last_time}") if dt.datetime.strptime(f"{current_year}/{last_time}", "%Y/%j") <= today: download_nasa(last_time, current_year) # Scan local files to see what's the latest we've processed last_doy = scan_hdf_files(PROCESS_LOCATION, current_year) do_tifs( current_year, last_doy, folder=PROCESS_LOCATION, product=product, layers=layers, ) # Convert to WGS84 dodgy grid data_loc = Path(PROCESS_LOCATION) for layer in ["Lai_500m", "Fpar_500m"]: src_fname = data_loc / f"{layer}_{current_year}.tif" dst_fname = data_loc / f"{layer}_{current_year}wgs84.tif" dst_fname.unlink() _ = gdal.Warp( dst_fname.as_posix(), src_fname.as_posix(), format="GTiff", dstSRS="EPSG:4326", xRes=0.02, yRes=0.02, outputBounds=[-3.5, 4.5, 1.1, 12.0], creationOptions=[ "COMPRESS=DEFLATE", "TILED=YES", "BIGTIFF=YES", "PREDICTOR=1", ], )
def transform_raster(self, gdal_dataset, dst_epsg_code): """ Transforms the given GDAL dataset in memory to the SRS of the given EPSG code. :param gdal_dataset: GDAL dataset :param dst_epsg_code: destination EPSG Code :return: The memory map string of the transformed GDAL dataset. """ from uuid import uuid4 mmap_name = "/vsimem/{0}".format(uuid4().get_hex()) gdal.Warp(mmap_name, gdal_dataset, dstSRS='EPSG:{0}'.format(dst_epsg_code)) return mmap_name
def clipRegion(self): """输入数据转utm投影,并裁切研究区""" dependDir = globalCfg['depend_path'] utmPrjFile = os.path.join(dependDir, 'prj', 'UTM_Zone_51N.prj') if not os.path.exists(utmPrjFile): raise FileNotFoundError('Cannot Found Path: %s' % utmPrjFile) l2TifName = os.path.basename( self.inputPath).split('.')[0] + '_taihu_algae_ndvi.l2.tif' self.l2TifPath = os.path.join(self.outputDir, l2TifName) gdal.Warp(self.l2TifPath, self.inputPath, dstSRS=utmPrjFile, format='GTiff', outputBounds=TAIHU_BOUNDS, dstNodata=65535, xRes=self.res, yRes=self.res)
def totif(self, epsg=None, path_totif=None): #Open output format driver, see gdal_translate --formats for list format = "GTiff" driver = gdal.GetDriverByName(format) print('Compression is %s' % (self.compression)) if driver is None: raise Exception('Error: TIF file format is not supported') if path_totif is None: path_totif = os.path.splitext(self.path_input)[0] + '.tif' if epsg is not None: # Converting to different epsg code dst_proj = 'EPSG:%s' % (str(epsg)) print('Reprojecting data to EPSG:%s' % (epsg)) try: ds = gdal.Warp('', self.path_input, dstSRS=dst_proj, format='VRT') except Exception as e: raise Exception('Error: Unable to reproject dataset to %s' % (dst_proj)) #Output to new format try: dst_ds = driver.CreateCopy(path_totif, ds, 0, [ 'NUM_THREADS=ALL_CPUS', 'COMPRESS=%s' % (self.compression) ]) except Exception as e: raise Exception('Error: Converting data to TIF') else: #Output to new format try: dst_ds = driver.CreateCopy(path_totif, self.ds, 0, [ 'NUM_THREADS=ALL_CPUS', 'COMPRESS=%s' % (self.compression) ]) except Exception as e: raise Exception('Error: Converting data to TIF') print('Successfully converted : %s' % (path_totif))
def gdal_regrid_to_dem_grid(ds_in, tmp_file, nodata, DEM_bounds, DEM_xres, DEM_yres, RESAMPLE_ALGO='bilinear'): # ----------------------------------- # Specify the resampling algorithm # ----------------------------------- algo_dict = { 'nearest': gdal.GRA_NearestNeighbour, 'bilinear': gdal.GRA_Bilinear, 'cubic': gdal.GRA_Cubic, 'cubicspline': gdal.GRA_CubicSpline, 'lanczos': gdal.GRA_Lanczos, 'average': gdal.GRA_Average, 'min': gdal.GRA_Min, 'max': gdal.GRA_Max, 'mode': gdal.GRA_Mode, 'med': gdal.GRA_Med } resample_algo = algo_dict[RESAMPLE_ALGO] # -------------------------------------------------- # Use gdal.Warp to clip and resample to DEM grid # then save results to a GeoTIFF file (tmp_file). # -------------------------------------------------- # gdal_bbox = [DEM_bounds[0], DEM_bounds[2], DEM_bounds[1], DEM_bounds[3]] ds_tmp = gdal.Warp( tmp_file, ds_in, format='GTiff', # (output format string) outputBounds=DEM_bounds, xRes=DEM_xres, yRes=DEM_yres, srcNodata=nodata, ######## ### dstNodata=nodata, ######## resampleAlg=resample_algo) grid = ds_tmp.ReadAsArray() ds_tmp = None # Close tmp_file return grid
def clip_raster(inRas, inShape, outRas): """ Clip a raster using the extent of a shapefile Parameters ---------- inRas : string the input image inShape : string the input polygon file path outRas : string (optional) the clipped raster nodata_value : numerical (optional) self explanatory """ vds = ogr.Open(inShape) rds = gdal.Open(inRas, gdal.GA_ReadOnly) lyr = vds.GetLayer() extent = lyr.GetExtent() extent = [extent[0], extent[2], extent[1], extent[3]] print('cropping') ds = gdal.Warp(outRas, rds, format = 'GTiff', outputBounds = extent) ds.FlushCache() ds = None
def Georegister(wdir, path0, file, gcplist): pbar3 = tqdm(total=1, position=0, desc="Georeg ") path1 = wdir + "\\" + file + "_geodep.vrt" path2 = wdir + "\\" + file + "_georeg.vrt" if os.path.isfile(path1.replace("\\", "/")): os.remove(path1) if os.path.isfile(path2.replace("\\", "/")): os.remove(path2) #os.system("gdal_translate -a_srs EPSG:4326 -of VRT"+gcplist+"\""+path0+"\" \""+path1+"\"") #os.system("gdalwarp -r bilinear -tps -co COMPRESS=LZW \""+path1+"\" \""+path2+"\"") gdal.Translate(path1, path0, format='VRT', outputSRS='EPSG:4326', GCPs=gcplist) gdal.Warp(path2, path1, tps=True, resampleAlg='bilinear') pbar3.update(1) pbar3.close()
def mask(inpath, outpath, shp_path): filepath = [] # 存储路径+数据列表 for dirpath, dirname, dirfile in os.walk(inpath): for file in dirfile: filepath.append(os.path.join(dirpath, file)) # 空列表添加路径数据 print(filepath) for i in filepath: # 遍历数据列表、 file_name = str(i) if file_name.endswith(".tif"): print(file_name) (file_path, data_name) = os.path.split(file_name) outdata_path = os.path.join(outpath, data_name) # datapath=os.path.join(inpath,file_name) mask_data = gdal.Warp(outdata_path, file_name, cutlineDSName=shp_path, srcNodata=-2, dstNodata=-2, cropToCutline=True, dstSRS="WGS84") print("裁剪完成") else: print(i+"裁剪失败,数据格式非.tif")
def build_df(filename): # convert to lat/lon gdal.Warp(srcDSOrSrcDSTab=filename, destNameOrDestDS='outfile.vrt', format='VRT', dstSRS='EPSG:4326') # open the data set ds = xr.open_rasterio(tmp_vrt) # convert it to a 2 dimensional dataframe df = ds.to_dataframe('lai') # Extract lat/lon to seperate columns df['latitude'] = df.index.get_level_values('y') df['longitude'] = df.index.get_level_values('x') return df
def resample(self, new_xres, new_yres, save_path): """ Decrease the pixel size of the raster. Args: new_resx: desired resolution in x-direction new_resy: desired resolution in y-direction save_path: filepath to where the resampled file should be stored Returns: Nothing, it writes a raster file with decreased resolution. """ props = self.dem.GetGeoTransform() print('current pixel xsize:', props[1], 'current pixel ysize:', -props[-1]) options = gdal.WarpOptions(options=['tr'], xRes=new_xres, yRes=new_yres) newfile = gdal.Warp(save_path, self.dem, options=options) newprops = newfile.GetGeoTransform() print('new pixel xsize:', newprops[1], 'new pixel ysize:', -newprops[-1]) print('file saved in ' + save_path)
def lse_from_file(self, lse_file_path): base_raster = gdal.Open(self.band_10_path) geotransform = base_raster.GetGeoTransform() projection = base_raster.GetProjection() xMin = geotransform[0] yMax = geotransform[3] xMax = xMin + geotransform[1] * base_raster.RasterXSize yMin = yMax + geotransform[5] * base_raster.RasterYSize xRes = geotransform[1] yRes = geotransform[5] lse = gdal.Warp('', lse_file_path, format='MEM', xRes=xRes, yRes=yRes, dstSRS=projection, outputBounds=[xMin, yMin, xMax, yMax]) lse_b10 = lse.GetRasterBand(0).ReadAsArray() lse_b11 = lse.GetRasterBand(1).ReadAsArray() return lse_b10, lse_b11
def step1(x, y, z, X0, X1, Y0, Y1, targetDir, rasterFile, resAlg, res, formatOptions, verbose): if verbose >= 1: print('Zoom level %d, x %d/%d, y %d/%d, time %s' % (z, x - X0, X1 - X0, y - Y0, Y1 - Y0, str(datetime.datetime.now()))) (x0, y0) = XYZ.xy2coord(x, y + 1, 3857, z) (x1, y1) = XYZ.xy2coord(x + 1, y, 3857, z) dstFile = XYZ.xyz2path(targetDir, z, x, y) gdal.Warp(dstFile, rasterFile, resampleAlg=resAlg, outputBounds=(x0, y0, x1, y1), dstSRS='EPSG:3857', xRes=res, yRes=res, format='GTiff', creationOptions=formatOptions)
def reproject_dataset(dataset: Union[str, gdal.Dataset], bounds: Sequence[float], x_res: int, y_res: int, destination_srs: osr.SpatialReference, bounds_srs: Optional[osr.SpatialReference], resampling_mode: Optional[str]) -> gdal.Dataset: """ Reprojects a gdal dataset to a reference system with the given bounds and the given spatial resolution. :param dataset: A dataset :param bounds: A 1-d float array specifying the bounds of the resulting dataset. Must consist of the following four float values: xmin, ymin, xmax, ymax. :param x_res: The resolution the resulting dataset shall have in x-direction. Must be set in accordance to destination_srs. :param y_res: The resolution the resulting dataset shall have in y-direction. Must be set in accordance to destination_srs. :param destination_srs: The spatial reference system that the resulting data set shall show. :param bounds_srs: The spatial reference system in which the bounds are specified. If not given, it is assumed that the bounds are given in the destination_srs. :param resampling_mode: The mode by which the values from the source dataset shall be combined to values in the target dataset. Available modes are: * near (Nearest Neighbour) * bilinear * cubic * cubicspline * lanczos * average * mode * max * min * med * q1 * q3 If none is selected, 'bilinear' will be selected in case the source values need to be sampled up to a finer destination resolution and 'average' in case the values need to be sampled down to a coarser destination resolution. :return: A spatial dataset with the chosen destination spatial reference system, in the bounds and the x- and y- resolutions that have been set. """ if type(dataset) is str: dataset = gdal.Open(dataset) if bounds_srs is None: bounds_srs = destination_srs if resampling_mode is None: resampling_mode = _get_resampling(dataset, bounds, x_res, y_res, bounds_srs, destination_srs) warp_options = gdal.WarpOptions(format='Mem', outputBounds=bounds, outputBoundsSRS=bounds_srs, xRes=x_res, yRes=y_res, dstSRS=destination_srs, resampleAlg=resampling_mode) reprojected_data_set = gdal.Warp('', dataset, options=warp_options) return reprojected_data_set
def generate_vrt(camera, raster_size, fpath, outpath=None, no_data_value=0): gcps = generate_gcps(camera) xsize, ysize = raster_size if outpath is None: outpath = os.path.dirname(fpath) outname = os.path.splitext(os.path.basename(fpath))[0] + '.vrt' outname = os.path.join(outpath, outname) xsize, ysize = raster_size vrt = r'''<VRTDataset rasterXSize="{{ xsize }}" rasterYSize="{{ ysize }}"> <Metadata/> <GCPList Projection="{{ proj }}"> {% for gcp in gcps -%} {{gcp}} {% endfor -%} </GCPList> <VRTRasterBand dataType="Float32" band="1"> <NoDataValue>{{ no_data_value }}</NoDataValue> <Metadata/> <ColorInterp>Gray</ColorInterp> <SimpleSource> <SourceFilename relativeToVRT="0">{{ fpath }}</SourceFilename> <SourceBand>1</SourceBand> <SourceProperties rasterXSize="{{ xsize }}" rasterYSize="{{ ysize }}" DataType="Float32" BlockXSize="512" BlockYSize="512"/> <SrcRect xOff="0" yOff="0" xSize="{{ xsize }}" ySize="{{ ysize }}"/> <DstRect xOff="0" yOff="0" xSize="{{ xsize }}" ySize="{{ ysize }}"/> </SimpleSource> </VRTRasterBand> </VRTDataset>''' context = { 'xsize': xsize, 'ysize': ysize, 'gcps': gcps, 'proj': '+proj=longlat +a=3396190 +b=3376200 +no_defs', 'fpath': fpath, 'no_data_value': no_data_value } template = jinja2.Template(vrt) tmp = template.render(context) warp_options = gdal.WarpOptions(format='VRT', dstNodata=0) gdal.Warp(outname, tmp, options=warp_options)
def resample(src_dir, dst_dir, resolution): class_band_list = ['B02', 'B03', 'B04', 'B08', 'B10', 'B11', 'B12'] mid_list1 = glob(os.path.join(src_dir, 'GRANULE', '*'))[0] mid_list2 = os.path.join(mid_list1, 'IMG_DATA') fn_list = glob(os.path.join(mid_list2, '*.jp2')) if resolution == 20 or resolution == 60: resample_alg = gdal.gdalconst.GRIORA_Average else: resample_alg = gdal.gdalconst.GRIORA_Bilinear for item in fn_list: warp_options = gdal.WarpOptions(xRes=resolution, yRes=-1 * resolution, resampleAlg=resample_alg, format='GTiff') name_short = os.path.split(item)[1] dst_fn = os.path.join(dst_dir, name_short.replace('.jp2', '.tiff')) if (name_short[-7:-4]) in class_band_list: print(os.path.split(item)[1]) gdal.Warp(dst_fn, item, options=warp_options)
def crop2grid(self): ''' evtl in anderer Klasse weil xyz kann gecroppt werden output_path: Returns: ''' cornerpoints_geo = self._get_cornerpoints(self.grid.extent) cornerpoints_dtm = self._get_cornerpoints(self.extent) if np.any(cornerpoints_geo[:2] - cornerpoints_dtm[:2]) != 0: path_dest = '_cropped_DEM.tif' new_bounds = (self.grid.extent[[0, 2, 1, 3]]) gdal.Warp(path_dest, self.dem, options=gdal.WarpOptions( options=['outputBounds'], outputBounds=new_bounds)) self.dem = gdal.Open(path_dest) self.dem_zval = self.dem.ReadAsArray() self._get_raster_dimensions() print('Cropped raster to geo_model.grid.extent.')
def __get_prepared_band(self, band_number, projection, xRes, yRes, xMin, yMin, xMax, yMax): band_full_file_path = self.file_path_to_product_name( self.file_path, 'sur_refl_b0%s_1' % band_number) band = gdal.Warp('', band_full_file_path, format='MEM', dstSRS=projection, dstNodata=-32768, outputType=gdal.GDT_Float32, xRes=xRes, yRes=yRes, outputBounds=[xMin, yMin, xMax, yMax], copyMetadata=False) band_array = band.GetRasterBand(1).ReadAsArray() band_array = band_array / 10000.0 band.GetRasterBand(1).WriteArray(band_array) return band
def crop2grid(self): ''' Crops raster to extent of the geomodel grid. ''' cornerpoints_geo = self._get_cornerpoints(self.grid.extent) cornerpoints_dtm = self._get_cornerpoints(self.extent) if np.any(cornerpoints_geo[:2] - cornerpoints_dtm[:2]) != 0: path_dest = '_cropped_DEM.tif' new_bounds = (self.grid.extent[[0, 2, 1, 3]]) gdal.Warp(path_dest, self.dem, options=gdal.WarpOptions(options=['outputBounds'], outputBounds=new_bounds)) self.dem = gdal.Open(path_dest) self.dem_zval = self.dem.ReadAsArray() self._get_raster_dimensions() print('Cropped raster to geo_model.grid.extent.')
def OpenDEMs(plist, path): pbar1 = tqdm(total=1, position=0, desc="DemOpening") path_dem = path_to_path_dem(path) file = gdal.Open(path_dem) gt = file.GetGeoTransform() x_s, y_s = calc_pixsize2(file.RasterXSize, file.RasterYSize, gt) w = round(file.RasterXSize / (0.05 / y_s)) h = round(file.RasterYSize / (0.05 / x_s)) dest = path_dem[:-4] + "_s.vrt" gdal.Warp(dest, path_dem, width=w, format='VRT', height=h, resampleAlg='average', dstAlpha=True, dstNodata=255) file_s = gdal.Open(dest) gt = file_s.GetGeoTransform() dem = file_s.GetRasterBand(1).ReadAsArray() mask = np.zeros(dem.shape) mask[dem == 255] = 1 dem_f = cv2.GaussianBlur(dem, (11, 11), 0) kernel = np.ones((15, 15), np.float32) / (15**2) smooth = cv2.filter2D(dem, -1, kernel) ridges = (dem_f - smooth) mask_b = cv2.GaussianBlur(mask, (51, 51), 0) ridges[mask_b > 10**-10] = 0 temp1 = np.zeros(ridges.shape) temp2 = np.zeros(ridges.shape) temp1[ridges < -0.01] = 1 temp2[ridges > -0.11] = 1 ridges = (temp1 * temp2).astype(np.uint8) p = plt.figure() plt.title('Ridges 0.05m') plt.imshow(ridges, cmap='Greys') file_s = None gdal.Unlink(dest) pbar1.update(1) pbar1.close() plt.close() plist.append(p) return plist, mask_b, gt, ridges
def clip_by_bbox(tif_obj, bbox_tup): """ :param tif_obj: GTiff file of the globe :param bbox_tup: a tuple of four float values corresponding to the bounding box :return: A reduced GTiff file of a specific country """ global country_iso, country_dir output = tif_obj.split('/')[-1] output = country_iso + '_' + output # tag the iso at the front output = country_dir + '/' + output # make the full pathname file_path = Path(output) if file_path.exists(): # do nothing if file already there return # do gdalwarp -te bbox input output dataset = gdal.Warp(output, tif_obj, format='GTiff', outputBounds=bbox_tup) dataset = None return output
def make_vrt(data_list, wkt_dst_srs, outputDir, output_vrt_file, outputBounds=None, resolution='average', resampling=gdal.GRA_Bilinear, srcNodata=0, error_threshold=0.125): if len(data_list) == 0: return 0 output_tmp_file_list=[os.path.join(outputDir, os.path.basename(file)+".VRT") for file in data_list] output_tmp_file_list2=[] for file, dst_file in zip(data_list, output_tmp_file_list): # Open source dataset and read source SRS gdal_data = gdal.Open(file) data_proj = gdal_data.GetProjection() if data_proj == '': print("Data without geoprojection info. DISCARDED!") else: # Warp to dst_srs dst_ds = gdal.Warp(dst_file, gdal_data, resampleAlg=resampling, srcNodata=srcNodata, dstNodata=srcNodata, dstSRS=wkt_dst_srs, errorThreshold=error_threshold) del dst_ds output_tmp_file_list2.append(dst_file) print(os.path.join(outputDir, output_vrt_file)) if outputBounds: vrt_data=gdal.BuildVRT(output_vrt_file, output_tmp_file_list2, separate=True, outputBounds=outputBounds, resolution=resolution, srcNodata=srcNodata) else: vrt_data=gdal.BuildVRT(output_vrt_file, output_tmp_file_list2, separate=True, resolution=resolution, srcNodata=srcNodata) bands = vrt_data.RasterCount if bands > 0: del vrt_data vrt_data = gdal.Open(output_vrt_file) xml = et.ElementTree(file=output_vrt_file) for band_num in range(1, bands+1): filename = xml.xpath('//VRTRasterBand[@band=%d]//SourceFilename/text()' % band_num)[0] band = vrt_data.GetRasterBand(band_num) band.SetDescription(filename) # to force gdal to write file del vrt_data return bands
def archive_to_npy(self, save_path): if os.path.isdir(save_path) == False: os.mkdir(save_path) warpOptions, imageVRTPath = self.__preparation() geotiff_name = self.datetime + '.tif' geotiff_path = os.path.join(self.temporary_path, geotiff_name) raster = gdal.Warp(geotiff_path, imageVRTPath, dstNodata = self.key_values.get('NoData'), options = warpOptions) # Сохраняем матрицу в формате .npy npy_name = self.datetime + '.npy' npy_path = os.path.join(save_path, npy_name) matrix = raster.ReadAsArray() matrix = np.array(matrix) np.save(npy_path, matrix) raster = None # Удаляем временную директорию shutil.rmtree(self.temporary_path, ignore_errors = True)
def crop_to_shape(raster_file, shape_file): output_file = raster_file.replace('.tif', '_cropped.tif') if not os.path.exists(output_file): print('Cropping raster:\n ' + raster_file + '\nto shape:\n ' + shape_file + '\n') warped = gdal.Warp( '', raster_file, format='MEM', dstAlpha=True, cropToCutline=True, cutlineDSName=shape_file ) gdal.Translate( output_file, warped, creationOptions=["COMPRESS=LZW", "TILED=YES", "BIGTIFF=IF_SAFER", "NUM_THREADS=ALL_CPUS"] ) del warped return output_file
def raster_to_point(fn_dem): extent, proj_wkt = ot.extent_rast(fn_dem) poly = ot.poly_from_extent(extent) transform = ot.coord_trans(True, proj_wkt, False, 4326) poly.Transform(transform) center_lon, center_lat = ot.get_poly_centroid(poly) epsg, utm_zone = ot.latlon_to_UTM(center_lat, center_lon) print('Reprojecting in ' + str(epsg)) img_vhr = GeoImg(fn_dem) dest = gdal.Warp('', img_vhr.gd, format='MEM', dstSRS='EPSG:{}'.format(epsg), xRes=out_res, yRes=out_res, resampleAlg=gdal.GRA_Bilinear, dstNodata=-9999) img_lr = GeoImg(dest) print('Extracting coords...') elevs = img_lr.img.flatten() x, y = img_lr.xy(ctype='center') coords = list(zip(x.flatten(), y.flatten())) coords_latlon = point_to_lonlat_trans(int(epsg), coords) lon, lat = zip(*coords_latlon) lon = np.array(lon) lat = np.array(lat) keep = ~np.isnan(elevs) h = elevs[keep] lat = lat[keep] lon = lon[keep] print('Done for this DEM') return h, lat, lon
def start(self): """ 校正hdf文件 :return: """ # 检查参数 if self._width == 0 or self._height == 0 or self._geoloc_path == None or self._xRes == 0 or self._yRes == 0 or \ self._source_folder == None or self._source_file_reg == None or self._target_folder == None: print('参数有误!') return -1 # 遍历校正hdf文件 i = 1 for hdf_path in self._hdf_file_paths: self.construct_vrt(hdf_path) # 获取新的文件名 filename = os.path.split(hdf_path)[1][:-4] # 删除目标文件夹中已存在文件 for root, dir, files in os.walk(self._target_folder): for x in files: if re.search(filename, x): os.remove(os.path.join(root, x)) new_path = os.path.join(self._target_folder, filename + '.nc') # 输出文件名 # 成功 gdal.Warp( new_path, self._vrt_path, geoloc=True, format='ENVI', outputBounds=[ self._longitude_start, self._latitude_start, self._longitude_end, self._latitude_end ], dstSRS='EPSG:4326', width=self._width, height=self._height) # xRes=0.04,yRes=0.04 指定输出面积后无法指定分辨率 os.remove(self._vrt_path) # 删除临时校正参数文件 print('[{}/{}]{}已校正!'.format(i, len(self._hdf_file_paths), filename)) i += 1 return 0