Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
xpose single vector tile encoding method
- Loading branch information
Showing
5 changed files
with
144 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
# -*- coding: utf-8 -*- | ||
''' | ||
test_vectortile.py | ||
-------------------------------------- | ||
Date : September 2021 | ||
Copyright : (C) 2021 David Marteau | ||
email : david dot marteau at 3liz dot com | ||
*************************************************************************** | ||
* * | ||
* 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 qgis # NOQA | ||
import tempfile | ||
import shutil | ||
|
||
from qgis.testing import unittest, start_app | ||
from utilities import unitTestDataPath | ||
|
||
from qgis.PyQt.QtCore import QUrl | ||
from qgis.core import (QgsVectorLayer, | ||
QgsVectorTileWriter, | ||
QgsDataSourceUri, | ||
QgsTileXYZ) | ||
|
||
from pathlib import Path | ||
|
||
TEST_DATA_PATH = Path(unitTestDataPath()) | ||
|
||
|
||
start_app() | ||
|
||
|
||
class TestVectorTile(unittest.TestCase): | ||
|
||
@classmethod | ||
def setUpClass(cls): | ||
cls.tempdir = Path(tempfile.mkdtemp()) | ||
|
||
@classmethod | ||
def tearDownClass(cls): | ||
shutil.rmtree(cls.tempdir, True) | ||
|
||
def setUp(self): | ||
"""Run before each test.""" | ||
pass | ||
|
||
def tearDown(self): | ||
"""Run after each test.""" | ||
pass | ||
|
||
def testSingleTileEncode(self): | ||
""" Test vector tile encoding from python | ||
""" | ||
vlPoints = QgsVectorLayer(str(TEST_DATA_PATH / "points.shp"), "points", "ogr") | ||
vlLines = QgsVectorLayer(str(TEST_DATA_PATH / "lines.shp"), "lines", "ogr") | ||
vlPolys = QgsVectorLayer(str(TEST_DATA_PATH / "polys.shp"), "polys", "ogr") | ||
|
||
layers = [QgsVectorTileWriter.Layer(vl) for vl in (vlPoints, vlLines, vlPolys)] | ||
|
||
writer = QgsVectorTileWriter() | ||
writer.setMaxZoom(3) | ||
writer.setLayers(layers) | ||
|
||
data = writer.writeSingleTile(QgsTileXYZ(0, 0, 0)) | ||
|
||
ds = QgsDataSourceUri() | ||
ds.setParam("type", "xyz") | ||
ds.setParam("url", (self.tempdir / "{z}-{x}-{y}.pbf").as_uri()) | ||
|
||
# Create pbf files | ||
writer.setDestinationUri(ds.encodedUri().data().decode()) | ||
res = writer.writeTiles() | ||
self.assertEqual(writer.errorMessage(), "") | ||
self.assertTrue(res) | ||
|
||
# Compare encoded data to written file | ||
# Read data from file | ||
with (self.tempdir / "0-0-0.pbf").open("rb") as fp: | ||
output = fp.read() | ||
|
||
# Compare binary data | ||
self.assertEqual(ascii(data.data()), ascii(output)) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |