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

发布于 2025-01-18 00:53:45 字数 341 浏览 0 评论 0原文

我有一个大的数据框,DF,我是从多个散布的,不规则数据的文件中创建的。

DF非常长(8131596),并具有列:日期,纬度,经度,var1,var2。

理想情况下,我想创建一个一定纬度/经度边界框的2D网格/地图,并应用我的DF,以便将任何属于该LAT/LON的数据都包括在内。或者,从我的数据中创建2D LAT / LON网格,然后可以稍后将其剪切(即,最终得到2D“ image” / matrix)。

然后,我将在这些不规则的点之间进行插值,最终以DF中每个变量的数据字段结束。

但是,我对此有点困扰。我认为也许会参与网格,但我似乎找不到任何与此情况有关的问题/帮助。

任何建议都非常感谢!

I have a large dataframe, df, which I have created from multiple files of scattered, irregular data.

The df is very long (8131596) and has the columns: date, latitude, longitude, 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. Or, create 2D lat/lon grid from my data and I can cut it later (ie, end up with a 2D 'image' / matrix).

I will then interpolate between these irregular points, ending up with essentially a data field for each of the variables in the df.

However I am a bit stuck with what is the best way to go about this. 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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笔芯 2025-01-25 00:53:45

这是针对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()

This is for var1 you would have to repeat this for 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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文