コード例 #1
0
 def gdal_grid(self):
     u_polygon = ogr.CreateGeometryFromWkt(self.u_Wkt)
     v_polygon = ogr.CreateGeometryFromWkt(self.v_Wkt)
     print[
         np.min(self.u_lon),
         np.min(self.u_lat),
         np.max(self.u_lon),
         np.max(self.u_lat)
     ]
     print[
         np.min(self.u_lon),
         np.min(self.u_lat),
         np.max(self.u_lon),
         np.max(self.u_lat)
     ]
     ds_u = gdal.Grid("", u_polygon.ExportToJson(), \
                      width=self.options.gridWidth, height=self.options.gridHeight, outputType=gdal.GDT_Float32,
                      outputSRS='EPSG:4326',
                      outputBounds=[np.min(self.u_lon), np.min(self.u_lat), np.max(self.u_lon), np.max(self.u_lat)], \
                      format='MEM', algorithm='nearest', noData=self.options.inputNoData)
     cols_u = ds_u.RasterXSize  # 获取文件的列数
     rows_u = ds_u.RasterYSize  # 获取文件的行数
     currentBand_u = ds_u.GetRasterBand(1)
     current_data_u = currentBand_u.ReadAsArray(0, 0, cols_u, rows_u)
     current_geotransf_u = ds_u.GetGeoTransform()  # 获取放射矩阵
     (current_lat_u, current_lon_u) = self.createXY(current_geotransf_u,
                                                    cols_u, rows_u)
     self.u_lon = current_lon_u
     self.u_lat = current_lat_u
     self.u_data = current_data_u[::-1]
     del ds_u
     # ===============================
     ds_v = gdal.Grid("", v_polygon.ExportToJson(), \
                      width=self.options.gridWidth, height=self.options.gridHeight, outputType=gdal.GDT_Float32,
                      outputSRS='EPSG:4326',
                      outputBounds=[np.min(self.u_lon), np.min(self.u_lat), np.max(self.u_lon), np.max(self.u_lat)], \
                      format='MEM', algorithm='nearest', noData=self.options.inputNoData)
     cols_v = ds_v.RasterXSize  # 获取文件的列数
     rows_v = ds_v.RasterYSize  # 获取文件的行数
     currentBand_v = ds_v.GetRasterBand(1)
     current_data_v = currentBand_v.ReadAsArray(0, 0, cols_v, rows_v)
     current_geotransf_v = ds_v.GetGeoTransform()  # 获取放射矩阵
     (current_lat_v, current_lon_v) = self.createXY(current_geotransf_v,
                                                    cols_v, rows_v)
     self.v_lon = current_lon_v
     self.v_lat = current_lat_v
     self.v_data = current_data_v[::-1]
     del ds_v
コード例 #2
0
def test_gdal_grid_lib_2():

    shape_ds = ogr.Open('/vsimem/tmp', update=1)
    shape_lyr = shape_ds.CreateLayer('test_gdal_grid_lib_2')
    dst_feat = ogr.Feature(feature_def=shape_lyr.GetLayerDefn())
    dst_feat.SetGeometry(ogr.CreateGeometryFromWkt('POINT(0 0 100)'))
    shape_lyr.CreateFeature(dst_feat)
    shape_ds = None

    for env_list in [[('GDAL_USE_AVX', 'NO'), ('GDAL_USE_SSE', 'NO')],
                     [('GDAL_USE_AVX', 'NO')], []]:

        for (key, value) in env_list:
            gdal.SetConfigOption(key, value)

        # Point strictly on grid
        ds1 = gdal.Grid('',
                        '/vsimem/tmp/test_gdal_grid_lib_2.shp',
                        format='MEM',
                        outputBounds=[-0.5, -0.5, 0.5, 0.5],
                        width=1,
                        height=1,
                        outputType=gdal.GDT_Byte)

        ds2 = gdal.Grid('',
                        '/vsimem/tmp/test_gdal_grid_lib_2.shp',
                        format='MEM',
                        outputBounds=[-0.4, -0.4, 0.6, 0.6],
                        width=10,
                        height=10,
                        outputType=gdal.GDT_Byte)

        gdal.SetConfigOption('GDAL_USE_AVX', None)
        gdal.SetConfigOption('GDAL_USE_SSE', None)

        cs = ds1.GetRasterBand(1).Checksum()
        if cs != 2:
            gdaltest.post_reason('fail')
            print(cs)
            return 'fail'

        cs = ds2.GetRasterBand(1).Checksum()
        if cs != 1064:
            gdaltest.post_reason('fail')
            print(cs)
            return 'fail'

    return 'success'
