Newer
Older
# -*- coding: utf-8 -*-
"""
***************************************************************************
---------------------
Date : 30.11.2017
Copyright : (C) 2017 by Benjamin Jakimow
Email : benjamin jakimow at geo dot hu-berlin dot de
***************************************************************************
* *
* 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. *
* *
***************************************************************************
"""
# noinspection PyPep8Naming
import os
import sys
import configparser
import xmlrunner
from eotimeseriesviewer.tests import start_app, testRasterFiles, EOTSVTestCase
from qgis.PyQt.QtGui import *
from qgis.PyQt.QtCore import *
from qgis.core import *
from qgis.gui import *
from eotimeseriesviewer.mapcanvas import *
from eotimeseriesviewer.tests import TestObjects

Benjamin Jakimow
committed
from eotimeseriesviewer.main import EOTimeSeriesViewer
class TestQGISInteraction(EOTSVTestCase):
"""Test that the plugin init is usable for QGIS.
Based heavily on the validator class by Alessandro
Passoti available here:
http://github.com/qgis/qgis-django/blob/master/qgis-app/
plugins/validator.py
"""
def test_syncExtents(self):

Benjamin Jakimow
committed
TSV.loadExampleTimeSeries(loadAsync=False)
QApplication.processEvents()
from example import exampleEvents
lyr = QgsVectorLayer(exampleEvents)
QgsProject.instance().addMapLayer(lyr)
from qgis.utils import iface
self.assertIsInstance(iface, QgisInterface)
qgisCanvas = iface.mapCanvas()
world = SpatialExtent.world()
qgisCanvas.setDestinationCrs(world.crs())
qgisCanvas.setExtent(world)

Benjamin Jakimow
committed
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
def moveCanvasToCorner(canvas: QgsMapCanvas, pos:str) -> SpatialPoint:
pos = pos.upper()
assert pos in ['UL', 'LR', 'UR', 'LL']
assert isinstance(canvas, QgsMapCanvas)
m2p: QgsMapToPixel = canvas.mapSettings().mapToPixel()
tol = m2p.toMapCoordinates(0,0) - m2p.toMapCoordinates(1,1)
center = canvas.center()
crs: QgsCoordinateReferenceSystem = canvas.mapSettings().destinationCrs()
bounds : QgsRectangle = crs.bounds()
w = canvas.width()
h = canvas.height()
if pos == 'UL':
newCenter = m2p.toMapCoordinates(1, 1)
elif pos == 'LR':
newCenter = m2p.toMapCoordinates(w-1, h-1)
elif pos == 'UR':
newCenter = m2p.toMapCoordinates(w-1, 0)
elif pos == 'LL':
newCenter = m2p.toMapCoordinates(0, h-1)
else:
raise NotImplementedError()
if newCenter.x() < bounds.xMinimum():
newCenter.setX(bounds.xMinimum() + tol.x())
elif newCenter.x() > bounds.xMaximum():
newCenter.setX(bounds.xMaximum() - tol.x())
if newCenter.y() < bounds.yMinimum():
newCenter.setX(bounds.yMinimum() + tol.y())
elif newCenter.y() > bounds.yMaximum():
newCenter.setX(bounds.yMaximum() - tol.y())
canvas.setCenter(newCenter)
return SpatialPoint.fromMapCanvasCenter(canvas)
TSV.ui.optionSyncMapCenter.setChecked(True)
TSV.mapWidget().timedRefresh()
# 1. move QGIS
pt = moveCanvasToCorner(qgisCanvas, 'UL')
self.assertTrue(qgisCanvas.mapSettings().destinationCrs().isValid())
self.assertIsInstance(qgisCanvas, QgsMapCanvas)

Benjamin Jakimow
committed
TSV.mapWidget().timedRefresh()
pt2 = TSV.spatialCenter().toCrs(pt.crs())
s = ""
self.assertIsInstance(extent, SpatialExtent)
center = extent.spatialCenter()
self.assertIsInstance(center, SpatialPoint)

Benjamin Jakimow
committed
TSV.close()
if __name__ == '__main__':
unittest.main(testRunner=xmlrunner.XMLTestRunner(output='test-reports'), buffer=False)
exit(0)