/
projestions_dialog.py
92 lines (81 loc) · 3.41 KB
/
projestions_dialog.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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
# -*- coding: utf-8 -*-
"""
/***************************************************************************
ProjestionsDialog
A QGIS plugin
projection suggestions
-------------------
begin : 2016-02-29
git sha : $Format:%H$
copyright : (C) 2016 by Eric Brelsford
email : ebrelsford@gmail.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 os
from PyQt5 import QtWidgets, uic
from PyQt5.QtGui import QColor
from qgis.core import (
Qgis,
QgsApplication,
QgsCoordinateReferenceSystem,
QgsGeometry,
QgsRectangle,
QgsVectorLayer,
QgsWkbTypes
)
from qgis.gui import QgsMapCanvas, QgsMapToolPan, QgsRubberBand
FORM_CLASS, _ = uic.loadUiType(os.path.join(
os.path.dirname(__file__), 'projestions_dialog_base.ui'))
class ProjestionsDialog(QtWidgets.QDialog, FORM_CLASS):
def __init__(self, parent=None):
"""Constructor."""
super(ProjestionsDialog, self).__init__(parent)
# Set up the user interface from Designer.
# After setupUI you can access any designer object by doing
# self.<objectname>, and you can use autoconnect slots - see
# http://qt-project.org/doc/qt-4.8/designer-using-a-ui-file.html
# #widgets-and-dialogs-with-auto-connect
self.setupUi(self)
worldMapPath = QgsApplication.instance().pkgDataPath() + '/resources/data/world_map.gpkg'
countriesPath = worldMapPath + '|layername=countries'
statesPath = worldMapPath + '|layername=states_provinces'
self.mapLayers = [
QgsVectorLayer(countriesPath),
QgsVectorLayer(statesPath)
]
self.mapCrs = QgsCoordinateReferenceSystem(
4326,
QgsCoordinateReferenceSystem.EpsgCrsId
)
self.previewBand = QgsRubberBand(self.mAreaCanvas, QgsWkbTypes.PolygonGeometry)
def show(self):
self.mAreaCanvas.setDestinationCrs(self.mapCrs)
self.mAreaCanvas.setLayers(self.mapLayers)
self.mAreaCanvas.zoomToFullExtent()
super().show()
def set_crs(self, crsCode):
crs = QgsCoordinateReferenceSystem(
crsCode,
QgsCoordinateReferenceSystem.EpsgCrsId
)
rect = crs.bounds()
if rect.area() != 0.0:
# Add bounding box to map
geom = QgsGeometry.fromRect(rect)
self.previewBand.setToGeometry(geom)
self.previewBand.setColor(QColor(255, 0, 0, 65))
# Zoom to extent of bounding box
rect.scale(1.1)
self.mAreaCanvas.setExtent(rect)
else:
self.previewBand.hide()
self.mAreaCanvas.zoomToFullExtent()
self.mAreaCanvas.refresh()