-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathadd_linestrings.py
More file actions
40 lines (36 loc) · 881 Bytes
/
add_linestrings.py
File metadata and controls
40 lines (36 loc) · 881 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
from sys import argv
import psycopg2
conn = psycopg2.connect("dbname=%s user=postgres" % argv[1])
paths_qry = conn.cursor()
cur = conn.cursor()
paths_qry.execute("""
SELECT id FROM paths WHERE linestring IS NULL
""")
for (path_id,) in paths_qry:
print path_id
cur.execute("""
UPDATE paths
SET linestring = (
SELECT
st_makeline(st_accum(geom))
FROM (
SELECT nodes.geom
FROM path_nodes
INNER JOIN nodes ON (path_nodes.node_id = nodes.id)
WHERE path_nodes.path_id = %s
ORDER BY path_nodes.sequence_id
) AS foo
) WHERE id = %s
""", (path_id, path_id))
conn.commit()
print "all paths annotated with linestrings. Adding lengths..."
cur.execute("""
UPDATE paths
SET length = ST_Length(ST_GeogFromWKB(linestring))
WHERE length IS NULL AND linestring IS NOT NULL
""")
conn.commit()
print "done."
paths_qry.close()
cur.close()
conn.close()