-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsynccomposerwithmap.py
153 lines (120 loc) · 6.31 KB
/
synccomposerwithmap.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# -*- coding: utf-8 -*-
"""
/***************************************************************************
syncComposerWithMap
A QGIS plugin
Sync the map canvas extents with composer map
-------------------
begin : 2014-07-08
copyright : (C) 2014 by Ed Boesenberg
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. *
* *
***************************************************************************/
"""
# To do:
# Test for active composers
# Below helped me to figure out where to start
# http://gis.stackexchange.com/questions/2515/altering-composer-label-items-in-qgis-with-python
# Import the PyQt and QGIS libraries
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.core import *
from qgis.utils import *
from qgis.gui import QgsMessageBar
# Initialize Qt resources from file resources.py
import resources_rc
# Import the code for the dialog
# from synccomposerwithmapdialog import syncComposerWithMapDialog
import os.path
import sys
class syncComposerWithMap:
def __init__(self, iface):
# Save reference to the QGIS interface
self.iface = iface
# initialize plugin directory
self.plugin_dir = os.path.dirname(__file__)
# initialize locale
locale = QSettings().value("locale/userLocale")[0:2]
localePath = os.path.join(self.plugin_dir, 'i18n', 'synccomposerwithmap_{}.qm'.format(locale))
if os.path.exists(localePath):
self.translator = QTranslator()
self.translator.load(localePath)
if qVersion() > '4.3.3':
QCoreApplication.installTranslator(self.translator)
# Create the dialog (after translation) and keep reference
# self.dlg = syncComposerWithMapDialog()
def initGui(self):
# Create action that will start plugin configuration
self.action = QAction(
QIcon(":/plugins/synccomposerwithmap/icon.png"),
u"Sync Composer With Map", self.iface.mainWindow())
# connect the action to the run method
self.action.triggered.connect(self.run)
# Add toolbar button and menu item
self.iface.addToolBarIcon(self.action)
self.iface.addPluginToMenu(u"&Sync Composer With Map", self.action)
def unload(self):
# Remove the plugin menu item and icon
self.iface.removePluginMenu(u"&Sync Composer With Map", self.action)
self.iface.removeToolBarIcon(self.action)
# run method that performs all the real work
def run(self):
#TO DO: Add something for signal and slot
#get canvas
canvas = self.iface.mapCanvas()
#get active composers in a list
composerList = self.iface.activeComposers()
#check for Active Composers
if len(composerList) > 0:
#get first object in list
composerView = composerList[0]
#get the composition object
composition = composerView.composition()
#http://gis.stackexchange.com/questions/109335/how-to-test-for-multiple-map-items-in-composer-using-python
#get all maps in composer
maps = [item for item in composition.items() if item.type() == QgsComposerItem.ComposerMap and item.scene()]
if len(maps) > 0:
#get all selected map in composer
selMaps = [item for item in composition.selectedItems() if item.type() == QgsComposerItem.ComposerMap and item.scene()]
#check for selected maps
if len(selMaps) > 0:
#Set map to first selected map found
map = selMaps[0]
message = "Map Canvas extents and scale have been synchronized with Selected Map in Composer"
else:
#If no maps are selected than set map to first found
map = maps[0]
message = "Map Canvas extents and scale have been synchronized with first map found in Composer"
#print str(map)
#calculate x move distance
moveX = map.extent().center().x()-canvas.extent().center().x()
#print "Move X = " + str(moveX)
#calculate y move distance
moveY = map.extent().center().y()-canvas.extent().center().y()
#print "Move Y = " + str(moveY)
#Get units conversion
unitCon = map.mapUnitsToMM()
#print "Conversion: " + str (unitCon)
try:
#Move composer map to equal canvas map
map.moveContent(-moveX * unitCon, moveY * unitCon)
#map.moveContent(-100 * unitCon, 100 * unitCon)
#set new composer scale
map.setNewScale(canvas.scale())
#put a nice message on canvas
iface.messageBar().pushMessage("Sync Composer with Map",message , QgsMessageBar.INFO, 2)
except:
iface.messageBar().pushMessage("Sync Composer with Map","Something went wrong", QgsMessageBar.WARNING, 3)
#No Composers
else:
iface.messageBar().pushMessage("Sync Composer with Map","There are no maps in Composer", QgsMessageBar.WARNING, 3)
#No Active Composer Found
else:
iface.messageBar().pushMessage("Sync Composer with Map","There are no active composers", QgsMessageBar.WARNING, 3)