Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys, os
- #set up psycopg2 environment
- import psycopg2
- def river_dist_matrix(table_name):
- #driving_distance module
- query = """
- select *
- from pgr_drivingDistance ($$
- select
- id as id,
- source::int4 as source,
- target::int4 as target,
- shape_leng::double precision as cost
- from public.'table_name'
- $$, %s, %s, %s, %s
- )
- """
- #make connection between python and postgresql
- conn = psycopg2.connect("dbname = 'routing_test' user = 'postgres' host = 'localhost' password = 'xxxx'")
- cur = conn.cursor()
- #count rows in the table
- cur.execute("select count(*) from 'table_name'")
- result = cur.fetchone()
- k = result[0] + 1 #number of points = number of segments + 1
- print k
- #run loops
- rs = []
- i = 1
- while i <= k:
- cur.execute(query, (i, 100000000000, False, False))
- rs.append(cur.fetchall())
- i = i + 1
- # print rs
- #import csv module
- import csv
- import tempfile
- import shutil
- j = 0
- h = 0
- ars = []
- element = list(rs)
- # print element
- #export data to every row
- filename = 'ck_dist_m.csv'
- with open(filename, 'wb') as f:
- writer = csv.writer(f, delimiter = ',')
- while j <= k - 1:
- while h <= k - 1:
- rp = element[j][h][3]
- ars.append(rp)
- h = h + 1
- else:
- h = 0
- writer.writerow(ars)
- ars = []
- j = j + 1
- # concerning about flow-connection
- with open(filename, 'rb') as f, tempfile.NamedTemporaryFile(mode='wb', delete=False) as g:
- writer = csv.writer(g, delimiter = ',')
- for row in csv.reader(f):
- row = [element if float(element) < 10**6 else 'nan' for element in row]
- writer.writerow(row)
- shutil.move(g.name, filename)
- conn.close()
- if __name__ == "__main__":
- table_name = 'tc_500wa'
- river_dist_matrix(table_name)
- Traceback (most recent call last):
- File "C:UsersHeinzDesktopriver_dist_matrix.py", line 95, in <module>
- river_dist_matrix(table_name)
- File "C:UsersHeinzDesktopriver_dist_matrix.py", line 40, in river_dist_matrix
- cur.execute("select count(*) from 'table_name'")
- psycopg2.ProgrammingError: syntax error at or near "'table_name'"
- LINE 1: select count(*) from 'table_name'
- ^
- [Finished in 0.2s]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement