Skip to content

Commit

Permalink
Resolve #110
Browse files Browse the repository at this point in the history
  • Loading branch information
yellow-sky committed Jun 19, 2017
1 parent b65c502 commit 9ae9efd
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 53 deletions.
12 changes: 12 additions & 0 deletions src/plugin_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import ast

from PyQt4.QtCore import QSettings, QDir, Qt, QByteArray
from qgis.core import QGis

import extra_sources

Expand Down Expand Up @@ -142,3 +143,14 @@ def set_last_used_services(cls, services):
for geoservice in services:
geoservice.saveSelf(settings)


@classmethod
def use_native_tms(cls):
if QGis.QGIS_VERSION_INT >= 21808:
return cls.get_settings().value('tile_layer/use_native_tms', False, bool)
else:
return False

@classmethod
def set_use_native_tms(cls, bool_val):
return cls.get_settings().setValue('tile_layer/use_native_tms', bool_val)
32 changes: 2 additions & 30 deletions src/py_tiled_layer/tilelayer.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

from ..plugin_settings import PluginSettings
from ..qgis_settings import QGISSettings

from ..qgis_proj_helper import ProjectionHelper
from .tiles import *
from .downloader import Downloader

Expand Down Expand Up @@ -83,35 +83,7 @@ def __init__(self, layerDef, creditVisibility=1):
self.setCustomProperty("creditVisibility", self.creditVisibility)

# set standard/custom crs
self.setCrs(self.CRS_3857)
try:
crs = None
if layerDef.epsg_crs_id is not None:
crs = QgsCoordinateReferenceSystem(layerDef.epsg_crs_id, QgsCoordinateReferenceSystem.EpsgCrsId)
if layerDef.postgis_crs_id is not None:
crs = QgsCoordinateReferenceSystem(layerDef.postgis_crs_id, QgsCoordinateReferenceSystem.PostgisCrsId)
if layerDef.custom_proj is not None:
# create form proj4 str
custom_crs = QgsCoordinateReferenceSystem()
custom_crs.createFromProj4(layerDef.custom_proj)
# try to search in db
searched = custom_crs.findMatchingProj()
if searched:
crs = QgsCoordinateReferenceSystem(searched, QgsCoordinateReferenceSystem.InternalCrsId)
else:
# create custom and use it
custom_crs.saveAsUserCRS('quickmapservices %s' % layerDef.title)
searched = custom_crs.findMatchingProj()
if searched:
crs = QgsCoordinateReferenceSystem(searched, QgsCoordinateReferenceSystem.InternalCrsId)
else:
crs = custom_crs

if crs:
self.setCrs(crs)
except:
msg = self.tr("Custom crs can't be set for layer {0}!").format(layerDef.title)
self.showBarMessage(msg, QgsMessageBar.WARNING, 4)
ProjectionHelper.set_tile_layer_proj(self, layerDef.epsg_crs_id, layerDef.postgis_crs_id, layerDef.custom_proj)

if layerDef.bbox:
self.setExtent(BoundingBox.degreesToMercatorMeters(layerDef.bbox).toQgsRectangle())
Expand Down
59 changes: 38 additions & 21 deletions src/qgis_map_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@

from .plugin_settings import PluginSettings
from .supported_drivers import KNOWN_DRIVERS
from .py_tiled_layer.tiles import TileServiceInfo
from .py_tiled_layer.tiles import TileServiceInfo, TileDefaultSettings
from .py_tiled_layer.tilelayer import TileLayer
from .qgis_proj_helper import ProjectionHelper

service_layers = []

Expand All @@ -23,26 +24,42 @@ def add_layer_to_map(ds):
layers4add = []

