Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 31 additions & 10 deletions functions/CostBase.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,6 @@ class CostBase(FunctionBase):
def __init__(self, ui):
FunctionBase.__init__(self, ui)

@classmethod
def isSupportedVersion(self, version):
''' Checks supported version '''
# valid starting pgr v2.1
return version >= 2.1

@classmethod
def canExportMerged(self):
return False
Expand Down Expand Up @@ -67,12 +61,39 @@ def getExportQuery(self, args):

return sql.SQL("""
WITH
result AS ( {result_query} )
SELECT result.*, ST_MakeLine(a.the_geom, b.the_geom) AS path_geom
result AS ( {result_query} ),
departure AS (
SELECT DISTINCT ON (start_vid, end_vid)
start_vid, end_vid,
CASE
WHEN {edge_table}.{source} = start_vid
THEN ST_StartPoint({edge_table}.{geometry})
ELSE ST_EndPoint({edge_table}.{geometry})
END AS depart
FROM result
JOIN {edge_schema}.{edge_table}
ON ({edge_table}.{source} = start_vid OR {edge_table}.{target} = start_vid)
ORDER BY start_vid, end_vid, {edge_table}.{id}
),
destination AS (
SELECT DISTINCT ON (start_vid, end_vid)
start_vid, end_vid,
CASE
WHEN {edge_table}.{source} = end_vid
THEN ST_StartPoint({edge_table}.{geometry})
ELSE ST_EndPoint({edge_table}.{geometry})
END AS arrive
FROM result
JOIN {edge_schema}.{edge_table}
ON ({edge_table}.{source} = end_vid OR {edge_table}.{target} = end_vid)
ORDER BY start_vid, end_vid, {edge_table}.{id}
)

SELECT result.*, ST_MakeLine(depart, arrive) AS path_geom

FROM result
JOIN {vertex_schema}.{vertex_table} AS a ON (start_vid = a.id)
JOIN {vertex_schema}.{vertex_table} AS b ON (end_vid = b.id)
JOIN departure USING (start_vid, end_vid)
JOIN destination USING (start_vid, end_vid)
""").format(**args)

