简体   繁体   English

从 python 中给定变量的纬度/经度点数据创建纬度/经度网格?

[英]Creating a lat/lon grid from lat/lon point data of a given variable in python?

I have a large dataframe, df, which I have created from multiple files of scattered, irregular data.我有一个很大的 dataframe,df,它是我从多个分散的、不规则的数据文件创建的。

The df is very long (8131596) and has the columns: date, latitude, longitude, var1, var2. df 很长 (8131596) 并且包含以下列:日期、纬度、经度、var1、var2。

I would ideally like to create a 2D grid/map of certain latitude/longitude bounding box, and apply my df so any data that falls within that lat/lon is included.理想情况下,我想创建一个特定纬度/经度边界框的 2D 网格/地图,并应用我的 df,以便包含属于该纬度/经度的任何数据。 Or, create 2D lat/lon grid from my data and I can cut it later (ie, end up with a 2D 'image' / matrix).或者,根据我的数据创建 2D 纬度/经度网格,我可以稍后对其进行切割(即,以 2D“图像”/矩阵结束)。

I will then interpolate between these irregular points, ending up with essentially a data field for each of the variables in the df.然后我将在这些不规则点之间进行插值,最终得到 df 中每个变量的基本数据字段。

However I am a bit stuck with what is the best way to go about this.但是,关于此问题,我对 go 的最佳方式有点困惑。 I think perhaps mesh grid will be involved, but I can't seem to find any previous questions/help that relates this situation.我认为可能会涉及网格,但我似乎找不到与这种情况相关的任何先前问题/帮助。

Any suggestions greatly appreciated!非常感谢任何建议!

This is for var1 you would have to repeat this for var2这是针对 var1 的,您必须对 var2 重复此操作

import numpy as np
import pandas as pd
from scipy.interpolate import griddata
import matplotlib.pyplot as plt
import rasterio
from rasterio.crs import CRS

# define interpolation inputs
points = list(zip(df.longitude,df.latitude))
values = df.var1.values # change to var2 for second grid

# define raster resolution
rRes = 50

# create coord ranges over the desired raster extension
xRange = np.arange(df.longitude.min(),df.longitude.max()+rRes,rRes)
yRange = np.arange(df.latitude.min(),df.latitude.max()+rRes,rRes)

# create arrays of x,y over the raster extension
gridX,gridY = np.meshgrid(xRange, yRange)

# interpolate over the grid
gridPh = griddata(points, values, (gridX,gridY), method='linear')

# show interpolated values
plt.imshow(gridPh)
plt.colorbar()

# definition of the raster transform array
from rasterio.transform import Affine
transform = Affine.translation(gridX[0][0]-rRes/2, gridY[0][0]-rRes/2)*Affine.scale(rRes,rRes)
transform

# get crs as wkt
# use your crs here
rasterCrs = CRS.from_epsg('32718')
rasterCrs.data

#definition, register and close of interpolated raster
interpRaster = rasterio.open('./interpRaster3.tif',
                                'w',
                                driver='GTiff',
                                height=gridPh.shape[0],
                                width=gridPh.shape[1],
                                count=1,
                                dtype=gridPh.dtype,
                                crs=rasterCrs,
                                transform=transform,
                                )
interpRaster.write(gridPh,1)
interpRaster.close()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM