def OMBBox(self, geom): g = geom.convexHull() if g.type() != QGis.Polygon: return None, None, None, None, None, None r = g.asPolygon()[0] p0 = QgsPoint(r[0][0], r[0][1]) i = 0 l = len(r) OMBBox = QgsGeometry() gBBox = g.boundingBox() OMBBox_area = gBBox.height() * gBBox.width() OMBBox_angle = 0 OMBBox_width = 0 OMBBox_heigth = 0 OMBBox_perim = 0 while i < l - 1: x = QgsGeometry(g) angle = self.GetAngleOfLineBetweenTwoPoints(r[i], r[i + 1]) x.rotate(angle, p0) bbox = x.boundingBox() bb = QgsGeometry.fromWkt(bbox.asWktPolygon()) bb.rotate(-angle, p0) areabb = bb.area() if areabb <= OMBBox_area: OMBBox = QgsGeometry(bb) OMBBox_area = areabb OMBBox_angle = angle OMBBox_width = bbox.width() OMBBox_heigth = bbox.height() OMBBox_perim = 2 * OMBBox_width + 2 * OMBBox_heigth i += 1 return OMBBox, OMBBox_area, OMBBox_perim, OMBBox_angle, OMBBox_width, OMBBox_heigth
def clipped(self, clip_geometry): transform_func = self.settings.mapTo3d().transformRotatedXY # create a grid geometry and split polygons with the grid grid = self.provider.readAsGridGeometry(self.grid_size.width(), self.grid_size.height(), self.extent) if self.extent.rotation(): clip_geometry = QgsGeometry(clip_geometry) clip_geometry.rotate(self.extent.rotation(), self.extent.center()) bnds = grid.segmentizeBoundaries(clip_geometry) polys = grid.splitPolygon(clip_geometry) tin = TINGeometry.fromQgsGeometry(polys, None, transform_func, centroid=False, use_earcut=True) d = tin.toDict(flat=True) polygons = [] for bnd in bnds: geom = LineGeometry.fromQgsGeometry(bnd, None, transform_func, useZM=VectorGeometry.UseZ) polygons.append(geom.toList()) d["polygons"] = polygons return d
def processAlgorithm(self, parameters, context, feedback): if DEBUG_MODE: logMessage( "processAlgorithm(): {}".format(self.__class__.__name__), False) clayer = self.parameterAsLayer(parameters, self.INPUT, context) title_field = self.parameterAsString(parameters, self.TITLE_FIELD, context) cf_filter = self.parameterAsBool(parameters, self.CF_FILTER, context) fixed_scale = self.parameterAsEnum(parameters, self.SCALE, context) # == 1 buf = self.parameterAsDouble(parameters, self.BUFFER, context) tex_width = self.parameterAsInt(parameters, self.TEX_WIDTH, context) orig_tex_height = self.parameterAsInt(parameters, self.TEX_HEIGHT, context) header_exp = QgsExpression( self.parameterAsExpression(parameters, self.HEADER, context)) footer_exp = QgsExpression( self.parameterAsExpression(parameters, self.FOOTER, context)) exp_context = QgsExpressionContext() exp_context.appendScope(QgsExpressionContextUtils.layerScope(clayer)) out_dir = self.parameterAsString(parameters, self.OUTPUT, context) if not QDir(out_dir).exists(): QDir().mkpath(out_dir) if DEBUG_MODE: openDirectory(out_dir) mapSettings = self.controller.settings.mapSettings baseExtent = self.controller.settings.baseExtent rotation = mapSettings.rotation() orig_size = mapSettings.outputSize() if cf_filter: cf_layer = QgsMemoryProviderUtils.createMemoryLayer( "current feature", clayer.fields(), clayer.wkbType(), clayer.crs()) layers = [ cf_layer if lyr == clayer else lyr for lyr in mapSettings.layers() ] mapSettings.setLayers(layers) doc = QDomDocument("qgis") clayer.exportNamedStyle(doc) cf_layer.importNamedStyle(doc) total = clayer.featureCount() for current, feature in enumerate(clayer.getFeatures()): if feedback.isCanceled(): break if cf_filter: cf_layer.startEditing() cf_layer.deleteFeatures( [f.id() for f in cf_layer.getFeatures()]) cf_layer.addFeature(feature) cf_layer.commitChanges() title = feature.attribute(title_field) feedback.setProgressText("({}/{}) Exporting {}...".format( current + 1, total, title)) logMessage("Exporting {}...".format(title), False) # extent geometry = QgsGeometry(feature.geometry()) geometry.transform(self.transform) center = geometry.centroid().asPoint() if fixed_scale or geometry.type() == QgsWkbTypes.PointGeometry: tex_height = orig_tex_height or int( tex_width * orig_size.height() / orig_size.width()) rect = RotatedRect(center, baseExtent.width(), baseExtent.width() * tex_height / tex_width, rotation).scale(1 + buf / 100) else: geometry.rotate(rotation, center) rect = geometry.boundingBox().scaled(1 + buf / 100) center = RotatedRect.rotatePoint(rect.center(), rotation, center) if orig_tex_height: tex_height = orig_tex_height tex_ratio = tex_width / tex_height rect_ratio = rect.width() / rect.height() if tex_ratio > rect_ratio: rect = RotatedRect(center, rect.height() * tex_ratio, rect.height(), rotation) else: rect = RotatedRect(center, rect.width(), rect.width() / tex_ratio, rotation) else: # fit to buffered geometry bounding box rect = RotatedRect(center, rect.width(), rect.height(), rotation) tex_height = tex_width * rect.height() / rect.width() rect.toMapSettings(mapSettings) mapSettings.setOutputSize(QSize(tex_width, tex_height)) self.controller.settings.setMapSettings(mapSettings) # labels exp_context.setFeature(feature) self.controller.settings.setHeaderLabel( header_exp.evaluate(exp_context)) self.controller.settings.setFooterLabel( footer_exp.evaluate(exp_context)) self.export(title, out_dir, feedback) feedback.setProgress(int(current / total * 100)) if P_OPEN_DIRECTORY and not DEBUG_MODE: openDirectory(out_dir) return {}
def circleArc2(layer, data, index, layer2, i): fcount = featureCount(layer) if fcount > 0: fant = [f for f in layer.getFeatures()][fcount - 1] l1 = fant.geometry() l2 = QgsGeometry(l1) dd = diff( qgsGeometryToPolyline(fant.geometry())[-1], qgsGeometryToPolyline(fant.geometry())[0]) l2.translate(dd.x(), dd.y()) PI = qgsGeometryToPolyline(fant.geometry())[-1] d = float(data["D"]) if not index == "S" else 90.0 l2.rotate(d, QgsPointXY(PI)) else: data["Disable"].append("C") data["Disable"].append("D") data["Disable"].append("R") data["Disable"].append("L") data["Disable"].append("T") return data data["Disable"].append("C") startAngle = azi(qgsGeometryToPolyline(l1)) angle = 90 - (data["D"] + startAngle) angle = angle if angle > 0 else 360 + angle angle = angle if angle < 360 else angle - 360 angleInternal = 180 - abs(d) p1 = p2QgsPoint(PI) corda = 2 * data["R"] * np.sin(np.deg2rad(angleInternal / 2)) p2 = p2QgsPoint(corda * np.cos(np.deg2rad(angle)) + p1.x(), corda * np.sin(np.deg2rad(angle)) + p1.y()) T = None E = None p = None arc = None if index == "R": pass elif index == "L": data["R"] = data["L"] / (np.deg2rad(abs(data["D"]))) corda = 2 * data["R"] * np.sin(np.deg2rad(abs(data["D"]) / 2)) T = abs(corda / (2 * np.tan(np.deg2rad(abs(data["D"]) / 2)))) p1 = p2QgsPoint(l1.interpolate(l1.length() - data["T"]).asPoint()) l2 = QgsGeometry(l1) dd = diff(p1, qgsGeometryToPolyline(l2)[0]) l2.translate(dd.x(), dd.y()) dd = diff(l2.interpolate(T).asPoint(), qgsGeometryToPolyline(l2)[0]) l2.translate(dd.x(), dd.y()) l2.rotate(data["D"], QgsPointXY(qgsGeometryToPolyline(l2)[0])) p1 = p2QgsPoint(l1.interpolate(l1.length() - data["T"]).asPoint()) p2 = p2QgsPoint(l2.interpolate(data["T"]).asPoint()) PI = p2QgsPoint(qgsGeometryToPolyline(l2)[0]) elif index == "T": corda = 2 * data["R"] * np.sin(np.deg2rad(abs(data["D"]) / 2)) T = abs(corda / (2 * np.tan(np.deg2rad((abs(data["D"])) / 2)))) p1 = p2QgsPoint(l1.interpolate(l1.length() - data["T"]).asPoint()) l2 = QgsGeometry(l1) dd = diff(p1, qgsGeometryToPolyline(l2)[0]) l2.translate(dd.x(), dd.y()) dd = diff(l2.interpolate(T).asPoint(), qgsGeometryToPolyline(l2)[0]) l2.translate(dd.x(), dd.y()) l2.rotate(data["D"], QgsPointXY(qgsGeometryToPolyline(l2)[0])) p1 = p2QgsPoint(l1.interpolate(l1.length() - data["T"]).asPoint()) p2 = p2QgsPoint(l2.interpolate(data["T"]).asPoint()) PI = p2QgsPoint(qgsGeometryToPolyline(l2)[0]) elif index == "D": data["L"] = np.deg2rad(abs(data["D"])) * data["R"] corda = 2 * data["R"] * np.sin(np.deg2rad(abs(data["D"]) / 2)) T = abs(corda / (2 * np.tan(np.deg2rad((abs(data["D"])) / 2)))) p1 = p2QgsPoint(l1.interpolate(l1.length() - data["T"]).asPoint()) l2 = QgsGeometry(l1) dd = diff(p1, qgsGeometryToPolyline(l2)[0]) l2.translate(dd.x(), dd.y()) PI = p2QgsPoint(l2.interpolate(T).asPoint()) dd = diff(PI, qgsGeometryToPolyline(l2)[0]) l2.translate(dd.x(), dd.y()) l2.rotate(data["D"], qgsGeometryToPolyline(l2)[0]) p2 = p2QgsPoint(l2.interpolate(T).asPoint()) elif index == "C": pass elif index == "S": data["C"] = True data["D"] = 90 data["L"] = np.deg2rad(abs(data["D"])) * data["R"] data["T"] = 0 if (data["T"] > l1.length() or data["T"] > l2.length()) and data["C"]: data["L"] = np.deg2rad(abs(d)) * data["R"] * abs(data["D"] / d) corda = 2 * data["R"] * np.sin(np.deg2rad(abs(data["D"]) / 2)) data["T"] = abs(corda / (2 * np.tan(np.deg2rad((d) / 2)))) p1 = p2QgsPoint(l1.interpolate(l1.length() - data["T"]).asPoint()) PI = p2QgsPoint(PI) corda = 2 * data["R"] * np.sin(np.deg2rad(angleInternal / 2)) p2 = p2QgsPoint(corda * np.cos(np.deg2rad(angle)) + p1.x(), corda * np.sin(np.deg2rad(angle)) + p1.y()) T = float(data["T"]) if T is None else T E = abs(T * np.tan(np.deg2rad(data["D"] / 4))) if E is None else E p = p2QgsPoint((p1.x() + p2.x()) / 2, (p2.y() + p1.y()) / 2) tmp_line = QgsGeometry.fromPolyline([p2QgsPoint(PI), p]) p = p2QgsPoint(tmp_line.interpolate(E).asPoint()) feat = QgsFeature(layer.fields()) feat.setAttribute('Tipo', 'C') # Create a QgsCircularStringV2 circularRing = QgsCircularString() # Set first point, intermediate point for curvature and end point circularRing.setPoints([p1, p, p2]) # data["R"]=QgsGeometryUtils.circleCenterRadius(p1,p,p2)[0] circularRing = arc if arc else circularRing feat.setGeometry(circularRing) f2 = QgsFeature(layer.fields()) f2.setGeometry(l2) layer.dataProvider().addFeatures([feat]) data["Disable"].append("C") return data
def circleArc(layer, data, index, layer2, i, ic): fcount = featureCount(layer) if fcount > 0: l1, l2 = getTangentesGeometry(layer2, ic, i) d = deflection(layer2, i) PI = qgsGeometryToPolyline(l1)[-1] else: l1, l2 = getTangentesGeometry(layer2, ic, i) d = deflection(layer2, i) PI = qgsGeometryToPolyline(l1)[-1] if l1 is None: l1 = QgsGeometry(l2) dd = diff( qgsGeometryToPolyline(l2)[0], qgsGeometryToPolyline(l2)[-1]) l2.translate(dd.x(), dd.y()) elif l2 is None: l2 = QgsGeometry(l1) dd = diff( qgsGeometryToPolyline(l1)[-1], qgsGeometryToPolyline(l2)[0]) l2.translate(dd.x(), dd.y()) pl = qgsGeometryToPolyline(l1) startAngle = azi(pl) angle = 90 - (data["D"] + startAngle) angle = angle if angle > 0 else 360 + angle angle = angle if angle < 360 else angle - 360 angleInternal = 180 - d p1 = p2QgsPoint(PI) corda = 2 * data["R"] * np.sin(np.deg2rad(angleInternal / 2)) p2 = p2QgsPoint(corda * np.cos(np.deg2rad(angle)) + p1.x(), corda * np.sin(np.deg2rad(angle)) + p1.y()) T = None E = None p = None arc = None if index == "R": if data["C"]: data["L"] = np.deg2rad(abs(d)) * data["R"] * abs(data["D"] / d) corda = 2 * data["R"] * np.sin(np.deg2rad(abs(data["D"]) / 2)) data["T"] = abs(corda / (2 * np.tan(np.deg2rad((abs(data["D"])) / 2)))) p1 = p2QgsPoint(l1.interpolate(l1.length() - data["T"]).asPoint()) p2 = p2QgsPoint(l2.interpolate(data["T"]).asPoint()) PI = p2QgsPoint(PI) else: data["L"] = np.deg2rad(abs(data["D"])) * data["R"] corda = 2 * data["R"] * np.sin(np.deg2rad(abs(data["D"]) / 2)) T = abs(corda / (2 * np.tan(np.deg2rad((abs(data["D"])) / 2)))) p1 = p2QgsPoint(l1.interpolate(l1.length() - data["T"]).asPoint()) l2 = QgsGeometry(l1) dd = diff(p1, qgsGeometryToPolyline(l2)[0]) l2.translate(dd.x(), dd.y()) dd = diff( l2.interpolate(T).asPoint(), qgsGeometryToPolyline(l2)[0]) l2.translate(dd.x(), dd.y()) l2.rotate(data["D"], QgsPointXY(qgsGeometryToPolyline(l2)[0])) l2.extendLine(0, 500000) p1 = p2QgsPoint(l1.interpolate(l1.length() - data["T"]).asPoint()) p2 = p2QgsPoint(l2.interpolate(data["T"]).asPoint()) PI = p2QgsPoint(qgsGeometryToPolyline(l2)[0]) elif index == "L": if data["C"]: if data["L"] > np.deg2rad(abs(d)) * data["R"] * abs(data["D"] / d): data["D"] = d / abs(d) * abs( abs(d) * data["L"] / (np.deg2rad(abs(d)) * data["R"])) if data["L"] > np.deg2rad(abs(d)) * data["R"]: data["R"] = data["L"] / (np.deg2rad(abs(d))) corda = 2 * data["R"] * np.sin(np.deg2rad(abs(d) / 2)) data["T"] = abs(corda / (2 * np.tan(np.deg2rad((abs(d)) / 2)))) p1 = p2QgsPoint( l1.interpolate(l1.length() - data["T"]).asPoint()) p2 = p2QgsPoint(l2.interpolate(data["T"]).asPoint()) PI = p2QgsPoint(PI) else: data["R"] = data["L"] / (np.deg2rad(abs(data["D"]))) corda = 2 * data["R"] * np.sin(np.deg2rad(abs(data["D"]) / 2)) T = abs(corda / (2 * np.tan(np.deg2rad(abs(data["D"]) / 2)))) p1 = p2QgsPoint(l1.interpolate(l1.length() - data["T"]).asPoint()) l2 = QgsGeometry(l1) dd = diff(p1, qgsGeometryToPolyline(l2)[0]) l2.translate(dd.x(), dd.y()) dd = diff( l2.interpolate(T).asPoint(), qgsGeometryToPolyline(l2)[0]) l2.translate(dd.x(), dd.y()) l2.rotate(data["D"], QgsPointXY(qgsGeometryToPolyline(l2)[0])) p1 = p2QgsPoint(l1.interpolate(l1.length() - data["T"]).asPoint()) p2 = p2QgsPoint(l2.interpolate(data["T"]).asPoint()) PI = p2QgsPoint(qgsGeometryToPolyline(l2)[0]) elif index == "T": if data["C"]: corda = data["T"] * abs(2 * np.tan(np.deg2rad((d) / 2))) data["L"] = np.deg2rad(abs(d)) * data["R"] * abs(data["D"] / d) data["R"] = corda / (2 * np.sin(np.deg2rad(angleInternal / 2))) data["L"] = np.deg2rad(abs(d)) * data["R"] * abs(data["D"] / d) p1 = p2QgsPoint(l1.interpolate(l1.length() - data["T"]).asPoint()) p2 = p2QgsPoint(l2.interpolate(data["T"]).asPoint()) PI = p2QgsPoint(PI) T = data["T"] else: corda = 2 * data["R"] * np.sin(np.deg2rad(abs(data["D"]) / 2)) T = abs(corda / (2 * np.tan(np.deg2rad((abs(data["D"])) / 2)))) p1 = p2QgsPoint(l1.interpolate(l1.length() - data["T"]).asPoint()) l2 = QgsGeometry(l1) dd = diff(p1, qgsGeometryToPolyline(l2)[0]) l2.translate(dd.x(), dd.y()) dd = diff( l2.interpolate(T).asPoint(), qgsGeometryToPolyline(l2)[0]) l2.translate(dd.x(), dd.y()) l2.rotate(data["D"], QgsPointXY(qgsGeometryToPolyline(l2)[0])) p1 = p2QgsPoint(l1.interpolate(l1.length() - data["T"]).asPoint()) p2 = p2QgsPoint(l2.interpolate(data["T"]).asPoint()) PI = p2QgsPoint(qgsGeometryToPolyline(l2)[0]) elif index == "D": if data["C"]: T = data["T"] data["L"] = np.deg2rad(abs(d)) * data["R"] * abs(data["D"] / d) p1 = p2QgsPoint(l1.interpolate(l1.length() - data["T"]).asPoint()) p2 = p2QgsPoint(l2.interpolate(data["T"]).asPoint()) PI = p2QgsPoint(PI) E = abs(T * np.tan(np.deg2rad(d / 4))) l2 = QgsGeometry(l1) dd = diff(PI, qgsGeometryToPolyline(l2)[0]) l2.translate(dd.x(), dd.y()) l2.rotate( abs((abs(d) - 180) / d * data["D"] + 180 - abs(d)) * d / abs(d) + d, qgsGeometryToPolyline(l2)[0]) p = p2QgsPoint((p1.x() + p2.x()) / 2, (p2.y() + p1.y()) / 2) tmp_line = QgsGeometry.fromPolyline([p2QgsPoint(PI), p]) p = p2QgsPoint(tmp_line.interpolate(E).asPoint()) arc = QgsCircularString() arc.setPoints([p1, p, p2]) l3 = QgsGeometry(l2) dd = diff( qgsGeometryToPolyline(l3)[-1], qgsGeometryToPolyline(l3)[0]) l3.translate(dd.x(), dd.y()) l3.rotate(-90 * d / abs(d), qgsGeometryToPolyline(l3)[0]) l2 = unifyGeometry(l2, l3) arc = QgsCircularString() arc.setPoints([p1, p, p2]) arc = splitGeometry(l2, createGeometry(arc)) else: data["L"] = np.deg2rad(abs(data["D"])) * data["R"] corda = 2 * data["R"] * np.sin(np.deg2rad(abs(data["D"]) / 2)) T = abs(corda / (2 * np.tan(np.deg2rad((abs(data["D"])) / 2)))) p1 = p2QgsPoint(l1.interpolate(l1.length() - data["T"]).asPoint()) l2 = QgsGeometry(l1) dd = diff(p1, qgsGeometryToPolyline(l2)[0]) l2.translate(dd.x(), dd.y()) PI = p2QgsPoint(l2.interpolate(T).asPoint()) dd = diff(PI, qgsGeometryToPolyline(l2)[0]) l2.translate(dd.x(), dd.y()) l2.rotate(data["D"], qgsGeometryToPolyline(l2)[0]) p2 = p2QgsPoint(l2.interpolate(T).asPoint()) elif index == "C": if data["C"]: data["D"] = d data["L"] = np.deg2rad(abs(data["D"])) * data["R"] corda = 2 * data["R"] * np.sin(np.deg2rad(abs(data["D"]) / 2)) data["T"] = abs(corda / (2 * np.tan(np.deg2rad((d) / 2)))) p1 = p2QgsPoint(l1.interpolate(l1.length() - data["T"]).asPoint()) p2 = p2QgsPoint(l2.interpolate(data["T"]).asPoint()) PI = p2QgsPoint(PI) else: p1 = p2QgsPoint(l1.interpolate(l1.length() - data["T"]).asPoint()) p2 = p2QgsPoint(l2.interpolate(data["T"]).asPoint()) PI = p2QgsPoint(PI) elif index == "S": data["C"] = True # data["D"]=d data["L"] = np.deg2rad(abs(data["D"])) * data["R"] corda = 2 * data["R"] * np.sin(np.deg2rad(abs(data["D"]) / 2)) #data["T"]=abs(corda/(2*np.tan(np.deg2rad((d)/2)))) p1 = p2QgsPoint(l1.interpolate(l1.length() - data["T"]).asPoint()) p2 = p2QgsPoint(l2.interpolate(data["T"]).asPoint()) PI = p2QgsPoint(PI) if (data["T"] > l1.length() or data["T"] > l2.length()) and data["C"]: data["L"] = np.deg2rad(abs(d)) * data["R"] * abs(data["D"] / d) corda = 2 * data["R"] * np.sin(np.deg2rad(abs(data["D"]) / 2)) data["T"] = abs(corda / (2 * np.tan(np.deg2rad((d) / 2)))) p1 = p2QgsPoint(l1.interpolate(l1.length() - data["T"]).asPoint()) p2 = p2QgsPoint(l2.interpolate(data["T"]).asPoint()) PI = p2QgsPoint(PI) T = data["T"] if T is None else T E = abs(T * np.tan(np.deg2rad(data["D"] / 4))) if E is None else E p = p2QgsPoint((p1.x() + p2.x()) / 2, (p2.y() + p1.y()) / 2) #msgLog("p: " + str(p.x()) +"," +str(p.y())) #msgLog("PI: " + str(PI.x()) +"," +str(PI.y())) tmp_line = QgsGeometry.fromPolyline([p2QgsPoint(PI), p]) p = p2QgsPoint(tmp_line.interpolate(E).asPoint()) feat = QgsFeature(layerFields()) feat.setAttributes(['C', "", data["R"], data["D"], data["T"], data["L"]]) # Create a QgsCircularStringV2 circularRing = QgsCircularString() # Set first point, intermediate point for curvature and end point circularRing.setPoints([p1, p, p2]) # data["R"]=QgsGeometryUtils.circleCenterRadius(p1,p,p2)[0] circularRing = arc if arc else circularRing feat.setGeometry(circularRing) f2 = QgsFeature(layer.fields()) f2.setGeometry(l2) layer.dataProvider().addFeatures([feat]) return data
def processAlgorithm(self, parameters, context, feedback): if DEBUG_MODE: logMessage("processAlgorithm(): {}".format( self.__class__.__name__)) source = self.parameterAsSource(parameters, self.INPUT, context) source_layer = self.parameterAsLayer(parameters, self.INPUT, context) title_field = self.parameterAsString(parameters, self.TITLE_FIELD, context) cf_filter = self.parameterAsBool(parameters, self.CF_FILTER, context) fixed_scale = self.parameterAsEnum(parameters, self.SCALE, context) # == 1 buf = self.parameterAsDouble(parameters, self.BUFFER, context) tex_width = self.parameterAsInt(parameters, self.TEX_WIDTH, context) orig_tex_height = self.parameterAsInt(parameters, self.TEX_HEIGHT, context) out_dir = self.parameterAsString(parameters, self.OUTPUT, context) mapSettings = self.controller.settings.mapSettings baseExtent = self.controller.settings.baseExtent rotation = mapSettings.rotation() orig_size = mapSettings.outputSize() if cf_filter: #TODO: FIX ME #cf_layer = QgsMemoryProviderUtils.createMemoryLayer("current feature", # source_layer.fields(), # source_layer.wkbType(), # source_layer.crs()) doc = QDomDocument("qgis") source_layer.exportNamedStyle(doc) orig_layers = mapSettings.layers() total = source.featureCount() for current, feature in enumerate(source.getFeatures()): if feedback.isCanceled(): break if cf_filter: cf_layer = QgsMemoryProviderUtils.createMemoryLayer( "current feature", source_layer.fields(), source_layer.wkbType(), source_layer.crs()) cf_layer.startEditing() cf_layer.addFeature(feature) cf_layer.commitChanges() cf_layer.importNamedStyle(doc) layers = [ cf_layer if lyr == source_layer else lyr for lyr in orig_layers ] mapSettings.setLayers(layers) title = feature.attribute(title_field) feedback.setProgressText("({}/{}) Exporting {}...".format( current + 1, total, title)) # extent geometry = QgsGeometry(feature.geometry()) geometry.transform(self.transform) center = geometry.centroid().asPoint() if fixed_scale or geometry.type() == QgsWkbTypes.PointGeometry: tex_height = orig_tex_height or int( tex_width * orig_size.height() / orig_size.width()) rect = RotatedRect(center, baseExtent.width(), baseExtent.width() * tex_height / tex_width, rotation).scale(1 + buf / 100) else: geometry.rotate(rotation, center) rect = geometry.boundingBox().scaled(1 + buf / 100) center = RotatedRect.rotatePoint(rect.center(), rotation, center) if orig_tex_height: tex_height = orig_tex_height tex_ratio = tex_width / tex_height rect_ratio = rect.width() / rect.height() if tex_ratio > rect_ratio: rect = RotatedRect(center, rect.height() * tex_ratio, rect.height(), rotation) else: rect = RotatedRect(center, rect.width(), rect.width() / tex_ratio, rotation) else: # fit to buffered geometry bounding box rect = RotatedRect(center, rect.width(), rect.height(), rotation) tex_height = tex_width * rect.height() / rect.width() rect.toMapSettings(mapSettings) mapSettings.setOutputSize(QSize(tex_width, tex_height)) self.controller.settings.setMapSettings(mapSettings) self.export(title, out_dir, feedback) feedback.setProgress(int(current / total * 100)) return {}
def getSubzones(z, paper, scale): ''' ''' divisiones = [] for f in QgsProject.instance().mapLayer(DVL).getFeatures(): QgsProject.instance().mapLayer(DVL).dataProvider().deleteFeatures( [f.id()]) for f in QgsProject.instance().mapLayer(DVL2).getFeatures(): QgsProject.instance().mapLayer(DVL2).dataProvider().deleteFeatures( [f.id()]) pr = QgsProject.instance().mapLayer(DVL).dataProvider() pr2 = QgsProject.instance().mapLayer(DVL2).dataProvider() geom, area, angle, width, height = z.geometry().orientedMinimumBoundingBox( ) centroid = geom.centroid() ## l1 = (z.geometry().orientedMinimumBoundingBox()[0].vertexAt(0).distance( z.geometry().orientedMinimumBoundingBox()[0].vertexAt(1))) l2 = (z.geometry().orientedMinimumBoundingBox()[0].vertexAt(1).distance( z.geometry().orientedMinimumBoundingBox()[0].vertexAt(2))) ##calcular angulo a1 = math.degrees( QgsGeometryUtils.angleBetweenThreePoints( geom.vertexAt(0).x(), geom.vertexAt(0).y(), geom.vertexAt(1).x(), geom.vertexAt(1).y(), geom.vertexAt(1).x(), geom.vertexAt(1).y() + 1000000, )) a2 = math.degrees( QgsGeometryUtils.angleBetweenThreePoints( geom.vertexAt(2).x(), geom.vertexAt(2).y(), geom.vertexAt(1).x(), geom.vertexAt(1).y(), geom.vertexAt(1).x(), geom.vertexAt(1).y() + 1000000, )) a = a1 if l1 < l2 else a2 geom.rotate(a, centroid.asPoint()) ##numero de divisiones # nx = int(l1 / paper[0] * 0.001 / scale + 1) # ny = int(l2 / paper[1] * 0.001 / scale + 1) nx = int(l1 / paper[0] * 1000 * scale + 1) ny = int(l2 / paper[1] * 1000 * scale + 1) debug(('matrix: ', nx, ny)) ##si son enteras cual es el tamaƱo final # flx = nx * paper[0] / 0.001 * scale # fly = ny * paper[1] / 0.001 * scale flx = nx * paper[0] / scale / 1000 fly = ny * paper[1] / scale / 1000 # longitudes de los nuevos cuadrados l_x = flx / nx l_y = fly / ny # desplazamiento del resultado respecto a una matriz # o centro del nuevo bbox cx = flx / 2 cy = fly / 2 #generar las divisiones matrix = [nx, ny] for fex in range(matrix[0]): for fey in range(matrix[1]): feat = QgsFeature() ng = QgsGeometry().fromRect( QgsRectangle(fex * l_x, fey * l_y, (fex + 1) * l_x, (fey + 1) * l_y)) ng.translate(centroid.asPoint().x() - cx, centroid.asPoint().y() - cy) ng.rotate(-a, centroid.asPoint()) feat.setGeometry(ng) feat.setAttributes( [z.attribute('id'), z.attribute('name'), a, fex, fey]) pr.addFeatures([feat]) pr2.addFeatures([feat]) divisiones.append(feat) QgsProject.instance().mapLayer(DVL).commitChanges() QgsProject.instance().mapLayer(DVL2).commitChanges() return divisiones