示例#1
0
    def invdistnn(in_path,
                  out_path,
                  field_name,
                  output_bounds=None,
                  param=None):
        """
        最邻近反距离加权插值 将shp文件插值为TIF
        :param in_path: 输入的shp文件
        :param out_path: 输出的tif文件
        :param field_name: 被插值的属性字段
        :param output_bounds: 输出的tif四角范围 (xmin, ymin, xmax, ymax)
        :param param: 插值参数,字符串,不传参以默认值进行
                    [power:加权系数(默认为3.0)
                    smoothing:平滑参数(默认为0.0)
                    radius:搜索圆的半径,不应为零(默认值为1.0)
                    max_points:要使用的最大数据点数。搜索的点数不要超过此数字。 加权时,发现的点将从最远到最远的距离排名(默认值为12)
                    min_points:要使用的最小数据点数。如果发现的点数量较少,则网格节点被认为是空的,并将被NODATA标记填充(默认值为0)
                    nodata:NODATA标记以填充空白点(默认为0.0)]
        :return:
        """

        # TODO 是否为密集点时比较试用?
        # TODO radius设置太小会导致数据镂空
        if param:
            if len(param) != 6 or not all(isinstance(x, str) for x in param):
                print('input param format error.')
                return
            else:
                param_str = 'invdistnn'
                param_str += ':power=' + param[0]
                param_str += ':smoothing=' + param[1]
                param_str += ':radius=' + param[2]
                param_str += ':max_points=' + param[3]
                param_str += ':min_points=' + param[4]
                param_str += ':nodata=' + param[5]
        else:
            param = 'invdistnn:power=3.0:smoothing=0.0:radius=1.0:max_points=0:min_points=0:nodata=0.0'
        if output_bounds:
            options = gdal.GridOptions(algorithm=param,
                                       format="GTiff",
                                       outputType=gdal.GDT_Float32,
                                       zfield=field_name,
                                       outputBounds=output_bounds)
        else:
            options = gdal.GridOptions(algorithm=param,
                                       format="GTiff",
                                       outputType=gdal.GDT_Float32,
                                       zfield=field_name)

        gdal.Grid(destName=out_path, srcDS=in_path, options=options)
示例#2
0
    def invdist(in_path, out_path, field_name, output_bounds=None, param=None):
        """
        反距离加权插值 将shp文件插值为TIF
        :param in_path: 输入shp文件
        :param out_path: 输出tif文件
        :param field_name: 被插值的属性字段
        :param output_bounds: 输出tif四角范围 (xmin, ymin, xmax, ymax) 默认为点shp四角坐标
        :param param: 插值参数,字符串,不传参以默认值进行
                                [ power:加权系数(默认为3.0),
                                smoothing:平滑参数(默认为0.0),
                                radius1:搜索椭圆的第一个半径(如果旋转角度为0,则为X轴),将此参数设置为零以使用整个点数组(默认值为0.0),
                                radius2:搜索椭圆的第二个半径(如果旋转角度为0,则为Y轴),将此参数设置为零以使用整个点数组(默认值为0.0),
                                angle:搜索椭圆的旋转角度,以度为单位(逆时针,默认为0.0),
                                max_points:要使用的最大数据点数。搜索的点数不要超过此数字。仅在设置搜索椭圆(两个半径都不为零)时使用。零表示应使用所有找到的点(默认值为0),
                                min_points:要使用的最小数据点数。如果发现的点数量较少,则网格节点被认为是空的,并将被NODATA标记填充。仅在设置搜索椭圆(两个半径都不为零)时使用(默认值为0),
                                nodata:NODATA标记以填充空白点(默认为0.0)]
        :return:
        """
        if param:
            if len(param) != 8 or not all(isinstance(x, str) for x in param):
                print('input param format error.')
                return
            else:
                param_str = 'invdist'
                param_str += ':power=' + param[0]
                param_str += ':smoothing=' + param[1]
                param_str += ':radius1=' + param[2]
                param_str += ':radius2=' + param[3]
                param_str += ':angle=' + param[4]
                param_str += ':max_points=' + param[5]
                param_str += ':min_points=' + param[6]
                param_str += ':nodata=' + param[7]

        else:
            param_str = 'invdist:power=3.0:smoothing=0.0:radius1=0.0:radius2=0.0:angle=0.0:max_points=0:min_points=0:nodata=0.0'
        if output_bounds:
            options = gdal.GridOptions(algorithm=param_str,
                                       format="GTiff",
                                       outputType=gdal.GDT_Float32,
                                       zfield=field_name,
                                       outputBounds=output_bounds)
        else:
            options = gdal.GridOptions(algorithm=param_str,
                                       format="GTiff",
                                       outputType=gdal.GDT_Float32,
                                       zfield=field_name)

        gdal.Grid(destName=out_path, srcDS=in_path, options=options)
