def createFeature(self, feedback, feature_id, type, geometries, class_field=None): attrs = [feature_id] if class_field is not None: attrs.append(class_field) multi_point = QgsMultiPoint() for g in geometries: if feedback.isCanceled(): break vid = QgsVertexId() while True: if feedback.isCanceled(): break found, point = g.constGet().nextVertex(vid) if found: multi_point.addGeometry(point) else: break geometry = QgsGeometry(multi_point) output_geometry = None if type == 0: # envelope rect = geometry.boundingBox() output_geometry = QgsGeometry.fromRect(rect) attrs.append(rect.width()) attrs.append(rect.height()) attrs.append(rect.area()) attrs.append(rect.perimeter()) elif type == 1: # oriented rect output_geometry, area, angle, width, height = geometry.orientedMinimumBoundingBox( ) attrs.append(width) attrs.append(height) attrs.append(angle) attrs.append(area) attrs.append(2 * width + 2 * height) elif type == 2: # circle output_geometry, center, radius = geometry.minimalEnclosingCircle( segments=72) attrs.append(radius) attrs.append(math.pi * radius * radius) elif type == 3: # convex hull output_geometry = geometry.convexHull() attrs.append(output_geometry.constGet().area()) attrs.append(output_geometry.constGet().perimeter()) f = QgsFeature() f.setAttributes(attrs) f.setGeometry(output_geometry) return f
def convertToNodes(self, geom): mp = QgsMultiPoint() # TODO: mega inefficient - needs rework when geometry iterators land # (but at least it doesn't lose Z/M values) for g in geom.constGet().coordinateSequence(): for r in g: for p in r: mp.addGeometry(p) return [QgsGeometry(mp)]
def createFeature(self, feedback, feature_id, type, geometries, class_field=None): attrs = [feature_id] if class_field is not None: attrs.append(class_field) multi_point = QgsMultiPoint() for g in geometries: if feedback.isCanceled(): break vid = QgsVertexId() while True: if feedback.isCanceled(): break found, point = g.constGet().nextVertex(vid) if found: multi_point.addGeometry(point) else: break geometry = QgsGeometry(multi_point) output_geometry = None if type == 0: # envelope rect = geometry.boundingBox() output_geometry = QgsGeometry.fromRect(rect) attrs.append(rect.width()) attrs.append(rect.height()) attrs.append(rect.area()) attrs.append(rect.perimeter()) elif type == 1: # oriented rect output_geometry, area, angle, width, height = geometry.orientedMinimumBoundingBox() attrs.append(width) attrs.append(height) attrs.append(angle) attrs.append(area) attrs.append(2 * width + 2 * height) elif type == 2: # circle output_geometry, center, radius = geometry.minimalEnclosingCircle(segments=72) attrs.append(radius) attrs.append(math.pi * radius * radius) elif type == 3: # convex hull output_geometry = geometry.convexHull() attrs.append(output_geometry.constGet().area()) attrs.append(output_geometry.constGet().perimeter()) f = QgsFeature() f.setAttributes(attrs) f.setGeometry(output_geometry) return f
def createGeom(self, coords): crsDest = self.__layer.crs() rc = ReprojectCoordinates(self.crsId, crsDest.srsid(), self.__hasZ, self.__hasM) if self.crsId != crsDest.srsid(): coordsPoint = list(rc.reproject(coords, True)) else: coordsPoint = list(rc.copyCoordstoPoints(coords)) # Point and multipoint Geometry # Always 1 part, 0 element of matrix if self.__layergeometryType == QgsWkbTypes.PointGeometry: if self.__isMultiType: multipoint = QgsMultiPoint() for coords_item in coordsPoint[0][1]: multipoint.addGeometry(coords_item) geom = QgsGeometry(multipoint) self.createFeature(geom) else: geom = QgsGeometry(coordsPoint[0][1][0]) self.createFeature(geom) elif self.__layergeometryType == QgsWkbTypes.LineGeometry: if self.__isMultiType: multiline = QgsGeometry(QgsMultiLineString()) for j in range(len(coordsPoint)): line = QgsLineString(coordsPoint[j][1]) multiline.addPart(line) self.createFeature(multiline) else: line = QgsGeometry(QgsLineString(coordsPoint[0][1])) self.createFeature(line) elif self.__layergeometryType == QgsWkbTypes.PolygonGeometry: if self.__isMultiType: multipoly = QgsGeometry(QgsMultiPolygon()) for i in range(len(coordsPoint)): if int(coordsPoint[i][0]) > 0: mycurve = QgsLineString(coordsPoint[i][1]) poly = QgsPolygon() poly.setExteriorRing(mycurve) polyGeometry = QgsGeometry(QgsPolygon(poly)) for j in range(len(coordsPoint)): if int(coordsPoint[j][0]) < 0: containsAllPoints = True for k in range(len(coordsPoint[j][1])): containsAllPoints = True curPoint = coordsPoint[j][1][k].clone() containsAllPoints = containsAllPoints \ and polyGeometry.contains(QgsPointXY(curPoint.x(), curPoint.y())) if containsAllPoints: mycurve = QgsLineString(coordsPoint[j][1]) poly.addInteriorRing(mycurve) multipoly.addPart(poly) self.createFeature(multipoly) else: extRing = 0 for i in range(len(coordsPoint)): if int(coordsPoint[i][0]) > 0: extRing = i mycurve = QgsLineString(coordsPoint[extRing][1]) poly = QgsPolygon() poly.setExteriorRing(mycurve) polyGeometry = QgsGeometry(QgsPolygon(poly)) for i in range(len(coordsPoint)): if int(coordsPoint[i][0]) < 0: containsAllPoints = True for j in range(len(coordsPoint[i][1])): containsAllPoints = True curPoint = coordsPoint[i][1][j].clone() containsAllPoints = containsAllPoints \ and polyGeometry.contains(QgsPointXY(curPoint.x(), curPoint.y())) if containsAllPoints: mycurve = QgsLineString(coordsPoint[i][1]) poly.addInteriorRing(mycurve) else: QMessageBox.question(self.iface.mainWindow(), self.translate_str("Ring not in exterior contour"), self.translate_str("The new geometry of the feature" " isn't valid. Do you want to use it anyway?"), QMessageBox.Yes, QMessageBox.No) self.createFeature(QgsGeometry(poly))