Exemplo n.º 1
0
 def _testInit(self):
     """
     Preliminary checks for each test
     """
     # goes offline
     ol = QgsOfflineEditing()
     online_layer = list(self.registry.mapLayers().values())[0]
     self.assertTrue(online_layer.isSpatial())
     # Check we have features
     self.assertEqual(len([f for f in online_layer.getFeatures()]), len(TEST_FEATURES))
     self.assertTrue(ol.convertToOfflineProject(self.temp_path, 'offlineDbFile.sqlite', [online_layer.id()]))
     offline_layer = list(self.registry.mapLayers().values())[0]
     self.assertTrue(offline_layer.isSpatial())
     self.assertTrue(offline_layer.isValid())
     self.assertTrue(offline_layer.name().find('(offline)') > -1)
     self.assertEqual(len([f for f in offline_layer.getFeatures()]), len(TEST_FEATURES))
     return ol, offline_layer
Exemplo n.º 2
0
 def _testInit(self):
     """
     Preliminary checks for each test
     """
     # goes offline
     ol = QgsOfflineEditing()
     online_layer = list(self.registry.mapLayers().values())[0]
     self.assertTrue(online_layer.isSpatial())
     # Check we have features
     self.assertEqual(len([f for f in online_layer.getFeatures()]), len(TEST_FEATURES))
     self.assertTrue(ol.convertToOfflineProject(self.temp_path, 'offlineDbFile.sqlite', [online_layer.id()]))
     offline_layer = list(self.registry.mapLayers().values())[0]
     self.assertTrue(offline_layer.isSpatial())
     self.assertTrue(offline_layer.isValid())
     self.assertTrue(offline_layer.name().find('(offline)') > -1)
     self.assertEqual(len([f for f in offline_layer.getFeatures()]), len(TEST_FEATURES))
     return ol, offline_layer
Exemplo n.º 3
0
    def test_offlineConversion(self):
        # goes offline
        ol = QgsOfflineEditing()
        online_layer = list(self.registry.mapLayers().values())[0]
        self.assertTrue(online_layer.hasGeometryType())
        # Check we have 3 features
        self.assertEqual(len([f for f in online_layer.getFeatures()]), len(TEST_FEATURES))
        self.assertTrue(ol.convertToOfflineProject(self.temp_path, 'offlineDbFile.sqlite', [online_layer.id()]))
        offline_layer = list(self.registry.mapLayers().values())[0]
        self.assertTrue(offline_layer.hasGeometryType())
        self.assertTrue(offline_layer.isValid())
        self.assertTrue(offline_layer.name().find('(offline)') > -1)
        self.assertEqual(len([f for f in offline_layer.getFeatures()]), len(TEST_FEATURES))
        # Edit feature 2
        feat2 = self._getFeatureByAttribute(offline_layer, 'name', "'name 2'")
        self.assertTrue(offline_layer.startEditing())
        self.assertTrue(offline_layer.changeAttributeValue(feat2.id(), offline_layer.fieldNameIndex('name'), 'name 2 edited'))
        self.assertTrue(offline_layer.changeGeometry(feat2.id(), QgsGeometry.fromPoint(QgsPoint(33.0, 60.0))))
        self.assertTrue(offline_layer.commitChanges())
        feat2 = self._getFeatureByAttribute(offline_layer, 'name', "'name 2 edited'")
        self.assertTrue(ol.isOfflineProject())
        # Sync
        ol.synchronize()
        # Does anybody know why the sleep is needed? Is that a threaded WFS consequence?
        sleep(1)
        online_layer = list(self.registry.mapLayers().values())[0]
        self.assertTrue(online_layer.isValid())
        self.assertFalse(online_layer.name().find('(offline)') > -1)
        self.assertEqual(len([f for f in online_layer.getFeatures()]), len(TEST_FEATURES))
        # Check that data have changed in the backend (raise exception if not found)
        feat2 = self._getFeatureByAttribute(self._getLayer('test_point'), 'name', "'name 2 edited'")
        feat2 = self._getFeatureByAttribute(online_layer, 'name', "'name 2 edited'")
        self.assertEqual(feat2.geometry().asPoint().toString(), QgsPoint(33.0, 60.0).toString())
        # Check that all other features have not changed
        layer = self._getLayer('test_point')
        self.assertTrue(self._compareFeature(layer, TEST_FEATURES[1 - 1]))
        self.assertTrue(self._compareFeature(layer, TEST_FEATURES[3 - 1]))
        self.assertTrue(self._compareFeature(layer, TEST_FEATURES[4 - 1]))

        # Test for regression on double sync (it was a SEGFAULT)
        # goes offline
        ol = QgsOfflineEditing()
        offline_layer = list(self.registry.mapLayers().values())[0]
        # Edit feature 2
        feat2 = self._getFeatureByAttribute(offline_layer, 'name', "'name 2 edited'")
        self.assertTrue(offline_layer.startEditing())
        self.assertTrue(offline_layer.changeAttributeValue(feat2.id(), offline_layer.fieldNameIndex('name'), 'name 2'))
        self.assertTrue(offline_layer.changeGeometry(feat2.id(), QgsGeometry.fromPoint(TEST_FEATURES[1][2])))
        # Edit feat 4
        feat4 = self._getFeatureByAttribute(offline_layer, 'name', "'name 4'")
        self.assertTrue(offline_layer.changeAttributeValue(feat4.id(), offline_layer.fieldNameIndex('name'), 'name 4 edited'))
        self.assertTrue(offline_layer.commitChanges())
        # Sync
        ol.synchronize()
        # Does anybody knows why the sleep is needed? Is that a threaded WFS consequence?
        sleep(1)
        online_layer = list(self.registry.mapLayers().values())[0]
        layer = self._getLayer('test_point')
        # Check that data have changed in the backend (raise exception if not found)
        feat4 = self._getFeatureByAttribute(layer, 'name', "'name 4 edited'")
        feat4 = self._getFeatureByAttribute(online_layer, 'name', "'name 4 edited'")
        feat2 = self._getFeatureByAttribute(layer, 'name', "'name 2'")
        feat2 = self._getFeatureByAttribute(online_layer, 'name', "'name 2'")
        # Check that all other features have not changed
        layer = self._getLayer('test_point')
        self.assertTrue(self._compareFeature(layer, TEST_FEATURES[1 - 1]))
        self.assertTrue(self._compareFeature(layer, TEST_FEATURES[2 - 1]))
        self.assertTrue(self._compareFeature(layer, TEST_FEATURES[3 - 1]))
Exemplo n.º 4
0
def isNotTemp(layer):
    return not layer.isTemporary()


# QgsApplication.setPrefixPath("/path/to/qgis/installation", True)

qgs = QgsApplication([], False)
qgs.initQgis()

project = QgsProject.instance()

project.read(SOURCE_PROJECT_PATH)

project.write(OFFLINE_PROJECT_PATH)

layers = [l for l in project.mapLayers().values()]

vectorLayers = filter(isVector, layers)

notTempLayers = filter(isNotTemp, vectorLayers)

ids = [l.id() for l in notTempLayers]

# print(ids)

offlineEditing = QgsOfflineEditing()

offlineEditing.convertToOfflineProject(OFFLINE_DATA_PATH, OFFLINE_DB_FILE, ids,
                                       False, QgsOfflineEditing.GPKG)

qgs.exitQgis()