-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpostgis_sqls.py
146 lines (119 loc) · 4.41 KB
/
postgis_sqls.py
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# -*- coding: utf-8 -*-
from os.path import abspath
from psycopg2.extensions import AsIs
OPERATION_INTERSECT = 'ST_INTERSECTION'
# OPERATION_UNION = 'ST_UNION'
OPERATION_MINUS = 'ST_DIFFERENCE'
OPERATION_DIFF_W_UNION = 'INTERNAL_DIFF_W_UNION'
def set_global_geom_type(geo_type='MULTILINESTRING'):
''' Set the global type of geometry '''
global g_geo_type
g_geo_type = geo_type
def sqlstr_reset_all_tables(geom_tablename, srid):
''' Get SQL string to reset tables (geom). '''
global g_geo_type
sql_str = '''
DROP TABLE IF EXISTS %s;
create table %s (
gid SERIAL NOT NULL PRIMARY KEY,
wkt VARCHAR
);
select AddGeometryColumn('%s', 'geom', %s, '%s', 2);
''' % (AsIs(geom_tablename), AsIs(geom_tablename), AsIs(geom_tablename), srid, g_geo_type)
return sql_str
def sqlstr_op_records(operation, geom_tablename, segment_1_gid, list_of_gids, buffer_size):
''' Get SQL string to perform operation 'op' between two records . '''
global g_geo_type
sub_op = operation
if operation == OPERATION_DIFF_W_UNION:
sub_op = OPERATION_MINUS
sql_str = '''
INSERT INTO %s (wkt, geom)
SELECT ST_ASTEXT(res.lr), lr
FROM(
SELECT ST_MULTI( ''' % (AsIs(geom_tablename))
if g_geo_type == 'MULTILINESTRING':
sql_str += '''
ST_INTERSECTION(
l.geom,
%s(
st_buffer(l.geom, %s),
st_buffer(r.geom, %s)
)
)
''' % (sub_op, buffer_size, buffer_size)
else:
sql_str += '''
%s(
st_buffer(l.geom, 0),
st_buffer(r.geom, 0)
)
''' % (sub_op)
sql_str += '''
) as lr
FROM (
SELECT geom
FROM %s
WHERE %s.gid = %s
) as l, ''' % (AsIs(geom_tablename), AsIs(geom_tablename), segment_1_gid)
gid_2_sql_substring = sqlstr_build_or_clause_of_gids(geom_tablename, list_of_gids)
if operation == OPERATION_DIFF_W_UNION:
sql_str += '''
(
SELECT ST_Multi(ST_Union(f.geom)) as geom
FROM (
SELECT geom
FROM %s
WHERE %s
) as f
) as r
''' % (AsIs(geom_tablename), gid_2_sql_substring)
else:
sql_str += '''
(
SELECT geom
FROM %s
WHERE %s
) as r
''' % (AsIs(geom_tablename), gid_2_sql_substring)
st_geo_type = 'ST_MultiLineString'
if g_geo_type != 'MULTILINESTRING':
st_geo_type = 'ST_MultiPolygon'
sql_str += '''
) res
where ST_geometrytype(res.lr) = '%s'
RETURNING gid
''' % (st_geo_type)
return sql_str
def sqlstr_create_gid_geom_table(active_tablename, srid):
''' Create a table with gid, geom data. '''
global g_geo_type
sql_str = '''
DROP TABLE IF EXISTS %s;
CREATE TABLE %s (gid SERIAL NOT NULL PRIMARY KEY);
SELECT AddGeometryColumn('%s', 'geom', %s, '%s', 2);
''' % (AsIs(active_tablename), AsIs(active_tablename), AsIs(active_tablename), srid, g_geo_type)
return sql_str
def sqlstr_insert_new_record_to_geom_table(geom_tablename, active_tablename):
''' Create a table with gid, geom data. '''
sql_str = '''
INSERT INTO %s (wkt, geom)
SELECT ST_ASTEXT(ST_UNION(geom)), ST_UNION(geom)
FROM %s
RETURNING gid;
''' % (AsIs(geom_tablename), AsIs(active_tablename))
return sql_str
def sqlstr_build_or_clause_of_gids(geom_tablename, list_of_gids):
''' Union (clause) of gids from a list of gid/gids. '''
sql_substr = ""
for gid_idx, gid_val in enumerate(list_of_gids):
if gid_idx > 0:
sql_substr += ' or'
sql_substr += ' %s.gid = %s' % (AsIs(geom_tablename), gid_val)
return sql_substr
def sqlstr_export_geom_table_to_file(geom_tablename, jl_filename):
''' Get SQL commaind for exporting the geometry file to some json-lines file. '''
sql_str = '''
COPY (SELECT ROW_TO_JSON(t) FROM (SELECT * FROM %s) t) TO '%s'
''' % (AsIs(geom_tablename), abspath(jl_filename))
return sql_str