0

我必须对 30 个大 shapefile(>800 万个单元格)的每 62 列进行栅格化,从而产生 1860 个栅格。

我能够在 R 中以较小的形状(约 8000 个特征)轻松快速地做到这一点。然而,当只是尝试在 R 中加载这个大形状时,我花了 2 个小时,16G RAM 和 47% 的 64G 交换。调用 rasterize 函数时,由于内存问题,无法运行。但是我发现 QGIS 可以非常快地做到这一点,但是对于每列一次,这将花费我的生命来运行它。我尝试在 QGIS 中使用带有循环的 Python 控制台来遍历列,但没有成功。

from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.core import *
from qgis.gui import *
import processing
import sys

layer = "path/to/my.shp"
iface.addVectorLayer(layer, "pam", "ogr")

attrs = layer.attributes()

extent = layer.extent()
xmin = extent.xMinimum()
xmax = extent.xMaximum()
ymin = extent.yMinimum()
ymax = extent.yMaximum()

for n in attrs:
    processing.runalg("gdalogr:rasterize",
                   {"INPUT":layer,
                   "FIELD":n,
                   "DIMENSIONS":0,
                   "WIDTH":0.008333,
                   "HEIGHT":0.008333,
                   "RAST_EXT":"%f,%f,%f,%f"% (xmin, xmax, ymin, ymax),
                   "TFW":1,
                   "RTYPE":5,
                   "NO_DATA":0,
                   "COMPRESS":0,
                   "JPEGCOMPRESSION":1,
                   "ZLEVEL":1,
                   "PREDICTOR":1,
                   "TILED":False,
                   "BIGTIFF":2,
                   "EXTRA": '',
                   "OUTPUT":output/dir/test.tif})

我首先收到以下错误:

Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/tmp/tmpMAXfPw.py", line 11, in <module>
    attrs = layer.attributes()
AttributeError: 'str' object has no attribute 'attributes'

然后,似乎 gdalogr:rasterize 无法处理FIELD = n,。我对么?

请你帮我处理这段代码好吗?谢谢!

4

1 回答 1

0

In layer you have only defined the path to a fine with a string. A simple Python string has no attribute 'attributes'. You need to actually create the layer first:

layer_path = "/home/klab-guest/Documents/shared-klab/PAM/Grid/grid_pam_1988.shp"
layer = iface.addVectorLayer(layer_path, "pam", "ogr")
attrs = layer.attributes()
于 2019-01-24T11:15:19.353 回答