def test1(self): d = QgsVirtualLayerDefinition() self.assertEqual(d.toString(), "") d.setFilePath("/file") self.assertEqual(d.toString(), "file:///file") self.assertEqual(QgsVirtualLayerDefinition.fromUrl(d.toUrl()).filePath(), "/file") self.assertEqual(QgsVirtualLayerDefinition.fromUrl(QUrl.fromEncoded(d.toString())).filePath(), "/file") d.setFilePath(os.path.join('C:/', 'file')) self.assertEqual(d.toString(), "file:///C:/file") self.assertEqual(QgsVirtualLayerDefinition.fromUrl(d.toUrl()).filePath(), os.path.join('C:/', 'file')) d.setQuery("SELECT * FROM mytable") self.assertEqual(QgsVirtualLayerDefinition.fromUrl(d.toUrl()).query(), "SELECT * FROM mytable") self.assertEqual(QgsVirtualLayerDefinition.fromUrl(QUrl.fromEncoded(d.toString())).query(), "SELECT * FROM mytable") q = u"SELECT * FROM tableéé /*:int*/" d.setQuery(q) self.assertEqual(QgsVirtualLayerDefinition.fromUrl(d.toUrl()).query(), q) self.assertEqual(QgsVirtualLayerDefinition.fromUrl(QUrl.fromEncoded(d.toString())).query(), q) s1 = u"file://foo&bar=okié" d.addSource("name", s1, "provider", "utf8") self.assertEqual(QgsVirtualLayerDefinition.fromUrl(d.toUrl()).sourceLayers()[0].source(), s1) self.assertEqual(QgsVirtualLayerDefinition.fromUrl(QUrl.fromEncoded(d.toString())).sourceLayers()[0].source(), s1) n1 = u"éé ok" d.addSource(n1, s1, "provider") self.assertEqual(QgsVirtualLayerDefinition.fromUrl(d.toUrl()).sourceLayers()[1].name(), n1) self.assertEqual(QgsVirtualLayerDefinition.fromUrl(QUrl.fromEncoded(d.toString())).sourceLayers()[1].name(), n1) d.addSource("ref1", "id0001") self.assertEqual(QgsVirtualLayerDefinition.fromUrl(d.toUrl()).sourceLayers()[2].reference(), "id0001") self.assertEqual(QgsVirtualLayerDefinition.fromUrl(QUrl.fromEncoded(d.toString())).sourceLayers()[2].reference(), "id0001") s = "dbname='C:\\tt' table=\"test\" (geometry) sql=" d.addSource("nn", s, "spatialite") self.assertEqual(QgsVirtualLayerDefinition.fromUrl(d.toUrl()).sourceLayers()[3].source(), s) self.assertEqual(QgsVirtualLayerDefinition.fromUrl(QUrl.fromEncoded(d.toString())).sourceLayers()[3].source(), s) d.setGeometryField("geom") self.assertEqual(QgsVirtualLayerDefinition.fromUrl(d.toUrl()).geometryField(), "geom") self.assertEqual(QgsVirtualLayerDefinition.fromUrl(QUrl.fromEncoded(d.toString())).geometryField(), "geom") d.setGeometryWkbType(QgsWKBTypes.Point) self.assertEqual(QgsVirtualLayerDefinition.fromUrl(d.toUrl()).geometryWkbType(), QgsWKBTypes.Point) self.assertEqual(QgsVirtualLayerDefinition.fromUrl(QUrl.fromEncoded(d.toString())).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())
def getQueryGeometryName(sqlite_file): # introspect the file with sqlite3_connection(sqlite_file) as conn: c = conn.cursor() for r in c.execute("SELECT url FROM _meta"): d = QgsVirtualLayerDefinition.fromUrl(QUrl.fromEncoded(r[0])) if d.hasDefinedGeometry(): return d.geometryField() return None
def remote_open_from_qgis(uri): """Opens a remote URL using QGIS proxy preferences""" reply, is_ok = _sync_get(QUrl.fromEncoded(bytes(uri, "utf8"))) if not is_ok: raise RuntimeError("Network problem when downloading {}".format(uri)) redirect = reply.attribute(QNetworkRequest.RedirectionTargetAttribute) # Handle HTTP 302 redirections while redirect is not None and not redirect.isEmpty(): reply, is_ok = _sync_get(redirect) if not is_ok: raise RuntimeError("Network problem when downloading {}".format(uri)) redirect = reply.attribute(QNetworkRequest.RedirectionTargetAttribute) r = bytes(reply.readAll()) reply.close() return BytesIO(r)
def strToUrl(s): if sys.version_info.major == 3: return QUrl.fromEncoded(bytes(s, "utf8")) else: return QUrl.fromEncoded(s)
def strToUrl(s): return QUrl.fromEncoded(bytes(s, "utf8"))
def test1(self): d = QgsVirtualLayerDefinition() self.assertEqual(d.toString(), "") d.setFilePath("/file") self.assertEqual(d.toString(), "file:///file") self.assertEqual( QgsVirtualLayerDefinition.fromUrl(d.toUrl()).filePath(), "/file") self.assertEqual( QgsVirtualLayerDefinition.fromUrl(QUrl.fromEncoded( d.toString())).filePath(), "/file") d.setFilePath(os.path.join('C:/', 'file')) self.assertEqual(d.toString(), "file:///C:/file") self.assertEqual( QgsVirtualLayerDefinition.fromUrl(d.toUrl()).filePath(), os.path.join('C:/', 'file')) d.setQuery("SELECT * FROM mytable") self.assertEqual( QgsVirtualLayerDefinition.fromUrl(d.toUrl()).query(), "SELECT * FROM mytable") self.assertEqual( QgsVirtualLayerDefinition.fromUrl(QUrl.fromEncoded( d.toString())).query(), "SELECT * FROM mytable") q = u"SELECT * FROM tableéé /*:int*/" d.setQuery(q) self.assertEqual( QgsVirtualLayerDefinition.fromUrl(d.toUrl()).query(), q) self.assertEqual( QgsVirtualLayerDefinition.fromUrl(QUrl.fromEncoded( d.toString())).query(), q) s1 = u"file://foo&bar=okié" d.addSource("name", s1, "provider", "utf8") self.assertEqual( QgsVirtualLayerDefinition.fromUrl( d.toUrl()).sourceLayers()[0].source(), s1) self.assertEqual( QgsVirtualLayerDefinition.fromUrl(QUrl.fromEncoded( d.toString())).sourceLayers()[0].source(), s1) n1 = u"éé ok" d.addSource(n1, s1, "provider") self.assertEqual( QgsVirtualLayerDefinition.fromUrl( d.toUrl()).sourceLayers()[1].name(), n1) self.assertEqual( QgsVirtualLayerDefinition.fromUrl(QUrl.fromEncoded( d.toString())).sourceLayers()[1].name(), n1) d.addSource("ref1", "id0001") self.assertEqual( QgsVirtualLayerDefinition.fromUrl( d.toUrl()).sourceLayers()[2].reference(), "id0001") self.assertEqual( QgsVirtualLayerDefinition.fromUrl(QUrl.fromEncoded( d.toString())).sourceLayers()[2].reference(), "id0001") s = "dbname='C:\\tt' table=\"test\" (geometry) sql=" d.addSource("nn", s, "spatialite") self.assertEqual( QgsVirtualLayerDefinition.fromUrl( d.toUrl()).sourceLayers()[3].source(), s) self.assertEqual( QgsVirtualLayerDefinition.fromUrl(QUrl.fromEncoded( d.toString())).sourceLayers()[3].source(), s) d.setGeometryField("geom") self.assertEqual( QgsVirtualLayerDefinition.fromUrl(d.toUrl()).geometryField(), "geom") self.assertEqual( QgsVirtualLayerDefinition.fromUrl(QUrl.fromEncoded( d.toString())).geometryField(), "geom") d.setGeometryWkbType(QgsWKBTypes.Point) self.assertEqual( QgsVirtualLayerDefinition.fromUrl(d.toUrl()).geometryWkbType(), QgsWKBTypes.Point) self.assertEqual( QgsVirtualLayerDefinition.fromUrl(QUrl.fromEncoded( d.toString())).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())