~ubuntu-branches/ubuntu/wily/qgis/wily

« back to all changes in this revision

Viewing changes to python/plugins/fTools/tools/doDefineProj.py

  • Committer: Package Import Robot
  • Author(s): Francesco Paolo Lovergine
  • Date: 2012-04-24 15:12:20 UTC
  • mfrom: (3.1.9 sid)
  • Revision ID: package-import@ubuntu.com-20120424151220-r88g00af5fpn5fc3
Tags: 1.7.4+1.7.5~20120320-1
The "Sometimes they come back" release.

* Branching from Qgis tree and adapting to current Debian Policy and
  standards. The target tree is currently set to release-1.7.
  (closes: #661491, #606304, #615683, #616182, #600308)
* Policy bumped to 3.9.3.
* Moving to debhelper compatibility level 9.
* Source format is now 3.0 with quilt support.
* Merged with 2bf42287 upstream git snapshot.
* Migrated to dh_python2 instead of python-central.
  (closes: #617048)
* Snapshot in qgis.org release-1.7: c936d031
* Added an automagic creation of a lintian override for sqlite embedding.
  This is required for uploading currently.
* Added missing ${misc:Depends} to make lintian happy.
* Copyright notes updated and debian/copyright moved to format 1.0.
* More licenses notices now reported in debian/copyright. Thanks ftpmasters.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
#-----------------------------------------------------------
 
3
#
 
4
# fTools
 
5
# Copyright (C) 2008-2011  Carson Farmer
 
6
# EMAIL: carson.farmer (at) gmail.com
 
7
# WEB  : http://www.ftools.ca/fTools.html
 
8
#
 
9
# A collection of data management and analysis tools for vector data
 
10
#
 
11
#-----------------------------------------------------------
 
12
#
 
13
# licensed under the terms of GNU GPL 2
 
14
#
 
15
# This program is free software; you can redistribute it and/or modify
 
16
# it under the terms of the GNU General Public License as published by
 
17
# the Free Software Foundation; either version 2 of the License, or
 
18
# (at your option) any later version.
 
19
#
 
20
# This program is distributed in the hope that it will be useful,
 
21
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
22
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
23
# GNU General Public License for more details.
 
24
#
 
25
# You should have received a copy of the GNU General Public License along
 
26
# with this program; if not, write to the Free Software Foundation, Inc.,
 
27
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 
28
#
 
29
#---------------------------------------------------------------------
 
30
 
1
31
from PyQt4.QtCore import *
2
32
from PyQt4.QtGui import *
3
33
 
4
34
from qgis.core import *
5
35
from qgis.gui import *
6
 
 
 
36
import ftools_utils
7
37
from ui_frmReProject import Ui_Dialog
8
38
 
9
39
class Dialog(QDialog, Ui_Dialog):
10
 
        def __init__(self, iface):
11
 
                QDialog.__init__(self)
12
 
                self.iface = iface
13
 
                self.setupUi(self)
14
 
                self.toolOut.setEnabled(False)
15
 
                self.toolOut.setVisible(False)
16
 
                self.outShape.setEnabled(False)
17
 
                self.outShape.setVisible(False)
18
 
                self.label_2.setVisible(False)
19
 
                self.label_2.setEnabled(False)
20
 
                self.setWindowTitle("Define current projection")
21
 
                QObject.connect(self.btnProjection, SIGNAL("clicked()"), self.outProjFile)
22
 
                QObject.connect(self.inShape, SIGNAL("currentIndexChanged(QString)"), self.updateProj1)
23
 
                QObject.connect(self.cmbLayer, SIGNAL("currentIndexChanged(QString)"), self.updateProj2)
24
 
                # populate layer list
25
 
                self.progressBar.setValue(0)
26
 
                mapCanvas = self.iface.mapCanvas()
27
 
                for i in range(mapCanvas.layerCount()):
28
 
                        layer = mapCanvas.layer(i)
29
 
                        if layer.type() == layer.VectorLayer:
30
 
                                self.inShape.addItem(layer.name())
31
 
                                self.cmbLayer.addItem(layer.name())
32
 
 
33
 
        def updateProj1(self, layerName):
34
 
                tempLayer = self.getVectorLayerByName(layerName)
35
 
                crs = tempLayer.dataProvider().crs().toProj4()
36
 
                self.inRef.insert(unicode(crs)) 
37
 
 
38
 
        def updateProj2(self, layerName):
39
 
                tempLayer = self.getVectorLayerByName(layerName)
40
 
                crs = tempLayer.dataProvider().crs().toProj4()
41
 
                self.outRef.insert(unicode(crs))        
42
 
 
43
 
        def accept(self):
44
 
                if self.inShape.currentText() == "":
45
 
                        QMessageBox.information(self, "Define current projection", "No input shapefile specified")
46
 
                elif self.txtProjection.text() == "" and self.rdoProjection.isChecked():
47
 
                        QMessageBox.information(self, "Define current projection", "Please specify spatial reference system")
48
 
                elif self.cmbLayer.currentText() == "" and self.rdoLayer.isChecked():
49
 
                        QMessageBox.information(self, "Define current projection", "Please specify spatial reference system")
50
 
                else:
51
 
                        self.progressBar.setValue(5)
52
 
                        inName = self.inShape.currentText()
53
 
                        self.progressBar.setValue(10)
54
 
                        vLayer = self.getVectorLayerByName(inName)
55
 
                        self.progressBar.setValue(30)
56
 
                        if vLayer == "Error":
57
 
                                QMessageBox.information(self, "Define current projection", "Cannot define projection for PostGIS data...yet!")
58
 
                        else:
59
 
                                srsDefine = QgsCoordinateReferenceSystem()
60
 
                                if self.rdoProjection.isChecked():
61
 
                                        outProj = self.txtProjection.text()
62
 
                                        srsDefine.createFromProj4(outProj)
63
 
                                else:
64
 
                                        destLayer = self.getVectorLayerByName(self.cmbLayer.currentText())
65
 
                                        srsDefine = destLayer.srs()
66
 
                                if srsDefine == vLayer.srs():
67
 
                                        QMessageBox.information(self, "Define current projection", "Identical output spatial reference system chosen")
68
 
                                else:
69
 
                                        provider = vLayer.dataProvider()
70
 
                                        self.progressBar.setValue(35)
71
 
                                        inPath = provider.dataSourceUri()
72
 
                                        self.progressBar.setValue(40)
73
 
                                        if inPath.endsWith(".shp"):
74
 
                                                inPath = inPath.left(inPath.length() - 4)
75
 
                                        self.progressBar.setValue(55)
76
 
                                        if not srsDefine.isValid():
77
 
                                                QMessageBox.information(self, "Define current projection", "Output spatial reference system is not valid")
78
 
                                        else:
79
 
                                                self.progressBar.setValue(60)
80
 
                                                outputWkt = srsDefine.toWkt()
81
 
                                                self.progressBar.setValue(65)
82
 
                                                outputPrj = open(inPath + ".prj", "w")
83
 
                                                self.progressBar.setValue(70)
84
 
                                                outputPrj.write(outputWkt)
85
 
                                                self.progressBar.setValue(75)
86
 
                                                outputPrj.close()
87
 
                                                mLayer = self.getMapLayerByName(inName)
88
 
                                                self.progressBar.setValue(90)
89
 
                                                if not mLayer.isValid():
90
 
                                                        QMessageBox.information(self, "Define current projection", "Unable to dynamically define projection.\n"
91
 
                                                + "Please reload layer manually for projection definition to take effect.")
92
 
                                                else:
93
 
                                                        self.progressBar.setValue(95)
94
 
                                                        mLayer.setCrs(srsDefine)
95
 
                                                        self.progressBar.setValue(100)
96
 
                                                        QMessageBox.information(self, "Define current projection", "Defined Projection For:\n" + inPath + ".shp")
97
 
                self.progressBar.setValue(0)
98
 
 
99
 
        def outProjFile(self):
100
 
                format = QString( "<h2>%1</h2>%2 <br/> %3" )
101
 
                header = QString( "Define layer CRS:" )
102
 
                sentence1 = QString( "Please select the projection system that defines the current layer." )
103
 
                sentence2 = QString( "Layer CRS information will be updated to the selected CRS." )
104
 
                self.projSelect = QgsGenericProjectionSelector(self, Qt.Widget)
105
 
                self.projSelect.setMessage( format.arg( header ).arg( sentence1 ).arg( sentence2 ))
106
 
                if self.projSelect.exec_():
107
 
                        projString = self.projSelect.selectedProj4String()
108
 
                        if projString == "":
109
 
                                QMessageBox.information(self, "Export to new projection", "No Valid CRS selected")
110
 
                                return
111
 
                        else:
112
 
                                self.txtProjection.clear()
113
 
                                self.txtProjection.insert(projString)
114
 
                else:
115
 
                        return
116
 
 
117
 
# Gets vector layer by layername in canvas
118
 
        def getVectorLayerByName(self, myName):
119
 
                mc = self.iface.mapCanvas()
120
 
                nLayers = mc.layerCount()
121
 
                for l in range(nLayers):
122
 
                        layer = mc.layer(l)
123
 
                        if layer.name() == unicode(myName,'latin1'):
124
 
                                if unicode(layer.dataProvider().name()) == "postgres":
125
 
                                        return "Error"
126
 
                                else:
127
 
                                        vlayer = QgsVectorLayer(unicode(layer.source()),  unicode(myName),  unicode(layer.dataProvider().name()))
128
 
                                        if vlayer.isValid():
129
 
                                                return vlayer
130
 
 
131
 
#Gets map layer by layername in canvas
132
 
#Return: QgsMapLayer
133
 
        def getMapLayerByName(self,myName):
134
 
                mc = self.iface.mapCanvas()
135
 
                nLayers = mc.layerCount()
136
 
                for l in range(nLayers):
137
 
                        layer = mc.layer(l)
138
 
                        if layer.name() == unicode(myName,'latin1'):
139
 
                                return layer
 
40
    def __init__(self, iface):
 
41
        QDialog.__init__(self)
 
42
        self.iface = iface
 
43
        self.setupUi(self)
 
44
        self.toolOut.setEnabled(False)
 
45
        self.toolOut.setVisible(False)
 
46
        self.outShape.setEnabled(False)
 
47
        self.outShape.setVisible(False)
 
48
        self.label_2.setVisible(False)
 
49
        self.label_2.setEnabled(False)
 
50
        self.setWindowTitle(self.tr("Define current projection"))
 
51
        self.buttonOk = self.buttonBox_2.button( QDialogButtonBox.Ok )
 
52
        QObject.connect(self.btnProjection, SIGNAL("clicked()"), self.outProjFile)
 
53
        QObject.connect(self.inShape, SIGNAL("currentIndexChanged(QString)"), self.updateProj1)
 
54
        QObject.connect(self.cmbLayer, SIGNAL("currentIndexChanged(QString)"), self.updateProj2)
 
55
        # populate layer list
 
56
        self.progressBar.setValue(0)
 
57
        mapCanvas = self.iface.mapCanvas()
 
58
        layers = ftools_utils.getLayerNames([QGis.Point, QGis.Line, QGis.Polygon])
 
59
        self.inShape.addItems(layers)
 
60
        self.cmbLayer.addItems(layers)
 
61
 
 
62
    def updateProj1(self, layerName):
 
63
        self.inRef.clear()
 
64
        tempLayer = ftools_utils.getVectorLayerByName(layerName)
 
65
        crs = tempLayer.dataProvider().crs()
 
66
        self.inRef.insert(crs.authid() + " - " +  crs.description())
 
67
 
 
68
    def updateProj2(self, layerName):
 
69
        self.outRef.clear()
 
70
        tempLayer = ftools_utils.getVectorLayerByName(layerName)
 
71
        crs = tempLayer.dataProvider().crs()
 
72
        self.outRef.insert(crs.authid() + " - " +  crs.description())
 
73
 
 
74
    def accept(self):
 
75
        self.buttonOk.setEnabled( False )
 
76
        if self.inShape.currentText() == "":
 
77
            QMessageBox.information(self, self.tr("Define current projection"), self.tr("No input shapefile specified"))
 
78
        elif self.txtProjection.text() == "" and self.rdoProjection.isChecked():
 
79
            QMessageBox.information(self, self.tr("Define current projection"), self.tr("Please specify spatial reference system"))
 
80
        elif self.cmbLayer.currentText() == "" and self.rdoLayer.isChecked():
 
81
            QMessageBox.information(self, self.tr("Define current projection"), self.tr("Please specify spatial reference system"))
 
82
        else:
 
83
            self.progressBar.setValue(5)
 
84
            inName = self.inShape.currentText()
 
85
            self.progressBar.setValue(10)
 
86
            vLayer = ftools_utils.getVectorLayerByName(inName)
 
87
            self.progressBar.setValue(30)
 
88
            if vLayer == "Error":
 
89
                QMessageBox.information(self, self.tr("Define current projection"), self.tr("Cannot define projection for PostGIS data...yet!"))
 
90
            else:
 
91
                srsDefine = QgsCoordinateReferenceSystem()
 
92
                if self.rdoProjection.isChecked():
 
93
                    outProj = self.txtProjection.text()
 
94
                    srsDefine.createFromProj4(outProj)
 
95
                else:
 
96
                    destLayer = ftools_utils.getVectorLayerByName(self.cmbLayer.currentText())
 
97
                    srsDefine = destLayer.crs()
 
98
                if srsDefine == vLayer.crs():
 
99
                    responce = QMessageBox.question(self, self.tr("Define current projection"),
 
100
                    self.tr("Identical output spatial reference system chosen\n\nAre you sure you want to proceed?"),
 
101
                    QMessageBox.Yes, QMessageBox.No, QMessageBox.NoButton)
 
102
                    if responce == QMessageBox.No:
 
103
                        return
 
104
                provider = vLayer.dataProvider()
 
105
                self.progressBar.setValue(35)
 
106
                inPath = provider.dataSourceUri()
 
107
                inPath = inPath.remove( QRegExp( "\|.*" ) )
 
108
                self.progressBar.setValue(40)
 
109
                if inPath.endsWith(".shp"):
 
110
                    inPath = inPath.left(inPath.length() - 4)
 
111
                self.progressBar.setValue(55)
 
112
                if not srsDefine.isValid():
 
113
                    QMessageBox.information(self, self.tr("Define current projection"), self.tr("Output spatial reference system is not valid"))
 
114
                else:
 
115
                    self.progressBar.setValue(60)
 
116
                    outputWkt = srsDefine.toWkt()
 
117
                    self.progressBar.setValue(65)
 
118
                    outputFile = QFile( inPath + ".prj" )
 
119
                    outputFile.open( QIODevice.WriteOnly | QIODevice.Text )
 
120
                    outputPrj = QTextStream( outputFile )
 
121
                    outputPrj << outputWkt
 
122
                    outputPrj.flush()
 
123
                    outputFile.close()
 
124
                    self.progressBar.setValue(70)
 
125
                    checkFile = QFile( inPath + ".qpj" )
 
126
                    if checkFile.exists():
 
127
                        checkFile.open( QIODevice.WriteOnly | QIODevice.Text )
 
128
                        outputPrj = QTextStream( checkFile )
 
129
                        outputPrj << outputWkt
 
130
                        outputPrj.flush()
 
131
                        checkFile.close()
 
132
                    self.progressBar.setValue(95)
 
133
                    vLayer.setCrs(srsDefine)
 
134
                    self.progressBar.setValue(100)
 
135
                    QMessageBox.information(self, self.tr("Define current projection"), self.tr("Defined Projection For:\n%1.shp").arg( inPath ) )
 
136
        self.progressBar.setValue(0)
 
137
        self.buttonOk.setEnabled( True )
 
138
 
 
139
    def outProjFile(self):
 
140
        format = QString( "<h2>%1</h2>%2 <br/> %3" )
 
141
        header = QString( "Define layer CRS:" )
 
142
        sentence1 = self.tr( "Please select the projection system that defines the current layer." )
 
143
        sentence2 = self.tr( "Layer CRS information will be updated to the selected CRS." )
 
144
        projSelector = QgsGenericProjectionSelector(self)
 
145
        projSelector.setMessage( format.arg( header ).arg( sentence1 ).arg( sentence2 ))
 
146
        if projSelector.exec_():
 
147
            crs = QgsCoordinateReferenceSystem()
 
148
            crs.createFromOgcWmsCrs( projSelector.selectedAuthId() )
 
149
            if projSelector.selectedAuthId().isEmpty():
 
150
                QMessageBox.information(self, self.tr("Export to new projection"), self.tr("No Valid CRS selected"))
 
151
                return
 
152
            else:
 
153
                self.txtProjection.clear()
 
154
                self.txtProjection.insert(crs.authid() + " - " + crs.description())
 
155
        else:
 
156
            return