if ds.type.lower() == KNOWN_DRIVERS.TMS.lower():
service_info = TileServiceInfo(tr(ds.alias), ds.copyright_text, ds.tms_url)
service_info.zmin = ds.tms_zmin or service_info.zmin
service_info.zmax = ds.tms_zmax or service_info.zmax
if ds.tms_y_origin_top is not None:
service_info.yOriginTop = ds.tms_y_origin_top
service_info.epsg_crs_id = ds.tms_epsg_crs_id
service_info.postgis_crs_id = ds.tms_postgis_crs_id
service_info.custom_proj = ds.tms_custom_proj

if ds.tms_tile_ranges is not None: # needs try block & checks that keys are integers etc..
service_info.tile_ranges = ast.literal_eval(ds.tms_tile_ranges)
if ds.tms_tsize1 is not None:
service_info.tsize1 = ds.tms_tsize1
if ds.tms_origin_x is not None:
service_info.originX = ds.tms_origin_x
if ds.tms_origin_y is not None:
service_info.originY = ds.tms_origin_y

layer = TileLayer(service_info, False)
layers4add.append(layer)
if PluginSettings.use_native_tms(): # add version check
service_url = ds.tms_url.replace("=", "%3D").replace("&", "%26")
if ds.tms_y_origin_top is not None and ds.tms_y_origin_top==False:
service_url = service_url.replace('{y}', '{-y}')

qgis_tms_uri = 'type=xyz&zmin={0}&zmax={1}&url={2}'.format(
ds.tms_zmin or TileDefaultSettings.ZMIN,
ds.tms_zmax or TileDefaultSettings.ZMAX,
service_url
)
#print ">>> qgis_tms_uri: ", qgis_tms_uri

layer = QgsRasterLayer(qgis_tms_uri, tr(ds.alias), KNOWN_DRIVERS.WMS.lower())
ProjectionHelper.set_tile_layer_proj(layer, ds.tms_epsg_crs_id, ds.tms_postgis_crs_id, ds.tms_custom_proj)
layers4add.append(layer)
else:
service_info = TileServiceInfo(tr(ds.alias), ds.copyright_text, ds.tms_url)
service_info.zmin = ds.tms_zmin or service_info.zmin
service_info.zmax = ds.tms_zmax or service_info.zmax
if ds.tms_y_origin_top is not None:
service_info.yOriginTop = ds.tms_y_origin_top
service_info.epsg_crs_id = ds.tms_epsg_crs_id
service_info.postgis_crs_id = ds.tms_postgis_crs_id
service_info.custom_proj = ds.tms_custom_proj

if ds.tms_tile_ranges is not None: # needs try block & checks that keys are integers etc..
service_info.tile_ranges = ast.literal_eval(ds.tms_tile_ranges)
if ds.tms_tsize1 is not None:
service_info.tsize1 = ds.tms_tsize1
if ds.tms_origin_x is not None:
service_info.originX = ds.tms_origin_x
if ds.tms_origin_y is not None:
service_info.originY = ds.tms_origin_y

layer = TileLayer(service_info, False)
layers4add.append(layer)
if ds.type.lower() == KNOWN_DRIVERS.GDAL.lower():
layer = QgsRasterLayer(ds.gdal_source_file, tr(ds.alias))
layers4add.append(layer)
Expand Down
50 changes: 50 additions & 0 deletions src/qgis_proj_helper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
from qgis.core import QgsCoordinateReferenceSystem
from qgis.gui import QgsMessageBar

from .plugin_settings import PluginSettings


class ProjectionHelper:
CRS_3857 = QgsCoordinateReferenceSystem(3857)

@classmethod
def set_tile_layer_proj(cls, layer, epsg_crs_id, postgis_crs_id, custom_proj):

