首页 > 编程语言 >常用的Python代码片段(地理相关)

常用的Python代码片段(地理相关)

时间:2023-11-15 19:23:30浏览次数:32  
标签:crs 片段 Python 代码 mask print path tif out

把pandas的dataframe转为geopandas的地理格式(df to geodf)

def df2gdf(df, lon_col='longitude', lat_col='latitude', epsg=4326, crs=None):
    gdf = gpd.GeoDataFrame(df)
    gdf['geometry'] = gpd.points_from_xy(x=df[lon_col], y=df[lat_col])
    gdf.geometry = gdf['geometry']
    if crs is None:
        gdf.set_crs(epsg=epsg, inplace=True)
    else:
        gdf.set_crs(crs=crs, inplace=True)
    return gdf

重采样

from osgeo import gdal
import glob
import os
os.chdir("/mnt/f/analysis/")
def resample(image, new_xres, new_yres, save_path):
    """
    Decrease the pixel size of the raster.
    Args:
        new_xres (int): desired resolution in x-direction
        new_yres (int): desired resolution in y-direction
        save_path (str): filepath to where the output file should be stored
    Returns: Nothing, it writes a raster file with decreased resolution.
    """
    props = image.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, image, options=options)
    newprops = newfile.GetGeoTransform()
    print('new pixel xsize:', newprops[1], 'new pixel ysize:', -newprops[-1])
    print('file saved in ' + save_path)
IN_DIR = "CDL-clip"
OUT_DIR = "CDL_resample"
if not os.path.exists(OUT_DIR):
    os.mkdir(OUT_DIR)
for i in glob.glob(f"{IN_DIR}/*.tif"):
    print(f"[{i}]","* "*5)
    resample(gdal.Open(i), 0.008983152841195214,0.008983152841195214, i.replace(IN_DIR,OUT_DIR))

栅格运算

os.system(f"gdal_calc.py --calc \(B=={CROP_CLASS}\)*A --outfile {out_filename} \
        -A {IN_FILENAMES[i]} -B {MASK_FILENAMES[i]} \
        --extent union  --A_band 1 --B_band 1  --quiet --NoDataValue=0")

但是在windows下的gdal运行出错,在ubuntu系统内运行成功

读取地理栅格图像

def read_tif(tif_path, display=False):
    dataset = rio.open(tif_path)
   
    if display:
        print(f"width:{dataset.width}, height:{dataset.height}, bands:{dataset.count}")
       
        types = {i: dtype for i, dtype in zip(dataset.indexes, dataset.dtypes)}
        for k in types.keys():
            print(f"band_{k}: {types[k]}")
       
        print(f"{dataset.crs}")
        print(dataset.transform)
   
    return dataset
# 根据经纬度,从栅格中提取值(适用于EPSG:4326,其他坐标系未做测试)
def extract_value_from_tif(rs, lon,lat):
    '''
    geometry: geodataframe的geometry列
    rs: 栅格数据
    '''
    x = lon
    y = lat
    row, col = rs.index(x,y)
    value = rs.read(1)[row,col]
    return value

批量裁剪

import geopandas as gpd
import rasterio as rio
#from rasterio.mask import mask
import rasterio.mask  
from geopandas import GeoSeries
import os
def handle_grid(shpdatafile, rasterfile, out_file):
    """
    handle gird
    :param shpdatafile: path of shpfile
    :param rasterfile: path of rasterfile
    :param out_file
    :return:
    """
    shpdata = gpd.GeoDataFrame.from_file(shpdatafile)
    # print(shpdata.crs)
    # shpdata.crs = {'init': 'epsg:4326'}
    print(shpdata.crs)
    rasterdata = rio.open(rasterfile)
    print(rasterdata.crs)
    print(f"total {len(shpdata)}")
    for i in range(0, len(shpdata)):
        # getting vector data features
        print(f"current {i}th",end="")
        geo = shpdata.geometry[i]
        out_name = shpdata.gridid[i]
        #out_name = str(i)
        feature = [geo.__geo_interface__]
        # try:
        # Using feature to get building mask tif
        # print("running mask...")
        out_image, out_transform = rio.mask.mask(rasterdata, feature, all_touched=True, crop=True, nodata=rasterdata.nodata)
        #out_image, out_transform = rio.mask.mask(rasterdata, feature, all_touched=True, invert=True, nodata=rasterdata.nodata)
        # get tif Value值,and convert to list
        out_meta = rasterdata.meta.copy()
        out_meta.update({"driver": "GTiff",
                            "height": out_image.shape[1],
                            "width": out_image.shape[2],
                            "transform": out_transform})
        band_mask = rasterio.open(os.path.join(out_file, f"{out_name}.tif"), "w", **out_meta)
        # print(out_image)
        band_mask.write(out_image)
        # except Exception as e:
        #    print(e)
        #    pass
           
           
shp = "/home/data/jyx/population-5-5/run-data-result/urban_vitality/beijing_unicom/bj3857.shp"
raster = "/home/data/jyx/population-5-5/rs/uv_sentinel/beijing.tif"
out = "/home/data/jyx/population-5-5/rs/uv_sentinel/split_tif/"
handle_grid(shp,raster,out)

log_resolver.ipynb用来绘图

