Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conditional rendering on vector layer intersection #59

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 32 additions & 6 deletions qtilesdialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ def __init__(self, iface):
self.grpParameters.setSettings(self.settings)
self.btnClose = self.buttonBox.button(QDialogButtonBox.Close)
self.rbExtentLayer.toggled.connect(self.__toggleLayerSelector)
self.rbVectorIntersect.toggled.connect(self.__toggleLayerSelector_VectorIntersection)
self.chkLockRatio.stateChanged.connect(self.__toggleHeightEdit)
self.spnTileWidth.valueChanged.connect(self.__updateTileSize)
self.btnBrowse.clicked.connect(self.__select_output)
Expand Down Expand Up @@ -122,12 +123,14 @@ def formatChanged(self):
def manageGui(self):
layers = utils.getMapLayers()
relations = self.iface.legendInterface().groupLayerRelationship()
combos = [self.cmbLayers, self.cmbVectorIntersect]
for layer in sorted(layers.iteritems(), cmp=locale.strcoll, key=operator.itemgetter(1)):
groupName = utils.getLayerGroup(relations, layer[0])
if groupName == '':
self.cmbLayers.addItem(layer[1], layer[0])
else:
self.cmbLayers.addItem('%s - %s' % (layer[1], groupName), layer[0])
for cmb in combos:
if groupName == '':
cmb.addItem(layer[1], layer[0])
else:
cmb.addItem('%s - %s' % (layer[1], groupName), layer[0])

self.rbOutputZip.setChecked(self.settings.value('outputToZip', True, type=bool))
self.rbOutputDir.setChecked(self.settings.value('outputToDir', False, type=bool))
Expand All @@ -151,6 +154,7 @@ def manageGui(self):
self.leTilesFroNGM.setText(self.settings.value('outputToNGM_Path', ''))

self.cmbLayers.setEnabled(False)
self.cmbVectorIntersect.setEnabled(False)
self.leRootDir.setText(self.settings.value('rootDir', 'Mapnik'))
self.rbExtentCanvas.setChecked(self.settings.value('extentCanvas', True, type=bool))
self.rbExtentFull.setChecked(self.settings.value('extentFull', False, type=bool))
Expand Down Expand Up @@ -228,14 +232,20 @@ def accept(self):
self.settings.setValue('write_viewer', self.chkWriteViewer.isChecked())
self.settings.setValue('renderOutsideTiles', self.chkRenderOutsideTiles.isChecked())
canvas = self.iface.mapCanvas()
geomTransform = QgsCoordinateTransform(canvas.mapRenderer().destinationCrs(), QgsCoordinateReferenceSystem('EPSG:4326'))
self.spatialIndex = None
if self.rbExtentCanvas.isChecked():
extent = canvas.extent()
elif self.rbExtentFull.isChecked():
extent = canvas.fullExtent()
elif self.rbVectorIntersect.isChecked():
layer = utils.getLayerById(self.cmbVectorIntersect.itemData(self.cmbVectorIntersect.currentIndex()))
extent = canvas.mapRenderer().layerExtentToOutputExtent(layer, layer.extent())
self.spatialIndex = self.__prepare_spatial_index(layer, geomTransform)
else:
layer = utils.getLayerById(self.cmbLayers.itemData(self.cmbLayers.currentIndex()))
extent = canvas.mapRenderer().layerExtentToOutputExtent(layer, layer.extent())
extent = QgsCoordinateTransform(canvas.mapRenderer().destinationCrs(), QgsCoordinateReferenceSystem('EPSG:4326')).transform(extent)
extent = geomTransform.transform(extent)
arctanSinhPi = math.degrees(math.atan(math.sinh(math.pi)))
extent = extent.intersect(QgsRectangle(-180, -arctanSinhPi, 180, arctanSinhPi))
layers = canvas.layers()
Expand All @@ -260,7 +270,8 @@ def accept(self):
self.chkWriteOverview.isChecked(),
self.chkRenderOutsideTiles.isChecked(),
writeMapurl,
writeViewer
writeViewer,
self.spatialIndex
)

self.workThread.rangeChanged.connect(self.setProgressRange)
Expand Down Expand Up @@ -356,6 +367,9 @@ def __toggleTarget(self, checked):
def __toggleLayerSelector(self, checked):
self.cmbLayers.setEnabled(checked)

def __toggleLayerSelector_VectorIntersection(self, checked):
self.cmbVectorIntersect.setEnabled(checked)

def __toggleHeightEdit(self, state):
if state == Qt.Checked:
self.lblHeight.setEnabled(False)
Expand Down Expand Up @@ -398,3 +412,15 @@ def __select_output(self):
outPath += '.ngrc'
self.leTilesFroNGM.setText(outPath)
self.settings.setValue('outputToNGM_Path', QFileInfo(outPath).absoluteFilePath())

def __prepare_spatial_index(self, layer, geomTransform):
'''Returns a QgsSpatialIndex of the features of the layer selected in
self.cmbVectorIntersect. <geomTransform> should be a
QgsCoordianteTransform allowing transformation from the source CRS to
EPSG:4326 for <layer>'''
spatialIndex = QgsSpatialIndex()
vals = {feature.id(): feature for (feature) in layer.getFeatures()}.values()
for v in vals:
v.geometry().transform(geomTransform)
map(spatialIndex.insertFeature, vals)
return spatialIndex
6 changes: 6 additions & 0 deletions tile.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,9 @@ def toPoint(self):

