本文整理汇总了Python中qgis.core.QgsVirtualLayerDefinition.setQuery方法的典型用法代码示例。如果您正苦于以下问题:Python QgsVirtualLayerDefinition.setQuery方法的具体用法?Python QgsVirtualLayerDefinition.setQuery怎么用?Python QgsVirtualLayerDefinition.setQuery使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类qgis.core.QgsVirtualLayerDefinition
的用法示例。
在下文中一共展示了QgsVirtualLayerDefinition.setQuery方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: processAlgorithm
# 需要导入模块: from qgis.core import QgsVirtualLayerDefinition [as 别名]
# 或者: from qgis.core.QgsVirtualLayerDefinition import setQuery [as 别名]
def processAlgorithm(self, feedback):
layers = self.getParameterValue(self.INPUT_DATASOURCES)
query = self.getParameterValue(self.INPUT_QUERY)
uid_field = self.getParameterValue(self.INPUT_UID_FIELD)
geometry_field = self.getParameterValue(self.INPUT_GEOMETRY_FIELD)
geometry_type = self.getParameterValue(self.INPUT_GEOMETRY_TYPE)
geometry_crs = self.getParameterValue(self.INPUT_GEOMETRY_CRS)
df = QgsVirtualLayerDefinition()
layerIdx = 1
if layers:
for layerSource in layers.split(';'):
layer = dataobjects.getObjectFromUri(layerSource)
if layer:
df.addSource('input{}'.format(layerIdx), layer.id())
layerIdx += 1
if query == '':
raise GeoAlgorithmExecutionException(
self.tr('Empty SQL. Please enter valid SQL expression and try again.'))
else:
df.setQuery(query)
if uid_field:
df.setUid(uid_field)
if geometry_type == 1: # no geometry
df.setGeometryWkbType(QgsWkbTypes.NullGeometry)
else:
if geometry_field:
df.setGeometryField(geometry_field)
if geometry_type > 1:
df.setGeometryWkbType(geometry_type - 1)
if geometry_crs:
crs = QgsCoordinateReferenceSystem(geometry_crs)
if crs.isValid():
df.setGeometrySrid(crs.postgisSrid())
vLayer = QgsVectorLayer(df.toString(), "temp_vlayer", "virtual")
if not vLayer.isValid():
raise GeoAlgorithmExecutionException(vLayer.dataProvider().error().message())
writer = self.getOutputFromName(self.OUTPUT_LAYER).getVectorWriter(
vLayer.fields().toList(),
# Create a point layer (without any points) if 'no geometry' is chosen
vLayer.wkbType() if geometry_type != 1 else 1,
vLayer.crs())
features = vector.features(vLayer)
total = 100.0 / len(features)
outFeat = QgsFeature()
for current, inFeat in enumerate(features):
outFeat.setAttributes(inFeat.attributes())
if geometry_type != 1:
outFeat.setGeometry(inFeat.geometry())
writer.addFeature(outFeat)
feedback.setProgress(int(current * total))
del writer
示例2: test_qgisExpressionFunctions
# 需要导入模块: from qgis.core import QgsVirtualLayerDefinition [as 别名]
# 或者: from qgis.core.QgsVirtualLayerDefinition import setQuery [as 别名]
def test_qgisExpressionFunctions(self):
QgsProject.instance().setTitle('project')
self.assertEqual(QgsProject.instance().title(), 'project')
df = QgsVirtualLayerDefinition()
df.setQuery("SELECT format('hello %1', 'world') as a, year(todate('2016-01-02')) as b, title('This') as t, var('project_title') as c")
l = QgsVectorLayer(df.toString(), "testq", "virtual")
self.assertEqual(l.isValid(), True)
for f in l.getFeatures():
self.assertEqual(f.attributes(), ['hello world', 2016, 'This', 'project'])
示例3: toSqlLayer
# 需要导入模块: from qgis.core import QgsVirtualLayerDefinition [as 别名]
# 或者: from qgis.core.QgsVirtualLayerDefinition import setQuery [as 别名]
def toSqlLayer(self, sql, geomCol, uniqueCol, layerName="QueryLayer", layerType=None, avoidSelectById=False, _filter=""):
df = QgsVirtualLayerDefinition()
df.setQuery(sql)
if uniqueCol is not None:
uniqueCol = uniqueCol.strip('"').replace('""', '"')
df.setUid(uniqueCol)
if geomCol is not None:
df.setGeometryField(geomCol)
vl = QgsVectorLayer(df.toString(), layerName, "virtual")
if _filter:
vl.setSubsetString(_filter)
return vl
示例4: processAlgorithm
# 需要导入模块: from qgis.core import QgsVirtualLayerDefinition [as 别名]
# 或者: from qgis.core.QgsVirtualLayerDefinition import setQuery [as 别名]
def processAlgorithm(self, parameters, context, feedback):
layers = self.parameterAsLayerList(parameters, self.INPUT_DATASOURCES, context)
query = self.parameterAsString(parameters, self.INPUT_QUERY, context)
uid_field = self.parameterAsString(parameters, self.INPUT_UID_FIELD, context)
geometry_field = self.parameterAsString(parameters, self.INPUT_GEOMETRY_FIELD, context)
geometry_type = self.parameterAsEnum(parameters, self.INPUT_GEOMETRY_TYPE, context)
geometry_crs = self.parameterAsCrs(parameters, self.INPUT_GEOMETRY_CRS, context)
df = QgsVirtualLayerDefinition()
for layerIdx, layer in enumerate(layers):
df.addSource('input{}'.format(layerIdx + 1), layer.id())
if query == '':
raise QgsProcessingException(
self.tr('Empty SQL. Please enter valid SQL expression and try again.'))
else:
localContext = self.createExpressionContext(parameters, context)
expandedQuery = QgsExpression.replaceExpressionText(query, localContext)
df.setQuery(expandedQuery)
if uid_field:
df.setUid(uid_field)
if geometry_type == 1: # no geometry
df.setGeometryWkbType(QgsWkbTypes.NoGeometry)
else:
if geometry_field:
df.setGeometryField(geometry_field)
if geometry_type > 1:
df.setGeometryWkbType(geometry_type - 1)
if geometry_crs.isValid():
df.setGeometrySrid(geometry_crs.postgisSrid())
vLayer = QgsVectorLayer(df.toString(), "temp_vlayer", "virtual")
if not vLayer.isValid():
raise QgsProcessingException(vLayer.dataProvider().error().message())
(sink, dest_id) = self.parameterAsSink(parameters, self.OUTPUT, context,
vLayer.fields(), vLayer.wkbType() if geometry_type != 1 else 1, vLayer.crs())
if sink is None:
raise QgsProcessingException(self.invalidSinkError(parameters, self.OUTPUT))
features = vLayer.getFeatures()
total = 100.0 / vLayer.featureCount() if vLayer.featureCount() else 0
for current, inFeat in enumerate(features):
if feedback.isCanceled():
break
sink.addFeature(inFeat, QgsFeatureSink.FastInsert)
feedback.setProgress(int(current * total))
return {self.OUTPUT: dest_id}
示例5: __init__
# 需要导入模块: from qgis.core import QgsVirtualLayerDefinition [as 别名]
# 或者: from qgis.core.QgsVirtualLayerDefinition import setQuery [as 别名]
def __init__(self, db, sql, parent):
super().__init__(db, sql, parent)
tf = QTemporaryFile()
tf.open()
path = tf.fileName()
tf.close()
df = QgsVirtualLayerDefinition()
df.setFilePath(path)
df.setQuery(sql)
self.subtask = QgsVirtualLayerTask(df)
self.addSubTask(self.subtask, [], QgsTask.ParentDependsOnSubTask)
示例6: test_layer_with_accents
# 需要导入模块: from qgis.core import QgsVirtualLayerDefinition [as 别名]
# 或者: from qgis.core.QgsVirtualLayerDefinition import setQuery [as 别名]
def test_layer_with_accents(self):
l1 = QgsVectorLayer(os.path.join(self.testDataDir, "france_parts.shp"), "françéà", "ogr", False)
self.assertEqual(l1.isValid(), True)
QgsProject.instance().addMapLayer(l1)
df = QgsVirtualLayerDefinition()
df.setQuery('select * from "françéà"')
vl = QgsVectorLayer(df.toString(), "testq", "virtual")
self.assertEqual(vl.isValid(), True)
ids = [f.id() for f in vl.getFeatures()]
self.assertEqual(len(ids), 4)
QgsProject.instance().removeMapLayer(l1.id())
示例7: test_query_with_accents
# 需要导入模块: from qgis.core import QgsVirtualLayerDefinition [as 别名]
# 或者: from qgis.core.QgsVirtualLayerDefinition import setQuery [as 别名]
def test_query_with_accents(self):
# shapefile with accents and latin1 encoding
df = QgsVirtualLayerDefinition()
df.addSource("vtab", os.path.join(self.testDataDir, "france_parts.shp"), "ogr", "ISO-8859-1")
df.setQuery("SELECT * FROM vtab WHERE TYPE_1 = 'Région'")
vl = QgsVectorLayer(df.toString(), "testq", "virtual")
self.assertEqual(vl.isValid(), True)
ids = [f.id() for f in vl.getFeatures()]
self.assertEqual(len(ids), 4)
# the same shapefile with a wrong encoding
df.addSource("vtab", os.path.join(self.testDataDir, "france_parts.shp"), "ogr", "UTF-8")
df.setQuery("SELECT * FROM vtab WHERE TYPE_1 = 'Région'")
vl2 = QgsVectorLayer(df.toString(), "testq", "virtual")
self.assertEqual(vl2.isValid(), True)
ids = [f.id() for f in vl2.getFeatures()]
self.assertEqual(ids, [])
示例8: test1
# 需要导入模块: from qgis.core import QgsVirtualLayerDefinition [as 别名]
# 或者: from qgis.core.QgsVirtualLayerDefinition import setQuery [as 别名]
def test1(self):
d = QgsVirtualLayerDefinition()
self.assertEqual(d.toString(), "")
d.setFilePath("/file")
self.assertEqual(d.toString(), "/file")
self.assertEqual(QgsVirtualLayerDefinition.fromUrl(d.toUrl()).filePath(), "/file")
d.setFilePath("C:\\file")
self.assertEqual(d.toString(), "C:%5Cfile")
self.assertEqual(QgsVirtualLayerDefinition.fromUrl(d.toUrl()).filePath(), "C:\\file")
d.setQuery("SELECT * FROM mytable")
self.assertEqual(QgsVirtualLayerDefinition.fromUrl(d.toUrl()).query(), "SELECT * FROM mytable")
q = u"SELECT * FROM tableéé /*:int*/"
d.setQuery(q)
self.assertEqual(QgsVirtualLayerDefinition.fromUrl(d.toUrl()).query(), q)
s1 = u"file://foo&bar=okié"
d.addSource("name", s1, "provider", "utf8")
self.assertEqual(QgsVirtualLayerDefinition.fromUrl(d.toUrl()).sourceLayers()[0].source(), s1)
n1 = u"éé ok"
d.addSource(n1, s1, "provider")
self.assertEqual(QgsVirtualLayerDefinition.fromUrl(d.toUrl()).sourceLayers()[1].name(), n1)
d.addSource("ref1", "id0001")
self.assertEqual(QgsVirtualLayerDefinition.fromUrl(d.toUrl()).sourceLayers()[2].reference(), "id0001")
d.setGeometryField("geom")
self.assertEqual(QgsVirtualLayerDefinition.fromUrl(d.toUrl()).geometryField(), "geom")
d.setGeometryWkbType(QgsWKBTypes.Point)
self.assertEqual(QgsVirtualLayerDefinition.fromUrl(d.toUrl()).geometryWkbType(), QgsWKBTypes.Point)
f = QgsFields()
f.append(QgsField("a", QVariant.Int))
f.append(QgsField("f", QVariant.Double))
f.append(QgsField("s", QVariant.String))
d.setFields(f)
f2 = QgsVirtualLayerDefinition.fromUrl(d.toUrl()).fields()
self.assertEqual(f[0].name(), f2[0].name())
self.assertEqual(f[0].type(), f2[0].type())
self.assertEqual(f[1].name(), f2[1].name())
self.assertEqual(f[1].type(), f2[1].type())
self.assertEqual(f[2].name(), f2[2].name())
self.assertEqual(f[2].type(), f2[2].type())
示例9: _get_cursor_columns
# 需要导入模块: from qgis.core import QgsVirtualLayerDefinition [as 别名]
# 或者: from qgis.core.QgsVirtualLayerDefinition import setQuery [as 别名]
def _get_cursor_columns(self, c):
tf = QTemporaryFile()
tf.open()
tmp = tf.fileName()
tf.close()
df = QgsVirtualLayerDefinition()
df.setFilePath(tmp)
df.setQuery(c.sql)
p = QgsVectorLayer(df.toString(), "vv", "virtual")
if not p.isValid():
return []
f = [f.name() for f in p.fields()]
if p.geometryType() != QgsWkbTypes.NullGeometry:
gn = getQueryGeometryName(tmp)
if gn:
f += [gn]
return f
示例10: __init__
# 需要导入模块: from qgis.core import QgsVirtualLayerDefinition [as 别名]
# 或者: from qgis.core.QgsVirtualLayerDefinition import setQuery [as 别名]
def __init__(self, db, sql, parent=None):
# create a virtual layer with non-geometry results
t = QTime()
t.start()
tf = QTemporaryFile()
tf.open()
tmp = tf.fileName()
tf.close()
df = QgsVirtualLayerDefinition()
df.setFilePath(tmp)
df.setQuery(sql)
p = QgsVectorLayer(df.toString(), "vv", "virtual")
self._secs = t.elapsed() / 1000.0
if not p.isValid():
data = []
header = []
raise DbError(p.dataProvider().error().summary(), sql)
else:
header = [f.name() for f in p.fields()]
has_geometry = False
if p.geometryType() != QgsWkbTypes.NullGeometry:
gn = getQueryGeometryName(tmp)
if gn:
has_geometry = True
header += [gn]
data = []
for f in p.getFeatures():
a = f.attributes()
if has_geometry:
if f.hasGeometry():
a += [f.geometry().exportToWkt()]
else:
a += [None]
data += [a]
self._secs = 0
self._affectedRows = len(data)
BaseTableModel.__init__(self, header, data, parent)
示例11: test
# 需要导入模块: from qgis.core import QgsVirtualLayerDefinition [as 别名]
# 或者: from qgis.core.QgsVirtualLayerDefinition import setQuery [as 别名]
def test(self):
l1 = QgsVectorLayer(os.path.join(self.testDataDir, "france_parts.shp"), "françéà", "ogr", QgsVectorLayer.LayerOptions(False))
self.assertEqual(l1.isValid(), True)
QgsProject.instance().addMapLayer(l1)
df = QgsVirtualLayerDefinition()
df.setQuery('select * from "françéà"')
self.task = QgsVirtualLayerTask(df)
ids = [f.id() for f in self.task.layer().getFeatures()]
self.assertEqual(len(ids), 0)
self.task.taskCompleted.connect(self.onSuccess)
self.task.taskTerminated.connect(self.onFail)
QgsApplication.taskManager().addTask(self.task)
while not self._success and not self._fail:
QCoreApplication.processEvents()
self.assertTrue(self._success)
self.assertFalse(self._fail)
self.assertEqual(len(self.ids), 4)
# Test exception
self._success = False
self._fail = False
df.setQuery('select *')
self.task = QgsVirtualLayerTask(df)
self.task.taskCompleted.connect(self.onSuccess)
self.task.taskTerminated.connect(self.onFail)
QgsApplication.taskManager().addTask(self.task)
while not self._success and not self._fail:
QCoreApplication.processEvents()
self.assertFalse(self._success)
self.assertTrue(self._fail)
self.assertEqual(self._exceptionText, 'Query preparation error on PRAGMA table_info(_tview): no tables specified', self._exceptionText)
示例12: testFiltersWithoutUid
# 需要导入模块: from qgis.core import QgsVirtualLayerDefinition [as 别名]
# 或者: from qgis.core.QgsVirtualLayerDefinition import setQuery [as 别名]
def testFiltersWithoutUid(self):
ml = QgsVectorLayer("Point?srid=EPSG:4326&field=a:int", "mem_no_uid", "memory")
self.assertEqual(ml.isValid(), True)
QgsProject.instance().addMapLayer(ml)
# a memory layer with 10 features
ml.startEditing()
for i in range(10):
f = QgsFeature(ml.fields())
f.setGeometry(QgsGeometry.fromWkt('POINT({} 0)'.format(i)))
f.setAttributes([i])
ml.addFeatures([f])
ml.commitChanges()
df = QgsVirtualLayerDefinition()
df.setQuery('select * from mem_no_uid')
vl = QgsVectorLayer(df.toString(), "vl", "virtual")
self.assertEqual(vl.isValid(), True)
# make sure the returned id with a filter is the same as
# if there is no filter
req = QgsFeatureRequest().setFilterRect(QgsRectangle(4.5, -1, 5.5, 1))
fids = [f.id() for f in vl.getFeatures(req)]
self.assertEqual(fids, [5])
req = QgsFeatureRequest().setFilterExpression("a = 5")
fids = [f.id() for f in vl.getFeatures(req)]
self.assertEqual(fids, [5])
req = QgsFeatureRequest().setFilterFid(5)
a = [(f.id(), f['a']) for f in vl.getFeatures(req)]
self.assertEqual(a, [(5, 5)])
req = QgsFeatureRequest().setFilterFids([5, 6, 8])
a = [(f.id(), f['a']) for f in vl.getFeatures(req)]
self.assertEqual(a, [(5, 5), (6, 6), (8, 8)])
QgsProject.instance().removeMapLayer(ml)
示例13: test
# 需要导入模块: from qgis.core import QgsVirtualLayerDefinition [as 别名]
# 或者: from qgis.core.QgsVirtualLayerDefinition import setQuery [as 别名]
def test(self):
l1 = QgsVectorLayer(os.path.join(self.testDataDir, "france_parts.shp"), "françéà", "ogr", QgsVectorLayer.LayerOptions(False))
self.assertEqual(l1.isValid(), True)
QgsProject.instance().addMapLayer(l1)
df = QgsVirtualLayerDefinition()
df.setQuery('select * from "françéà"')
self.task = QgsVirtualLayerTask(df)
ids = [f.id() for f in self.task.layer().getFeatures()]
self.assertEqual(len(ids), 0)
self.task.taskCompleted.connect(self.onSuccess)
self.task.taskTerminated.connect(self.onFail)
QgsApplication.taskManager().addTask(self.task)
while not self.success and not self.fail:
QCoreApplication.processEvents()
self.assertTrue(self.success)
self.assertFalse(self.fail)
self.assertEqual(len(self.ids), 4)