コード例 #3
0
    def get(self, factor_name, start_time):
        start_time = int2str(start_time)
        stations = select_data_to_geojson(factor_name, start_time)
        if os.path.exists('./Image_data//{factor}//{time}.png'.format(
                factor=factor_name, time=start_time.replace(':', '_'))):
            return "This time factor interpolation picture already exists"

        json_file = './geojson_data//{factor}//{time}.json'.format(
            factor=factor_name, time=start_time.replace(':', '_'))
        tif_file = './tif_data//{factor}//{time}.tif'.format(
            factor=factor_name, time=start_time.replace(':', '_'))
        write_content(json_file, stations, 'json')
        print("写入geojson中...")

        dataset = gdal.OpenEx(json_file)
        grid_opt = grid_option(
            factor_name,
            'd',
        )
        print("插值中...")
        gdal.Grid(tif_file, dataset, options=grid_opt)

        # 出图
        print("生成图片中...")
        img = Image.open('./state.png')
        tif = Image.open(tif_file)
        export_png(img,
                   factor_name,
                   tif,
                   'd',
                   img_name='{factor}//{time}'.format(factor=factor_name,
                                                      time=start_time.replace(
                                                          ':', '_')))
        print("出图成功!")
        return "The image is generated successfully. Please check the corresponding file"
コード例 #4
0
def test_gdal_grid_lib_3():

    wkt = 'POLYGON ((37.3495241627097 55.6901648563184 187.680953979492,37.349543273449 55.6901565410051 187.714370727539,37.3495794832707 55.6901531392856 187.67333984375,37.3496210575104 55.6901595647556 187.6396484375,37.3496398329735 55.6901716597552 187.596603393555,37.3496726900339 55.6901780852222 187.681350708008,37.3496793955565 55.6901829988139 187.933898925781,37.3496921360493 55.6901860225623 187.934280395508,37.3497162759304 55.6902037870796 187.435394287109,37.3497484624386 55.6902094566047 187.515319824219,37.3497618734837 55.6902241973661 190.329940795898,37.3497511446476 55.690238560154 190.345748901367,37.3497404158115 55.6902567026153 190.439697265625,37.3497142642736 55.6902650179072 189.086044311523,37.349688783288 55.6902608602615 187.763305664062,37.3496626317501 55.6902468754498 187.53678894043,37.3496378213167 55.6902412059301 187.598648071289,37.3496103286743 55.6902400720261 187.806274414062,37.3495902121067 55.6902313787607 187.759521484375,37.3495734483004 55.6902177719067 187.578125,37.349532879889 55.6902035980954 187.56965637207,37.3495161160827 55.6901939599008 187.541793823242,37.3495187982917 55.6901754394418 187.610427856445,37.3495241627097 55.6901648563184 187.680953979492))'
    polygon = ogr.CreateGeometryFromWkt(wkt)

    gdal.Grid('', polygon.ExportToJson(), \
        width=115, height=93, outputBounds=[37.3495161160827, 55.6901531392856, 37.3497618734837, 55.6902650179072], \
        format='MEM', algorithm='linear')

    return 'success'
