def testRenderCurvePolygon(self): cs = QgsCircularString() cs.setPoints([ QgsPoint(12, 13.2), QgsPoint(14, 13.4), QgsPoint(14, 15), QgsPoint(13, 15.1), QgsPoint(12, 13.2) ]) cp = QgsCurvePolygon() cp.setExteriorRing(cs) item = QgsAnnotationPolygonItem(cp) item.setSymbol( QgsFillSymbol.createSimple({ 'color': '200,100,100', 'outline_color': 'black', 'outline_width': '2' })) settings = QgsMapSettings() settings.setDestinationCrs(QgsCoordinateReferenceSystem('EPSG:4326')) settings.setExtent(QgsRectangle(10, 10, 18, 18)) settings.setOutputSize(QSize(300, 300)) settings.setFlag(QgsMapSettings.Antialiasing, False) rc = QgsRenderContext.fromMapSettings(settings) image = QImage(200, 200, QImage.Format_ARGB32) image.setDotsPerMeterX(96 / 25.4 * 1000) image.setDotsPerMeterY(96 / 25.4 * 1000) image.fill(QColor(255, 255, 255)) painter = QPainter(image) rc.setPainter(painter) try: item.render(rc, None) finally: painter.end() self.assertTrue( self.imageCheck('curvepolygon_item', 'curvepolygon_item', image))
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 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 polyTransCircle(layer, data, index, layer2, i, ic): LsMax = data["R"] * abs(data["D"]) * np.pi / 180 LsMin = 0 fcount = featureCount(layer) if fcount > 0: l1, l2 = getTangentesGeometry(layer2, ic, i) d = deflection(layer2, i) PI = qgsGeometryToPolyline(l1)[-1] data["Disable"].append("C") 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()) 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 p1 = p2QgsPoint(PI) p2 = p2QgsPoint(data["L"] * np.cos(np.deg2rad(angle)) + p1.x(), data["L"] * np.sin(np.deg2rad(angle)) + p1.y()) line = QgsGeometry.fromPolyline([p1, p2]) a1 = azi(polyline(l1)) a2 = azi(polyline(l2)) msgLog("a1: " + str(a1) + " " + "a2: " + str(a2)) data["C"] = True corda = 2 * data["R"] * np.sin(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) p1 = p2QgsPoint(p1) p2 = p2QgsPoint(p2) feat1 = QgsFeature(layerFields()) feat2 = QgsFeature(layerFields()) feat3 = QgsFeature(layerFields()) feat1.setAttributes(['E', "", data["R"], data["D"], data["T"], data["L"]]) feat2.setAttributes(['C', "", data["R"], data["D"], data["T"], data["L"]]) feat3.setAttributes(['E', "", data["R"], data["D"], data["T"], data["L"]]) # if ((a2 - a1 >= 0 and (a1 <= 90 or a1 >= 270)) or (a2 - a1 <= 0 and (a1 >= 90 or a1 <= 270))) and not ( # (abs(a1 - a2) > 180 or (a1 < 90 and a2 > 90) or (a1 < 90, a2 < 90)) and not (a2 - a1) < 0): pointsT1 = [ p2QgsPoint( clotX(L**2 / (2 * data["R"] * data["L"])) * L, clotY(L**2 / (2 * data["R"] * data["L"])) * L) for L in xrange(data["L"]) ] pointsT2 = list( reversed([ p2QgsPoint( clotX(L**2 / (2 * data["R"] * data["L"])) * L, 1000 - clotY(L**2 / (2 * data["R"] * data["L"])) * L) for L in xrange(data["L"]) ])) IpointsT1 = [ p2QgsPoint( clotX(L**2 / (2 * data["R"] * data["L"])) * L, 1000 - clotY(L**2 / (2 * data["R"] * data["L"])) * L) for L in xrange(data["L"]) ] IpointsT2 = list( reversed([ p2QgsPoint( clotX(L**2 / (2 * data["R"] * data["L"])) * L, clotY(L**2 / (2 * data["R"] * data["L"])) * L) for L in xrange(data["L"]) ])) g1 = QgsGeometry.fromPolyline(pointsT1) dd = diff(p1, pointsT1[0]) g1.translate(dd.x(), dd.y()) g1.rotate(polyline(l1)[0].azimuth(polyline(l1)[-1]) - 90, polyline(g1)[0]) g2 = QgsGeometry.fromPolyline(pointsT2) dd = diff(p2, pointsT2[-1]) g2.translate(dd.x(), dd.y()) g2.rotate(polyline(l2)[0].azimuth(polyline(l2)[-1]) + 90, polyline(g2)[-1]) Ig1 = QgsGeometry.fromPolyline(IpointsT1) dd = diff(p1, IpointsT1[0]) Ig1.translate(dd.x(), dd.y()) Ig1.rotate( polyline(l1)[0].azimuth(polyline(l1)[-1]) - 90, polyline(Ig1)[0]) Ig2 = QgsGeometry.fromPolyline(IpointsT2) dd = diff(p2, IpointsT2[-1]) Ig2.translate(dd.x(), dd.y()) Ig2.rotate( polyline(l2)[0].azimuth(polyline(l2)[-1]) + 90, polyline(Ig2)[-1]) g1, g2 = [g1, g2] if polyline(g1)[-1].distance(polyline(g2)[0]) \ <= polyline(Ig1)[-1].distance(polyline(Ig2)[0]) else [Ig1, Ig2] feat1.setGeometry(g1) feat3.setGeometry(g2) p = p2QgsPoint((p1.x() + p2.x()) / 2, (p2.y() + p1.y()) / 2) p1 = p2QgsPoint(polyline(g1)[-1]) p2 = p2QgsPoint(polyline(g2)[0]) tmp_line = QgsGeometry.fromPolyline([p2QgsPoint(PI), p]) theta = data["L"] / (2 * data["R"]) ys = clotY(theta) * data["L"] p = ys - data["R"] * (1 - np.cos(theta)) E = abs((data["R"] + p) / np.cos(np.deg2rad((data["D"] + 2 * np.rad2deg(theta)) / 2)) - data["R"]) msgLog("E=" + str(E)) p = p2QgsPoint(tmp_line.interpolate(E).asPoint()) circularRing = QgsCircularString() circularRing.setPoints([p1, p, p2]) feat2.setGeometry(circularRing) layer.dataProvider().addFeatures([feat1, feat2, feat3]) return data