Skip to content

Commit

Permalink
Port to QGIS 3 (#69)
Browse files Browse the repository at this point in the history
* added first working version for QGIS3 - loads properly but need to fix and ensure all works

* renamed files and changed code to be compliant to the new API

* Reformat code so that test can pass

* fix for selecting feature when editng parcels

* fix no database error

* update API call

* First iteration for porting to QGIS3 (#67)

* added qgis projects and sql for creating database

* updated metadata.txt, although how does it interact with metadata from Kartoza plugin repo?

* renamed folders and updated the sql file removing tables which are not needed by the plugin. Disabbled beacons trigger to revert when Gavin confirms its working properly

* added materialised views which I had deleted whilst testing failure for beacons delete

* modified script to create tables  properly

* modified function for deleting beacons

* added about tag

* Modified metadata for upload to QGIS plugin repository

* Removed database dumb from plugin

* added licence info into the plugin

* removed details that were duplicated in readme

* modified toolbar name to cogo

* fix lambda function error when editing parcel

* added first working version for QGIS3 - loads properly but need to fix and ensure all works

* renamed files and changed code to be compliant to the new API

* Reformat code so that test can pass

* fix for selecting feature when editng parcels

* fix no database error

* update API call
  • Loading branch information
mazano authored Jan 22, 2019
1 parent 99b3f1d commit 6d776b6
Show file tree
Hide file tree
Showing 11 changed files with 218 additions and 212 deletions.
2 changes: 1 addition & 1 deletion __init__.py
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,5 @@ def email():

def classFactory(iface):
# load SMLSurveyor class from file SMLSurveyor
from plugin import SMLSurveyor
from .plugin import SMLSurveyor
return SMLSurveyor(iface)
193 changes: 84 additions & 109 deletions PyQt4Dialogs.py → cogo_dialogs.py

Large diffs are not rendered by default.

7 changes: 3 additions & 4 deletions PyQt4Widgets.py → cogo_widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@
***************************************************************************/
"""

from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.gui import *
from qgis.core import *
from PyQt5.QtGui import QCursor
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QPushButton, QDialogButtonBox


class XQPushButton(QPushButton):
Expand Down
8 changes: 5 additions & 3 deletions database.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@
* *
***************************************************************************/
"""
from builtins import str
from builtins import object
import psycopg2


class Field:
class Field(object):

def __init__(self, name, type, required, unique):
self.name = name
Expand All @@ -27,7 +29,7 @@ def __init__(self, name, type, required, unique):
self.unique = unique


class Manager:
class Manager(object):

def __init__(self, parameters):
# test db settings
Expand Down Expand Up @@ -156,7 +158,7 @@ def get_schema(self, table_name, field_ignore):
"ON u.column_name = c.column_name "
"WHERE c.table_name = '{table}' "
"AND c.column_name NOT IN ({ignore});"
.format(
.format(
table=table_name,
ignore=", ".join(
"'%s'" % (i,) for i in field_ignore))))]
Expand Down
4 changes: 2 additions & 2 deletions metadata.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
[general]
name=CoGo Plugin
description=A plugin to capture coordinates, bearings and distances and to use these to construct and manage land parcels.
qgisMinimumVersion=2.14
qgisMaximumVersion=2.99
qgisMinimumVersion=2.99
qgisMaximumVersion=3.99
version=3.0
author=Kartoza
[email protected]
Expand Down
94 changes: 49 additions & 45 deletions plugin.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,28 @@
* *
***************************************************************************/
"""
from __future__ import absolute_import

# qgis imports
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.core import *
# python imports
import os
# user imports
import __init__ as metadata
from PyQt4Dialogs import *
import database
from constants import *
from datetime import datetime
from utilities import validate_plugin_actions

# qgis imports
from PyQt5.QtCore import QSettings, Qt
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QToolBar, QAction, QMessageBox
from qgis.core import QgsCoordinateReferenceSystem, QgsProject, QgsVectorLayer, QgsCredentials, QgsDataSourceUri

# user imports

from .cogo_dialogs import BearingDistanceFormDialog, SelectorDialog, FormParcelDialog, ManagerDialog, FormBeaconDialog, \
DatabaseConnectionDialog
from . import database
from .constants import *
from .utilities import validate_plugin_actions

class RequiredLayer:

class RequiredLayer(object):

def __init__(
self,
Expand All @@ -58,14 +63,14 @@ def __init__(
self.layer = layer


class Mode:
class Mode(object):

def __init__(self, actor, action):
self.actor = actor
self.action = action


class SMLSurveyor:
class SMLSurveyor(object):

def __init__(self, iface):
# save reference to the QGIS interface
Expand Down Expand Up @@ -102,14 +107,15 @@ def unload(self):
self.refresh_layers()
for l in self.required_layers:
if bool(l.layer):
QgsMapLayerRegistry.instance().removeMapLayers([l.layer.id()])
QgsProject.instance().removeMapLayers([l.layer.id()])

def create_plugin_toolbar(self):
""" Create plugin toolbar to house buttons
"""
# create plugin toolbar
self.plugin_toolbar = QToolBar(metadata.name())
self.plugin_toolbar.setObjectName(metadata.name())

self.plugin_toolbar = QToolBar('Parcel Plugin')
self.plugin_toolbar.setObjectName('Parcel Plugin')
# create Database Selection button
self.select_database_action = QAction(
QIcon(os.path.join(self.plugin_dir, "images", "database.png")),
Expand Down Expand Up @@ -170,7 +176,7 @@ def set_database_connection(self, connection=None, crs=None):
# fetch settings
settings_plugin = QSettings()
settings_postgis = QSettings()
settings_plugin.beginGroup(metadata.name().replace(" ", "_"))
settings_plugin.beginGroup('CoGo Plugin')
settings_postgis.beginGroup('PostgreSQL/connections')
self.connection = connection
if not bool(self.connection):
Expand Down Expand Up @@ -201,7 +207,7 @@ def set_database_connection(self, connection=None, crs=None):
db_password = settings_postgis.value(self.connection + '/password')

max_attempts = 3
self.uri = QgsDataSourceURI()
self.uri = QgsDataSourceUri()
self.uri.setConnection(
db_host,
db_port,
Expand Down Expand Up @@ -307,7 +313,7 @@ def refresh_layers(self):
target_group = root.findGroup(group_name)
if not bool(target_group):
target_group = root.insertGroup(0, group_name)
target_group.setVisible(Qt.Checked)
target_group.setItemVisibilityChecked(Qt.Checked)

for required_layer in reversed(self.required_layers):
for layer_node in target_group.findLayers():
Expand All @@ -325,14 +331,13 @@ def refresh_layers(self):
required_layer.primary_key)
added_layer = QgsVectorLayer(
self.uri.uri(), required_layer.name_plural, "postgres")
QgsMapLayerRegistry.instance().addMapLayer(
added_layer, False)
QgsProject.instance().addMapLayer(added_layer, False)
target_group.addLayer(added_layer)
for layer_node in target_group.findLayers():
layer = layer_node.layer()
if required_layer.name_plural == layer.name():
required_layer.layer = layer
layer_node.setVisible(Qt.Checked)
layer_node.setItemVisibilityChecked(Qt.Checked)
if self.crs:
layer.setCrs(self.crs)
self.iface.zoomToActiveLayer()
Expand Down Expand Up @@ -395,10 +400,11 @@ def manage_database_connection(self):
if connection:
self.set_database_connection(connection=connection, crs=crs)
self.refresh_layers()
validate_plugin_actions(self, self.database)
if self.database:
validate_plugin_actions(self, self.database)


class DatabaseManager():
class DatabaseManager(object):

def __init__(self):
self.dialog = DatabaseConnectionDialog()
Expand All @@ -409,20 +415,18 @@ def __init__(self):
self.current_connection = self.dialog.get_database_connection()
self.current_crs = self.dialog.get_crs()
settings_plugin = QSettings()
settings_plugin.beginGroup(metadata.name().replace(" ", "_"))
settings_plugin.beginGroup('CoGo Plugin')
settings_plugin.setValue(
"DatabaseConnection", self.current_connection)

def get_current_connection(self):

return self.current_connection

def get_current_crs(self):

return self.current_crs


class BeaconManager():
class BeaconManager(object):

def __init__(self, iface, database, required_layers):
self.iface = iface
Expand All @@ -444,9 +448,9 @@ def run(self):
# get fields
fields = self.database.get_schema(
self.required_layers[0].table, [
self.required_layers[0].geometry_column,
self.required_layers[0].primary_key
])
self.required_layers[0].geometry_column,
self.required_layers[0].primary_key
])
# display form
form_dialog = FormBeaconDialog(
self.database,
Expand All @@ -462,7 +466,7 @@ def run(self):
SQL_BEACONS["INSERT"].format(
fields=", ".join(sorted(new_values.keys())),
values=", ".join(
["%s" for k in new_values.keys()])),
["%s" for k in list(new_values.keys())])),
[new_values[k] for k in sorted(new_values.keys())])
self.iface.mapCanvas().refresh()
else:
Expand Down Expand Up @@ -498,9 +502,9 @@ def run(self):
# get fields
fields = self.database.get_schema(
self.required_layers[0].table, [
self.required_layers[0].geometry_column,
self.required_layers[0].primary_key
])
self.required_layers[0].geometry_column,
self.required_layers[0].primary_key
])
# get values
values = [value for value in self.database.query(
SQL_BEACONS["EDIT"].format(
Expand Down Expand Up @@ -577,7 +581,7 @@ def run(self):
required_layer.layer.removeSelection()


class ParcelManager():
class ParcelManager(object):

def __init__(self, iface, database, required_layers):
self.iface = iface
Expand Down Expand Up @@ -620,9 +624,9 @@ def run(self):
beacon,
i))
sql = self.database.preview_query(
SQL_PARCELS["INSERT_GENERAL"],
data=points,
multi_data=True)
SQL_PARCELS["INSERT_GENERAL"],
data=points,
multi_data=True)
self.database.query(sql)
self.iface.mapCanvas().refresh()
else:
Expand Down Expand Up @@ -677,9 +681,9 @@ def run(self):
beacon,
i))
sql = self.database.preview_query(
SQL_PARCELS["INSERT_GENERAL"],
data=points,
multi_data=True)
SQL_PARCELS["INSERT_GENERAL"],
data=points,
multi_data=True)
self.database.query(sql)
for required_layer in self.required_layers:
required_layer.layer.removeSelection()
Expand Down Expand Up @@ -710,7 +714,7 @@ def run(self):
required_layer.layer.removeSelection()


class BearDistManager():
class BearDistManager(object):

def __init__(self, iface, database, required_layers):
self.iface = iface
Expand All @@ -732,8 +736,8 @@ def run(self):
survey_plan, reference_beacon, beardist_chain = dialog.get_return()
# check whether survey plan is defined otherwise define it
if not self.database.query(
SQL_BEARDIST["IS_SURVEYPLAN"],
(survey_plan,))[0][0]:
SQL_BEARDIST["IS_SURVEYPLAN"],
(survey_plan,))[0][0]:
self.database.query(
SQL_BEARDIST["INSERT_SURVEYPLAN"],
(survey_plan, reference_beacon))
Expand Down
Loading

0 comments on commit 6d776b6

Please sign in to comment.