示例#3
0
def grid_option(factor_name='aqi', time_interval='d', start_time=None):
    '''
        构造插值选项
        factor--- 插值因子名称
        time_interval--- 时间尺度, 取值 y/m
        start_time--- 累计插值数据开始时间, 格式为 yyyymmdd / yyyymmddHH
        current_task_name--- 累计数据插值当前时间, 格式为 yyyymmdd / yyyymmddHH
    '''

    max_val = get_factor_max_val(factor_name, time_interval)
    return gdal.GridOptions(
        options=[],
        format='GTiff',
        outputType=gdal.GDT_Float32,
        width=1091,
        height=800,
        creationOptions=None,
        outputSRS='EPSG:3857',
        noData=None,
        algorithm=grid_algorithm,
        layers=None,
        SQLStatement=None,
        where='{factorName} >= 0 and {factorName} <= {maxVal}'.format(
            factorName=factor_name, maxVal=max_val),
        spatFilter=None,
        zfield=factor_name,
        z_increase=None,
        z_multiply=None,
        callback=None,
        callback_data=None)
示例#4
0
    def average(in_path, out_path, field_name, output_bounds=None, param=None):
        """
        移动平均法 将shp文件插值为TIF
        :param in_path: 输入的shp文件
        :param out_path: 输出的tif文件
        :param field_name: 被插值的属性字段
        :param output_bounds: 输出的tif四角范围 (xmin, ymin, xmax, ymax)
        :param param: 插值参数,字符串,不传参以默认值进行
                    [ radius1:搜索椭圆的第一个半径(如果旋转角度为0,则为X轴)。将此参数设置为零以使用整个点数组(默认值为0.0)
                    radius2:搜索椭圆的第二个半径(如果旋转角度为0,则为Y轴)。将此参数设置为零以使用整个点数组(默认值为0.0)
                    angle:搜索椭圆的旋转角度,以度为单位(逆时针,默认为0.0)
                    min_points:要使用的最小数据点数。 如果发现的点数量较少,则网格节点被认为是空的,并将被NODATA标记填充(默认值为0)
                    nodata:NODATA标记以填充空白点(默认为0.0)]
        :return:
        """

        # TODO 有问题??

        if param:
            if len(param) != 5 or not all(isinstance(x, str) for x in param):
                print('input param format error.')
                return
            else:
                param_str = 'average'
                param_str += ':radius1=' + param[0]
                param_str += ':radius2=' + param[1]
                param_str += ':angle=' + param[2]
                param_str += ':min_points=' + param[3]
                param_str += ':nodata=' + param[4]
        else:
            param = 'average:radius1=1:radius2=1:angle=0.0:min_points=0:nodata=0.0'
        if output_bounds:
            options = gdal.GridOptions(algorithm=param,
                                       format="GTiff",
                                       outputType=gdal.GDT_Float32,
                                       zfield=field_name,
                                       outputBounds=output_bounds)
        else:
            options = gdal.GridOptions(algorithm=param,
                                       format="GTiff",
                                       outputType=gdal.GDT_Float32,
                                       zfield=field_name)

        gdal.Grid(destName=out_path, srcDS=in_path, options=options)
示例#5
0
def interpolate():
    print(power_option)
    print(smoothing_option)
    #power_option = str(power_option)
    #smoothing_option = str(smoothing_option)
    algorithm_option = 'invdist:power='+str(power_option)+':smoothing='+str(smoothing_option)
    gridOptions = gdal.GridOptions(format='Gtiff', algorithm=algorithm_option, zfield="nitr_ran")
    out = gdal.Grid(outputTest, wellNitrate, options=gridOptions)
    out = None
    del out
