Skip to content

Commit

Permalink
QGIS3 support added
Browse files Browse the repository at this point in the history
  • Loading branch information
514ckw4r3 committed Jul 2, 2021
1 parent 53302a5 commit 2951d1b
Show file tree
Hide file tree
Showing 12 changed files with 737 additions and 82 deletions.
2 changes: 1 addition & 1 deletion __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@


def classFactory(iface):
from qtiles import QTilesPlugin
from .qtiles import QTilesPlugin
return QTilesPlugin(iface)
17 changes: 9 additions & 8 deletions aboutdialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,18 @@


import os
import ConfigParser

from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.PyQt.QtCore import QSettings, QUrl, QLocale
from qgis.PyQt.QtGui import QDesktopServices, QTextDocument, QPixmap
from qgis.PyQt.QtWidgets import QDialogButtonBox, QDialog
from qgis.PyQt import uic

from ui.ui_aboutdialogbase import Ui_Dialog
from . import resources_rc
from .compat import configparser

import resources_rc
FORM_CLASS, _ = uic.loadUiType(os.path.join(os.path.dirname(__file__), 'ui/aboutdialogbase.ui'))


class AboutDialog(QDialog, Ui_Dialog):
class AboutDialog(QDialog, FORM_CLASS):
def __init__(self):
QDialog.__init__(self)
self.setupUi(self)
Expand All @@ -46,7 +47,7 @@ def __init__(self):

self.lblLogo.setPixmap(QPixmap(':/icons/qtiles.png'))

cfg = ConfigParser.SafeConfigParser()
cfg = configparser.SafeConfigParser()
cfg.read(os.path.join(os.path.dirname(__file__), 'metadata.txt'))
version = cfg.get('general', 'version')

Expand Down
96 changes: 96 additions & 0 deletions compat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# -*- coding: utf-8 -*-
#******************************************************************************
#
# OSMInfo
# ---------------------------------------------------------
# This plugin takes coordinates of a mouse click and gets information about all
# objects from this point from OSM using Overpass API.
#
# Author: Denis Ilyin, denis.ilyin@nextgis.com
# *****************************************************************************
# Copyright (c) 2015-2021. NextGIS, info@nextgis.com
#
# This source 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.
#
# This code is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# A copy of the GNU General Public License is available on the World Wide Web
# at <http://www.gnu.org/licenses/>. You can also obtain it by writing
# to the Free Software Foundation, 51 Franklin Street, Suite 500 Boston,
# MA 02110-1335 USA.
#
#******************************************************************************

import os
import sys

from qgis import core

PY2 = sys.version_info[0] == 2
PY3 = sys.version_info[0] == 3

if PY3:
import configparser
else:
import ConfigParser as configparser

if hasattr(core, "QGis"):
from qgis.core import QGis
else:
from qgis.core import Qgis as QGis

if QGis.QGIS_VERSION_INT >= 30000:
QGIS_VERSION_3 = True

mapLayers = core.QgsProject.instance().mapLayers

from qgis.core import QgsPointXY
from qgis.core import QgsSettings

QgsMessageLogInfo = QGis.Info

qgisUserDatabaseFilePath = core.QgsApplication.qgisUserDatabaseFilePath
else:
QGIS_VERSION_3 = False

mapLayers = core.QgsMapLayerRegistry.instance().mapLayers

from qgis.core import QgsPoint as QgsPointXY
from qgis.PyQt.QtCore import QSettings as QgsSettings

QgsMessageLogInfo = core.QgsMessageLog.INFO

qgisUserDatabaseFilePath = core.QgsApplication.qgisUserDbFilePath

class QgsCoordinateTransform(core.QgsCoordinateTransform):
def __init__(self, src_crs, dst_crs):
super(QgsCoordinateTransform, self).__init__()

self.setSourceCrs(src_crs)
self.setDestinationCrs(dst_crs)

def setDestinationCrs(self, dst_crs):
if QGis.QGIS_VERSION_INT >= 30000:
super(QgsCoordinateTransform, self).setDestinationCrs(dst_crs)
else:
self.setDestCRS(dst_crs)

class QgsCoordinateReferenceSystem(core.QgsCoordinateReferenceSystem):
def __init__(self, id, type):
if QGis.QGIS_VERSION_INT >= 30000:
super(QgsCoordinateReferenceSystem, self).__init__(core.QgsCoordinateReferenceSystem.fromEpsgId(id))
else:
super(QgsCoordinateReferenceSystem, self).__init__(id, type)