def draw(self, rows, con, args, geomType, canvasItemList, mapCanvas):
Expand Down
11 changes: 5 additions & 6 deletions functions/DijkstraBase.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,12 @@ def prepare(self, canvasItemList):
@classmethod
def getQuery(self, args):
''' returns the sql query in required signature format of pgr_dijkstra '''
return sql.SQL("""
SELECT seq, '(' || start_vid || ',' || end_vid || ')' AS path_name,
return sql.SQL("""SELECT seq,
'(' || start_vid || ',' || end_vid || ')' AS path_name,
path_seq AS _path_seq, start_vid AS _start_vid, end_vid AS _end_vid,
node AS _node, edge AS _edge, cost AS _cost, lead(agg_cost) over() AS _agg_cost
FROM {function}('
{innerQuery}
',
node AS _node, edge AS _edge,
cost AS _cost, agg_cost AS _agg_cost
FROM {function}('{innerQuery}',
{source_ids}, {target_ids}, {directed})
""").format(**args)

Expand Down
106 changes: 32 additions & 74 deletions functions/FunctionBase.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,12 @@
from psycopg2 import sql

from pgRoutingLayer import pgRoutingLayer_utils as Utils
from pgRoutingLayer.utilities import pgr_queries as PgrQ


class FunctionBase(object):

minPGRversion = 2.1
minPGRversion = 3.0

# the mayority of the functions have this values
exportButton = True
Expand Down Expand Up @@ -239,47 +240,21 @@ def drawManyPaths(self, rows, columns, con, args, geomType, canvasItemList, mapC
resultPathsRubberBands.append(rubberBand)
rubberBand = None

@classmethod
def drawOnePath(self, rows, con, args, geomType, canvasItemList, mapCanvas):
''' draws line string on the mapCanvas. '''
resultPathRubberBand = canvasItemList['path']
for row in rows:
cur2 = con.cursor()
args['result_node_id'] = sql.Literal(row[1])
args['result_edge_id'] = sql.Literal(row[2])
args['result_cost'] = row[3]
if row[2] != -1:
query2 = sql.SQL("""
SELECT ST_AsText({geom_t} FROM {edge_schema}.{edge_table}
WHERE {source} = {result_node_id} AND {id} = {result_edge_id}
UNION
SELECT ST_AsText(ST_Reverse({geom_t}) FROM {edge_schema}.{edge_table}
WHERE {target} = {result_node_id} AND {id} = {result_edge_id};
""").format(**args)

cur2.execute(query2)
row2 = cur2.fetchone()

geom = QgsGeometry().fromWkt(str(row2[0]))
if geom.wkbType() == QgsWkbTypes.MultiLineString:
for line in geom.asMultiPolyline():
for pt in line:
resultPathRubberBand.addPoint(pt)
elif geom.wkbType() == QgsWkbTypes.LineString:
for pt in geom.asPolyline():
resultPathRubberBand.addPoint(pt)

@classmethod
def drawCostPaths(self, rows, con, args, geomType, canvasItemList, mapCanvas):
resultPathsRubberBands = canvasItemList['paths']
rubberBand = None
cur_path_id = -1
resultNodesTextAnnotations = canvasItemList['annotations']
for row in rows:
cur2 = con.cursor()
cursor = con.cursor()
midPointCursor = con.cursor()
args['result_path_id'] = row[0]
args['result_source_id'] = sql.Literal(row[1])
args['result_target_id'] = sql.Literal(row[2])
args['result_cost'] = row[3]
args['result_path_name'] = row[4]

if args['result_path_id'] != cur_path_id:
cur_path_id = args['result_path_id']
if rubberBand:
Expand All @@ -290,18 +265,14 @@ def drawCostPaths(self, rows, con, args, geomType, canvasItemList, mapCanvas):
rubberBand.setColor(QColor(255, 0, 0, 128))
rubberBand.setWidth(4)
if args['result_cost'] != -1:
query2 = sql.SQL("""
SELECT ST_AsText( ST_MakeLine(
(SELECT {geometry_vt} FROM {vertex_schema}.{vertex_table} WHERE id = {result_source_id}),
(SELECT {geometry_vt} FROM {vertex_schema}.{vertex_table} WHERE id = {result_target_id})
))
""").format(**args)
# Utils.logMessage(query2)
cur2.execute(query2)
row2 = cur2.fetchone()
# Utils.logMessage(str(row2[0]))

geom = QgsGeometry().fromWkt(str(row2[0]))
costLine = PgrQ.getCostLine(args, sql.Literal(row[1]), sql.Literal(row[2]))
# Utils.logMessage(costLine.as_string(cursor))
cursor.execute(costLine)
row2 = cursor.fetchone()
line = str(row2[0])
# Utils.logMessage(line)

geom = QgsGeometry().fromWkt(line)
if geom.wkbType() == QgsWkbTypes.MultiLineString:
for line in geom.asMultiPolyline():
for pt in line:
Expand All @@ -310,36 +281,23 @@ def drawCostPaths(self, rows, con, args, geomType, canvasItemList, mapCanvas):
for pt in geom.asPolyline():
rubberBand.addPoint(pt)

# TODO label the edge instead of labeling the target points
# Label the edge
midPoint = PgrQ.getMidPoint()
midPointCursor.execute(midPoint,(line,))
pointRow = midPointCursor.fetchone()
# Utils.logMessage("The point:" + str(pointRow[0]))

ptgeom = QgsGeometry().fromWkt(str(pointRow[0]))
pt = ptgeom.asPoint()
textDocument = QTextDocument("{0!s}:{1}".format(args['result_path_name'], args['result_cost']))
textAnnotation = QgsTextAnnotation()
textAnnotation.setMapPosition(pt)
textAnnotation.setFrameSizeMm(QSizeF(20, 5))
textAnnotation.setFrameOffsetFromReferencePointMm(QPointF(5, -5))
textAnnotation.setDocument(textDocument)
QgsMapCanvasAnnotationItem(textAnnotation, mapCanvas)
resultNodesTextAnnotations.append(textAnnotation)

if rubberBand:
resultPathsRubberBands.append(rubberBand)
rubberBand = None
resultNodesTextAnnotations = canvasItemList['annotations']
for row in rows:
cur2 = con.cursor()
args['result_seq'] = row[0]
args['result_source_id'] = sql.Literal(row[1])
result_target_id = row[2]
args['result_target_id'] = sql.Literal(result_target_id)
result_cost = row[3]
query2 = sql.SQL("""
SELECT ST_AsText( ST_startPoint({geometry}) ) FROM {edge_schema}.{edge_table}
WHERE {source} = {result_target_id}
UNION
SELECT ST_AsText( ST_endPoint( {geometry} ) ) FROM {edge_schema}.{edge_table}
WHERE {target} = {result_target_id}
""").format(**args)
cur2.execute(query2)
row2 = cur2.fetchone()

geom = QgsGeometry().fromWkt(str(row2[0]))
pt = geom.asPoint()
textDocument = QTextDocument("{0!s}:{1}".format(result_target_id, result_cost))
textAnnotation = QgsTextAnnotation()
textAnnotation.setMapPosition(geom.asPoint())
textAnnotation.setFrameSize(QSizeF(textDocument.idealWidth(), 20))
textAnnotation.setFrameOffsetFromReferencePoint(QPointF(20, -40))
textAnnotation.setDocument(textDocument)

QgsMapCanvasAnnotationItem(textAnnotation, mapCanvas)
resultNodesTextAnnotations.append(textAnnotation)
164 changes: 0 additions & 164 deletions functions/drivingDistance.py

This file was deleted.

Loading