I am creating my own map tool which is supposed to be able to draw multipart polygons and upload them to the database. ( I know QGIS 3.4 has multipart geometry tool bar built in but because I want to override standard features and customize my workflows I don't wish to use it)
So far I have created 2 tools- one which draws a simple polygon and another which inherits from the first tool but attempts to add a new polygon to an already selected feature geometry (in an attempt to form a multipolygon).
Although the geometry 'addition' doesn't fail, I suspect that the geometries are invalid because they don't display correctly in QGIS or pgAdmin 4. The code for the tool to add the geometries is given.
#the DrawPolygonMapTool Parent Class is the same except the right click method just adds a single feature
#to the database rather than updating an existing feature
class AddPolygonPartMapTool(DrawPolygonMapTool):
def __init__(self, canvas, layer, feature, *args, **kwargs):
super().__init__(canvas,layer)
self.feature = feature
self.geometry = feature.geometry()
def canvasPressEvent(self, mouseEvent):
if mouseEvent.button() == Qt.MiddleButton:
self.canvas.panAction(mouseEvent)
elif mouseEvent.button() == Qt.LeftButton:
self.rubberband = QgsRubberBand(self.canvas, True) # True = a polygon
self.rubberband.setStrokeColor(QColor(255,164,63))
self.rubberband.setWidth(1)
qgsPoint = self.toMapCoordinates(mouseEvent.pos())
self.vertices.append(qgsPoint)
points = [self.vertices]
print(points)
self.rubberband.reset()
self.rubberband = QgsRubberBand(self.canvas, True)
self.rubberband.setStrokeColor(QColor(255,164,63))
self.rubberband.setWidth(1)
self.rubberband.setToGeometry(QgsGeometry.fromPolygonXY(points), None)
elif mouseEvent.button() == Qt.RightButton:
print(self.layer.isEditable())
geometries = [self.geometry]
transformed = self.coord_transform()
geom = QgsGeometry().fromPolygonXY([transformed])
geometries.append(geom)
add_geometry = self.geometry.addPartGeometry(geom)
if add_geometry == 0:
self.geometry = QgsGeometry().collectGeometry(geometries)
#self.geometry = self.geometry.combine(geom)
self.layer.changeGeometry(self.feature.id(),self.geometry)
self.layer.changeAttributeValue(self.feature.id(), 9,datetime.strftime(datetime.now(), '%Y-%m-%d %H:%M:%S'))
#self.feature.setGeometry(self.geometry)
#self.feature['dt_updated'] = datetime.strftime(datetime.now(), '%Y-%m-%d %H:%M:%S')
self.layer.commitChanges()
print ('Commited')
else:
print(self.geometry)
error_dialog = QErrorMessage()
error_dialog.showMessage(self.geometry)
error_dialog.exec_()
self.vertices = []
self.canvas.scene().removeItem(self.rubberband)
self.rubberband.reset()
self.layer.commitChanges()
self.canvas.update()
self.canvas.refresh()
For example when I try to add polygons which don't intersect, pgAdmin4 displays the secondary polygons without any shading but QGIS does.
Similarly when I try to draw polygons with voids in, an intersection doesn't occur and the polygon appears 'as one'
I've extracted the Polygon geometries from the database as WKTs if that helps:
MULTIPOLYGON(((0.0789987830388195 57.0592707500649,0.470519081915522 57.0621757600838,0.508442385783828 57.2651140331119,0.175560767521918 57.2805367257712,0.00550660566522236 57.2035777493775,0.0789987830388195 57.0592707500649)),((-0.315366924917401 57.1966456207223,-0.306652681494841 57.1399752106895,-0.240239690891754 57.1298352766414,-0.217636720528489 57.1838527441328,-0.236851441862241 57.2264033095893,-0.315366924917401 57.1966456207223)),((-0.315366924917401 57.1966456207223,-0.306652681494841 57.1399752106895,-0.240239690891754 57.1298352766414,-0.217636720528489 57.1838527441328,-0.236851441862241 57.2264033095893,-0.315366924917401 57.1966456207223)),((0.683164687226491 57.261838774432,0.733348442989247 57.2136251674191,0.783306697545952 57.2298698173404,0.744741013432225 57.2742178088809,0.683164687226491 57.261838774432)),((0.683164687226491 57.261838774432,0.733348442989247 57.2136251674191,0.783306697545952 57.2298698173404,0.744741013432225 57.2742178088809,0.683164687226491 57.261838774432)))
MULTIPOLYGON(((-0.422395805629202 56.7937484977339,0.951797175188907 56.7452690154102,0.86404091528552 56.6707901809741,-0.0202221205411899 56.6300019517064,-0.401019041974015 56.6707331851972,-0.422395805629202 56.7937484977339)),((0.437429168340144 56.8069655778546,0.573642926377643 56.8139872250542,0.565099044375506 56.7980627151283,0.460452298492742 56.7883351813194,0.371599517091328 56.8022528663843,0.437429168340144 56.8069655778546)),((0.437429168340144 56.8069655778546,0.573642926377643 56.8139872250542,0.565099044375506 56.7980627151283,0.460452298492742 56.7883351813194,0.371599517091328 56.8022528663843,0.437429168340144 56.8069655778546)),((-0.310637326865195 56.7115764732731,-0.258438475118675 56.7010026885061,-0.218644114860903 56.7335880011436,-0.289355672043477 56.7465596798461,-0.310637326865195 56.7115764732731)),((-0.310637326865195 56.7115764732731,-0.258438475118675 56.7010026885061,-0.218644114860903 56.7335880011436,-0.289355672043477 56.7465596798461,-0.310637326865195 56.7115764732731)))
I know there is a specific way to draw Multipolygons as per the OGC standards from this post. I have tried drawing the polygons different ways but I still don't see what I expect.
Could someone please explain to me how QGIS handles multipart geometries and formation thereof?