コード例 #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 test_gdal_grid_lib_1():

    # Create an OGR grid from the values of n43.dt0
    ds = gdal.Open('../gdrivers/data/n43.dt0')
    geotransform = ds.GetGeoTransform()

    shape_drv = ogr.GetDriverByName('ESRI Shapefile')
    shape_ds = shape_drv.CreateDataSource('/vsimem/tmp')
    shape_lyr = shape_ds.CreateLayer('n43')

    data = ds.ReadRaster(0, 0, 121, 121)
    array_val = struct.unpack('h' * 121 * 121, data)
    for j in range(121):
        for i in range(121):
            wkt = 'POINT(%f %f %s)' % (
                geotransform[0] + (i + .5) * geotransform[1], geotransform[3] +
                (j + .5) * geotransform[5], array_val[j * 121 + i])
            dst_feat = ogr.Feature(feature_def=shape_lyr.GetLayerDefn())
            dst_feat.SetGeometry(ogr.CreateGeometryFromWkt(wkt))
            shape_lyr.CreateFeature(dst_feat)

    shape_ds.ExecuteSQL('CREATE SPATIAL INDEX ON n43')

    shape_ds = None

    spatFilter = None
    if ogrtest.have_geos():
        spatFilter = [-180, -90, 180, 90]

    # Create a GDAL dataset from the previous generated OGR grid
    ds2 = gdal.Grid(
        '',
        '/vsimem/tmp/n43.shp',
        format='MEM',
        outputBounds=[-80.0041667, 42.9958333, -78.9958333, 44.0041667],
        width=121,
        height=121,
        outputType=gdal.GDT_Int16,
        algorithm='nearest:radius1=0.0:radius2=0.0:angle=0.0',
        spatFilter=spatFilter)
    # We should get the same values as in n43.td0
    if ds.GetRasterBand(1).Checksum() != ds2.GetRasterBand(1).Checksum():
        print(
            'bad checksum : got %d, expected %d' %
            (ds.GetRasterBand(1).Checksum(), ds2.GetRasterBand(1).Checksum()))
        return 'fail'
    if ds2.GetRasterBand(1).GetNoDataValue() is not None:
        print('did not expect nodata value')
        return 'fail'

    ds = None
    ds2 = None

    return 'success'
コード例 #7
0
ファイル: interp.py プロジェクト: noaa-ocs-modeling/thyme
def gdal_interpolate_to_regular_grid(values, x_in, y_in, x_out, y_out):
    """Linear-interpolate irregularly-spaced values to regular grid.

    Uses `gdal.Grid` for linear interpolation.

    NOTE: as of 9/2019, this GDAL interpolation is not working, seemingly
    because of a bug with swig: https://github.com/OSGeo/gdal/issues/1677

    Args:
        values: `tuple` or `list` of `numpy.ma.masked_array`s containing values
            to be interpolated to the output grid.
        x_in: `numpy.ndarray` containing x-position of each input value. Length
            of this array must match the length of each array in `values`.
        y_in: `numpy.ndarray` containing y-position of each input value. Length
            of this array must match the length of each array in `values`.
        x_out: `numpy.ndarray` containing x-positions of output grid columns.
        y_out: `numpy.ndarray` containing y-positions of output grid rows.

    Returns:
        A `tuple` containing the interpolated values for each input array,
        corresponding with the order specified in `values`.
    """
    srs = osr.SpatialReference()
    srs.SetWellKnownGeogCS('WGS84')
    ds = gdal.GetDriverByName('Memory').Create('', 0, 0, 0, gdal.GDT_Float32)
    layer = ds.CreateLayer('irregular_points', srs=srs, geom_type=ogr.wkbPoint)

    def field_name(x):
        return 'field_{}'.format(x)

    for n in range(len(values)):
        layer.CreateField(ogr.FieldDefn(field_name(n + 1), ogr.OFTReal))

    for i in range(len(x_in)):
        point = ogr.Geometry(ogr.wkbPoint)
        point.AddPoint(x_in[i], y_in[i])
        feature = ogr.Feature(layer.GetLayerDefn())
        feature.SetGeometry(point)
        for n in range(len(values)):
            layer.SetField(field_name(n + 1), values[n][i])
        layer.CreateFeature(feature)

    calc_values = []
    for n in range(len(values)):
        calc_values.append(
            gdal.Grid('.tif'.format(field_name(n + 1)),
                      ds,
                      format='MEM',
                      width=len(x_out),
                      height=len(y_out),
                      algorithm='linear:nodata=0.0',
                      zfield=field_name(n + 1)).ReadAsArray())

    return tuple(calc_values)
コード例 #8
0
def createGrid(inputNCPath, outputTifPath, algorithm='invdist'):
    #output = gdal.Grid(outputTifPath,inputNCPath,algorithm=algorithm)
    output = gdal.Grid(
        outputTifPath,
        inputNCPath,
        algorithm=
        'invdist:power=2.0:smoothing=0.0:radius1=2000.0:radius2=4000.0:angle=0.0:max_points=500:min_points=1:nodata=0.0',
        format="NetCDF",
        width=50,
        height=50)
    output.FlushCache()
    return outputTifPath
