def testLoadLayerWithPreprocessor(self): """ Test that custom path preprocessor is used when loading layers """ lines_shp_path = os.path.join(TEST_DATA_DIR, 'moooooo.shp') lines_layer = QgsVectorLayer(lines_shp_path, 'Lines', 'ogr') self.assertFalse(lines_layer.isValid()) p = QgsProject() p.addMapLayer(lines_layer) # save project to a temporary file temp_path = tempfile.mkdtemp() temp_project_path = os.path.join(temp_path, 'temp.qgs') self.assertTrue(p.write(temp_project_path)) p2 = QgsProject() self.assertTrue(p2.read(temp_project_path)) l = p2.mapLayersByName('Lines')[0] self.assertEqual(l.name(), 'Lines') self.assertFalse(l.isValid()) # custom processor to fix path def my_processor(path): return path.replace('moooooo', 'lines') QgsPathResolver.setPathPreprocessor(my_processor) p3 = QgsProject() self.assertTrue(p3.read(temp_project_path)) l = p3.mapLayersByName('Lines')[0] self.assertEqual(l.name(), 'Lines') # layer should have correct path now self.assertTrue(l.isValid())
def testPathWriter(self): readerId = QgsPathResolver.setPathPreprocessor(self.__test_path_reader) writerId = QgsPathResolver.setPathWriter(self.__test__path_writer) lines_shp_path = os.path.join(TEST_DATA_DIR, 'lines.shp') lines_layer = QgsVectorLayer(lines_shp_path, 'Lines', 'ogr') self.assertTrue(lines_layer.isValid()) p = QgsProject() p.addMapLayer(lines_layer) # save project to a temporary file temp_path = tempfile.mkdtemp() temp_project_path = os.path.join(temp_path, 'temp.qgs') self.assertTrue(p.write(temp_project_path)) with open(temp_project_path) as f: self.assertTrue("@TEST_DATA_DIR@" in f.read()) p2 = QgsProject() self.assertTrue(p2.read(temp_project_path)) l = p2.mapLayersByName('Lines')[0] self.assertEqual(l.isValid(), True) self.assertEqual(l.source(), lines_shp_path) QgsPathResolver.removePathPreprocessor(readerId) QgsPathResolver.removePathWriter(writerId)
def __init__(self, iface): self.iface = iface self.pluginDir = os.path.dirname(__file__) self.dataPath = os.path.join(self.pluginDir, 'data') self.logos = RcLogos() self.logos.readFromCsv(os.path.join(self.dataPath, 'logos', 'logos.csv')) self.worldLayerPath = os.path.join(self.dataPath, 'sims_maps_resources.gpkg|layername=world_map') self.worldLayerId = None self.layoutBaseName = 'sims_layout' self.addIconPath() self.colorScheme = QgsSimsColorScheme() #self.addColorScheme() self.pathPreprocessorId = QgsPathResolver.setPathPreprocessor(self.simsMapsPathPreprocessor) # initialize locale locale = QSettings().value('locale/userLocale')[0:2] print(locale) locale_path = os.path.join(self.pluginDir, 'i18n', f'sims_maps_{locale}.qm') print(locale_path) if os.path.exists(locale_path): self.translator = QTranslator() self.translator.load(locale_path) if qVersion() > '4.3.3': QCoreApplication.installTranslator(self.translator) self.checkVersion()
def run_test(): def my_processor(path): return path.upper() id = QgsPathResolver.setPathPreprocessor(my_processor) self.assertTrue(id) self.assertEqual(QgsPathResolver().readPath('aaaaa'), 'AAAAA') return id
def run_test(): def my_processor(path): return 'x' + path + 'x' def my_processor2(path): return 'y' + path + 'y' id = QgsPathResolver.setPathPreprocessor(my_processor) self.assertTrue(id) self.assertEqual(QgsPathResolver().readPath('aaaaa'), 'xaaaaax') id2 = QgsPathResolver.setPathPreprocessor(my_processor2) self.assertTrue(id2) self.assertEqual(QgsPathResolver().readPath('aaaaa'), 'yxaaaaaxy') return id, id2