forked from perliedman/geojson-path-finder
-
Notifications
You must be signed in to change notification settings - Fork 1
/
query_template.sql
168 lines (145 loc) · 6.88 KB
/
query_template.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
CREATE OR REPLACE FUNCTION `$PROJECT_ID.$DATASET.find_points_around_from_geojson`(geojson STRING, startx FLOAT64, starty FLOAT64, max_cost FLOAT64)
RETURNS STRING LANGUAGE js
OPTIONS (
library=["$BUCKET_FILE_PATH"]
)
AS """
const start = {type: "Feature", geometry: { coordinates: [startx, starty], type: "Point" }};
const pathFinder = new geojsonPathFinder.PathFinder(JSON.parse(geojson));
return new Promise((resolve, reject) => {
const nodes = pathFinder.findPointsAround(start, max_cost);
const nodesJson = JSON.stringify({
type: "MultiPoint",
coordinates: nodes
});
resolve(nodesJson);
});
""";
-- get the concave hull
CREATE OR REPLACE FUNCTION `$PROJECT_ID.$DATASET.get_isodistance_concave_hull_from_geojson`(geojson STRING, startx FLOAT64, starty FLOAT64, max_cost FLOAT64)
RETURNS STRING LANGUAGE js
OPTIONS (
library=["$BUCKET_FILE_PATH"]
)
AS """
const start = {type: "Feature", geometry: { coordinates: [startx, starty], type: "Point" }};
const pathFinder = new geojsonPathFinder.PathFinder(JSON.parse(geojson));
return new Promise((resolve, reject) => {
const hull = pathFinder.getIsoDistanceConcaveHull(start, max_cost);
const geometry = JSON.stringify(hull.geometry);
resolve(geometry);
});
""";
-- get the convex hull
-- CREATE OR REPLACE FUNCTION `$PROJECT_ID.$DATASET.get_isodistance_convex_hull_from_geojson`(geojson STRING, startx FLOAT64, starty FLOAT64, max_cost FLOAT64)
-- RETURNS STRING LANGUAGE js
-- OPTIONS (
-- library=["$BUCKET_FILE_PATH"]
-- )
-- AS """
-- const start = {type: "Feature", geometry: { coordinates: [startx, starty], type: "Point" }};
-- const pathFinder = new geojsonPathFinder.PathFinder(JSON.parse(geojson));
-- const hull = pathFinder.getIsoDistanceConvexHull(start, max_cost);
-- try {
-- return JSON.stringify(hull.geometry);
-- } catch (e) {
-- return(null);
-- }
-- """;
-- get isochrone the concave hull
CREATE OR REPLACE FUNCTION `$PROJECT_ID.$DATASET.get_isochrone_concave_hull_from_geojson`(geojson STRING, startx FLOAT64, starty FLOAT64, max_cost FLOAT64)
RETURNS STRING LANGUAGE js
OPTIONS (
library=["$BUCKET_FILE_PATH"]
)
AS """
const start = {type: "Feature", geometry: { coordinates: [startx, starty], type: "Point" }};
const pathFinder = new geojsonPathFinder.PathFinder(JSON.parse(geojson), { weightFn: geojsonPathFinder.WeightFunctions.travelTimeWeightFn });
return new Promise((resolve, reject) => {
const hull = pathFinder.getIsoDistanceConcaveHull(start, max_cost);
const geometry = JSON.stringify(hull.geometry);
resolve(geometry);
});
""";
-- get isochrone the convex hull
-- CREATE OR REPLACE FUNCTION `$PROJECT_ID.$DATASET.get_isochrone_convex_hull_from_geojson`(geojson STRING, startx FLOAT64, starty FLOAT64, max_cost FLOAT64)
-- RETURNS STRING LANGUAGE js
-- OPTIONS (
-- library=["$BUCKET_FILE_PATH"]
-- )
-- AS """
-- const start = {type: "Feature", geometry: { coordinates: [startx, starty], type: "Point" }};
-- const pathFinder = new geojsonPathFinder.PathFinder(JSON.parse(geojson));
-- const hull = pathFinder.getIsoDistanceConvexHull(start, max_cost);
-- try {
-- return JSON.stringify(hull.geometry);
-- } catch (e) {
-- return(null);
-- }
-- """;
-- helper to find the nearest point if the input point is not in the dataset
CREATE OR REPLACE FUNCTION `$PROJECT_ID.$DATASET.find_nearest_point`(mypoint GEOGRAPHY, mypoints array<GEOGRAPHY>) AS ((
WITH EXTRACTED_POINTS AS (
SELECT SAFE.ST_GEOGFROMTEXT(CONCAT('POINT(', point, ')')) mypoints
FROM unnest(mypoints) geo_object,
UNNEST(REGEXP_EXTRACT_ALL(ST_ASTEXT(geo_object), r'[^,\(\)]+')) point WITH OFFSET pos
WHERE pos BETWEEN 1 AND ST_NUMPOINTS(geo_object)
)
SELECT ARRAY_AGG(a.mypoints ORDER BY ST_Distance(a.mypoints, mypoint) LIMIT 1)[ORDINAL(1)] as neighbor_id
FROM EXTRACTED_POINTS a
));
-- wrapper for GEOGRAPHY to GEOJSON
CREATE OR REPLACE FUNCTION `$PROJECT_ID.$DATASET.find_points_around`(lines array<GEOGRAPHY>, start GEOGRAPHY, max_cost FLOAT64) AS ((
WITH SOME_NETWORK AS (
SELECT concat('{"type": "FeatureCollection", "features": [{"type": "Feature","geometry":', string_agg(ST_ASGEOJSON(line), '},{"type":"Feature","geometry":'), "}]}") geojson,
`$PROJECT_ID.$DATASET.find_nearest_point`(start, array_agg(line)) start_nearest,
FROM unnest(lines) line
),
OUTPUT AS (
SELECT `$PROJECT_ID.$DATASET.find_points_around_from_geojson`(geojson, ST_X(start_nearest), ST_Y(start_nearest), max_cost) myresult
FROM SOME_NETWORK
)
SELECT * FROM OUTPUT
));
-- wrapper for GEOGRAPHY to GEOJSON
CREATE OR REPLACE FUNCTION `$PROJECT_ID.$DATASET.get_isodistance_concave_hull`(lines array<GEOGRAPHY>, start GEOGRAPHY, max_cost FLOAT64) AS ((
WITH SOME_NETWORK AS (
SELECT concat('{"type": "FeatureCollection", "features": [{"type": "Feature","geometry":', string_agg(ST_ASGEOJSON(line), '},{"type":"Feature","geometry":'), "}]}") geojson,
`$PROJECT_ID.$DATASET.find_nearest_point`(start, array_agg(line)) start_nearest,
FROM unnest(lines) line
),
OUTPUT AS (
SELECT `$PROJECT_ID.$DATASET.get_isodistance_concave_hull_from_geojson`(geojson, ST_X(start_nearest), ST_Y(start_nearest), max_cost) myresult
FROM SOME_NETWORK
)
SELECT * FROM OUTPUT
));
-- wrapper for GEOGRAPHY to GEOJSON
CREATE OR REPLACE FUNCTION `$PROJECT_ID.$DATASET.get_isochrone_concave_hull`(lines array<GEOGRAPHY>, start GEOGRAPHY, max_cost FLOAT64) AS ((
WITH SOME_NETWORK AS (
SELECT CONCAT('{"type": "FeatureCollection", "features": [{"type": "Feature", "geometry":', string_agg(ST_ASGEOJSON(line), '}, {"type":"Feature","geometry":'), "}]}") geojson,
`$PROJECT_ID.$DATASET.find_nearest_point`(start, array_agg(line)) start_nearest,
FROM UNNEST(lines) line
),
OUTPUT AS (
SELECT `$PROJECT_ID.$DATASET.get_isochrone_concave_hull_from_geojson`(geojson, ST_X(start_nearest), ST_Y(start_nearest), max_cost) myresult
FROM SOME_NETWORK
)
SELECT * FROM OUTPUT
));
-- wrapper for GEOGRAPHY to GEOJSON
-- CREATE OR REPLACE FUNCTION `$PROJECT_ID.$DATASET.get_isodistance_convex_hull`(lines array<GEOGRAPHY>, start GEOGRAPHY, max_cost FLOAT64) AS ((
-- WITH SOME_NETWORK AS (
-- SELECT concat('{"type": "FeatureCollection", "features": [{"type": "Feature","geometry":', string_agg(ST_ASGEOJSON(line), '},{"type":"Feature","geometry":'), "}]}") geojson,
-- `$PROJECT_ID.$DATASET.find_nearest_point`(start, array_agg(line)) start_nearest,
-- FROM unnest(lines) line
-- ),
-- OUTPUT AS (
-- SELECT `$PROJECT_ID.$DATASET.get_isodistance_convex_hull_from_geojson`(geojson, ST_X(start_nearest), ST_Y(start_nearest), max_cost) myresult
-- FROM SOME_NETWORK
-- )
-- SELECT * FROM OUTPUT
-- ));
CREATE OR REPLACE FUNCTION `$PROJECT_ID.$DATASET.get_isodistance_convex_hull`(lines array<GEOGRAPHY>, start GEOGRAPHY, max_cost FLOAT64) AS ((
SELECT ST_CONVEXHULL(ST_GEOGFROMGEOJSON(`$PROJECT_ID.$DATASET.find_points_around`(lines, start, max_cost)))
));