@staticmethod
def fromEpsgId(id):
if QGis.QGIS_VERSION_INT >= 30000:
return core.QgsCoordinateReferenceSystem.fromEpsgId(id)
else:
return core.QgsCoordinateReferenceSystem(id)
6 changes: 3 additions & 3 deletions mbutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def mbtiles_connect(mbtiles_file):
try:
con = sqlite3.connect(mbtiles_file, check_same_thread=False)
return con
except Exception, e:
except Exception as e:
logger.error("Could not connect to database")
logger.exception(e)
sys.exit(1)
Expand Down Expand Up @@ -149,7 +149,7 @@ def disk_to_mbtiles(directory_path, mbtiles_file, **kwargs):
try:
metadata = json.load(open(os.path.join(directory_path, 'metadata.json'), 'r'))
image_format = kwargs.get('format')
for name, value in metadata.items():
for name, value in list(metadata.items()):
cur.execute('insert into metadata (name, value) values (?, ?)',
(name, value))
logger.info('metadata from metadata.json restored')
Expand Down Expand Up @@ -245,7 +245,7 @@ def mbtiles_to_disk(mbtiles_file, directory_path, **kwargs):
y = t[2]
if kwargs.get('scheme') == 'xyz':
y = flip_y(z,y)
print 'flipping'
print('flipping')
tile_dir = os.path.join(base_path, str(z), str(x))
elif kwargs.get('scheme') == 'wms':
tile_dir = os.path.join(base_path,
Expand Down
8 changes: 6 additions & 2 deletions metadata.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@
name=QTiles
description=Generate tiles from QGIS project
category=Plugins
version=1.5.5
version=1.6.0
qgisMinimumVersion=2.4
qgisMaximumVersion=3.99

author=NextGIS
email=info@nextgis.com

changelog=1.5.5
changelog=
1.6.0
* QGIS 3 support added
1.5.5
* Fix rendering of tiles outside of layer extent
* Fix qgis warnings
1.5.4
Expand Down
16 changes: 9 additions & 7 deletions qtiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,26 @@
#******************************************************************************


from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.PyQt.QtCore import QCoreApplication, QSettings, QLocale, QTranslator, QFileInfo
from qgis.PyQt.QtWidgets import QAction, QMessageBox
from qgis.PyQt.QtGui import QIcon

from qgis.core import *

import qtilesdialog
import aboutdialog
from . import qtilesdialog
from . import aboutdialog
from . import resources_rc

import resources_rc
from .compat import QGis, qgisUserDatabaseFilePath


class QTilesPlugin:
def __init__(self, iface):
self.iface = iface

self.qgsVersion = unicode(QGis.QGIS_VERSION_INT)
self.qgsVersion = str(QGis.QGIS_VERSION_INT)

userPluginPath = QFileInfo(QgsApplication.qgisUserDbFilePath()).path() + '/python/plugins/qtiles'
userPluginPath = QFileInfo(qgisUserDatabaseFilePath()).path() + '/python/plugins/qtiles'
systemPluginPath = QgsApplication.prefixPath() + '/python/plugins/qtiles'

overrideLocale = QSettings().value('locale/overrideFlag', False, type=bool)
Expand Down
31 changes: 11 additions & 20 deletions qtiles_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,41 +26,32 @@
#******************************************************************************


from PyQt4.QtCore import *

from qgis.PyQt.QtCore import *
from qgis.core import *

from .compat import mapLayers


def getMapLayers():
layerMap = QgsMapLayerRegistry.instance().mapLayers()
layers = dict()
for name, layer in layerMap.iteritems():
for name, layer in list(mapLayers().items()):
if layer.type() == QgsMapLayer.VectorLayer:
if layer.id() not in layers.keys():
layers[layer.id()] = unicode(layer.name())
if layer.id() not in list(layers.keys()):
layers[layer.id()] = str(layer.name())
if layer.type() == QgsMapLayer.RasterLayer and layer.providerType() == 'gdal':
if layer.id() not in layers.keys():
layers[layer.id()] = unicode(layer.name())
if layer.id() not in list(layers.keys()):
layers[layer.id()] = str(layer.name())
return layers


def getLayerById(layerId):
layerMap = QgsMapLayerRegistry.instance().mapLayers()
for name, layer in layerMap.iteritems():
for name, layer in list(mapLayers().items()):
if layer.id() == layerId:
if layer.isValid():
return layer
else:
return None


def getLayerGroup(relations, layerId):
group = None

for item in relations:
group = unicode(item[0])
for lid in item[1]:
if unicode(lid) == unicode(layerId):
return group

return group
def getLayerGroup(layerId):
return QgsProject.instance().layerTreeRoot().findLayer(layerId).parent().name()
31 changes: 18 additions & 13 deletions qtilesdialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,21 @@
import locale
import math
import operator
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.core import *
import tilingthread
from ui.ui_qtilesdialogbase import Ui_Dialog
import qtiles_utils as utils

from qgis.PyQt.QtCore import QFileInfo, Qt, QDir, pyqtSignal, pyqtSlot
from qgis.PyQt.QtGui import QIcon
from qgis.PyQt.QtWidgets import QDialog, QDialogButtonBox, QMessageBox
from qgis.PyQt import uic
from qgis.core import QgsRectangle

class QTilesDialog(QDialog, Ui_Dialog):
from . import tilingthread
from . import qtiles_utils as utils

from .compat import QgsCoordinateTransform, QgsCoordinateReferenceSystem, QgsSettings

FORM_CLASS, _ = uic.loadUiType(os.path.join(os.path.dirname(__file__), 'ui/qtilesdialogbase.ui'))

class QTilesDialog(QDialog, FORM_CLASS):
# MAX_ZOOM_LEVEL = 18
MIN_ZOOM_LEVEL = 0

Expand Down Expand Up @@ -64,7 +70,7 @@ def __init__(self, iface):
self.tr('ZIP archives (*.zip *.ZIP)'): '.zip',
self.tr('MBTiles databases (*.mbtiles *.MBTILES)'): '.mbtiles'}

