Question

I use pyscripter to call PostgreSQL from outside and route my network, and here's my code,

import sys, os

#set up psycopg2 environment
import psycopg2

#driving_distance module
query = """
    select *
    from driving_distance ($$
        select
            gid as id,
            source::int4 as source,
            target::int4 as target,
            cost::double precision as cost
        from network
        $$, %s, %s, %s, %s
    )
"""

#make connection between python and postgresql
conn = psycopg2.connect("dbname = 'TC_area' user = 'postgres' host = 'localhost' password = 'xxxx'")
cur = conn.cursor()

#count rows in the table
cur.execute("select count(*) from network")
result = cur.fetchone()
k = result[0] + 1                #number of points = number of segments + 1

#run loops
rs = []
i = 1
while i <= k:
    cur.execute(query, (i, 1000000, False, True))
    rs.append(cur.fetchall())
    i = i + 1

#import csv module
import csv

j = 0
h = 0
ars = []
element = list(rs)

#export data to every row
with open('distMatrix.csv', 'wb') as f:
    writer = csv.writer(f, delimiter = ',')
    while j <= k - 1:
        while h <= k - 1:
            rp = element[j][h][1]
            ars.append(rp)
            h = h + 1
        else:
            h = 0
            writer.writerow(ars)
            ars = []
        j = j + 1

conn.close()

The result is good, but if I want to use the reverse_cost function in the function driving_distance in PostgreSQL, I just add one line below the 'cost::double precision as cost',

rcost::double precision as reverse_cost

I got this error box popped out after I added this line,

enter image description here

and the error message in python IDLE,

Traceback (most recent call last):


File "C:\Users\Heinz\Documents\pyscript\postgresql\distMatrix.py.py", line 47, in <module>
    cur.execute(query, (i, 1000000, False, True))
ProgrammingError: 錯誤:  在"語法錯誤"附近發生 rcost
LINE 7:             rcost::double precision as reverse_cost
                    ^
QUERY:  
        select
            gid as id,
            source::int4 as source,
            target::int4 as target,
            cost::double precision as cost
            rcost::double precision as reverse_cost
        from network

PS. I have altered the table of this network, thus it does have a 'rcost' column, and here's the part of view,

enter image description here

If I execute the code inside pgAdmin, I could successfully get the right result,

SELECT * FROM driving_distance('
SELECT gid as id,
    source::int4 AS source, 
    target::int4 AS target,
    cost::double precision as cost,
    rcost::double precision as reverse_cost
    FROM network',
3, 10000000, false, True);

enter image description here

But I need python to do the loop, how can I solve this problem?

PS. If I set both boolean in the function to FALSE, theoretically the program will ignore rcost and return answers calculated from cost only, but I still got the same error,

enter image description here

The problem seems results from rcost.

I am using PostgreSQL 8.4, python 2.7.6 under Windows 8.1 x64.


UPDATE#1

I changed 2 lines in my script and then it works,

cost::double precision as cost,                    #need to add a trailing comma if followed by rcost

cur.execute(query, (i, 100000000000, False, True)) #the range must be greater than max of rcost
Was it helpful?

Solution

Your line "cost::double precision as cost" needs a trailing comma.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top