示例#6
0
    def nearest(in_path, out_path, field_name, output_bounds=None, param=None):
        """
        最邻近法 将shp文件插值为TIF
        :param in_path: 输入的shp文件
        :param out_path: 输出的tif文件
        :param field_name: 被插值的属性字段
        :param output_bounds: 输出的tif四角范围 (xmin, ymin, xmax, ymax)
        :param param: 插值参数,字符串,不传参以默认值进行
                    [radius1:搜索椭圆的第一个半径(如果旋转角度为0,则为X轴)。将此参数设置为零以使用整个点数组(默认值为0.0)
                    radius2:搜索椭圆的第二个半径(如果旋转角度为0,则为Y轴)。将此参数设置为零以使用整个点数组(默认值为0.0)
                    angle:搜索椭圆的旋转角度,以度为单位(逆时针,默认为0.0)
                    nodata:NODATA标记以填充空白点(默认为0.0)]
        :return:
        """
        if param:
            if len(param) != 4 or not all(isinstance(x, str) for x in param):
                print('input param format error.')
                return
            else:
                param_str = 'nearest'
                param_str += ':radius1=' + param[0]
                param_str += ':radius2=' + param[1]
                param_str += ':angle=' + param[2]
                param_str += ':nodata=' + param[3]
        else:
            param = 'nearest:radius1=0.0:radius2=0.0:angle=0.0:nodata=0.0'
        if output_bounds:
            options = gdal.GridOptions(algorithm=param,
                                       format="GTiff",
                                       outputType=gdal.GDT_Float32,
                                       zfield=field_name,
                                       outputBounds=output_bounds)
        else:
            options = gdal.GridOptions(algorithm=param,
                                       format="GTiff",
                                       outputType=gdal.GDT_Float32,
                                       zfield=field_name)

        gdal.Grid(destName=out_path, srcDS=in_path, options=options)
示例#7
0
    def linear(in_path, out_path, field_name, output_bounds=None, param=None):
        """
        线性法 将shp文件插值为TIF
        :param in_path: 输入的shp文件
        :param out_path: 输出的tif文件
        :param field_name: 被插值的属性字段
        :param output_bounds: 输出的tif四角范围 (xmin, ymin, xmax, ymax)
        :param param: 插值参数,字符串,不传参以默认值进行
                    [radius:如果要插入的点不适合Delaunay三角剖分的三角形,请使用该最大距离搜索最近的邻居,否则使用nodata
                            如果设置为-1,则搜索距离是无限的
                            如果设置为0,则将始终使用nodata值。默认值为-1
                    nodata:NODATA标记以填充空白点(默认为0.0)]
        :return:
        """
        # TODO 外扩性不太好
        if param:
            if len(param) != 2 or not all(isinstance(x, str) for x in param):
                print('input param format error.')
                return
            else:
                param_str = 'linear'
                param_str += ':radius=' + param[0]
                param_str += ':nodata=' + param[1]
        else:
            param = 'linear:radius=-1:nodata=0.0'
        if output_bounds:
            options = gdal.GridOptions(algorithm=param,
                                       format="GTiff",
                                       outputType=gdal.GDT_Float32,
                                       zfield=field_name,
                                       outputBounds=output_bounds)
        else:
            options = gdal.GridOptions(algorithm=param,
                                       format="GTiff",
                                       outputType=gdal.GDT_Float32,
                                       zfield=field_name)

        gdal.Grid(destName=out_path, srcDS=in_path, options=options)
def gdal_grid(path):

    grid_opt = gdal.GridOptions(
        format='GTiff',
        outputType=gdal.GDT_Int16,
        algorithm=
        'invdist:power=3.0:smothing=0.0:radius1=0.0:radius2=0.0:angle=0.0:max_points=0:min_points=0:nodata=0.0',
        layers='bugs',
        zfield='value',
    )

    # Use .tiff and not .tif
    try:
        output = gdal.Grid('/tmp/output.tiff', path, options=grid_opt)
        status = True
        return status
    except:
        status = False
        return status
