栅格栅格化功能:无法插入正确的几何形状

时间:2019-06-19 10:47:18

标签: python geopandas rasterio fiona

尝试使用Rasterio库中的栅格化功能对具有特定属性的折线shapefile进行栅格化。此函数需要一个可迭代的包含(几何,值)元组,并且该几何是类似于GeoJSON的对象(请参见documentation)。可以使用fiona或geopandas提取此几何,我已经读过this question并尝试过(因此,对于geopandas),但是几何不正确,因为我使用“ is_valid_geom”进行了测试,结果为“ false”这似乎合乎逻辑,因为当我打印此几何图形时,它显示:<generator object <genexpr> at 0x000001903856C048>。我也这样尝试了Fiona:

shapeee = fiona.open(shapefile) 
geom_fiona = [shapes['geometry'] for shapes in shapeee]
attrib_fiona = [shapes['properties']['OBJECTID'] for shapes in shapeee] # attribute
print(features.is_valid_geom(geom_fiona)) # FALSE

它也返回“ false”,但我不明白为什么,因为几何形状似乎是正确的:

print(geom_fiona)
"[{'type': 'LineString', 'coordinates': [(177421.98120000213, 142766.21020000055), (177409.1555000022, 142781.71609999985), (177392.76659999788, 142801.65300000086) and so on..."

所以最后,当我尝试进行栅格化时,我没有收到任何错误消息,但是输出是空白图像,所有值都为0 ...栅格化代码:

tuples = []
for i in range(0,len(geom_fiona)-1,1):
    tuples.append([geom_fiona[i],attrib_fiona[i]])
burned = features.rasterize(tuples, out_shape=new_dataset.shape,default_value=-99,dtype=rasterio.float64)

注意:我已经读过this,并且我的shapefile和用于分级的栅​​格之间的投影相同

任何人都知道可能是什么问题?

1 个答案:

答案 0 :(得分:0)

几何实际上是有效的! features.is_valid_geom(geom_fiona)返回了false,因为我将整个几何作为参数。写入features.is_valid_geom(geom_fiona[0])返回True。但是光栅化仍然是不正确的,并且什么都没有改变...