Skip to content

Commit

Permalink
Merge pull request #92 from opengisch/convert_fix
Browse files Browse the repository at this point in the history
Fix project cloud convert code
  • Loading branch information
suricactus authored Aug 21, 2024
2 parents c670dcf + 5e61fa3 commit 7901b65
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 5 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/continuous_integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ jobs:
uses: actions/checkout@v2
with:
submodules: recursive
- name: Test on QGIS LTR 3.
- name: Run tests
run: |
docker-compose -f .docker/docker-compose.yml run qgis /usr/src/.docker/run-docker-tests.sh
docker compose -f .docker/docker-compose.yml run qgis /usr/src/.docker/run-docker-tests.sh
test_packaging:
runs-on: ubuntu-latest
steps:
Expand Down
19 changes: 16 additions & 3 deletions libqfieldsync/layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1138,9 +1138,22 @@ def convert_to_gpkg(self, target_path):
options = QgsVectorFileWriter.SaveVectorOptions()
options.fileEncoding = "UTF-8"
options.driverName = "GPKG"
(error, returned_dest_file) = QgsVectorFileWriter.writeAsVectorFormatV3(
source_layer, dest_file, QgsCoordinateTransformContext(), options
)
if Qgis.QGIS_VERSION_INT > 32000:
(
error,
error_msg,
returned_dest_file,
returned_dest_layer,
) = QgsVectorFileWriter.writeAsVectorFormatV3(
source_layer, dest_file, QgsCoordinateTransformContext(), options
)
else:
(
error,
returned_dest_file,
) = QgsVectorFileWriter.writeAsVectorFormatV2(
source_layer, dest_file, QgsCoordinateTransformContext(), options
)
if error != QgsVectorFileWriter.NoError:
return
if returned_dest_file:
Expand Down
72 changes: 72 additions & 0 deletions tests/test_layer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# -*- coding: utf-8 -*-

"""
/***************************************************************************
QFieldSync
-------------------
begin : 2024.08.21
copyright : (C) 2024 by Mathieu Pellerin
email : [email protected]
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
"""

import shutil
import tempfile
import unittest
from pathlib import Path

from qgis.core import QgsProject, QgsVectorLayer
from qgis.testing import start_app
from qgis.testing.mocked import get_iface

from libqfieldsync.layer import LayerSource

start_app()


class LayerTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.iface = get_iface()

def setUp(self):
QgsProject.instance().clear()
self.source_dir = Path(tempfile.mkdtemp())
self.target_dir = Path(tempfile.mkdtemp())

def tearDown(self):
shutil.rmtree(self.source_dir)
shutil.rmtree(self.target_dir)

@property
def data_dir(self) -> Path:
return Path(__file__).parent.joinpath("data")

def test_conver_to_gpkg(self):
shutil.copytree(
self.data_dir.joinpath("simple_project"),
self.source_dir.joinpath("simple_project"),
)

vector_layer = QgsVectorLayer(
str(self.source_dir.joinpath("simple_project/france_parts_shape.shp")),
"france_parts",
)
source_feature_count = vector_layer.featureCount()

layer_source = LayerSource(vector_layer)
target_file = layer_source.convert_to_gpkg(str(self.target_dir))
target_feature_count = layer_source.layer.featureCount()

self.assertTrue(self.target_dir.joinpath("franceparts.gpkg").exists())
self.assertEqual(str(self.target_dir.joinpath("franceparts.gpkg")), target_file)
self.assertEqual(source_feature_count, target_feature_count)

0 comments on commit 7901b65

Please sign in to comment.