コード例 #9
0
    def gdal_grid(self):
        polygon = ogr.CreateGeometryFromWkt(self.myWkt)
        grid_data = gdal.Grid('', polygon.ExportToJson(), \
                              width=self.pCols, height=self.pRows, outputType=gdal.GDT_Float32, outputSRS='EPSG:4326',
                              outputBounds=[self.lon0, self.lat0, self.lon1, self.lat1], noData=0.0, \
                              # format='MEM', algorithm='invdist')
                              # format='MEM', algorithm='nearest')


                              format='MEM', algorithm='invdistnn')
        # format='GTiff', algorithm='linear')

        self.grid_data = grid_data
コード例 #10
0
ファイル: GdalUtil.py プロジェクト: CodePrimer/idea
    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)
コード例 #11
0
ファイル: GdalUtil.py プロジェクト: CodePrimer/idea
    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)
コード例 #12
0
def convertCsv2Tif(inputPath, outputPath, inputFilename, vrtTemplateString):
    """
    Convert CSV to GeoTIFF using gdal.Grid. Output will be written to <outputPath>/<inputFilename>.tif

    CSV is decribed by the template given as parameter. The following tokens are replaced:
        - $NAME$ by inputFilename without extension
        - $FILENAME$ by inputFilename
        - $PATH$ by the absolute inputPath

    :param inputPath: path to input CSV
    :param outputPath: path to where the output GeoTIFF is written
    :param inputFilename: filename (without path!) of input CSV
    :param vrtTemplateString: template string for VRT file
    """
    # make input and output path absolute in case it is not already
    fullInputPath = os.path.abspath(inputPath)
    fullOutputPath = os.path.abspath(outputPath)

    fullVrtFilename = fullOutputPath + "/" + inputFilename + ".vrt"

    logging.info("Opening '{}' for writing...".format(fullVrtFilename))

    with open(fullVrtFilename, "w+") as currentVrtFile:
        logging.info("writing template to: {}".format(fullVrtFilename))

        currentVrtString = vrtTemplateString \
            .replace("$NAME$", os.path.splitext(inputFilename)[0]) \
            .replace("$FILENAME$", inputFilename) \
            .replace("$PATH$", fullInputPath)

        currentVrtFile.write(currentVrtString)

        logging.info("Closing: {}".format(fullVrtFilename))
        currentVrtFile.flush()
    del currentVrtFile

    logging.info("Converting CSV to TIF")
    gdal.Grid(
        srcDS=fullVrtFilename,
        destName=fullOutputPath + "/" + inputFilename + ".tif"
        # as GDAL will interpolate between the points of the CSV, here we make sure, that no values are created,
        # that has not been in the original CSV file.
        ,
        algorithm="nearest:radius1=0.0:radius2=0.0:angle=0.0:nodata=9999.0")
    os.remove(fullVrtFilename)
    logging.info("done")
コード例 #13
0
ファイル: GdalUtil.py プロジェクト: CodePrimer/idea
    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)
コード例 #14
0
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
コード例 #15
0
ファイル: GdalUtil.py プロジェクト: CodePrimer/idea
    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)
コード例 #16
0
ファイル: GdalUtil.py プロジェクト: CodePrimer/idea
    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)
