Skip to content
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
5 changes: 4 additions & 1 deletion splitmultipart/metadata.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@
[general]
name=Multipart Split
qgisMinimumVersion=3.0
qgisMaximumVersion=4.99
description=Split selected multipart features during edit session
about=The plugins adds a "Splits feature(s) parts" item to the Edit menu, and a similar button in the Advanced digitizing toolbar. The buttons are only enabled when the current layer is in edit mode and has features selected.
version=0.10.0
version=1.1.0
author=Alexandre Neto
email=senhor.neto@gmail.com

Expand All @@ -23,6 +24,8 @@ email=senhor.neto@gmail.com

# Uncomment the following line and add your changelog entries:
changelog=
1.1.0
Add QGIS 4 / Qt6 compatibility while keeping QGIS 3 support
0.4
code adapted to work in master version
0.5
Expand Down
64 changes: 43 additions & 21 deletions splitmultipart/splitmultipart.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,18 @@
from __future__ import absolute_import
from builtins import range
from builtins import object
from qgis.core import *
from PyQt5.QtCore import *
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QAction
import os.path

from qgis.PyQt.QtCore import QCoreApplication, QFileInfo, QSettings, QTranslator
from qgis.PyQt.QtGui import QIcon

try:
from qgis.PyQt.QtGui import QAction
except ImportError:
from qgis.PyQt.QtWidgets import QAction

from qgis.core import Qgis, QgsMapLayer, QgsVectorLayerUtils

# Initialize Qt resources from file resources.py
#from . import resources_rc

Expand All @@ -42,7 +48,8 @@ def __init__(self, iface):
self.plugin_dir = os.path.dirname(__file__)
# initialize locale
localePath = ""
locale = QSettings().value("locale/userLocale", type=str)[0:2]
locale_setting = QSettings().value("locale/userLocale", "", type=str) or ""
locale = locale_setting[0:2]

if QFileInfo(self.plugin_dir).exists():
localePath = os.path.join(self.plugin_dir,
Expand All @@ -52,9 +59,7 @@ def __init__(self, iface):
if QFileInfo(localePath).exists():
self.translator = QTranslator()
self.translator.load(localePath)

if qVersion() > '4.3.3':
QCoreApplication.installTranslator(self.translator)
QCoreApplication.installTranslator(self.translator)

def initGui(self):
# Create action that will start plugin configuration
Expand All @@ -66,19 +71,22 @@ def initGui(self):

# connect to signals for button behavior
self.action.triggered.connect(self.run)
self.iface.currentLayerChanged["QgsMapLayer *"].connect(self.toggle)
self.canvas.selectionChanged.connect(self.toggle)
self.iface.currentLayerChanged.connect(self._refresh_action_state)
self.canvas.selectionChanged.connect(self._refresh_action_state)

# Add toolbar button and menu item
self.iface.advancedDigitizeToolBar().addAction(self.action)
self.iface.editMenu().addAction(self.action)

def _refresh_action_state(self, *_args):
self.toggle()

# function to activate or deactivate the plugin buttons
def toggle(self):
# get current active layer
layer = self.canvas.currentLayer()

if layer and layer.type() == layer.VectorLayer:
if layer and layer.type() == QgsMapLayer.LayerType.VectorLayer:
# disconnect all previously connect signals in current layer
try:
layer.editingStarted.disconnect(self.toggle)
Expand Down Expand Up @@ -106,6 +114,14 @@ def toggle(self):

def unload(self):
# Remove the plugin menu item and icon
try:
self.iface.currentLayerChanged.disconnect(self._refresh_action_state)
except Exception:
pass
try:
self.canvas.selectionChanged.disconnect(self._refresh_action_state)
except Exception:
pass
self.iface.editMenu().removeAction(self.action)
self.iface.advancedDigitizeToolBar().removeAction(self.action)

Expand All @@ -121,7 +137,7 @@ def run(self):
for feature in layer.selectedFeatures():
geom = feature.geometry()
# if feature geometry is multipart starts split processing
if geom != None:
if geom is not None:
if geom.isMultipart():
n_split_feats += 1

Expand All @@ -140,9 +156,11 @@ def run(self):
# single geometry and the attributes of the original feature
for i in range(1,len(parts)):
n_new_feats += 1
new_feat = QgsVectorLayerUtils.createFeature(layer,
parts[i],
attributes)
new_feat = QgsVectorLayerUtils.createFeature(
layer,
parts[i],
attributes,
)
layer.addFeature(new_feat)
# update feature geometry to hold first part of geometry
# (this way one of the output features keeps the original Id)
Expand All @@ -152,16 +170,20 @@ def run(self):
# End process and inform user about the results
if n_new_feats > 0:
layer.endEditCommand()
message = self.tr("Splited {} multipart feature(s) into {} "
"singlepart ones.".format(n_split_feats,
n_new_feats +
n_split_feats))
message = self.tr(
"Split {} multipart feature(s) into {} singlepart ones."
).format(n_split_feats, n_new_feats + n_split_feats)
else:
layer.destroyEditCommand()
message = self.tr("No multipart features selected.")

self.iface.messageBar().pushMessage("Multipart split plugin",message,0,10)
self.iface.messageBar().pushMessage(
"Multipart split plugin",
message,
level=Qgis.MessageLevel.Info,
duration=10,
)


def tr(self, text):
return QCoreApplication.translate("Multipart split", text)
return QCoreApplication.translate("Multipart split", text)