# set standard crs for tiled layer
layer.setCrs(cls.CRS_3857)
# set standard/custom crs
try:
crs = None
if epsg_crs_id is not None:
crs = QgsCoordinateReferenceSystem(epsg_crs_id, QgsCoordinateReferenceSystem.EpsgCrsId)
if postgis_crs_id is not None:
crs = QgsCoordinateReferenceSystem(postgis_crs_id, QgsCoordinateReferenceSystem.PostgisCrsId)
if custom_proj is not None:
# create form proj4 str
custom_crs = QgsCoordinateReferenceSystem()
custom_crs.createFromProj4(custom_proj)
# try to search in db
searched = custom_crs.findMatchingProj()
if searched:
crs = QgsCoordinateReferenceSystem(searched, QgsCoordinateReferenceSystem.InternalCrsId)
else:
# create custom and use it
custom_crs.saveAsUserCRS(u'quickmapservices ' + layer.name())
searched = custom_crs.findMatchingProj()
if searched:
crs = QgsCoordinateReferenceSystem(searched, QgsCoordinateReferenceSystem.InternalCrsId)
else:
crs = custom_crs

if crs:
layer.setCrs(crs)
except:
msg = "Custom crs can't be set for layer {0}!".format(layer.name())
self.show_bar_message(msg, QgsMessageBar.WARNING, 4)

@classmethod
def show_bar_message(cls, text, level=QgsMessageBar.INFO, duration=0, title=None):
if PluginSettings.show_messages_in_bar():
if title is None:
title = PluginSettings.product_name()
iface.messageBar().pushMessage(title, text, level, duration)
10 changes: 9 additions & 1 deletion src/settings_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
from PyQt4.QtCore import *
from PyQt4.QtGui import *

from qgis.core import QgsApplication
from qgis.core import QgsApplication, QGis

from .extra_sources import ExtraSources
from .plugin_settings import PluginSettings
Expand Down Expand Up @@ -78,6 +78,12 @@ def fill_pages(self):
self.spnConnCount.setValue(PluginSettings.default_tile_layer_conn_count())
self.spnCacheExp.setValue(QGISSettings.get_default_tile_expiry())
self.spnNetworkTimeout.setValue(QGISSettings.get_default_network_timeout())
if QGis.QGIS_VERSION_INT >= 21808:
self.chkUseNativeRenderer.setChecked(PluginSettings.use_native_tms())
else:
self.chkUseNativeRenderer.setChecked(False)
self.chkUseNativeRenderer.setEnabled(False)

# contrib pack

def save_settings(self):
Expand All @@ -89,6 +95,8 @@ def save_settings(self):
PluginSettings.set_default_tile_layer_conn_count(self.spnConnCount.value())
QGISSettings.set_default_tile_expiry(self.spnCacheExp.value())
QGISSettings.set_default_network_timeout(self.spnNetworkTimeout.value())
if QGis.QGIS_VERSION_INT >= 21808:
PluginSettings.set_use_native_tms(self.chkUseNativeRenderer.isChecked())
# contrib pack

# ds visibility
Expand Down
15 changes: 14 additions & 1 deletion src/settings_dialog_base.ui
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<item>
<widget class="QTabWidget" name="tabUserServices">
<property name="currentIndex">
<number>2</number>
<number>0</number>
</property>
<widget class="QWidget" name="tabCommon">
<attribute name="title">
Expand Down Expand Up @@ -82,6 +82,9 @@
</sizepolicy>
</property>
<layout class="QFormLayout" name="formLayout_3">
<property name="fieldGrowthPolicy">
<enum>QFormLayout::ExpandingFieldsGrow</enum>
</property>
<item row="0" column="0">
<widget class="QLabel" name="label_5">
<property name="text">
Expand Down Expand Up @@ -127,6 +130,16 @@
</property>
</widget>
</item>
<item row="3" column="0">
<widget class="QLabel" name="useNativeRenderer2188AndHigherLabel">
<property name="text">
<string>Use native renderer (2.18.8 and higher):</string>
</property>
</widget>
</item>
<item row="3" column="1">
<widget class="QCheckBox" name="chkUseNativeRenderer"/>
</item>
</layout>
</widget>
</item>
Expand Down

0 comments on commit 9ae9efd

Please sign in to comment.