从numpy数组创建栅格,并从csv文件中获取值

时间:2019-11-05 20:50:50

标签: python numpy raster gdal

我有个土工。我想用csv表中的相应值替换栅格中的值。

栅格的类值为0到n,而csv具有栅格的每个n类的计算值(例如点密度)。 我想从csv中的相应值创建一个新的栅格

我正在使用GDAL和numpy。我尝试使用pandas,但是遇到了从csv到栅格pandas数据框提取值的问题。我将在相应的csv表的栅格列表上执行此操作。

下面是我的数据示例(一个栅格)

#Example raster array
[5 2 2 3
 0 3 1 4
 2 0 1 3]

#Corresponding csv table
  Class   Count  Density
    0       2       6
    1       2       9
    2       2       4
    3       3       9
    4       1       7
    5       1       2


#Output Raster (to take the corresponding density values, 
#i.e. if class = 0, then output raster = 6, the corresponding density value)
    [2 4 4 9
     6 9 9 7
     4 6 9 9]

我有用于从栅格创建数组并从数组写回栅格的代码。我从各个stackexchange网站发现了它。 我不知道如何构造循环以从新栅格中的csv获取值。 我下面的“ for循环”代码不完整。 谁能帮忙

import numpy, sys
from osgeo import gdal
from osgeo.gdalconst import *

inRst = gdal.Open(r"c:/Raster1.tif")
band = inRst.GetRasterBand(1)
rows = inRst.RasterYSize
cols = inRst.RasterXSize
rstr_arry = band.ReadAsArray(0,0,cols,rows)

# create the output image
driver = inRst.GetDriver()
#print driver
outRst = driver.Create(r"c:/NewRstr.tif", cols, rows, 1, GDT_Int32)
outBand = outRst.GetRasterBand(1)
outData = numpy.zeros((rows,cols), numpy.int32)

for i in range(0, rows):
    for j in range(0, cols):
        if rstr_arry[i,j] =  :
            outData[i,j] = 
        elif rstr_arry[i,j] = :
            outData[i,j] = 
        else:
            outData[i,j] = 


# write the data
outRst= outBand.WriteArray(outData, 0, 0)
# flush data to disk, set the NoData value and calculate stats
outBand.FlushCache()
outBand.SetNoDataValue(-99)
# georeference the image and set the projection
outDs.SetGeoTransform(inDs.GetGeoTransform())
outDs.SetProjection(inDs.GetProjection())

1 个答案:

答案 0 :(得分:0)

如果我没记错要实现的目标,则首先必须阅读csv文件,并创建Class值到Density值的映射。可以这样完成:

import csv

mapping = {}

with open('test.csv') as csv_file:
    csv_reader = csv.DictReader(csv_file)
    for row in csv_reader:
        mapping[int(row['Class'])] = int(row['Density'])

您将获得一个dict

{0: 6, 1: 9, 2: 4, 3: 9, 4: 7, 5: 2}

然后,您可以使用np.in1d创建需要替换的掩码矩阵,并使用np.searchsorted替换元素。在这样做之前,您将需要先平整栅格阵列,然后恢复其形状,然后再写回结果。 (替换numpy数组中的元素的替代方法可以在以下问题的答案中找到:Fast replacement of values in a numpy array

# Save the shape of the raster array
s = rstr_arry.shape
# Flatten the raster array
rstr_arry = rstr_arry.reshape(-1)
# Create 2D replacement matrix:
replace = numpy.array([list(mapping.keys()), list(mapping.values())])
# Find elements that need replacement:
mask = numpy.in1d(rstr_arry, replace[0, :])
# Replace them:
rstr_arry[mask] = replace[1, numpy.searchsorted(replace[0, :], rstr_arry[mask])]
# Restore the shape of the raster array
rstr_arry = rstr_arry.reshape(s)

然后您可以按计划执行几乎数据:

outBand.WriteArray(rstr_arry, 0, 0)
outBand.SetNoDataValue(-99)

outDs.SetGeoTransform(inRst.GetGeoTransform())
outDs.SetProjection(inRst.GetProjection())

outBand.FlushCache()

在示例数据上对其进行测试:

rstr_arry = np.asarray([
    [5, 2, 2, 3],
    [0, 3, 1, 4],
    [2, 0, 1, 3]])

mapping = {0: 6, 1: 9, 2: 4, 3: 9, 4: 7, 5: 2}

s = rstr_arry.shape
rstr_arry = rstr_arry.reshape(-1)
replace = numpy.array([list(mapping.keys()), list(mapping.values())])
mask = numpy.in1d(rstr_arry, replace[0, :])
rstr_arry[mask] = replace[1, numpy.searchsorted(replace[0, :], rstr_arry[mask])]
rstr_arry = rstr_arry.reshape(s)

print(rstr_arry)
# [[2 4 4 9]
#  [6 9 9 7]
#  [4 6 9 9]]