def toRectangle(self):
return QgsRectangle(self.toPoint(), Tile(self.x + 1, self.y + 1, self.z, self.tms).toPoint())

def intersects_spatial_index(self, spatial_index):
if spatial_index.intersects(self.toRectangle()):
return True
else:
return False
14 changes: 10 additions & 4 deletions tilingthread.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class TilingThread(QThread):
processFinished = pyqtSignal()
processInterrupted = pyqtSignal()

def __init__(self, layers, extent, minZoom, maxZoom, width, height, transp, quality, format, outputPath, rootDir, antialiasing, tmsConvention, mbtilesCompression, jsonFile, overview, renderOutsideTiles, mapUrl, viewer):
def __init__(self, layers, extent, minZoom, maxZoom, width, height, transp, quality, format, outputPath, rootDir, antialiasing, tmsConvention, mbtilesCompression, jsonFile, overview, renderOutsideTiles, mapUrl, viewer, spatialIndex):
QThread.__init__(self, QThread.currentThread())
self.mutex = QMutex()
self.stopMe = 0
Expand Down Expand Up @@ -103,6 +103,7 @@ def __init__(self, layers, extent, minZoom, maxZoom, width, height, transp, qual
self.settings.setFlag(QgsMapSettings.Antialiasing, True)
else:
self.settings.setFlag(QgsMapSettings.DrawLabeling, True)
self.spatialIndex = spatialIndex

def run(self):
self.mutex.lock()
Expand Down Expand Up @@ -137,7 +138,7 @@ def run(self):
self.rangeChanged.emit(self.tr('Rendering: %v from %m (%p%)'), len(self.tiles))
for t in self.tiles:
self.render(t)
self.updateProgress.emit()
self.updateProgress.emit()
self.mutex.lock()
s = self.stopMe
self.mutex.unlock()
Expand Down Expand Up @@ -228,17 +229,22 @@ def writeLeafletViewer(self):
fOut.write(viewer.substitute(substitutions))
templateFile.close()

def append_tile_index_check(self, tile):
if not self.spatialIndex or tile.intersects_spatial_index(self.spatialIndex):
self.tiles.append(tile)
return

def countTiles(self, tile):
if self.interrupted or not self.extent.intersects(tile.toRectangle()):
return
if self.minZoom <= tile.z and tile.z <= self.maxZoom:
if not self.renderOutsideTiles:
for layer in self.layers:
if layer.extent().intersects(tile.toRectangle()):
self.tiles.append(tile)
self.append_tile_index_check(tile)
break
else:
self.tiles.append(tile)
self.append_tile_index_check(tile)
if tile.z < self.maxZoom:
for x in xrange(2 * tile.x, 2 * tile.x + 2, 1):
for y in xrange(2 * tile.y, 2 * tile.y + 2, 1):
Expand Down
48 changes: 31 additions & 17 deletions ui/qtilesdialogbase.ui
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
<x>0</x>
<y>0</y>
<width>515</width>
<height>619</height>
<height>607</height>
</rect>
</property>
<property name="styleSheet">
Expand Down Expand Up @@ -277,20 +277,17 @@
<string>Extent</string>
</property>
<layout class="QGridLayout" name="gridLayout_2">
<property name="topMargin">
<number>6</number>
</property>
<item row="1" column="0">
<widget class="QRadioButton" name="rbExtentFull">
<item row="0" column="0">
<widget class="QRadioButton" name="rbExtentCanvas">
<property name="text">
<string>Full extent</string>
<string>Canvas extent</string>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QRadioButton" name="rbExtentCanvas">
<item row="1" column="0">
<widget class="QRadioButton" name="rbExtentFull">
<property name="text">
<string>Canvas extent</string>
<string>Full extent</string>
</property>
</widget>
</item>
Expand All @@ -311,6 +308,23 @@
</property>
</widget>
</item>
<item row="3" column="0">
<widget class="QRadioButton" name="rbVectorIntersect">
<property name="text">
<string>Feature intersect</string>
</property>
</widget>
</item>
<item row="3" column="1">
<widget class="QComboBox" name="cmbVectorIntersect">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
</widget>
</item>
</layout>
</widget>
</item>
Expand Down Expand Up @@ -402,10 +416,10 @@
<property name="title">
<string>Parameters</string>
</property>
<property name="collapsed" stdset="0">
<property name="collapsed">
<bool>true</bool>
</property>
<property name="saveCollapsedState" stdset="0">
<property name="saveCollapsedState">
<bool>true</bool>
</property>
<layout class="QGridLayout">
Expand Down Expand Up @@ -631,7 +645,7 @@
<customwidget>
<class>QgsCollapsibleGroupBox</class>
<extends>QGroupBox</extends>
<header>qgis.gui</header>
<header>qgscollapsiblegroupbox.h</header>
<container>1</container>
</customwidget>
</customwidgets>
Expand All @@ -644,8 +658,8 @@
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>248</x>
<y>254</y>
<x>257</x>
<y>680</y>
</hint>
<hint type="destinationlabel">
<x>157</x>
Expand All @@ -660,8 +674,8 @@
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
<x>316</x>
<y>260</y>
<x>325</x>
<y>680</y>
</hint>
<hint type="destinationlabel">
<x>286</x>
Expand Down