Guest User

Untitled

a guest
Jun 15th, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. import sys
  2.  
  3. #set up psycopg2 environment
  4. import psycopg2
  5.  
  6. #driving_distance module
  7. query = """
  8. select *
  9. from driving_distance ($$
  10. select
  11. gid as id,
  12. start_id::int4 as source,
  13. end_id::int4 as target,
  14. shape_leng::double precision as cost
  15. from network
  16. $$, %s, %s, %s, %s
  17. )
  18. ;"""
  19.  
  20. #make connection between python and postgresql
  21. conn = psycopg2.connect("dbname = 'routing_template' user = 'postgres' host = 'localhost' password = 'ntubse40'")
  22. cur = conn.cursor()
  23.  
  24. #count rows in the table
  25. cur.execute("select count(*) from network")
  26. result = cur.fetchone()
  27. k = result[0] + 1
  28.  
  29. #run loops
  30. rs = []
  31. i = 1
  32. while i <= k:
  33. cur.execute(query, (i, 1000000, False, False))
  34. rs.append(cur.fetchall())
  35. i = i + 1
  36.  
  37. #print result
  38. for record in rs:
  39. print record
  40. conn.close()
  41.  
  42. [(1, 2, 35789.4069722436), (2, 2, 31060.0761437413), (3, 19, 30915.1312550546), (4, 3, 33438.0715007666), (5, 4, 29149.0894812718), (6, 7, 25504.020006665), (7, 7, 29594.741802956), (8, 5, 20736.2427352646), (9, 10, 19545.809601197), (10, 8, 22609.5146670393), (11, 9, 14134.5400189648), (12, 11, 12266.7845493204), (13, 18, 17426.7449057031), (14, 21, 11754.7277029158), (15, 18, 13128.3548040769), (16, 20, 21924.2253916803), (17, 11, 15209.9969992088), (18, 20, 26316.7797545076), (19, 13, 604.414419026164), (20, 16, 740.652673783403), (21, 15, 0.0), (22, 15, 2378.768084459)]
  43. [(1, 2, 38168.1750567026), (2, 2, 33438.8442282003), (3, 19, 33293.8993395136), (4, 3, 35816.8395852256), (5, 4, 31527.8575657308), (6, 7, 27882.788091124), (7, 7, 31973.509887415), (8, 5, 23115.0108197236), (9, 10, 21924.577685656), (10, 8, 24988.2827514983), (11, 9, 16513.3081034238), (12, 11, 14645.5526337793), (13, 18, 19805.5129901621), (14, 21, 14133.4957873748), (15, 18, 15507.1228885359), (16, 20, 24302.9934761393), (17, 11, 17588.7650836678), (18, 20, 28695.5478389666), (19, 13, 2983.18250348516), (20, 16, 3119.4207582424), (21, 15, 2378.768084459), (22, 15, 0.0)]
  44.  
  45. import csv
  46.  
  47. records = [(1, 2, 3), (4, 5, 6), (7, 8, 9)]
  48.  
  49. with open('somefile.csv', 'w') as f:
  50. writer = csv.writer(f, delimiter=',')
  51. for row in records:
  52. writer.writerow(row)
  53.  
  54. print "Done Writing"
  55.  
  56. query = """
  57. select *
  58. from driving_distance ($$
  59. select
  60. gid as id,
  61. start_id::int4 as source,
  62. end_id::int4 as target,
  63. shape_leng::double precision as cost
  64. from network
  65. $$, %s, %s, %s, %s
  66. )
  67. ;"""
  68. outputquery = "COPY ({0}) TO STDOUT WITH CSV HEADER".format(query)
  69. with open('archivo.csv', 'w') as f:
  70. cur.copy_expert(outputquery, f)
  71. conn.close()
Add Comment
Please, Sign In to add comment