|
| 1 | +from __future__ import absolute_import |
| 2 | +from builtins import str |
| 3 | +from qgis.PyQt.QtCore import QSizeF, QPointF |
| 4 | +from qgis.PyQt.QtGui import QColor, QTextDocument |
| 5 | +from qgis.core import QgsGeometry, Qgis, QgsTextAnnotation, QgsWkbTypes, QgsAnnotation |
| 6 | +from qgis.gui import QgsRubberBand |
| 7 | +import psycopg2 |
| 8 | +from pgRoutingLayer import pgRoutingLayer_utils as Utils |
| 9 | +from .FunctionBase import FunctionBase |
| 10 | + |
| 11 | +class Function(FunctionBase): |
| 12 | + |
| 13 | + @classmethod |
| 14 | + def getName(self): |
| 15 | + ''' returns Function name. ''' |
| 16 | + return 'with_PointsCost' |
| 17 | + |
| 18 | + @classmethod |
| 19 | + def getControlNames(self, version): |
| 20 | + ''' returns control names. ''' |
| 21 | + self.version = version |
| 22 | + if self.version < 2.1: |
| 23 | + # version 2.0 has only one to one |
| 24 | + return self.commonControls + self.commonBoxes + [ |
| 25 | + 'labelSourceId', 'lineEditSourceId', 'buttonSelectSourceId', |
| 26 | + 'labelTargetId','labelSide','lineEditSide','lineEditEdge_id','labelFraction','lineEditFraction', 'lineEditPointsTable', 'lineEditTargetId','labelPid','lineEditPid','labelEdge_id', 'buttonSelectTargetId', |
| 27 | + 'labelEdge_id','labelSide' |
| 28 | + |
| 29 | + ] |
| 30 | + else: |
| 31 | + return self.commonControls + self.commonBoxes + [ |
| 32 | + 'labelSourceIds', 'lineEditSourceIds', 'buttonSelectSourceIds', |
| 33 | + 'labelTargetIds', 'lineEditPointsTable', 'label_pointsTable', 'lineEditTargetIds', 'buttonSelectTargetIds', |
| 34 | + 'labelSide','lineEditSide','lineEditEdge_id','labelEdge_id','labelFraction','lineEditFraction','labelPid','lineEditPid', |
| 35 | + 'labelDrivingSide','checkBoxLeft','checkBoxRight' |
| 36 | + ] |
| 37 | + |
| 38 | + |
| 39 | + @classmethod |
| 40 | + def isSupportedVersion(self, version): |
| 41 | + ''' Checks supported version ''' |
| 42 | + # valid starting pgr v2.1 |
| 43 | + return version >= 2.1 |
| 44 | + |
| 45 | + |
| 46 | + @classmethod |
| 47 | + def canExportQuery(self): |
| 48 | + return False |
| 49 | + |
| 50 | + @classmethod |
| 51 | + def canExportMerged(self): |
| 52 | + return False |
| 53 | + |
| 54 | + |
| 55 | + |
| 56 | + def prepare(self, canvasItemList): |
| 57 | + resultNodesTextAnnotations = canvasItemList['annotations'] |
| 58 | + for anno in resultNodesTextAnnotations: |
| 59 | + anno.setVisible(False) |
| 60 | + canvasItemList['annotations'] = [] |
| 61 | + |
| 62 | + |
| 63 | + def getQuery(self, args): |
| 64 | + ''' returns the sql query in required signature format of pgr_withPointsCost ''' |
| 65 | + args['where_clause'] = self.whereClause(args['edge_table'], args['geometry'], args['BBOX']) |
| 66 | + args['where_clause_with'] = self.whereClause(args['points_table'], args['geometry'], args['BBOX']) |
| 67 | + return """ |
| 68 | + SELECT start_vid AS start_vid , end_vid AS end_vid, cost AS cost |
| 69 | + FROM pgr_withPointsCost(' |
| 70 | + SELECT %(id)s AS id, |
| 71 | + %(source)s AS source, |
| 72 | + %(target)s AS target, |
| 73 | + %(cost)s AS cost |
| 74 | + %(reverse_cost)s |
| 75 | + FROM %(edge_table)s |
| 76 | + %(where_clause)s |
| 77 | + ', |
| 78 | + 'SELECT %(pid)s AS pid, |
| 79 | + %(edge_id)s AS edge_id, |
| 80 | + %(fraction)s AS fraction, |
| 81 | + %(side)s AS side |
| 82 | + FROM %(points_table)s |
| 83 | + %(where_clause_with)s', |
| 84 | + array[%(source_ids)s]::BIGINT[], array[%(target_ids)s]::BIGINT[], %(directed)s, '%(driving_side)s') |
| 85 | + """ % args |
| 86 | + |
| 87 | + def getExportQuery(self, args): |
| 88 | + args['result_query'] = self.getQuery(args) |
| 89 | + args['vertex_table'] = """ |
| 90 | + %(edge_table)s_vertices_pgr |
| 91 | + """ % args |
| 92 | + |
| 93 | + return """ |
| 94 | + WITH |
| 95 | + result AS ( %(result_query)s ) |
| 96 | + SELECT result.*, ST_MakeLine(a.the_geom, b.the_geom) AS path_geom |
| 97 | +
|
| 98 | + FROM result |
| 99 | + JOIN %(vertex_table)s AS a ON (start_vid = a.id) |
| 100 | + JOIN %(vertex_table)s AS b ON (end_vid = b.id) |
| 101 | + """ % args |
| 102 | + |
| 103 | + |
| 104 | + |
| 105 | + |
| 106 | + def draw(self, rows, con, args, geomType, canvasItemList, mapCanvas): |
| 107 | + ''' draw the result ''' |
| 108 | + resultPathsRubberBands = canvasItemList['paths'] |
| 109 | + rubberBand = None |
| 110 | + cur_path_id = -1 |
| 111 | + for row in rows: |
| 112 | + cur2 = con.cursor() |
| 113 | + args['result_path_id'] = row[0] |
| 114 | + args['result_source_id'] = row[1] |
| 115 | + args['result_target_id'] = row[2] |
| 116 | + args['result_cost'] = row[3] |
| 117 | + if args['result_path_id'] != cur_path_id: |
| 118 | + cur_path_id = args['result_path_id'] |
| 119 | + if rubberBand: |
| 120 | + resultPathsRubberBands.append(rubberBand) |
| 121 | + rubberBand = None |
| 122 | + |
| 123 | + rubberBand = QgsRubberBand(mapCanvas, Utils.getRubberBandType(False)) |
| 124 | + rubberBand.setColor(QColor(255, 0, 0, 128)) |
| 125 | + rubberBand.setWidth(4) |
| 126 | + if args['result_cost'] != -1: |
| 127 | + query2 = """ |
| 128 | + SELECT ST_AsText( ST_MakeLine( |
| 129 | + (SELECT the_geom FROM %(edge_table)s_vertices_pgr WHERE id = %(result_source_id)d), |
| 130 | + (SELECT the_geom FROM %(edge_table)s_vertices_pgr WHERE id = %(result_target_id)d) |
| 131 | + )) |
| 132 | + """ % args |
| 133 | + ##Utils.logMessage(query2) |
| 134 | + cur2.execute(query2) |
| 135 | + row2 = cur2.fetchone() |
| 136 | + ##Utils.logMessage(str(row2[0])) |
| 137 | + assert row2, "Invalid result geometry. (path_id:%(result_path_id)d, saource_id:%(result_source_id)d, target_id:%(result_target_id)d)" % args |
| 138 | + |
| 139 | + geom = QgsGeometry().fromWkt(str(row2[0])) |
| 140 | + if geom.wkbType() == QgsWkbTypes.MultiLineString: |
| 141 | + for line in geom.asMultiPolyline(): |
| 142 | + for pt in line: |
| 143 | + rubberBand.addPoint(pt) |
| 144 | + elif geom.wkbType() == QgsWkbTypes.LineString: |
| 145 | + for pt in geom.asPolyline(): |
| 146 | + rubberBand.addPoint(pt) |
| 147 | + |
| 148 | + if rubberBand: |
| 149 | + resultPathsRubberBands.append(rubberBand) |
| 150 | + rubberBand = None |
| 151 | + resultNodesTextAnnotations = canvasItemList['annotations'] |
| 152 | + Utils.setStartPoint(geomType, args) |
| 153 | + Utils.setEndPoint(geomType, args) |
| 154 | + for row in rows: |
| 155 | + cur2 = con.cursor() |
| 156 | + args['result_seq'] = row[0] |
| 157 | + args['result_source_id'] = row[1] |
| 158 | + args['result_target_id'] = row[2] |
| 159 | + args['result_cost'] = row[3] |
| 160 | + query2 = """ |
| 161 | + SELECT ST_AsText(%(transform_s)s%(startpoint)s%(transform_e)s) FROM %(edge_table)s |
| 162 | + WHERE %(source)s = %(result_target_id)d |
| 163 | + UNION |
| 164 | + SELECT ST_AsText(%(transform_s)s%(endpoint)s%(transform_e)s) FROM %(edge_table)s |
| 165 | + WHERE %(target)s = %(result_target_id)d |
| 166 | + """ % args |
| 167 | + cur2.execute(query2) |
| 168 | + row2 = cur2.fetchone() |
| 169 | + assert row2, "Invalid result geometry. (target_id:%(result_target_id)d)" % args |
| 170 | + |
| 171 | + geom = QgsGeometry().fromWkt(str(row2[0])) |
| 172 | + pt = geom.asPoint() |
| 173 | + textDocument = QTextDocument("%(result_target_id)d:%(result_cost)f" % args) |
| 174 | + textAnnotation = QgsTextAnnotation() |
| 175 | + textAnnotation.setMapPosition(geom.asPoint()) |
| 176 | + textAnnotation.setFrameSize(QSizeF(textDocument.idealWidth(), 20)) |
| 177 | + textAnnotation.setFrameOffsetFromReferencePoint(QPointF(20, -40)) |
| 178 | + textAnnotation.setDocument(textDocument) |
| 179 | + |
| 180 | + QgsMapCanvasAnnotationItem(textAnnotation, mapCanvas) |
| 181 | + resultNodesTextAnnotations.append(textAnnotation) |
| 182 | + |
| 183 | + |
| 184 | + |
| 185 | + def __init__(self, ui): |
| 186 | + FunctionBase.__init__(self, ui) |
0 commit comments