forked from exasol/exa-toolbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
geospatial_functions.sql
197 lines (174 loc) · 4.31 KB
/
geospatial_functions.sql
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
CREATE SCHEMA IF NOT EXISTS EXA_toolbox;
-- Functions without spatial reference
-- ST_AsText
--/
CREATE OR REPLACE FUNCTION ST_AsText(geo GEOMETRY) RETURN VARCHAR(2000000) IS
BEGIN
RETURN CAST(geo AS VARCHAR(2000000));
END ST_AsText;
/
-- ST_MinX, ST_MinY, ST_MaxX, ST_MaxY
--/
CREATE OR REPLACE FUNCTION ST_MinX(geo GEOMETRY) RETURN FLOAT IS
env GEOMETRY;
BEGIN
env := ST_Envelope(geo);
IF ST_GeometryType(env) = 'POINT' THEN
RETURN ST_X(env);
END IF;
RETURN ST_X(ST_PointN(ST_ExteriorRing(env),1));
END ST_MinX;
/
--/
CREATE OR REPLACE FUNCTION ST_MinY(geo GEOMETRY) RETURN FLOAT IS
env GEOMETRY;
BEGIN
env := ST_Envelope(geo);
IF ST_GeometryType(env) = 'POINT' THEN
RETURN ST_Y(env);
END IF;
RETURN ST_Y(ST_PointN(ST_ExteriorRing(env),1));
END ST_MinY;
/
--/
CREATE OR REPLACE FUNCTION ST_MaxX(geo GEOMETRY) RETURN FLOAT IS
env GEOMETRY;
BEGIN
env := ST_Envelope(geo);
IF ST_GeometryType(env) = 'POINT' THEN
RETURN ST_X(env);
END IF;
RETURN ST_X(ST_PointN(ST_ExteriorRing(env),2));
END ST_MaxX;
/
--/
CREATE OR REPLACE FUNCTION ST_MaxY(geo GEOMETRY) RETURN FLOAT IS
env GEOMETRY;
BEGIN
env := ST_Envelope(geo);
IF ST_GeometryType(env) = 'POINT' THEN
RETURN ST_Y(env);
END IF;
RETURN ST_Y(ST_PointN(ST_ExteriorRing(env),3));
END ST_MaxY;
/
--/
CREATE OR REPLACE FUNCTION ST_XMin(geo GEOMETRY) RETURN FLOAT IS
BEGIN
RETURN ST_MinX(geo);
END ST_XMin;
/
--/
CREATE OR REPLACE FUNCTION ST_YMin(geo GEOMETRY) RETURN FLOAT IS
BEGIN
RETURN ST_MinY(geo);
END ST_YMin;
/
--/
CREATE OR REPLACE FUNCTION ST_XMax(geo GEOMETRY) RETURN FLOAT IS
BEGIN
RETURN ST_MaxX(geo);
END ST_XMax;
/
--/
CREATE OR REPLACE FUNCTION ST_YMax(geo GEOMETRY) RETURN FLOAT IS
BEGIN
RETURN ST_MaxY(geo);
END ST_YMax;
/
-- ST_Point
--/
CREATE OR REPLACE FUNCTION ST_Point(x_lon FLOAT, y_lat FLOAT) RETURN GEOMETRY IS
BEGIN
RETURN CAST('POINT(' || TO_CHAR(x_lon) || ' ' || TO_CHAR(y_lat) || ')' AS GEOMETRY);
END;
/
-- ST_WKTToSQL
--/
CREATE OR REPLACE FUNCTION ST_WKTToSQL(wkt VARCHAR(2000000)) RETURN GEOMETRY IS
BEGIN
RETURN CAST(wkt AS GEOMETRY);
END ST_WKTToSQL;
/
-- ST_GeomFromText
--/
CREATE OR REPLACE FUNCTION ST_GeomFromText(txt VARCHAR(2000000)) RETURN GEOMETRY IS
BEGIN
RETURN ST_WKTToSQL(txt);
END ST_GeomFromText;
/
-- Functions with WGS 1984 / EPSG:4326
-- Function overloading is not supported, thus SRID specific implementations are required.
-- ST_AsText
--/
CREATE OR REPLACE FUNCTION ST_AsTextWGS(geo GEOMETRY(4326)) RETURN VARCHAR(2000000) IS
BEGIN
RETURN CAST(geo AS VARCHAR(2000000));
END ST_AsTextWGS;
/
-- ST_MinX, ST_MinY, ST_MaxX, ST_MaxY
--/
CREATE OR REPLACE FUNCTION ST_MinXWGS(geo GEOMETRY(4326)) RETURN FLOAT IS
env GEOMETRY(4326);
BEGIN
env := ST_Envelope(geo);
IF ST_GeometryType(env) = 'POINT' THEN
RETURN ST_X(env);
END IF;
RETURN ST_X(ST_PointN(ST_ExteriorRing(env),1));
END ST_MinXWGS;
/
--/
CREATE OR REPLACE FUNCTION ST_MinYWGS(geo GEOMETRY(4326)) RETURN FLOAT IS
env GEOMETRY(4326);
BEGIN
env := ST_Envelope(geo);
IF ST_GeometryType(env) = 'POINT' THEN
RETURN ST_Y(env);
END IF;
RETURN ST_Y(ST_PointN(ST_ExteriorRing(env),1));
END ST_MinYWGS;
/
--/
CREATE OR REPLACE FUNCTION ST_MaxXWGS(geo GEOMETRY(4326)) RETURN FLOAT IS
env GEOMETRY(4326);
BEGIN
env := ST_Envelope(geo);
IF ST_GeometryType(env) = 'POINT' THEN
RETURN ST_X(env);
END IF;
RETURN ST_X(ST_PointN(ST_ExteriorRing(env),2));
END ST_MaxXWGS;
/
--/
CREATE OR REPLACE FUNCTION ST_MaxYWGS(geo GEOMETRY(4326)) RETURN FLOAT IS
env GEOMETRY(4326);
BEGIN
env := ST_Envelope(geo);
IF ST_GeometryType(env) = 'POINT' THEN
RETURN ST_Y(env);
END IF;
RETURN ST_Y(ST_PointN(ST_ExteriorRing(env),3));
END ST_MaxYWGS;
/
-- ST_Point
--/
CREATE OR REPLACE FUNCTION ST_PointWGS(x_lon FLOAT, y_lat FLOAT) RETURN GEOMETRY(4326) IS
BEGIN
RETURN CAST('POINT(' || TO_CHAR(x_lon) || ' ' || TO_CHAR(y_lat) || ')' AS GEOMETRY(4326));
END;
/
-- ST_WKTToSQL
--/
CREATE OR REPLACE FUNCTION ST_WKTToSQLWGS(wkt VARCHAR(2000000)) RETURN GEOMETRY(4326) IS
BEGIN
RETURN CAST(wkt AS GEOMETRY(4326));
END ST_WKTToSQLWGS;
/
--/
CREATE OR REPLACE FUNCTION ST_GeomFromTextWGS(txt VARCHAR(2000000)) RETURN GEOMETRY(4326) IS
BEGIN
RETURN ST_WKTToSQLWGS(txt);
END ST_GeomFromTextWGS;
/
-- EOF