简体   繁体   English

使用python和gdal对点数据进行IDW插值

[英]IDW interpolation of point data using python and gdal

I have a CSV file with the Lat, Long and Rainfall Information. 我有一个包含Lat,Long和Rainfall信息的CSV文件。 I would like to interpolate those point and create tiff file. 我想插入这些点并创建tiff文件。 Can any one can suggest me the easiest way to do that. 任何人都可以建议我最简单的方法。

I am trying to using gdal_grid. 我正在尝试使用gdal_grid。 I am very new on using gdal in python. 我在python中使用gdal非常新。

This is actually several questions. 这实际上是几个问题。 Assuming you have some scattered data for lats and longs you'll to build all the location were you want to make estimation (all lats and longs for the pixels of you Tiff image). 假设你有一些分散的数据用于拉特和多头你将构建所有的位置你想要进行估计(所有拉特和长度为你的像素Tiff图像)。

Once you have that you can use any of the solutions around to do IWD over your data (using a recent example in another question ): 完成后,您可以使用任何解决方案对您的数据进行IWD(使用另一个问题中的最新示例):

class Estimation():
    # IWD. Check: https://stackoverflow.com/questions/36031338/interpolate-z-values-in-a-3d-surface-starting-from-an-irregular-set-of-points/36037288#36037288
    def __init__(self,lon,lat,values):
        self.x = lat
        self.y = lon
        self.v = values

    def estimate(self,x,y,using='ISD'):
        """
        Estimate point at coordinate x,y based on the input data for this
        class.
        """
        if using == 'ISD':
            return self._isd(x,y)

    def _isd(self,x,y):
        #d = np.sqrt((x-self.x)**2+(y-self.y)**2)
        d =  x.copy()
        for i in range(d.shape[0]):
            d[i] = haversine(self.x[i],self.y[i],x,y)
        if d.min() > 0:
            v = np.sum(self.v*(1/d**2)/np.sum(1/d**2))
            return v
        else:
            return self.v[d.argmin()]

The code above is actually adapted to calculate distance with the Haversine formula (which gives great-circle distances between two points on a sphere from their longitudes and latitudes). 上面的代码实际上适用于计算与Haversine公式的距离(它给出了球体上两个点之间距离其经度和纬度的大圆距离)。 Notice again you can find all sorts of solutions for the haversine distance like this one : 再次注意,您可以找到各种类似于此的半径距离的解决方案:

def haversine(lon1, lat1, lon2, lat2):
    """
    Check: https://stackoverflow.com/questions/15736995/how-can-i-quickly-estimate-the-distance-between-two-latitude-longitude-points
    Calculate the great circle distance between two points 
    on the earth (specified in decimal degrees)
    """
    # convert decimal degrees to radians 
    lon1, lat1, lon2, lat2 = map(radians, [lon1, lat1, lon2, lat2])
    # haversine formula 
    dlon = lon2 - lon1 
    dlat = lat2 - lat1 
    a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon/2)**2
    c = 2 * asin(sqrt(a)) 
    km = 6367 * c
    return km

Finally once you have your array ready you should just build the Tiff using GDAL. 最后,一旦准备好阵列,就应该使用GDAL构建Tiff。 For this check the following question for which I quote a part of it's solution: 为此,请检查以下问题 ,我引用它的一部分解决方案:

driver = gdal.GetDriverByName('GTiff')
ds = driver.Create('output.tif',xsize, ysize, 1, gdal.GDT_Float32, )
# this assumes the projection is Geographic lat/lon WGS 84
srs = osr.SpatialReference()
srs.ImportFromEPSG(4326)
ds.SetProjection(srs.ExportToWkt())
gt = [ulx, xres, 0, uly, 0, yres ]
ds.SetGeoTransform(gt)
outband=ds.GetRasterBand(1)
outband.SetStatistics(np.min(mag_grid), np.max(mag_grid), np.average(mag_grid), np.std(mag_grid))
outband.WriteArray(mag_grid)

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

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