原因是通过程序绘制的图更规范,有一些指标,如散点图的拟合公式、\(R^2\)等 ,以前在excel中需要点点点才能显示,在观察、分析大量log文件时,很繁琐,不利于发现规律。
而在程序写好之后,log可以很直观的看到,并进行对比。并且由于程序制图较规范,便于不同实验之间进行比较,美观度也还可以,后续略作调整即可用于论文插图
以下代码实现了plt的density scatter.

def density_scatter( x , y, ax = None, sort = True, bins = 20, is_cbar=True, **kwargs )   :
    """
    Scatter plot colored by 2d histogram
    """
    if ax is None :
        fig , ax = plt.subplots()
    data , x_e, y_e = np.histogram2d( x, y, bins = bins, density = True )
    z = interpn( ( 0.5*(x_e[1:] + x_e[:-1]) , 0.5*(y_e[1:]+y_e[:-1]) ) , data , np.vstack([x,y]).T , method = "splinef2d", bounds_error = False)
    #To be sure to plot all data
    z[np.where(np.isnan(z))] = 0.0
    # Sort the points by density, so that the densest points are plotted last
    if sort :
        idx = z.argsort()
        x, y, z = x[idx], y[idx], z[idx]
    ax.scatter( x, y, c=z, **kwargs )
    ax.grid(linestyle='--',linewidth=0.5)
    norm = Normalize(vmin = np.min(z), vmax = np.max(z))
    if is_cbar:
        cbar = plt.colorbar(cm.ScalarMappable(norm = norm), ax=ax)
        cbar.ax.set_ylabel('Density')
    return ax

标签:crs,片段,Python,代码,mask,print,path,tif,out
From: https://www.cnblogs.com/geoli/p/17834554.html

相关文章

  • 常用的Python代码片段(通用)
    递归Merge数据表df=functools.reduce(lambdaleft,right:pd.merge(left,right,how='left',on=['id','year']),[maps,pp,pp_doy_rainDayCounts,pp_moy_rainZscore,modis_temp,pop,])深复制Importcopycopy.deepcopy(init_map......
  • python初学者学习笔记-第十章-pandas
    Chapter10/pandas10.1dataframe简介dataframe是pandas中最基础的数据结构,当然它也是pandas中最常见的对象,它跟表格类似。dataframe的行和列是分别存储的数据集;这种存储方式,加快了列和行的操作效率。10.1.1创建dataframe一般情况下,可以通过列表和字典这些类型的数据源来创建......
  • Streamlit 快速构建交互式页面的python库
    基础介绍streamlit是什么Streamlit是一个面向机器学习和数据科学团队的开源应用程序框架,通过它可以用python代码方便快捷的构建交互式前端页面。streamlit特别适合结合大模型快速的构建一些对话式的应用,可以看到一些行业内热门的使用。项目本身也比较成熟,release版本,start数量等都......
  • python 打包exe并可以在别人电脑上运行
    1:下载安装installerpipinstallpyinstaller2:打包pyinstaller-Fxxxx.py(-F打包的是带python环境的包 不带f,打的是本地可执行的包)3:运行效果......
  • 自动评论类脚本的通用代码分享,适用于多个软件!
    在当今数字化时代,自动评论类脚本已经成为许多人在多个软件平台上的得力助手,它们可以帮助我们自动化重复的评论任务,节省时间和精力,本文将分享一些自动评论类脚本的通用代码,这些代码适用于多个软件平台,帮助您更好地利用自动评论功能。一、自动评论类脚本的原理自动评论类脚本是基于软......
  • 开发管理类软件通用代码分享
    随着企业运营的日益复杂化,管理类软件已经成为企业不可或缺的工具,然而,对于开发者来说,如何编写一款高效、稳定、易用的管理类软件是一大挑战,本文将分享一些开发管理类软件的通用代码,帮助开发者提高效率、减少错误、优化用户体验。一、数据库设计和访问管理类软件通常需要处理大量的数......
  • python if判断和循环判断
    if判断在写代码的时候,往往需要根据某些条件进行判断,并根据判断结果执行不同的分支代码。#单个条件a=1ifa==1:print(11111)ifa==2:print(2222)else:print(333)#多个条件,加多少个都可以ifa==1:print(11111)elifa==2:print(22222)else:pri......
  • Python简史
    Python的历史可以追溯到上世纪80年代末和90年代初,由荷兰计算机科学家GuidovanRossum在荷兰国家研究所(CWI)开发而成。以下是Python的详细历史:1980年代:Python的起源可以追溯到1980年代末期。GuidovanRossum作为一个编程爱好者,受到ABC语言的启发,希望创建一种简......
  • Python:dcm转jpg脚本
    importpydicomfromPILimportImageimportnumpyasnpimportosdefconvert_dicom_to_jpeg(dicom_file_path,output_folder):#读取DICOM文件dicom_file=pydicom.dcmread(dicom_file_path)#将DICOM数据转换为numpy数组image_array=dicom_file......
  • python tkinter treeview 仿 excel表格
    代码:fromtkinterimportttkfromtkinterimport*root=Tk()#初始框的声明columns=("姓名","IP地址")treeview=ttk.Treeview(root,height=18,show="headings",columns=columns)#表格treeview.column("姓名",width=100,a......