def grid_var(var_array, coordinates, grid_kwargs):
    """
    A function for gridding a variable array in gdal
    :param var_array: a column array of the variable to grid
    :param coordinates: utm coordinate array
    :param grid_kwargs: keyword argument dictionary for gridding using  gdal_grid
    :return:
    :param gridded: A gridded array with shape (height, width)
    :param geotransform: the gdal geotransform for the gridded array

    """
    if len(var_array.shape) == 1:
        var_array = var_array.reshape([-1, 1])

    if grid_kwargs['log_grid']:
        var_array = np.log10(var_array)

    temp_a = np.concatenate((coordinates, var_array), axis=1)

    # Output the points into a temporary csv

    temp_dir = r'C:\temp\tempfiles'
    # temp_dir = tempfile.gettempdir() # getting permissions error

    if not os.path.exists(temp_dir):
        os.mkdir(temp_dir)

    os.chdir(temp_dir)

    temp_file = 'pts2grid_temp.csv'

    with open(temp_file, 'w') as f:
        f.write('x,y,z\n')
        # Write the array
        np.savetxt(f, temp_a, delimiter=',')
    # Write vrt
    vrt_file = misc_utils.write_vrt_from_csv(temp_file, 'x', 'y', 'z')

    # Now grid using gdal_grid

    gridopt = gdal.GridOptions(
        format=grid_kwargs['format'],
        outputType=gdal.GDT_Float32,
        algorithm=grid_kwargs['gdal_algorithm'],
        width=grid_kwargs['width'],
        height=grid_kwargs['height'],
        outputSRS=grid_kwargs['outputSRS'],  # "EPSG:28352"
        outputBounds=grid_kwargs['outputBounds'],
        zfield='z',
        layers=vrt_file.split('\\')[-1].split('.')[0])

    # Getting errors frmo tempdir
    outfile = 'temp_grid.tif'
    print("Gridding ", outfile)
    out_ds = gdal.Grid(destName=outfile, srcDS=vrt_file, options=gridopt)
    print("Finished gridding ", outfile)
    geotransform = out_ds.GetGeoTransform()

    gridded = out_ds.ReadAsArray()

    # Currently the nulls are very annoyingly retunr zeros

    null = np.float(grid_kwargs['gdal_algorithm'].split('nodata=')[-1])

    gridded[gridded == null] = np.nan

    if grid_kwargs['log_grid']:
        gridded = 10**gridded

    # Remove trash
    temp_a = None
    out_ds = None

    gc.collect()

    # Delete the temporary files
    for file in [temp_file, vrt_file, outfile]:
        try:
            os.remove(file)
        except PermissionError:
            print('Permission error. Unable to delete ', file)

    return gridded, geotransform
def processFile(job_data, file_data):
    try:
        #Set output filename
        tileName = file_data.fileName + ".tif"
        tile = os.path.join(dem_directory, tileName).replace("\\","/")

        csv_data = []

        #Open source file and create a array representing csv data
        with open(file_data.filePath, 'r') as dmr_file:
            for l in dmr_file:
                if l != "":
                    xyz_line = l.strip().split(job_data.sourceDel)
                    csv_data.append("{xc},{yc},{h}\n".format(xc=xyz_line[0],yc=xyz_line[1],h=xyz_line[2]))

        #Check if last row is empty, and remove it
        if csv_data[len(csv_data) - 1] == "":
            csv_data.pop()

        #Check if last row ends with new line, trim row
        lastrow = csv_data[len(csv_data) - 1]
        if lastrow.endswith('\n'):
            lastrow = lastrow.rstrip()
            csv_data[len(csv_data) - 1] = lastrow

        csv_file = os.path.join(dem_directory, file_data.fileName + ".csv").replace("\\","/")
        if os.path.isfile(csv_file):
            os.remove(csv_file)

        #Write csv data to file
        with open(csv_file, 'w') as csf:
            csf.writelines(csv_data)


        #Create array representing lines in a vrt file
        vrtData = []
        vrtData.append("<OGRVRTDataSource>\n")
        vrtData.append('    <OGRVRTLayer name="{name}">\n'.format(name=file_data.fileName))
        vrtData.append('        <SrcDataSource>{csvfile}</SrcDataSource>\n'.format(csvfile=csv_file))
        vrtData.append('        <GeometryType>wkbPoint25D</GeometryType>\n')
        vrtData.append('        <LayerSRS>{sepsg}</LayerSRS>\n'.format(sepsg=job_data.sourceEpsg))
        vrtData.append('        <GeometryField encoding="PointFromColumns" x="field_1" y="field_2" z="field_3"/>\n')
        vrtData.append('    </OGRVRTLayer>\n')
        vrtData.append('</OGRVRTDataSource>')



        vrt_file = os.path.join(dem_directory, file_data.fileName + ".vrt").replace("\\","/")
        if os.path.isfile(vrt_file):
            os.remove(vrt_file)

        #Write vrt to file
        with open(vrt_file, 'w') as vrf:
            vrf.writelines(vrtData)
        sleep(0.01)

        vrts = gdal.OpenEx(vrt_file, 0)

        #Visit https://gdal.org/python/osgeo.gdal-module.html#GridOptions to see other options, like width and height for a higher resolution
        option = gdal.GridOptions(format='GTiff',width=job_data.tileHeight,height=job_data.tileWidth,outputSRS=job_data.sourceEpsg,algorithm='invdist:power={pow}:smoothing={smo}'.format(pow=job_data.powerVal,smo=job_data.smoothingVal),zfield='field_3')
        #Interapolate TIN to a grid
        ds = gdal.Grid(tile,vrts,options=option)
        ds = None
        del ds

        vrts = None

        #Remove csv and vrt file
        if cleanup:
            os.remove(csv_file)
            os.remove(vrt_file)

        sleep(0.05)
        return file_data
    except Exception as e:
        QgsMessageLog.logMessage('Error while converting {file}, Error: {er}'.format(file=file_data.fileName, er=str(e)),CATEGORY, Qgis.Info)

    return None