self.settings = QSettings('NextGIS', 'QTiles')
self.settings = QgsSettings('NextGIS', 'QTiles')
self.grpParameters.setSettings(self.settings)
self.btnClose = self.buttonBox.button(QDialogButtonBox.Close)
self.rbExtentLayer.toggled.connect(self.__toggleLayerSelector)
Expand Down Expand Up @@ -121,9 +127,8 @@ def formatChanged(self):

def manageGui(self):
layers = utils.getMapLayers()
relations = self.iface.legendInterface().groupLayerRelationship()
for layer in sorted(layers.iteritems(), cmp=locale.strcoll, key=operator.itemgetter(1)):
groupName = utils.getLayerGroup(relations, layer[0])
for layer in sorted(iter(list(layers.items())), key=operator.itemgetter(1)):
groupName = utils.getLayerGroup(layer[0])
if groupName == '':
self.cmbLayers.addItem(layer[1], layer[0])
else:
Expand Down Expand Up @@ -238,7 +243,7 @@ def accept(self):

extent = QgsCoordinateTransform(
canvas.mapSettings().destinationCrs(),
QgsCoordinateReferenceSystem('EPSG:4326')
QgsCoordinateReferenceSystem.fromEpsgId(4326)
).transform(extent)

arctanSinhPi = math.degrees(math.atan(math.sinh(math.pi)))
Expand Down Expand Up @@ -392,7 +397,7 @@ def __updateTileSize(self, value):
def __select_output(self):
if self.rbOutputZip.isChecked():
file_directory = QFileInfo(self.settings.value('outputToZip_Path', '.')).absolutePath()
outPath, outFilter = QFileDialog.getSaveFileNameAndFilter(self, self.tr('Save to file'), file_directory, ';;'.join(self.FORMATS.iterkeys()), self.FORMATS.keys()[self.FORMATS.values().index('.zip')])
outPath, outFilter = QFileDialog.getSaveFileNameAndFilter(self, self.tr('Save to file'), file_directory, ';;'.join(iter(list(self.FORMATS.keys()))), list(self.FORMATS.keys())[list(self.FORMATS.values()).index('.zip')])
if not outPath:
return
if not outPath.lower().endswith(self.FORMATS[outFilter]):
Expand Down
Loading

0 comments on commit 2951d1b

Please sign in to comment.