Skip to content

Commit

Permalink
Porting to Qgis 3 (#16)
Browse files Browse the repository at this point in the history
* port to qgis 3

* Porting to Qgis 3 rev01

* Removed unnecessary changes for porting

* Rev01 after Tests
  • Loading branch information
jorgealmerio authored and olivierdalang committed Feb 6, 2019
1 parent 4ab0179 commit 63738da
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 66 deletions.
2 changes: 1 addition & 1 deletion __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@
"""
def classFactory(iface):
# load VectorBender class from file VectorBender
from vectorbender import VectorBender
from .vectorbender import VectorBender
return VectorBender(iface)
10 changes: 6 additions & 4 deletions metadata.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@

[general]
name=Vector Bender
qgisMinimumVersion=2.2
qgisMinimumVersion=2.99
qgisMaximumVersion=3.99
description=Does to vectors what georefencers does to raster. This feature is also known as "rubber sheeting".
category=Vector
version=0.1.1
version=3.0.0
author=Olivier Dalang
email=olivier.dalang@gmail.com

Expand All @@ -23,8 +24,9 @@ email=olivier.dalang@gmail.com

# Uncomment the following line and add your changelog entries:
changelog=- 2014-05-22 - Version 0.0 : intial release
- 2014-05-26 - Version 0.1 : linear and uniform transformation method, scipy dependency no more needed, better management of pinned points
- 2014-07-17 - Version 0.1.1 : fixed bug due to misspelling
- 2014-05-26 - Version 0.1 : linear and uniform transformation method, scipy dependency no more needed, better management of pinned points
- 2014-07-17 - Version 0.1.1 : fixed bug due to misspelling
- 2019-01-18 - Version 3.0.0 : Updated to Qgis 3 (Thanks to Jorge Almerio)



Expand Down
40 changes: 20 additions & 20 deletions vectorbender.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@
"""

# Import the PyQt and QGIS libraries
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.PyQt.QtCore import *
from qgis.PyQt.QtWidgets import *
from qgis.PyQt.QtGui import *
from qgis.core import *
from qgis.gui import *

Expand All @@ -40,14 +41,14 @@
if StrictVersion(matplotlib.__version__) < StrictVersion(minVersion):
dependenciesStatus=1
QgsMessageLog.logMessage("Matplotlib version too old (%s instead of %s). You won't be able to use the bending algorithm" % (matplotlib.__version__,minVersion), 'VectorBender')
except Exception, e:
except Exception:
QgsMessageLog.logMessage("Matplotlib is missing. You won't be able to use the bending algorithm", 'VectorBender')
dependenciesStatus = 0

# Other classes
from vectorbendertransformers import *
from vectorbenderdialog import VectorBenderDialog
from vectorbenderhelp import VectorBenderHelp
from .vectorbendertransformers import *
from .vectorbenderdialog import VectorBenderDialog
from .vectorbenderhelp import VectorBenderHelp

class VectorBender:

Expand Down Expand Up @@ -103,16 +104,15 @@ def determineTransformationType(self):
0 if no pairs Found
1 if one pair found => translation
2 if two pairs found => linear
3 if two pairs found => linear
4 if three or more pairs found => bending
5 if bending but unmet dependencies"""
3 if three pairs found => affine
4 if four or more pairs found => bending"""

pairsLayer = self.dlg.pairsLayer()

if pairsLayer is None:
return 0

featuresCount = len(pairsLayer.selectedFeaturesIds()) if self.dlg.restrictBox_pairsLayer.isChecked() else len(pairsLayer.allFeatureIds())
featuresCount = len(pairsLayer.selectedFeatureIds()) if self.dlg.restrictBox_pairsLayer.isChecked() else len(pairsLayer.allFeatureIds())

if featuresCount == 1:
return 1
Expand Down Expand Up @@ -159,7 +159,7 @@ def run(self):
# Starting to iterate
features = toBendLayer.getFeatures() if not self.dlg.restrictBox_toBendLayer.isChecked() else toBendLayer.selectedFeatures()

count = toBendLayer.pendingFeatureCount() if not self.dlg.restrictBox_toBendLayer.isChecked() else len(features)
count = toBendLayer.featureCount() if not self.dlg.restrictBox_toBendLayer.isChecked() else len(features)
self.dlg.displayMsg( "Starting to iterate through %i features..." % count )
QCoreApplication.processEvents()

Expand All @@ -174,7 +174,7 @@ def run(self):

#TODO : this cood be much simple if we could iterate through to vertices and use QgsGeometry.moveVertex(x,y,index), but QgsGeometry.vertexAt(index) doesn't tell wether the index exists, so there's no clean way to iterate...

if geom.type() == QGis.Point:
if geom.type() == QgsWkbTypes.PointGeometry:

if not geom.isMultipart():
# SINGLE PART POINT
Expand All @@ -189,15 +189,15 @@ def run(self):
newListA.append( self.transformer.map(p) )
newGeom = QgsGeometry.fromMultiPoint( newListA )

elif geom.type() == QGis.Line:
elif geom.type() == QgsWkbTypes.LineGeometry:

if not geom.isMultipart():
# SINGLE PART LINESTRING
listA = geom.asPolyline()
newListA = []
for p in listA:
newListA.append( self.transformer.map(p) )
newGeom = QgsGeometry.fromPolyline( newListA )
newGeom = QgsGeometry.fromPolylineXY( newListA )

else:
# MULTI PART LINESTRING
Expand All @@ -208,9 +208,9 @@ def run(self):
for p in listB:
newListB.append( self.transformer.map(p) )
newListA.append( newListB )
newGeom = QgsGeometry.fromMultiPolyline( newListA )
newGeom = QgsGeometry.fromMultiPolylineXY( newListA )

elif geom.type() == QGis.Polygon:
elif geom.type() == QgsWkbTypes.PolygonGeometry:

if not geom.isMultipart():
# SINGLE PART POLYGON
Expand All @@ -221,7 +221,7 @@ def run(self):
for p in listB:
newListB.append( self.transformer.map(p) )
newListA.append( newListB )
newGeom = QgsGeometry.fromPolygon( newListA )
newGeom = QgsGeometry.fromPolygonXY( newListA )

else:
# MULTI PART POLYGON
Expand All @@ -235,7 +235,7 @@ def run(self):
newListC.append( self.transformer.map(p) )
newListB.append( newListC )
newListA.append( newListB )
newGeom = QgsGeometry.fromMultiPolygon( newListA )
newGeom = QgsGeometry.fromMultiPolygonXY( newListA )

else:
# FALLBACK, JUST IN CASE ;)
Expand All @@ -252,7 +252,7 @@ def run(self):

features = pairsLayer.getFeatures() if not self.dlg.restrictBox_pairsLayer.isChecked() else pairsLayer.selectedFeatures()

count = pairsLayer.pendingFeatureCount() if not self.dlg.restrictBox_pairsLayer.isChecked() else len(features)
count = pairsLayer.featureCount() if not self.dlg.restrictBox_pairsLayer.isChecked() else len(features)
self.dlg.progressBar.setValue( 0 )
self.dlg.displayMsg( "Starting to transform %i pairs to pins..." % count )
QCoreApplication.processEvents()
Expand All @@ -266,7 +266,7 @@ def run(self):

geom = feature.geometry().asPolyline()

newGeom = QgsGeometry.fromPolyline( [geom[-1],geom[-1]] )
newGeom = QgsGeometry.fromPolylineXY( [geom[-1],geom[-1]] )
pairsLayer.changeGeometry( feature.id(), newGeom )

pairsLayer.endEditCommand()
Expand Down
42 changes: 21 additions & 21 deletions vectorbenderdialog.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
# -*- coding: utf-8 -*-

from PyQt4 import uic
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.PyQt import uic, QtWidgets
from qgis.PyQt.QtWidgets import QMessageBox
from qgis.PyQt.QtCore import *
from qgis.PyQt.QtGui import *

from qgis.core import *
from qgis.gui import *

import os.path

from vectorbendertransformers import *
from .vectorbendertransformers import *


class VectorBenderDialog(QWidget):
class VectorBenderDialog(QtWidgets.QDialog):
def __init__(self, iface, vb):
QWidget.__init__(self)
QtWidgets.QDialog.__init__(self)
uic.loadUi(os.path.join(os.path.dirname(__file__),'ui_main.ui'), self)
self.setFocusPolicy(Qt.ClickFocus)
#self.setWindowModality( Qt.ApplicationModal )
Expand Down Expand Up @@ -58,13 +58,13 @@ def toBendLayer(self):
Returns the current toBend layer depending on what is choosen in the comboBox_pairsLayer
"""
layerId = self.comboBox_toBendLayer.itemData(self.comboBox_toBendLayer.currentIndex())
return QgsMapLayerRegistry.instance().mapLayer(layerId)
return QgsProject.instance().mapLayer(layerId)
def pairsLayer(self):
"""
Returns the current pairsLayer layer depending on what is choosen in the comboBox_pairsLayer
"""
layerId = self.comboBox_pairsLayer.itemData(self.comboBox_pairsLayer.currentIndex())
return QgsMapLayerRegistry.instance().mapLayer(layerId)
return QgsProject.instance().mapLayer(layerId)
def bufferValue(self):
"""
Returns the current buffer value depending on the input in the spinbox
Expand Down Expand Up @@ -126,10 +126,10 @@ def updateLayersComboboxes(self):

self.comboBox_toBendLayer.clear()
self.comboBox_pairsLayer.clear()
for layer in self.iface.legendInterface().layers():
for layer in QgsProject.instance().mapLayers().values():
if layer.type() == QgsMapLayer.VectorLayer:
self.comboBox_toBendLayer.addItem( layer.name(), layer.id() )
if layer.geometryType() == QGis.Line :
if layer.geometryType() == QgsWkbTypes.LineGeometry :
self.comboBox_pairsLayer.addItem( layer.name(), layer.id() )

if oldBendLayer is not None:
Expand Down Expand Up @@ -190,13 +190,13 @@ def createMemoryLayer(self):

suffix = ""
name = "Vector Bender"
while len( QgsMapLayerRegistry.instance().mapLayersByName( name+suffix ) ) > 0:
while len( QgsProject.instance().mapLayersByName( name+suffix ) ) > 0:
if suffix == "": suffix = " 1"
else: suffix = " "+str(int(suffix)+1)

newMemoryLayer = QgsVectorLayer("Linestring", name+suffix, "memory")
newMemoryLayer.loadNamedStyle(os.path.join(os.path.dirname(__file__),'PairStyle.qml'), False)
QgsMapLayerRegistry.instance().addMapLayer(newMemoryLayer)
QgsProject.instance().addMapLayer(newMemoryLayer)

self.updateLayersComboboxes()

Expand All @@ -211,17 +211,17 @@ def displayMsg(self, msg, error=False):
self.statusLabel.setText( msg )
def hidePreview(self):
if self.rubberBands is not None:
self.rubberBands[0].reset(QGis.Polygon)
self.rubberBands[1].reset(QGis.Polygon)
self.rubberBands[2].reset(QGis.Polygon)
self.rubberBands[0].reset(QgsWkbTypes.PolygonGeometry)
self.rubberBands[1].reset(QgsWkbTypes.PolygonGeometry)
self.rubberBands[2].reset(QgsWkbTypes.PolygonGeometry)
self.rubberBands = None
def showPreview(self):

self.rubberBands = (QgsRubberBand(self.iface.mapCanvas(), QGis.Polygon),QgsRubberBand(self.iface.mapCanvas(), QGis.Polygon),QgsRubberBand(self.iface.mapCanvas(), QGis.Polygon))
self.rubberBands = (QgsRubberBand(self.iface.mapCanvas(), QgsWkbTypes.PolygonGeometry),QgsRubberBand(self.iface.mapCanvas(), QgsWkbTypes.PolygonGeometry),QgsRubberBand(self.iface.mapCanvas(), QgsWkbTypes.PolygonGeometry))

self.rubberBands[0].reset(QGis.Polygon)
self.rubberBands[1].reset(QGis.Polygon)
self.rubberBands[2].reset(QGis.Polygon)
self.rubberBands[0].reset(QgsWkbTypes.PolygonGeometry)
self.rubberBands[1].reset(QgsWkbTypes.PolygonGeometry)
self.rubberBands[2].reset(QgsWkbTypes.PolygonGeometry)

pairsLayer = self.pairsLayer()

Expand Down Expand Up @@ -263,7 +263,7 @@ def showPreview(self):

# Events
def eventFilter(self,object,event):
if event.type() == QEvent.FocusIn:
if event.type() == QEvent.WindowActivate:
self.refreshStates()
return False

Expand Down
10 changes: 6 additions & 4 deletions vectorbenderhelp.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,19 @@
***************************************************************************/
"""
# Import the PyQt and QGIS libraries
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.PyQt import QtWidgets
from qgis.PyQt.QtWidgets import QTextBrowser, QVBoxLayout, QPushButton
from qgis.PyQt.QtCore import *
from qgis.PyQt.QtGui import *
from qgis.core import *

# Basic dependencies
import os.path

class VectorBenderHelp(QDialog):
class VectorBenderHelp(QtWidgets.QDialog):

def __init__(self):
QDialog.__init__(self)
QtWidgets.QDialog.__init__(self)

self.setMinimumWidth(600)
self.setMinimumHeight(450)
Expand Down
Loading

0 comments on commit 63738da

Please sign in to comment.