コード例 #17
0
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
コード例 #18
0
def rasterize_shapefiles(algorithm, short_side, test=False):
    """
    Rasterize yield datasets with inbound algorithm. The generated image preserves the dimensions of the corresponding Block shape.

    The list of applicable algorithms can be found in http://www.gdal.org/gdal_grid.html.

    Args:

        algorithm: The algorithm with which to interpolate the data points. 
        short_side: The length of the shorter side of the generated raster.
        test: Optional. Whether the function call is for testing or not. Default is False.
    """
    print("Processing")
    start = time.time()
    shape_paths = [
        os.path.join(data_settings.YIELD_SHAPES_DIR, f)
        for f in os.listdir(data_settings.YIELD_SHAPES_DIR) if '.shp' in f
    ]

    for i, shape_path in enumerate(shape_paths):

        start_grid = time.time()

        target_path = os.path.join(data_settings.YIELD_RASTERS_DIR,
                                   "grid_{:03d}.tif".format(i))
        print("\tFrom\t{}\n\tTo\t{}".format(shape_path, target_path))

        try:

            shape = ogr.Open(shape_path)
            layer_name = shape.GetLayer(0).GetName()
            x_min, x_max, y_min, y_max = shape.GetLayer(0).GetExtent()

        finally:

            shape = None

        x_to_y = (x_max - x_min) / (y_max - y_min)
        x_ratio = max(1, x_to_y)
        y_ratio = min(1, x_to_y)

        try:

            raster = gdal.Grid(destName=target_path,
                               srcDS=shape_path,
                               format='GTiff',
                               noData=0.0,
                               width=short_side * x_ratio,
                               height=short_side / y_ratio,
                               outputType=gdal.GDT_Float32,
                               outputSRS='EPSG:4326',
                               outputBounds=[x_min, y_max, x_max, y_min],
                               zfield='Mass_Yield',
                               layers=[layer_name],
                               algorithm=algorithm)

            delta_grid = time.time() - start_grid
            print("\t\tDone in {:.0f} m {:.0f} s".format(
                delta_grid // 60, delta_grid % 60))

            arr = raster.ReadAsArray()

            plt.rcParams['figure.figsize'] = 2, 2
            plt.imshow(arr, cmap='gray', vmin=1500, vmax=15000)
            plt.axis('off')
            plt.show()

        finally:

            raster = None

            if test:
                break

    delta = time.time() - start
    print("Processing done in {:.0f} m {:.0f} s".format(
        delta // 60, delta % 60))
コード例 #19
0
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
コード例 #20
0
ファイル: csv2img_interp.py プロジェクト: OKzsy/work
def main():

    temp_dir = os.path.join(tempfile.gettempdir(), 'gdal_interp')
    if not os.path.exists(temp_dir):
        os.mkdir(temp_dir)

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

    prov_dict = {
        'SX1': [109.89, 34.3065, 115.00, 40.912],
        'SX2': [105.28, 31.59, 111.41, 39.71],
        'HB': [112.0, 36.0, 120.01, 43.01],
        'HN': [110.0, 31.0, 117.0, 36.0],
        'NX': [104.0, 35.0, 107.7, 39.5],
        'SC': [97.0, 26.0, 109.0, 34.5],
        'SD': [114.5, 34.0, 123.0, 38.5]
    }
    #
    # prov = os.path.splitext(os.path.basename(out_pm25))[0].split('_')[-2]
    # city = os.path.splitext(os.path.basename(out_pm25))[0].split('_')[-1]

    with open(csv_file, 'r') as in_csv:
        csv_str = in_csv.readlines()

    head_line = csv_str[0].replace('\n', '').split(',')

    value_id = head_line[-3:]
    lon_ind = head_line.index('Lon')
    lat_ind = head_line.index('Lat')

    for ivalue in range(len(value_id)):
        ipm_csvfile = os.path.join(
            temp_dir, 'qixiang_interp_%s.csv' % (value_id[ivalue]))

        value_ind = head_line.index(value_id[ivalue])

        with open(ipm_csvfile, 'w', newline='') as ipm_csv:
            ipmcsv_writer = csv.writer(ipm_csv, dialect=("excel"))
            ipmcsv_writer.writerow(['Lon', 'Lat', 'Value'])
            for idata in csv_str[1:]:
                idata = idata.replace('\n', '').split(',')

                ipmcsv_writer.writerow(
                    [idata[lon_ind], idata[lat_ind], idata[value_ind]])

            # ipmcsv_writer = None
            # ipm_csv.close()

        ivrt_file = os.path.join(
            temp_dir, '%s_csv2vrt.vrt' %
            (os.path.splitext(os.path.basename(ipm_csvfile))[0]))

        ivrt_data = '<OGRVRTDataSource>\n' \
                    '  <OGRVRTLayer name="%s">\n' \
                    '    <SrcDataSource>%s</SrcDataSource>\n' \
                    '    <GeometryType>wkbPoint</GeometryType>\n' \
                    '    <LayerSRS>WGS84</LayerSRS>\n' \
                    '    <GeometryField encoding="PointFromColumns" x="Lon" y="Lat" z="%s"/>\n' \
                    '  </OGRVRTLayer>\n' \
                    '</OGRVRTDataSource>' % \
                    (os.path.splitext(os.path.basename(ipm_csvfile))[0], ipm_csvfile, 'Value')

        with open(ivrt_file, 'wb') as ivrt:
            ivrt.write(ivrt_data.encode("utf-8"))

        iout_file = os.path.join(
            out_dir,
            '%s.tif' % (os.path.splitext(os.path.basename(ipm_csvfile))[0]))

        tiff_driver = gdal.GetDriverByName("GTiff")
        if os.path.exists(iout_file):
            tiff_driver.Delete(iout_file)

        in_prov = 'HN'

        prov_extent = prov_dict[in_prov]
        prov_width = (prov_extent[2] - prov_extent[0]) / 0.005 + 1
        prov_height = (prov_extent[3] - prov_extent[1]) / 0.005 + 1

        gdal.Grid(iout_file,
                  ivrt_file,
                  format="GTiff",
                  outputType=gdal.GDT_Float32,
                  algorithm='invdist:power=2.0:smoothing=0.0',
                  noData=0,
                  width=prov_width,
                  height=prov_height,
                  outputBounds=prov_extent)

        clip_file = os.path.join(
            out_dir, '%s_sub.tif' %
            (os.path.splitext(os.path.basename(ipm_csvfile))[0]))

        clip(iout_file, shp_file, clip_file)
        os.remove(iout_file)

    shutil.rmtree(temp_dir)
コード例 #21
0
                    df_final['NZTMX'] = X
                    df_final['NZTMY'] = Y
                    #-Convert UTC to NZ timezone
                    df_final['time'] = df_final['time'].dt.tz_localize('utc')
                    df_final['time'] = df_final['time'].dt.tz_convert(nzTimeZones)
                    df_final['time'] = df_final['time'].dt.tz_localize(None)
                    #-Get NZ time of the forecast
                    forecastTime = df_final.iloc[0,0]
                    df_final.drop('time', axis=1, inplace=True)
                    #-Write to csv before converting to GTiff
                    df_final.to_csv(csvFile, index=False)
                    df_final = None
                    #-Get the UTC of the nc filename and convert to a datestime of NZ time zone
                    ncFileStr = ncF[-13:].split('.nc')[0]
                    year = int(ncFileStr[:4])
                    month = int(ncFileStr[4:6])
                    day = int(ncFileStr[6:8])
                    hour = int(ncFileStr[8:10])
                    fileTime = pd.Timestamp(year=year, month=month, day=day, hour=hour, tz='utc').tz_convert(nzTimeZones)
                    fileTime = fileTime.tz_localize(None)
                    #-Convert csv to GTiff
                    tifOut = os.path.join(fProdTifDir, str(i) + 'h_' + forecastTime.strftime('%Y%m%d_%H%M') + '_' + fileTime.strftime('%Y%m%d_%H%M')  + '.tif')
                    z = gdal.Grid(tifOut, vrtFile, width = cols, height=rows, algorithm='linear',format='GTiff', outputSRS='EPSG:2193', 
                               spatFilter=(xmin,ymin,xmax,ymax), zfield='prec')
                    z=None
                i+=1
                
        except:
            errorLog.write('%s could not be processed because of unknown file format\n' %ncF)
    errorLog.close()
 data_time_str = "%s年%s月%s日%s时" % (year, month, day_str, clock_str)
 data_list = data_reader(db_path, data_time_str)
 if len(data_list) > 0:
     result_time = str(year) + str(month) + day_str + clock_str
     result_dir = os.path.join(output_dir, result_time)
     if not os.path.exists(result_dir):
         os.mkdir(result_dir)
     result_shp_path = os.path.join(result_dir,
                                    result_time + '.shp')
     result_ras_path = os.path.join(result_dir,
                                    result_time + '.tif')
     list2shp(data_list, result_shp_path)
     # convert shapefile to raster using gdal
     raw_raster = os.path.join(result_dir, 'raw_ratser.tif')
     gdal.Grid(raw_raster,
               result_shp_path,
               format='GTiff',
               zfield='Pm2.5',
               outputBounds=[73.4, 54, 135.1, 18.1],
               algorithm='invdist:power=2.0:smoothing=0.1')
     # clip to china extent using gdal
     gdal.Warp(result_ras_path,
               raw_raster,
               format="GTiff",
               warpOptions="NUM_THREADS=ALL_CPUS",
               warpMemoryLimit=1024,
               resampleAlg="cubic",
               multithread=True,
               cutlineDSName=china_shp,
               cropToCutline=True)
     os.remove(raw_raster)
コード例 #23
0
def main(input_xlsx, out_raster, china_shp):
    # input_xlsx = 'pm25_geoed.xlsx'
    # use openpyxl so we can access by field name
    wb = openpyxl.load_workbook(input_xlsx)
    ws = wb.get_sheet_by_name('pm25')
    max_row = ws.max_row

    gdal.AllRegister()
    # 为了支持中文路径,请添加下面这句代码
    gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "NO")

    # 为了使属性表字段支持中文,请添加下面这句
    gdal.SetConfigOption("SHAPE_ENCODING", "CP936")

    # 注册所有的驱动
    ogr.RegisterAll()

    # set up the shapefile driver
    driver = ogr.GetDriverByName("ESRI Shapefile")

    # create the data source
    output_shp = './geoed_excel_to_raster/air_pollution.shp'
    data_source = driver.CreateDataSource(output_shp)

    # create the spatial reference, WGS84
    srs = osr.SpatialReference()
    srs.ImportFromEPSG(4326)

    # create the layer
    layer = data_source.CreateLayer("surveillance_stations", srs, ogr.wkbPoint)

    # Add the fields we're interested in
    field_name = ogr.FieldDefn("Name", ogr.OFTString)
    field_name.SetWidth(24)
    layer.CreateField(field_name)

    field_city = ogr.FieldDefn("City", ogr.OFTString)
    field_city.SetWidth(24)
    layer.CreateField(field_city)

    field_airquality = ogr.FieldDefn("AQ", ogr.OFTString)
    field_airquality.SetWidth(24)
    layer.CreateField(field_airquality)

    field_primepollution = ogr.FieldDefn("PP", ogr.OFTString)
    field_primepollution.SetWidth(24)
    layer.CreateField(field_primepollution)

    field_time = ogr.FieldDefn("Time", ogr.OFTString)
    field_time.SetWidth(24)
    layer.CreateField(field_time)

    layer.CreateField(ogr.FieldDefn("Latitude", ogr.OFTReal))
    layer.CreateField(ogr.FieldDefn("Longitude", ogr.OFTReal))
    layer.CreateField(ogr.FieldDefn("Pm2.5", ogr.OFTInteger))
    layer.CreateField(ogr.FieldDefn("Pm10", ogr.OFTInteger))
    layer.CreateField(ogr.FieldDefn("CO", ogr.OFTReal))
    layer.CreateField(ogr.FieldDefn("NO2", ogr.OFTInteger))
    layer.CreateField(ogr.FieldDefn("O3/1HOUR", ogr.OFTInteger))
    layer.CreateField(ogr.FieldDefn("O3/8HOUR", ogr.OFTInteger))
    layer.CreateField(ogr.FieldDefn("SO2", ogr.OFTInteger))

    # Process the xlsx file and add the attributes and features to the shpfile
    for i in range(2, max_row + 1):
        lon = ws['O' + str(i)].value
        lat = ws['P' + str(i)].value

        if (lon is not None) and (lat is not None):
            # create the feature
            feature = ogr.Feature(layer.GetLayerDefn())
            # Set the attributes using the values from the delimited text file
            feature.SetField("Name", ws['C' + str(i)].value)
            feature.SetField("City", ws['B' + str(i)].value)
            feature.SetField("AQ", ws['E' + str(i)].value)
            feature.SetField("PP", ws['F' + str(i)].value)
            feature.SetField("Time", ws['N' + str(i)].value)
            feature.SetField("Latitude", ws['P' + str(i)].value)
            feature.SetField("Longitude", ws['O' + str(i)].value)
            feature.SetField("Pm2.5", ws['G' + str(i)].value)
            feature.SetField("Pm10", ws['H' + str(i)].value)
            feature.SetField("CO", ws['I' + str(i)].value)
            feature.SetField("NO2", ws['J' + str(i)].value)
            feature.SetField("O3/1HOUR", ws['K' + str(i)].value)
            feature.SetField("O3/8HOUR", ws['L' + str(i)].value)
            feature.SetField("SO2", ws['M' + str(i)].value)
            # create the WKT for the feature using Python string formatting
            wkt = "POINT(%f %f)" % (float(lon), float(lat))

            # Create the point from the Well Known Txt
            point = ogr.CreateGeometryFromWkt(wkt)

            # Set the feature geometry using the point
            feature.SetGeometry(point)
            # Create the feature in the layer (shapefile)
            layer.CreateFeature(feature)
            # Dereference the feature
            feature = None

    # Save and close the data source
    data_source = None

    # convert shapefile to raster using gdal
    raw_raster = './geoed_excel_to_raster/raw_ratser.tif'
    gdal.Grid(raw_raster,
              output_shp,
              format='GTiff',
              zfield='pm2.5',
              outputBounds=[73.4, 54, 135.1, 18.1],
              algorithm='invdist:power=2.0:smoothing=0.1')
    # clip to china extent using gdal
    gdal.Warp(out_raster,
              raw_raster,
              format="GTiff",
              warpOptions="NUM_THREADS=ALL_CPUS",
              warpMemoryLimit=1024,
              resampleAlg="cubic",
              multithread=True,
              cutlineDSName=china_shp,
              cropToCutline=True)
    os.remove(raw_raster)
コード例 #24
0
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')
コード例 #25
0
ファイル: GdalUtil.py プロジェクト: CodePrimer/idea
    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)
コード例 #26
0
def convert_xyz_to_raster(x, y, z, res, method, outfile):
    """Function to produce raster from point data
	presented with X, Y, Z Coordinates

	NOTE: This function still has some issues,
		particularly with outputting large rasters
		(which have to be cut into smaller chunks).

	:param float x: x coordinates
	:param float y: y coordinates
	:param float z: z coordinates or other variable
		that you want to display in the raster
	:param float res: resolution of the output 
		raster in relevant projection units
	:param str outfile: file to save raster.
		NOTE: if size of raster is large, outfile
		will be split into numbered raster files
	"""

    bean = 500  # Minimum size (square) of output raster before cutting it into smaller chunks
    # This is to resolve an issue with the slowness of gdal.Grid

    # Determine size of output grid based on desired resolution
    w1 = np.ceil(np.ptp(x) / res)
    h1 = np.ceil(np.ptp(y) / res)
    # Define the algorithm for extrapolating data:
    alg = method + ':radius1=' + str(res / 2) + ':radius2=' + str(
        res / 2) + ':nodata=-9999'

    # As long as output raster size is less than bean x bean, output one raster:
    if w1 * h1 < bean**2:

        xyz = np.column_stack((x, y, z))
        # Sort by x, then by y:
        xyz = xyz[xyz[:, 0].argsort()]
        xyz = xyz[xyz[:, 1].argsort(kind='mergesort')]

        write_csv_file('grid.csv', xyz)  # Write temporary xyz file
        #Convert to Grid
        gdal.Grid(outfile, 'grid.vrt', width=w1, height=h1, algorithm=alg)
        os.remove('./grid.csv')  # Remove temporary xyz file

        print
        print('Produced single map with filename {}'.format(outfile))
        print
    # If size larger than bean x bean, split large grid into subgrids for faster processing:
    else:
        #merge_command = ['', '-o', outfile] #attempting merging rasters; this never worked out
        # Cutting into squares size bean x bean:
        Wind = int(np.ceil(w1 / bean))
        Hind = int(np.ceil(h1 / bean))
        nn = 0
        for W in range(Wind):
            x1 = W * bean * res + np.min(x)
            x2 = (W + 1) * bean * res + np.min(x)
            xind = (x >= x1) & (x < x2)
            if W < Wind:
                wt = bean
            else:
                wt = w1 % bean
            for H in range(Hind):
                y1 = (H) * bean * res + np.min(y)
                y2 = (H + 1) * bean * res + np.min(y)
                yind = (y >= y1) & (y < y2)
                ind = xind & yind

                # Check for data within current square:
                if any(ind):
                    xyz_temp = np.column_stack((x[ind], y[ind], z[ind]))
                    xyz_temp = xyz_temp[xyz_temp[:, 0].argsort()]
                    xyz = xyz_temp[xyz_temp[:, 1].argsort(kind='mergesort')]
                    if H < Hind:
                        ht = bean
                    else:
                        ht = h1 % bean

                    out = outfile[:-4] + str(nn +
                                             1) + '.tif'  # Numbered outfile
                    write_csv_file('grid.csv', xyz_temp)
                    gdal.Grid(out,
                              'grid.vrt',
                              width=wt,
                              height=ht,
                              algorithm=alg)
                    os.remove('./grid.csv')  #Remove temporary xyz file
                    #merge_command.append(out)
                    nn = nn + 1
        print
        print 'Dataset too large for single grid production'
        print('Produced {} files with the following names: '.format(nn))
        for n in range(nn - 1):
            print('{}'.format(outfile[:-4] + str(n + 1) + '.tif'))
        print