def updateGeometriesPreview(self): ok = self.prepareDisplacement() self.rubberBandGeometriesPreview.reset(QGis.Line) if not ok: return if not self.isPreviewEnabled: return if self.isLimitPreviewEnabled: maxCount = self.maxLimitPreview else: maxCount = self.layer.featureCount() currentCount = 0 features = vectorlayerutils.features(self.layer) for f in features: inGeom = f.geometry() inGeom.transform(self.crsTransformReverse) inGeom = self.movePolyline(inGeom) self.rubberBandGeometriesPreview.addGeometry(inGeom, None) currentCount += 1 if currentCount >= maxCount: break self.rubberBandGeometriesPreview.show()
def buildSpatialIndex(self): if not self.cachedSpatialIndex: # Build the spatial index for faster lookup. index = QgsSpatialIndex() for f in vectorlayerutils.features(self.layer): index.insertFeature(f) self.cachedSpatialIndex = index
def explode(self): self.vlayer.beginEditCommand("Explode") _, progress = self.messageBarUtils.showProgress( "Explode", "Running...", QgsMessageBar.INFO) try: self.iface.mapCanvas().freeze(True) outFeat = QgsFeature() inGeom = QgsGeometry() current = 0 features = vectorlayerutils.features(self.vlayer) total = 100.0 / float(len(features)) for f in features: inGeom = f.geometry() attrs = f.attributes() geometries = self.extractAsSingle(inGeom) outFeat.setAttributes(attrs) for g in geometries: outFeat.setGeometry(g) self.vlayer.addFeature(outFeat) # delete original feature self.vlayer.deleteFeature(f.id()) current += 1 progress.setValue(int(current * total)) except Exception as e: QgsMessageLog.logMessage(repr(e)) self.messageBarUtils.removeAllMessages() self.messageBarUtils.showMessage( "Explode", "There was an error performing this command. See QGIS Message log for details.", QgsMessageBar.CRITICAL, duration=5) if self.vlayer.isEditable(): self.vlayer.destroyEditCommand() return finally: self.iface.mapCanvas().freeze(False) self.iface.mapCanvas().refresh() # if here: Success! self.messageBarUtils.removeAllMessages() self.messageBarUtils.showMessage("Explode", "Success", QgsMessageBar.INFO, 5) self.vlayer.endEditCommand()
def doMove(self, usePrevious=False): _, progress = self.messageBarUtils.showProgress(self.MESSAGE_HEADER, "Running...", QgsMessageBar.INFO) try: self.rubberBandGeometriesPreview.reset(QGis.Line) self.layer.beginEditCommand(self.MESSAGE_HEADER) if not usePrevious: ok = self.prepareDisplacement() if not ok: self.messageBarUtils.showMessage(self.MESSAGE_HEADER, "Invalid displacement.", QgsMessageBar.WARNING, duration=5) self.layer.destroyEditCommand() return self.iface.mapCanvas().freeze(True) inGeom = QgsGeometry() current = 0 features = vectorlayerutils.features(self.layer) total = 100.0 / float(len(features)) for f in features: inGeom = f.geometry() # perform displacement in map coordinates instead of layer coordinates inGeom.transform(self.crsTransformReverse) outGeom=self.movePolyline(inGeom) outGeom.transform(self.crsTransform) self.layer.changeGeometry(f.id(), outGeom) current += 1 progress.setValue(int(current * total)) self.messageBarUtils.showMessage(self.MESSAGE_HEADER, "Success", QgsMessageBar.INFO, 5) self.layer.endEditCommand() except Exception as e: QgsMessageLog.logMessage(repr(e)) self.messageBarUtils.showMessage(MoveDigitizingMode.MESSAGE_HEADER, "There was an error performing this command. See QGIS Message log for details.", QgsMessageBar.CRITICAL, duration=5) self.layer.destroyEditCommand() finally: self.iface.mapCanvas().freeze(False) self.iface.mapCanvas().refresh() self.iface.mapCanvas().unsetMapTool(self)
def updateGeometriesPreview(self): ok = self.prepareDisplacement() self.rubberBandGeometriesPreview.reset(QGis.Line) self.rubberBandPointsPreview.reset(QGis.Point) if not ok: return if not self.isPreviewEnabled: return layers = self.iface.legendInterface().selectedLayers() for layer in layers: if layer.type( ) == QgsMapLayer.VectorLayer and layer.selectedFeatureCount() > 0: crsDest = layer.crs() canvas = self.iface.mapCanvas() mapRenderer = canvas.mapSettings() crsSrc = mapRenderer.destinationCrs() crsTransformReverse = QgsCoordinateTransform(crsDest, crsSrc) if self.isLimitPreviewEnabled: maxCount = self.maxLimitPreview else: maxCount = layer.featureCount() currentCount = 0 features = vectorlayerutils.features(layer) for f in features: inGeom = f.geometry() inGeom.transform(crsTransformReverse) inGeom = self.moveGeometry(inGeom) # in map coordinates if inGeom.type() == QGis.Point: self.rubberBandPointsPreview.addGeometry(inGeom, None) else: self.rubberBandGeometriesPreview.addGeometry( inGeom, None) currentCount += 1 if currentCount >= maxCount: break self.rubberBandPointsPreview.show() self.rubberBandGeometriesPreview.show()
def updateGeometriesPreview(self): ok = self.prepareMirror() self.rubberBandGeometriesPreview.reset(QGis.Line) if not ok: return if not self.isPreviewEnabled: return if self.isLimitPreviewEnabled: maxCount = self.maxLimitPreview else: maxCount = self.layer.featureCount() currentCount = 0 features = vectorlayerutils.features(self.layer) for f in features: inGeom = f.geometry() inGeom.transform(self.crsTransformReverse) # break into multiple geometries if needed if inGeom.isMultipart(): geometries = inGeom.asMultiPolyline() for g in geometries: polyline = self.mirrorPolyline(g) # polyline in map coordinates self.rubberBandGeometriesPreview.addGeometry( QgsGeometry.fromPolyline(polyline), None) else: polyline = self.mirrorPolyline(inGeom.asPolyline()) self.rubberBandGeometriesPreview.addGeometry( QgsGeometry.fromPolyline(polyline), None) currentCount += 1 if currentCount >= maxCount: break self.rubberBandGeometriesPreview.show()
def doCopy(self): _, progress = self.messageBarUtils.showProgress( CopyDigitizingMode.MESSAGE_HEADER, "Running...", QgsMessageBar.INFO) try: self.rubberBandGeometriesPreview.reset(QGis.Line) self.layer.beginEditCommand(CopyDigitizingMode.MESSAGE_HEADER) ok = self.prepareDisplacement() if not ok: self.messageBarUtils.showMessage( CopyDigitizingMode.MESSAGE_HEADER, "Invalid displacement.", QgsMessageBar.WARNING, duration=5) self.layer.destroyEditCommand() return self.iface.mapCanvas().freeze(True) outFeat = QgsFeature() current = 0 features = vectorlayerutils.features(self.layer) total = 100.0 / float(len(features)) pgidIndex = self.layer.fieldNameIndex("gid") for f in features: inGeom = f.geometry() attrs = f.attributes() # perform displacement in map coordinates instead of layer coordinates inGeom.transform(self.crsTransformReverse) outGeom = self.movePolyline(inGeom) outGeom.transform(self.crsTransform) outFeat.initAttributes(len(attrs)) for index in range(len(attrs)): if index <> pgidIndex: outFeat.setAttribute(index, attrs[index]) outFeat.setGeometry(outGeom) self.layer.addFeature(outFeat) current += 1 progress.setValue(int(current * total)) self.messageBarUtils.showMessage(CopyDigitizingMode.MESSAGE_HEADER, "Success", QgsMessageBar.INFO, 5) self.layer.endEditCommand() except Exception as e: QgsMessageLog.logMessage(repr(e)) self.messageBarUtils.showMessage( CopyDigitizingMode.MESSAGE_HEADER, "There was an error performing this command. See QGIS Message log for details.", QgsMessageBar.CRITICAL, duration=5) self.layer.destroyEditCommand() finally: self.iface.mapCanvas().freeze(False) self.iface.mapCanvas().refresh() self.resetCopy() self.next()
def doCopy(self): _, progress = self.messageBarUtils.showProgress( self.MESSAGE_HEADER, "Running...", QgsMessageBar.INFO) self.rubberBandGeometriesPreview.reset(QGis.Line) self.rubberBandPointsPreview.reset(QGis.Point) currentLayer = None isEditable = [] try: ok = self.prepareDisplacement() if not ok: self.messageBarUtils.showMessage(self.MESSAGE_HEADER, "Invalid displacement.", QgsMessageBar.WARNING, duration=5) return self.iface.mapCanvas().freeze(True) inGeom = QgsGeometry() outFeat = QgsFeature() current = 0 layers = self.iface.legendInterface().selectedLayers() for layer in layers: # only deal with selected features of the selected layers if layer.type( ) == QgsMapLayer.VectorLayer and layer.selectedFeatureCount( ) > 0: currentLayer = layer if layer.isEditable(): isEditable.append(True) layer.beginEditCommand("Copy from multiple layers") else: isEditable.append(False) layer.startEditing() crsDest = layer.crs() canvas = self.iface.mapCanvas() mapRenderer = canvas.mapSettings() crsSrc = mapRenderer.destinationCrs() crsTransform = QgsCoordinateTransform(crsSrc, crsDest) # cannot indicate direction in QgsGeometry.transform crsTransformReverse = QgsCoordinateTransform( crsDest, crsSrc) features = vectorlayerutils.features(layer) total = 100.0 / float(len(features)) pgidIndex = layer.fieldNameIndex("gid") progress.setValue(0) for f in features: inGeom = f.geometry() attrs = f.attributes() # perform displacement in map coordinates instead of layer coordinates inGeom.transform(crsTransformReverse) outGeom = self.moveGeometry(inGeom) outGeom.transform(crsTransform) outFeat.initAttributes(len(attrs)) for index in range(len(attrs)): if index <> pgidIndex: outFeat.setAttribute(index, attrs[index]) outFeat.setGeometry(outGeom) layer.addFeature(outFeat) current += 1 progress.setValue(int(current * total)) currentLayer = None self.messageBarUtils.showMessage(self.MESSAGE_HEADER, "Success", QgsMessageBar.INFO, 5) # commit modifications in all layers at the end count = 0 layers = self.iface.legendInterface().selectedLayers() for layer in layers: # only deal with selected features of the selected layers if layer.type( ) == QgsMapLayer.VectorLayer and layer.selectedFeatureCount( ) > 0: if isEditable[count]: layer.endEditCommand() else: layer.commitChanges() count += 1 except Exception as e: raise QgsMessageLog.logMessage(repr(e)) self.messageBarUtils.showMessage( self.MESSAGE_HEADER, "There was an error performing this command. See QGIS Message log for details.", QgsMessageBar.CRITICAL, duration=5) # rollback modifications in all layers count = 0 layers = self.iface.legendInterface().selectedLayers() for layer in layers: # only deal with selected features of the selected layers if layer.type( ) == QgsMapLayer.VectorLayer and layer.selectedFeatureCount( ) > 0: if isEditable[count]: layer.destroyEditCommand() else: layer.rollBack() # subsequent layers have not been modified if layer == currentLayer: break count += 1 finally: self.iface.mapCanvas().freeze(False) self.iface.mapCanvas().refresh() self.resetCopyMultipleLayers() self.next()
def doScale(self): _, progress = self.messageBarUtils.showProgress( ScaleDigitizingMode.MESSAGE_HEADER, "Running...", QgsMessageBar.INFO) try: # get the actual vertices from the geometry instead of the snapping crsDest = self.layer.crs() canvas = self.iface.mapCanvas() mapRenderer = canvas.mapSettings() crsSrc = mapRenderer.destinationCrs() crsTransform = QgsCoordinateTransform(crsSrc, crsDest) # arcnode is still in map canvas coordinate => transform to layer coordinate self.basePoint = crsTransform.transform(self.basePoint) try: scaleFactor = float(self.scaleFactor) # save for next time QgsProject.instance().writeEntry( constants.SETTINGS_KEY, constants.SETTINGS_SCALE_FACTOR, scaleFactor) except ValueError: scaleFactor = ScaleDigitizingMode.DEFAULT_SCALE_FACTOR self.iface.mapCanvas().freeze(True) outFeat = QgsFeature() inGeom = QgsGeometry() current = 0 features = vectorlayerutils.features(self.layer) total = 100.0 / float(len(features)) for f in features: inGeom = f.geometry() attrs = f.attributes() # break into multiple geometries if needed if inGeom.isMultipart(): geometries = inGeom.asMultiPolyline() outGeom = [] for g in geometries: polyline = self.scalePolyline(g, scaleFactor) outGeom.append(polyline) outGeom = QgsGeometry.fromMultiPolyline(outGeom) else: polyline = self.scalePolyline(inGeom.asPolyline(), scaleFactor) outGeom = QgsGeometry.fromPolyline(polyline) outFeat.setAttributes(attrs) outFeat.setGeometry(outGeom) self.layer.addFeature(outFeat) self.layer.deleteFeature(f.id()) current += 1 progress.setValue(int(current * total)) except Exception as e: QgsMessageLog.logMessage(repr(e)) self.messageBarUtils.showMessage( ScaleDigitizingMode.MESSAGE_HEADER, "There was an error performing this command. See QGIS Message log for details.", QgsMessageBar.CRITICAL, duration=5) self.layer.destroyEditCommand() return finally: self.iface.mapCanvas().freeze(False) self.iface.mapCanvas().refresh() # if here: Success! self.messageBarUtils.showMessage(ScaleDigitizingMode.MESSAGE_HEADER, "Success", QgsMessageBar.INFO, 5) self.layer.endEditCommand() self.iface.mapCanvas().unsetMapTool(self)
def doMirror(self): _, progress = self.messageBarUtils.showProgress( "Mirror", "Running...", QgsMessageBar.INFO) try: self.rubberBandGeometriesPreview.reset(QGis.Line) self.layer.beginEditCommand("Mirror") ok = self.prepareMirror() if not ok: self.messageBarUtils.showMessage("Mirror", "Invalid mirror line.", QgsMessageBar.WARNING, duration=5) self.layer.destroyEditCommand() return self.iface.mapCanvas().freeze(True) outFeat = QgsFeature() current = 0 features = vectorlayerutils.features(self.layer) total = 100.0 / float(len(features)) pgidIndex = self.layer.fieldNameIndex("gid") for f in features: inGeom = f.geometry() attrs = f.attributes() # perform mirror in map coordinates instead of layer coordinates inGeom.transform(self.crsTransformReverse) # break into multiple geometries if needed if inGeom.isMultipart(): geometries = inGeom.asMultiPolyline() outGeom = [] for g in geometries: polyline = self.mirrorPolyline(g) outGeom.append(polyline) outGeom = QgsGeometry.fromMultiPolyline(outGeom) else: polyline = self.mirrorPolyline(inGeom.asPolyline()) outGeom = QgsGeometry.fromPolyline(polyline) outGeom.transform(self.crsTransform) # beeeeeuh... Cannot make QPyNullVariant work # deleteAttribute with string is masked... # so... outFeat.initAttributes(len(attrs)) for index in range(len(attrs)): if index <> pgidIndex: outFeat.setAttribute(index, attrs[index]) outFeat.setGeometry(outGeom) ok = self.layer.addFeature(outFeat) current += 1 progress.setValue(int(current * total)) self.messageBarUtils.showMessage("Mirror", "Success", QgsMessageBar.INFO, 5) self.layer.endEditCommand() except Exception as e: QgsMessageLog.logMessage(repr(e)) self.messageBarUtils.showMessage( "Mirror", "There was an error performing this command. See QGIS Message log for details.", QgsMessageBar.CRITICAL, duration=5) self.layer.destroyEditCommand() finally: self.iface.mapCanvas().freeze(False) self.iface.mapCanvas().refresh() self.iface.mapCanvas().unsetMapTool(self)