def setUpClass(cls): """Run before all tests""" # Create test layer srcpath = os.path.join(TEST_DATA_DIR, 'provider') cls.basetestfile = os.path.join(srcpath, 'delimited_wkt.csv') url = QUrl.fromLocalFile(cls.basetestfile) url.addQueryItem("crs", "epsg:4326") url.addQueryItem("type", "csv") url.addQueryItem("wktField", "wkt") url.addQueryItem("spatialIndex", "no") url.addQueryItem("subsetIndex", "no") url.addQueryItem("watchFile", "no") cls.vl = QgsVectorLayer(url.toString(), u'test', u'delimitedtext') assert cls.vl.isValid(), "{} is invalid".format(cls.basetestfile) cls.provider = cls.vl.dataProvider() cls.basetestpolyfile = os.path.join(srcpath, 'delimited_wkt_poly.csv') url = QUrl.fromLocalFile(cls.basetestpolyfile) url.addQueryItem("crs", "epsg:4326") url.addQueryItem("type", "csv") url.addQueryItem("wktField", "wkt") url.addQueryItem("spatialIndex", "no") url.addQueryItem("subsetIndex", "no") url.addQueryItem("watchFile", "no") cls.vl_poly = QgsVectorLayer(url.toString(), u'test_polygon', u'delimitedtext') assert cls.vl_poly.isValid(), "{} is invalid".format( cls.basetestpolyfile) cls.poly_provider = cls.vl_poly.dataProvider()
def setUpClass(cls): """Run before all tests""" # Create test layer srcpath = os.path.join(TEST_DATA_DIR, 'provider') cls.basetestfile = os.path.join(srcpath, 'delimited_wkt.csv') url = QUrl.fromLocalFile(cls.basetestfile) url.addQueryItem("crs", "epsg:4326") url.addQueryItem("type", "csv") url.addQueryItem("wktField", "wkt") url.addQueryItem("spatialIndex", "no") url.addQueryItem("subsetIndex", "no") url.addQueryItem("watchFile", "no") cls.vl = QgsVectorLayer(url.toString(), u'test', u'delimitedtext') assert cls.vl.isValid(), "{} is invalid".format(cls.basetestfile) cls.provider = cls.vl.dataProvider() cls.basetestpolyfile = os.path.join(srcpath, 'delimited_wkt_poly.csv') url = QUrl.fromLocalFile(cls.basetestpolyfile) url.addQueryItem("crs", "epsg:4326") url.addQueryItem("type", "csv") url.addQueryItem("wktField", "wkt") url.addQueryItem("spatialIndex", "no") url.addQueryItem("subsetIndex", "no") url.addQueryItem("watchFile", "no") cls.vl_poly = QgsVectorLayer(url.toString(), u'test_polygon', u'delimitedtext') assert cls.vl_poly.isValid(), "{} is invalid".format(cls.basetestpolyfile) cls.poly_provider = cls.vl_poly.dataProvider()
def test_refLayers2(self): l1 = QgsVectorLayer(QUrl.fromLocalFile(os.path.join(self.testDataDir, "delimitedtext/test.csv")).toString() + "?type=csv&geomType=none&subsetIndex=no&watchFile=no", "test", "delimitedtext", False) self.assertEqual(l1.isValid(), True) QgsMapLayerRegistry.instance().addMapLayer(l1) # referenced layers cannot be stored ! tmp = QUrl.fromLocalFile(os.path.join(tempfile.gettempdir(), "t.sqlite")).toString() l2 = QgsVectorLayer("%s?layer_ref=%s" % (tmp, l1.id()), "tt", "virtual", False) self.assertEqual(l2.isValid(), False) self.assertEqual("Cannot store referenced layers" in l2.dataProvider().error().message(), True)
def onHelp(self): helpPath = Utils.getHelpPath() if helpPath == '': url = QUrl("http://www.gdal.org/" + self.helpFileName) else: url = QUrl.fromLocalFile(helpPath + '/' + self.helpFileName) QDesktopServices.openUrl(url)
def test_source_escaping(self): # the source contains ':' source = QUrl.fromLocalFile(os.path.join(self.testDataDir, "delimitedtext/test.csv")).toString() + "?type=csv&geomType=none&subsetIndex=no&watchFile=no" d = QgsVirtualLayerDefinition() d.addSource("t", source, "delimitedtext") l = QgsVectorLayer(d.toString(), "vtab", "virtual", False) self.assertEqual(l.isValid(), True)
def help(self): localDoc = None html = self.grass7Name + '.html' if system.isWindows(): # For MS-Windows, use the configured GRASS7 path localPath = os.path.join(Grass7Utils.grassPath(), 'docs/html', html) if os.path.exists(localPath): localDoc = os.path.abspath(localPath) elif system.isMac(): # For MacOSX official package localPath = os.path.join( '/Applications/GRASS-7.0.app/Contents/MacOS/docs/html', html) if os.path.exists(localPath): localDoc = os.path.abspath(localPath) else: # For GNU/Linux distributions searchPaths = [ '/usr/share/doc/grass-doc/html', '/opt/grass/docs/html', '/usr/share/doc/grass/docs/html' ] for path in searchPaths: localPath = os.path.join(path, html) if os.path.exists(localPath): localDoc = os.path.abspath(localPath) # Found the local documentation if localDoc: localDoc = QUrl.fromLocalFile(localDoc).toString() return False, localDoc # Return the URL if local doc is not found return False, 'http://grass.osgeo.org/grass70/manuals/' + self.grass7Name + '.html'
def help(self): localDoc = None html = self.grass7Name + '.html' if system.isWindows(): # For MS-Windows, use the configured GRASS7 path localPath = os.path.join(Grass7Utils.grassPath(), 'docs/html', html) if os.path.exists(localPath): localDoc = os.path.abspath(localPath) elif system.isMac(): # For MacOSX official package localPath = os.path.join('/Applications/GRASS-7.0.app/Contents/MacOS/docs/html', html) if os.path.exists(localPath): localDoc = os.path.abspath(localPath) else: # For GNU/Linux distributions searchPaths = ['/usr/share/doc/grass-doc/html', '/opt/grass/docs/html', '/usr/share/doc/grass/docs/html'] for path in searchPaths: localPath = os.path.join(path, html) if os.path.exists(localPath): localDoc = os.path.abspath(localPath) # Found the local documentation if localDoc: localDoc = QUrl.fromLocalFile(localDoc).toString() return False, localDoc # Return the URL if local doc is not found return False, 'http://grass.osgeo.org/grass70/manuals/' + self.grass7Name + '.html'
def delimitedTextData(testname, filename, requests, verbose, **params): # Create a layer for the specified file and query parameters # and return the data for the layer (fields, data) filepath = os.path.join(unitTestDataPath("delimitedtext"), filename) url = QUrl.fromLocalFile(filepath) if not requests: requests = [{}] for k in params.keys(): url.addQueryItem(k, params[k]) urlstr = url.toString() log = [] with MessageLogger('DelimitedText') as logger: if verbose: print(testname) layer = QgsVectorLayer(urlstr, 'test', 'delimitedtext') uri = unicode(layer.dataProvider().dataSourceUri()) if verbose: print(uri) basename = os.path.basename(filepath) if not basename.startswith('test'): basename = 'file' uri = re.sub(r'^file\:\/\/[^\?]*', 'file://' + basename, uri) fields = [] fieldTypes = [] data = {} if layer.isValid(): for nr, r in enumerate(requests): if verbose: print("Processing request", nr + 1, repr(r)) if callable(r): r(layer) if verbose: print("Request function executed") if callable(r): continue rfields, rtypes, rdata = layerData(layer, r, nr * 1000) if len(rfields) > len(fields): fields = rfields fieldTypes = rtypes data.update(rdata) if not rdata: log.append("Request " + str(nr) + " did not return any data") if verbose: print("Request returned", len(rdata.keys()), "features") for msg in logger.messages(): filelogname = 'temp_file' if 'tmp' in filename.lower( ) else filename msg = re.sub(r'file\s+.*' + re.escape(filename), 'file ' + filelogname, msg) msg = msg.replace(filepath, filelogname) log.append(msg) return dict(fields=fields, fieldTypes=fieldTypes, data=data, log=log, uri=uri, geometryType=layer.geometryType())
def openScriptFileExtEditor(self): tabWidget = self.tabEditorWidget.currentWidget() path = tabWidget.path import subprocess try: subprocess.Popen([os.environ['EDITOR'], path]) except KeyError: QDesktopServices.openUrl(QUrl.fromLocalFile(path))
def test_CsvNoGeometry(self): l1 = QgsVectorLayer(QUrl.fromLocalFile(os.path.join(self.testDataDir, "delimitedtext/test.csv")).toString() + "?type=csv&geomType=none&subsetIndex=no&watchFile=no", "test", "delimitedtext", False) self.assertEqual(l1.isValid(), True) QgsMapLayerRegistry.instance().addMapLayer(l1) l2 = QgsVectorLayer("?layer_ref=" + l1.id(), "vtab", "virtual", False) self.assertEqual(l2.isValid(), True) QgsMapLayerRegistry.instance().removeMapLayer(l1.id())
def test_reopen2(self): source = QUrl.toPercentEncoding(os.path.join(self.testDataDir, "france_parts.shp")) tmp = QUrl.fromLocalFile(os.path.join(tempfile.gettempdir(), "t.sqlite")).toString() l = QgsVectorLayer("%s?layer=ogr:%s:vtab&nogeometry" % (tmp, source), "vtab2", "virtual", False) self.assertEqual(l.isValid(), True) l2 = QgsVectorLayer(tmp, "tt", "virtual", False) self.assertEqual(l2.isValid(), True) self.assertEqual(l2.dataProvider().geometryType(), 100) self.assertEqual(l2.dataProvider().featureCount(), 4)
def test_DynamicGeometry(self): l1 = QgsVectorLayer(QUrl.fromLocalFile(os.path.join(self.testDataDir, "delimitedtext/testextpt.txt")).toString() + "?type=csv&delimiter=%7C&geomType=none&subsetIndex=no&watchFile=no", "test", "delimitedtext", False) self.assertEqual(l1.isValid(), True) QgsMapLayerRegistry.instance().addMapLayer(l1) query = QUrl.toPercentEncoding("select *,makepoint(x,y) as geom from vtab1") l2 = QgsVectorLayer("?layer_ref=%s&query=%s&geometry=geom:point:0&uid=id" % (l1.id(), query), "vtab", "virtual", False) self.assertEqual(l2.isValid(), True) QgsMapLayerRegistry.instance().removeMapLayer(l1)
def test_sql(self): l1 = QgsVectorLayer(QUrl.fromLocalFile(os.path.join(self.testDataDir, "delimitedtext/test.csv")).toString() + "?type=csv&geomType=none&subsetIndex=no&watchFile=no", "test", "delimitedtext", False) self.assertEqual(l1.isValid(), True) QgsMapLayerRegistry.instance().addMapLayer(l1) l3 = QgsVectorLayer("?query=SELECT * FROM test", "tt", "virtual") self.assertEqual(l3.isValid(), True) s = sum(f.id() for f in l3.getFeatures()) self.assertEqual(s, 15)
def test_refLayer(self): l1 = QgsVectorLayer(QUrl.fromLocalFile(os.path.join(self.testDataDir, "delimitedtext/test.csv")).toString() + "?type=csv&geomType=none&subsetIndex=no&watchFile=no", "test", "delimitedtext", False) self.assertEqual(l1.isValid(), True) QgsMapLayerRegistry.instance().addMapLayer(l1) l2 = QgsVectorLayer("?layer_ref=" + l1.id(), "vtab", "virtual", False) self.assertEqual(l2.isValid(), True) # now delete the layer QgsMapLayerRegistry.instance().removeMapLayer(l1.id()) # check that it does not crash print(sum([f.id() for f in l2.getFeatures()]))
def test_refLayer(self): l1 = QgsVectorLayer(QUrl.fromLocalFile(os.path.join(self.testDataDir, "delimitedtext/test.csv")).toString() + "?type=csv&geomType=none&subsetIndex=no&watchFile=no", "test", "delimitedtext", False) self.assertEqual(l1.isValid(), True) QgsMapLayerRegistry.instance().addMapLayer(l1) l2 = QgsVectorLayer("?layer_ref=" + l1.id(), "vtab", "virtual", False) self.assertEqual(l2.isValid(), True) # now delete the layer QgsMapLayerRegistry.instance().removeMapLayer(l1.id()) # check that it does not crash print sum([f.id() for f in l2.getFeatures()])
def delimitedTextData(self, testname, filename, requests, verbose, **params): # Retrieve the data for a delimited text url # Create a layer for the specified file and query parameters # and return the data for the layer (fields, data) filepath = os.path.join(unitTestDataPath("delimitedtext"), filename) url = QUrl.fromLocalFile(filepath) if not requests: requests = [{}] for k in params.keys(): url.addQueryItem(k, params[k]) urlstr = url.toString() log = [] with MessageLogger('DelimitedText') as logger: if verbose: print(testname) layer = QgsVectorLayer(urlstr, 'test', 'delimitedtext') uri = unicode(layer.dataProvider().dataSourceUri()) if verbose: print(uri) basename = os.path.basename(filepath) if not basename.startswith('test'): basename = 'file' uri = re.sub(r'^file\:\/\/[^\?]*', 'file://' + basename, uri) fields = [] fieldTypes = [] data = {} if layer.isValid(): for nr, r in enumerate(requests): if verbose: print("Processing request", nr + 1, repr(r)) if callable(r): r(layer) if verbose: print("Request function executed") if callable(r): continue rfields, rtypes, rdata = self.layerData(layer, r, nr * 1000) if len(rfields) > len(fields): fields = rfields fieldTypes = rtypes data.update(rdata) if not rdata: log.append("Request " + str(nr) + " did not return any data") if verbose: print("Request returned", len(rdata.keys()), "features") for msg in logger.messages(): filelogname = 'temp_file' if 'tmp' in filename.lower() else filename msg = re.sub(r'file\s+.*' + re.escape(filename), 'file ' + filelogname, msg) msg = msg.replace(filepath, filelogname) log.append(msg) return dict(fields=fields, fieldTypes=fieldTypes, data=data, log=log, uri=uri, geometryType=layer.geometryType())
def test_refLayers(self): l1 = QgsVectorLayer(QUrl.fromLocalFile(os.path.join(self.testDataDir, "delimitedtext/test.csv")).toString() + "?type=csv&geomType=none&subsetIndex=no&watchFile=no", "test", "delimitedtext", False) self.assertEqual(l1.isValid(), True) QgsMapLayerRegistry.instance().addMapLayer(l1) # cf qgis bug #12266 for i in range(10): q = QUrl.toPercentEncoding("select * from t" + str(i)) l2 = QgsVectorLayer("?layer_ref=%s:t%d&query=%s&uid=id" % (l1.id(), i, q), "vtab", "virtual", False) QgsMapLayerRegistry.instance().addMapLayer(l2) self.assertEqual(l2.isValid(), True) s = sum([f.id() for f in l2.dataProvider().getFeatures()]) # NOQA self.assertEqual(sum([f.id() for f in l2.getFeatures()]), 21) QgsMapLayerRegistry.instance().removeMapLayer(l2.id())
def test_reopen4(self): source = QUrl.toPercentEncoding(os.path.join(self.testDataDir, "france_parts.shp")) tmp = QUrl.fromLocalFile(os.path.join(tempfile.gettempdir(), "t.sqlite")).toString() query = QUrl.toPercentEncoding("SELECT * FROM vtab") l = QgsVectorLayer("%s?layer=ogr:%s:vtab&query=%s&uid=objectid&nogeometry" % (tmp, source, query), "vtab2", "virtual", False) self.assertEqual(l.isValid(), True) l2 = QgsVectorLayer(tmp, "tt", "virtual", False) self.assertEqual(l2.isValid(), True) self.assertEqual(l2.dataProvider().geometryType(), 100) self.assertEqual(l2.dataProvider().featureCount(), 4) sumid = sum([f.id() for f in l2.getFeatures()]) self.assertEqual(sumid, 10659) suma = sum([f.attributes()[1] for f in l2.getFeatures()]) self.assertEqual(suma, 3064.0)
def _get_cursor_columns(self, c): tf = QTemporaryFile() tf.open() tmp = tf.fileName() tf.close() q = QUrl.toPercentEncoding(c.sql) p = QgsVectorLayer("%s?query=%s" % (QUrl.fromLocalFile(tmp).toString(), q), "vv", "virtual") if not p.isValid(): return [] f = [f.name() for f in p.fields()] if p.geometryType() != QGis.WKBNoGeometry: gn = getQueryGeometryName(tmp) if gn: f += [gn] return f
def __init__(self, db, sql, parent=None): # create a virtual layer with non-geometry results q = QUrl.toPercentEncoding(sql) t = QTime() t.start() tf = QTemporaryFile() tf.open() tmp = tf.fileName() tf.close() p = QgsVectorLayer( "%s?query=%s" % (QUrl.fromLocalFile(tmp).toString(), q), "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() != QGis.WKBNoGeometry: gn = getQueryGeometryName(tmp) if gn: has_geometry = True header += [gn] data = [] for f in p.getFeatures(): a = f.attributes() if has_geometry: if f.geometry(): a += [f.geometry().exportToWkt()] else: a += [None] data += [a] self._secs = 0 self._affectedRows = len(data) BaseTableModel.__init__(self, header, data, parent)
def __init__(self, db, sql, parent=None): # create a virtual layer with non-geometry results q = QUrl.toPercentEncoding(sql) t = QTime() t.start() tf = QTemporaryFile() tf.open() tmp = tf.fileName() tf.close() p = QgsVectorLayer("%s?query=%s" % (QUrl.fromLocalFile(tmp).toString(), q), "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() != QGis.WKBNoGeometry: gn = getQueryGeometryName(tmp) if gn: has_geometry = True header += [gn] data = [] for f in p.getFeatures(): a = f.attributes() if has_geometry: if f.geometry(): a += [f.geometry().exportToWkt()] else: a += [None] data += [a] self._secs = 0 self._affectedRows = len(data) BaseTableModel.__init__(self, header, data, parent)