将不规则网格重新定向到常规网格

时间:2016-08-13 20:08:19

标签: r matrix vectorization raster map-projections

我在EASE 2.0投影中有卫星图像,并希望将它们重新投影到常规的WGS84 0.25度网格。我尝试了几种选择,例如将它们栅格化为具有所需尺寸的空栅格,但希望更多地控制该过程,因为这仅允许双线性和最近邻插值。

目前我读取数据和相应的经度,经度,然后循环遍历我所需输出网格中的每个像素lat / lon,计算原始文件中所有测量值的距离。如果距离小于0.5像素间距(18km,我将lat,lon距离转换为以米为单位的距离),我使用该测量。输出像素是该距离内所有测量值的平均值(稍后我将根据距离扩展到加权平均值)。不幸的是,我目前的做法非常缓慢。有没有很好的可能性来加快速度,例如避免双循环?

    library(raster)
    library(rgdal)

    #input data, x=longitude, y=latitude, z=measurements
    #r
    # EXAMPLE DATA https://files.fm/u/qshgpzjp
    load ('image.Rdata')
    r[r == -9999] = NaN

    #output lat,lons on 0.25 degree grid
    long = seq(-180,180,0.25)
    lat = seq(90,-90,-0.25)
    long = long+0.125
    lat = lat-0.125
    lat = lat[1:720]
    long = long[1:1440]

    #matrix to put output in
    out.m = matrix(data = NaN, nrow=1440, ncol=720)


    #double loop over lon,lat of output grid
    for (lo in 1:1440) {
      for (la in 1:720) {

        # 2 columns with lon,lat
        xy = cbind(r[,1],r[,2])

        # convert lon,lat of original data to meters
        xy_meter <- project(xy,"+proj=cea +lon_0=0 +lat_ts=30 +x_0=0 +y_0=0 +a=6371228 +b=6371228 +units=m +datum=WGS84 +no_defs")

        #lon,lat of current output pixel to be computed
        lonlat = cbind(long[lo],lat[la])

        #convert lon,lat of current output pixel to meters
        xy_la = project(lonlat, "+proj=cea +lon_0=0 +lat_ts=30 +x_0=0 +y_0=0 +a=6371228 +b=6371228 +units=m +datum=WGS84 +no_defs")

        #compute distance and select original measurements within 18000m both in x and y direction, compute average of valid measurements
        dlo = xy_la[,1] - xy_meter[,1]
        dla = xy_la[,2] - xy_meter[,2]

        blb1 = dlo < 18000 & dlo > -18000
        blb2 = dla < 18000 & dla > -18000

        blb = blb1 & blb2


        check = max(blb)
        if (check < 1) next

        valids = r[blb,3]
        out.m[lo,la] = mean(r[blb,3],na.rm=T)

      }
      print(lo)
    }

0 个答案:

没有答案