Advertisement
Guest User

Untitled

a guest
Apr 10th, 2016
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.50 KB | None | 0 0
  1. from psycopg2 import connect
  2. from psycopg2.extras import RealDictCursor
  3. from collections import defaultdict
  4.  
  5. connection = connect(database='nskroads', cursor_factory=RealDictCursor)
  6. cursor = connection.cursor()
  7.  
  8. connection2 = connect(database='nskroads')
  9. write_cursor = connection2.cursor()
  10.  
  11. cursor.execute("""
  12.     select a.osm_id, b.nodes
  13.     from rd_line a, rd_ways b
  14.     where
  15.         a.osm_id = b.id and
  16.         a.highway in ('trunk', 'trunk_link', 'primary', 'primary_link', 'secondary',
  17.             'secondary_link', 'tertiary', 'tertiary_link', 'unclassified')""")
  18.  
  19.  
  20. nodes = defaultdict(list)
  21. ways = {}
  22. result = {}
  23.  
  24. for row in cursor:
  25.     for n in row['nodes']:
  26.         nodes[n].append(row['osm_id'])
  27.         ways[row['osm_id']] = row['nodes']
  28.  
  29. split_nodes = {k: v for k, v in nodes.items() if len(v) > 1}
  30.  
  31. for osm_id, nodes in ways.items():
  32.     start = 0
  33.     result[osm_id] = [0]
  34.     for i, node in enumerate(nodes):
  35.         if node in split_nodes and start < i < len(nodes) - 1:
  36.             result[osm_id].append(i)
  37.             start = i
  38.     result[osm_id].append(len(nodes))
  39.  
  40.  
  41. # write_cursor.execute('drop table if exists slice_order')
  42. # write_cursor.execute('create table slice_order (osm_id bigint, slices int[])')
  43. write_cursor.execute('truncate slice_order')
  44.  
  45. args_str = ','.join(write_cursor.mogrify("(%s,%s::int[])", (osm_id, '{' + ','.join(map(str, slices))+ '}'))
  46.     for osm_id, slices in result.items() if True or len(slices) > 2)
  47. print args_str
  48. write_cursor.execute("""
  49.     insert into slice_order values %s
  50.     """ % args_str)
  51.  
  52. connection2.commit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement