python word转jpg_tif文件转jpg

Python (2) 2024-06-03 20:12

Hi,大家好,我是编程小6,很荣幸遇见你,我把这些年在开发过程中遇到的问题或想法写出来,今天说一说python word转jpg_tif文件转jpg,希望能够帮助你!!!。

'''本程序用于将grd文件转换成tif文件并进行裁剪、校准'''
import pandas as pd
import numpy as np
from osgeo import gdal, gdalconst, osr, ogr
import matplotlib.pyplot as plt
import xarray as xr
import rioxarray as rio
import geopandas as gpd
#from shapely.geometry import mapping
#创建读取grd文件的函数
def read_grd(filepath):
    with open(filepath) as infile:
        ncols = int(infile.readline().split()[1])
        nrows = int(infile.readline().split()[1])
        xllcorner = float(infile.readline().split()[1])
        yllcorner = float(infile.readline().split()[1])
        cellsize = float(infile.readline().split()[1])
        nodata_value = float(infile.readline().split()[1])
    
    value = np.loadtxt(filepath, skiprows=6)
    return value, ncols, nrows, cellsize 

#调用函数
for yy in [2028,2029,2030,2031,2032,2048,2049,2050,2051,2052,2068,2069,2070,2071,2072,2096,2097,2098,2099,2100]:    
    filepath = 'F:/pre'+str(yy)+'.grd'
    inter_array, cols1, rows1, cellsize  = read_grd(filepath)  #读取grd文件
    filepath2 = 'F:/pre'+str(yy)+'_cov.grd'
    cov,_,_,_  = read_grd(filepath2)
    inter_array[np.where(cov>=100)] = inter_array[np.where(cov>=100)] - cov[np.where(cov>=100)]     #用误差数据进行校正
    inter_array[np.where(inter_array>=800)] = inter_array[np.where(inter_array>=800)] - cov[np.where(inter_array>=800)]
    inter_array1 = inter_array.reshape(rows1,cols1)
    inter_array1[np.where(inter_array1<-0.0)] = np.nan
    inputfilePath ="F:/shp_xj.tif"
    inputrasfile = gdal.Open(inputfilePath) #
    projection = inputrasfile.GetProjection()  # 获取投影
    geotrans2 = inputrasfile.GetGeoTransform()  # 几何信息
    #写出tif
    outtifpath = 'F:/pre'+str(yy)+'.tif'
    driver = gdal.GetDriverByName("GTiff")
    dataset = driver.Create(outtifpath, cols1, rows1, 1, gdal.GDT_Float64)
    dataset.SetGeoTransform(geotrans2)
    dataset.SetProjection(projection)
    dataset.GetRasterBand(1).WriteArray(inter_array1)
    outtifpath = 'F:/pre'+str(yy)+'.tif'
    driver = gdal.GetDriverByName("GTiff")
    dataset = driver.Create(outtifpath, cols1, rows1, 1, gdal.GDT_Float64)
    dataset.SetGeoTransform(geotrans2)
    dataset.SetProjection(projection)
    dataset.GetRasterBand(1).WriteArray(inter_array1)
            
    #用矢量数据裁剪栅格 
    shp_dir = 'F:/xj_wgs8445.shp'
    xj_shp = gpd.read_file(shp_dir)
    xj_shp_crs = xj_shp.crs
    
    file_name = 'F:/pre'+str(yy)+'.tif'
    tif = rio.open_rasterio(file_name)[0]
    tif_crs = tif.rio.crs
    
    if xj_shp_crs==tif_crs:
        #xj_tif = tif.rio.clip(xj_shp.geometry.apply(mapping), xj_shp.crs)
        xj_tif = tif.rio.clip(xj_shp.geometry)
        xj_tif.rio.to_raster('F:/pre'+str(yy)+'_1.tif')
    print(yy)
    plt.imshow(xj_tif)
    cb = plt.colorbar(label='color bar settings')
    plt.show()

今天的分享到此就结束了,感谢您的阅读,如果确实帮到您,您可以动动手指转发给其他人。

下一篇

已是最新文章

发表回复