示例#11
0
    def gridByTxt(inputPath, outputPath, prjFile, cellSize, extend=None):
        """
        txt文件插值为栅格  TODO 统一输入输出文件规范

        :param inputPath: str 输入txt文件路径
        :param outputPath: str 输出栅格文件路径
        :param cellSize: float 输出栅格分辨率,单位:度
        :param extend: tuple 输出栅格范围 (ulx, uly, lrx, lry)
        :return:
        """

        # shp生成csv
        tempPath = BaseUtil.file_path_info(inputPath)[0]
        baseName = BaseUtil.file_path_info(inputPath)[1]
        # csvName = baseName + ".csv"
        # csvPath = os.path.join(tempPath, csvName)
        # ShapeUtil.pointToCsv(inputPath, csvPath, zField)

        # 生成vrt
        vrtName = baseName + ".vrt"
        vrtPath = os.path.join(tempPath, vrtName)
        dom = XmlUtil.createDom()
        roodNode = XmlUtil.createRootNode(dom, "OGRVRTDataSource")
        OGRVRTLayerNode = XmlUtil.appendNode(dom,
                                             roodNode,
                                             "OGRVRTLayer",
                                             attrInfo={"name": baseName})
        XmlUtil.appendNode(dom,
                           OGRVRTLayerNode,
                           "SrcDataSource",
                           text=inputPath)
        XmlUtil.appendNode(dom,
                           OGRVRTLayerNode,
                           "GeometryType",
                           text="wkbPoint")
        XmlUtil.appendNode(dom,
                           OGRVRTLayerNode,
                           "GeometryField",
                           attrInfo={
                               "encoding": "PointFromColumns",
                               "x": "field_1",
                               "y": "field_2",
                               "z": "field_3"
                           })
        XmlUtil.saveDom(dom, vrtPath)

        # 计算输出范围
        if extend:
            xMin = extend[0]
            xMax = extend[2]
            yMin = extend[3]
            yMax = extend[1]
            outputBounds = extend
            width = int((xMax - xMin) / cellSize)
            height = int((yMax - yMin) / cellSize)
        else:
            csvData = pd.read_csv(inputPath, header=None, sep=" ")
            xMin = csvData[0].min()
            xMax = csvData[0].max()
            yMin = csvData[1].min()
            yMax = csvData[1].max()
            outputBounds = (xMin, yMax, xMax, yMin)
            width = int((xMax - xMin) / cellSize)
            height = int((yMax - yMin) / cellSize)

        # 插值算法参数
        algo = 'invdist:power=2.0:smoothing=0.0:radius1=0.0:radius2=0.0:angle=0.0:max_points=0:min_points=0:nodata=0.0'
        options = gdal.GridOptions(outputType=gdal.GDT_Float32,
                                   outputBounds=outputBounds,
                                   layers=baseName,
                                   format='GTiff',
                                   algorithm=algo,
                                   width=width,
                                   height=height,
                                   outputSRS=prjFile)
        gdal.Grid(outputPath, vrtPath, options=options)
示例#12
0
print(outputDirectory)
print(outputTest)



wellNitrate = str(wellsShapefile)
print(wellNitrate)

#creating interpolation for well nitrate data

power_option = input("enter a k value > 0: ")
smoothing_option = input("enter smoothing variable >= 0")
algorithm_option = 'invdist:power='+power_option+':smoothing='+smoothing_option
print(algorithm_option)

gridOptions = gdal.GridOptions(format='Gtiff', algorithm=algorithm_option, zfield="nitr_ran") 
out = gdal.Grid(outputTest, wellNitrate, options=gridOptions)
print('interpolation complete')
print('reprojecting raster')
print('input file:', outputTest)
print('output file:', rasterWarpOut)

out = None
del out

warpOptions = gdal.WarpOptions(srcSRS='EPSG:4269', dstSRS='EPSG:4269')
print('warp options input')
warp_out = gdal.Warp(rasterWarpOut, outputTest, options=warpOptions)

print('raster reprojected')
print('changing resolution')