是否可以在QGIS处理算法中使用存储层?


12

我正在构建一个QGIS插件,该插件连接到局域网中的MySQL数据库,然后将其中一个表的子集添加到内存层中;该子集基于数据货币(仅对进行测量的每个位置进行最新观察)。此存储层已成功创建。

但是,然后我想运行一些地理处理算法,而在其中任何一个中使用内存层都遇到了麻烦。

    self.stationuri = "point?crs=epsg:4326&field=id:integer&field={}:double&index=yes".format(self.cb_field.currentText())
    self.vlayer = QgsVectorLayer(self.stationuri,"scratch","memory")
    if not self.vlayer.isValid():
        raise Exception("Failed to create in-memory layer")
    self.vlayer.startEditing()
    for i,r in enumerate(result): # Result is row-by-row result of SQL query
        # Add features
        ...
    self.vlayer.commitChanges()
    self.vlayer.updateExtents()
    # Add layer to map
    QgsMapLayerRegistry.instance().addMapLayer(self.vlayer)
    # Layer is successfully added to map with all features and geometry
    # BELOW IS WHERE IT FALLS APART
    try:
        processing.runandload("gdalogr:gridinvdist",self.vlayer,self.cb_field.currentText(),2,0,0,0,0,0,0,0,'Float32',None) # None = in-memory output; I get the same error if I specify a string path and filename.
    except Exception, e:
        raise e

没有引发异常,但是没有产生任何输出或未将其添加到TOC中,但是在以下日志中进行了记录processing.log

INFO|Mon May 04 2015 11:28:23|GDAL execution console output|/bin/sh: 1: /tmp/processing/bbebe7599c83446d9c2b03a251879657/OUTPUT.tif: not found|/bin/sh: 1: -zfield: not found||FAILURE: Source datasource is not specified.|Usage: gdal_grid [--help-general] [--formats]|    [-ot {Byte/Int16/UInt16/UInt32/Int32/Float32/Float64/|          CInt16/CInt32/CFloat32/CFloat64}]|    [-of format] [-co "NAME=VALUE"]|    [-zfield field_name] [-z_increase increase_value] [-z_multiply multiply_value]|    [-a_srs srs_def] [-spat xmin ymin xmax ymax]|    [-clipsrc <xmin ymin xmax ymax>|WKT|datasource|spat_extent]|    [-clipsrcsql sql_statement] [-clipsrclayer layer]|    [-clipsrcwhere expression]|    [-l layername]* [-where expression] [-sql select_statement]|    [-txe xmin xmax] [-tye ymin ymax] [-outsize xsize ysize]|    [-a algorithm[:parameter1=value1]*]    [-q]|    <src_datasource> <dst_filename>||Available algorithms and parameters with their's defaults:|    Inverse distance to a power (default)|        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|    Moving average|        average:radius1=0.0:radius2=0.0:angle=0.0:min_points=0:nodata=0.0|    Nearest neighbor|        nearest:radius1=0.0:radius2=0.0:angle=0.0:nodata=0.0|    Various data metrics|        <metric name>:radius1=0.0:radius2=0.0:angle=0.0:min_points=0:nodata=0.0|        possible metrics are:|            minimum|            maximum|            range|            count|            average_distance|            average_distance_pts|

重要的部分似乎是FAILURE: Source datasource is not specified.但是self.vlayer.isValid() == True,所以我看不到输入内容有什么问题。我试图取代self.vlayer'memory:scratch'中调用processing.runandload,但后来我得到印刷以下错误控制台(但没有提出)Error: Wrong parameter value: memory:scratch

通过QGIS GUI运行此程序并使用下拉菜单选择scratch位于TOC中的图层时,我遇到相同的问题。无论将输出栅格指定为内存中还是指定磁盘上的位置,都会发生这种情况。

这个问题似乎很相似,但是他们的解决方案是在使用TOC之前将其添加到TOC中。我已经在这样做了,但是错误仍然存​​在。

我以为这是内存层和QGIS地理处理算法的普遍问题,但是以下工作毫无问题:

processing.runandload("qgis:fixeddistancebuffer",self.vlayer, 500, 5, True, "output_buffer.shp")

我究竟做错了什么?为什么在某些处理算法中无法“指定”我的内存源数据集?

编辑:这里的源代码gdalogr:gridinvdist,如果这是有用的。

Answers:


4

似乎不能将内存层用作GDAL / OGR处理脚本的输入,因为处理无法正确准备要与ogr2ogr一起使用的数据。例如,这就是为什么QGIS缓冲工具可以工作但GDAL / OGR缓冲工具失败的原因:

Algorithm Buffer vectors starting...
GDAL command:
cmd.exe /C ogr2ogr.exe "C:\Users\anita\AppData\Local\Temp\processing70e5e0852cb9456ba2e3780f8386122e\86d237c8f41443f58a230a8133172047\OUTPUTLAYER.shp" point?crs=EPSG:4326&memoryid={6772bccd-f55d-461d-aff6-6271ded02eea} point?crs=EPSG:4326&memoryid={6772bccd-f55d-461d-aff6-6271ded02eea} -dialect sqlite -sql "SELECT ST_Buffer( geometry , 1000 ),* FROM 'point?crs=EPSG:4326&memoryid={6772bccd-f55d-461d-aff6-6271ded02eea}' " 
GDAL command output:
FAILURE: 
Unable to open datasource `point?crs=EPSG:4326' with the following drivers. 
-> JP2ECW 
-> OCI 
-> SOSI 
...

处理将必须以某种方式准备数据(将其保存到文件中),然后将其提供给GDAL / OGR工具。

我打开了一张票:不能通过OGR工具使用存储层


2

如文档http://docs.qgis.org/2.14/es/docs/user_manual/processing/console.html中所述,这是正确的方法

下一个代码将在内存中使用,除了最后一个正在加载的代码

MDT=path/mdt.tif
drain=processing.runalg("grass:r.drain",MDT,"",(pun),False,False,False,"%f,%f,%f,%f"% (xmin, xmax, ymin, ymax),0,-1,0.00100,None)
vect=processing.runalg("grass:r.to.vect",drain['output'],0,False,"%f,%f,%f,%f"% (xmin, xmax, ymin, ymax),0,None)
bu=processing.runalg("qgis:fixeddistancebuffer",vect['output'],Metros_afecta,1,False,None)
buf=bu['OUTPUT']
bufe= QgsVectorLayer(buf,"area", "ogr")
#the last load the layer 
QgsMapLayerRegistry.instance().addMapLayers([bufe])

在这种情况下,processing.runalg返回一个字典bu ['OUTPUT'] OUTPUT是密钥,并且值是一个临时路径,您可以通过processeing.alghelp(“ name processing”)来查看该密钥,alghelp(“ grass :r.drain“)

返回

processing.alghelp("grass:r.drain")
ALGORITHM: r.drain - Traces a flow through an elevation model on a raster map.
input <ParameterRaster>
coordinate <ParameterString>
vector_points <ParameterMultipleInput>
-c <ParameterBoolean>
-a <ParameterBoolean>
-n <ParameterBoolean>
GRASS_REGION_PARAMETER <ParameterExtent>
GRASS_REGION_CELLSIZE_PARAMETER <ParameterNumber>
GRASS_SNAP_TOLERANCE_PARAMETER <ParameterNumber>
GRASS_MIN_AREA_PARAMETER <ParameterNumber>
output <OutputRaster>

在这种情况下,关键是输出,请注意必须使用大写字母或大写字母,在这种情况下,请不要大写


By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.