-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathspatial_query_lab_fixed.html
More file actions
327 lines (302 loc) · 11.3 KB
/
Copy pathspatial_query_lab_fixed.html
File metadata and controls
327 lines (302 loc) · 11.3 KB
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>Spatial Query Lab — Fixed</title>
<!-- Leaflet CSS -->
<link
rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/leaflet@1.9.4/dist/leaflet.css"
integrity="sha256-p4NxAoJBhIIN+hmNHrzRCf9tD/miZyoHS5obTRR9BMY="
crossorigin=""
/>
<style>
:root {
--bg: #f7f7fb;
--card: #ffffff;
--muted: #6b7280;
--text: #111827;
--accent: #2563eb;
--accent-2: #1f2937;
--border: #e5e7eb;
}
* { box-sizing: border-box; }
body {
margin: 0;
font-family: system-ui, -apple-system, Segoe UI, Roboto, Helvetica, Arial, "Apple Color Emoji","Segoe UI Emoji";
background: var(--bg);
color: var(--text);
}
.container {
display: grid;
grid-template-columns: 420px 1fr;
gap: 16px;
padding: 16px;
height: 100vh;
}
.panel {
background: var(--card);
border: 1px solid var(--border);
border-radius: 14px;
padding: 14px;
height: 100%;
overflow: auto;
}
.section {
border: 1px dashed var(--border);
border-radius: 12px;
padding: 12px;
margin-bottom: 12px;
background: #fff;
}
h1 {
font-size: 18px;
margin: 0 0 8px 0;
display: flex; align-items: center; gap: 8px;
}
h2 { font-size: 15px; margin: 0 0 8px 0; }
label { display: block; font-size: 12px; color: var(--muted); margin-bottom: 6px; }
input, textarea, select {
width: 100%;
padding: 8px 10px;
border: 1px solid var(--border);
border-radius: 10px;
outline: none;
font-size: 13px;
background: #fff;
}
textarea { min-height: 140px; font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace; }
.row { display: flex; gap: 8px; }
.btn {
padding: 8px 12px;
border-radius: 10px;
border: 1px solid var(--border);
background: var(--accent);
color: #fff;
font-weight: 600;
cursor: pointer;
}
.btn.secondary { background: #fff; color: var(--accent-2); }
.small { font-size: 12px; color: var(--muted); }
#map {
height: 100%;
width: 100%;
border-radius: 14px;
border: 1px solid var(--border);
}
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th, td { padding: 8px; border-bottom: 1px solid var(--border); vertical-align: top; }
th { text-align: left; color: var(--muted); font-weight: 600; }
.status { font-size: 12px; }
.ok { color: #047857; }
.err { color: #b91c1c; }
.chip { display:inline-block; padding:2px 6px; border-radius:8px; background:#eef2ff; color:#3730a3; font-size:11px; }
.kbd { font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace; background:#f3f4f6; padding:0 6px; border-radius:6px; border:1px solid #e5e7eb; }
</style>
</head>
<body>
<div class="container">
<!-- Left Panel -->
<div class="panel">
<h1>Spatial Query Lab <span class="chip">v3-fixed</span></h1>
<!-- 1) Supabase connector -->
<div class="section">
<h2>1) Supabase (SQL Runner)</h2>
<label>Supabase Project URL</label>
<input id="project_url" placeholder="https://xxxxx.supabase.co" />
<label style="margin-top:8px">Supabase anon public key</label>
<input id="anon_key" placeholder="eyJhbGciOiJI..." />
<div class="row" style="margin-top:10px">
<button class="btn" onclick="connectSupabase()">Connect Supabase</button>
<div id="connect_status" class="status" style="padding-top:10px;margin-left:8px;">Not connected</div>
</div>
<div class="small" style="margin-top:8px">Backend expects Postgres function <span class="kbd">run_open_sql(sql_text)</span> to execute arbitrary SQL.</div>
</div>
<!-- 2) SQL Editor -->
<div class="section">
<h2>2) SQL Editor</h2>
<div class="small" style="margin-bottom:8px">
Click the map to set variables <span class="kbd">{{lon}}</span>, <span class="kbd">{{lat}}</span>, <span class="kbd">{{dist_m}}</span> (default 200).
</div>
<textarea id="sql_text">
-- Try a simple spatial query (edit me)
select id, name, type,
st_asgeojson(geom)::jsonb as geom
from campus_safety
limit 10;
</textarea>
<div class="row" style="margin-top:8px">
<button class="btn" onclick="runSQL()">Run SQL</button>
<button class="btn secondary" onclick="clearResults()">Clear Results</button>
</div>
<div id="last_click" class="small" style="margin-top:6px">Ready. No map click yet.</div>
<div id="sql_error" class="small err" style="margin-top:6px; display:none;"></div>
</div>
<!-- 3) Results -->
<div class="section">
<h2>3) Results</h2>
<table id="results_table">
<thead id="results_head"></thead>
<tbody id="results_body"></tbody>
</table>
<div id="rows_count" class="small" style="margin-top:6px"></div>
</div>
</div>
<!-- Right Panel: Map -->
<div class="panel" style="padding:8px;">
<div id="map"></div>
</div>
</div>
<!-- Leaflet JS -->
<script
src="https://cdn.jsdelivr.net/npm/leaflet@1.9.4/dist/leaflet.js"
integrity="sha256-20nQCchB9co0qIjJZRGuk2/Z9VM+kNiyxNV1lvTlZBo="
crossorigin=""
></script>
<!-- Supabase JS v2 -->
<script src="https://cdn.jsdelivr.net/npm/@supabase/supabase-js@2"></script>
<script>
// ---- Map init (FIX: use raster tiles instead of GL style JSON) ----
const map = L.map('map', { zoomControl: true }).setView([30.615, -96.34], 16);
L.tileLayer('https://{s}.basemaps.cartocdn.com/light_all/{z}/{x}/{y}{r}.png', {
attribution: '© <a href="https://carto.com/">CARTO</a>',
maxZoom: 20
}).addTo(map);
let clickMarker = null;
let geoLayer = L.geoJSON(null).addTo(map);
let currentClick = { lon: null, lat: null, dist_m: 200 };
map.on('click', (e) => {
const { lat, lng } = e.latlng;
currentClick.lat = +lat.toFixed(6);
currentClick.lon = +lng.toFixed(6);
if (clickMarker) map.removeLayer(clickMarker);
clickMarker = L.marker([lat, lng]).addTo(map);
document.getElementById('last_click').textContent =
`Click set → lat=${currentClick.lat}, lon=${currentClick.lon}, dist=${currentClick.dist_m}m`;
});
// ---- Supabase ----
const { createClient } = supabase;
let sb = null;
async function connectSupabase() {
setError('');
const url = document.getElementById('project_url').value.trim();
const key = document.getElementById('anon_key').value.trim();
if (!url || !key) {
setStatus('Please enter project URL and anon key.', false);
return;
}
try {
sb = createClient(url, key);
// Simple smoke test
const { error } = await sb.rpc('run_open_sql', { sql_text: 'select 1' });
if (error) throw error;
setStatus('Connected ✅', true);
} catch (err) {
setStatus('Connection failed', false);
setError(err.message || String(err));
}
}
function setStatus(msg, ok) {
const el = document.getElementById('connect_status');
el.textContent = msg;
el.className = 'status ' + (ok ? 'ok' : 'err');
}
function setError(msg) {
const el = document.getElementById('sql_error');
if (!msg) { el.style.display = 'none'; el.textContent=''; return; }
el.style.display = 'block';
el.textContent = msg;
}
function applyTemplateVars(sqlRaw) {
let sql = sqlRaw;
const hasLatLon = currentClick.lat !== null && currentClick.lon !== null;
sql = sql.replaceAll('{{dist_m}}', String(currentClick.dist_m));
if (hasLatLon) {
sql = sql.replaceAll('{{lat}}', String(currentClick.lat));
sql = sql.replaceAll('{{lon}}', String(currentClick.lon));
}
return sql;
}
function clearResults() {
document.getElementById('results_head').innerHTML = '';
document.getElementById('results_body').innerHTML = '';
document.getElementById('rows_count').textContent = '';
geoLayer.clearLayers();
setError('');
}
function renderTable(rows) {
if (!rows || !rows.length) { clearResults(); return; }
const keys = Object.keys(rows[0]);
const thead = '<tr>' + keys.map(k => `<th>${k}</th>`).join('') + '</tr>';
const tbody = rows.map(r => {
return '<tr>' + keys.map(k => `<td>${escapeHtml(formatCell(r[k]))}</td>`).join('') + '</tr>';
}).join('');
document.getElementById('results_head').innerHTML = thead;
document.getElementById('results_body').innerHTML = tbody;
document.getElementById('rows_count').textContent = rows.length + ' row(s)';
}
function formatCell(v) {
if (v === null || v === undefined) return '';
if (typeof v === 'object') {
try { return JSON.stringify(v); } catch { return String(v); }
}
return String(v);
}
function escapeHtml(s) {
return s.replaceAll('&', '&').replaceAll('<', '<').replaceAll('>', '>');
}
function collectGeoJSON(rows) {
const features = [];
for (const r of rows) {
// Accept common field names: geom / geojson / geometry
const cand = r.geom ?? r.geojson ?? r.geometry ?? null;
if (!cand) continue;
try {
const g = (typeof cand === 'string') ? JSON.parse(cand) : cand;
// If it's already a Feature/FeatureCollection/Geometry
if (g && (g.type === 'Feature' || g.type === 'FeatureCollection' || g.type === 'Point' || g.type === 'LineString' || g.type === 'Polygon' || g.type?.endsWith('Collection'))) {
if (g.type === 'FeatureCollection') {
for (const f of g.features) features.push(f);
} else if (g.type === 'Feature') {
features.push(g);
} else {
features.push({ type: 'Feature', properties: {}, geometry: g });
}
}
} catch (e) {
// ignore parse errors for non-geo rows
}
}
if (!features.length) return null;
return { type: 'FeatureCollection', features };
}
async function runSQL() {
setError('');
if (!sb) { setStatus('Not connected', false); return; }
let sqlRaw = document.getElementById('sql_text').value;
const sql = applyTemplateVars(sqlRaw);
try {
const { data, error } = await sb.rpc('run_open_sql', { sql_text: sql });
if (error) throw error;
renderTable(data);
// Map rendering if there is geo
geoLayer.clearLayers();
const fc = collectGeoJSON(data);
if (fc) {
const layer = L.geoJSON(fc, {
onEachFeature: (feat, lyr) => {
const props = Object.assign({}, feat.properties);
lyr.bindPopup('<pre style="margin:0; font-size:12px;">' + escapeHtml(JSON.stringify(props, null, 2)) + '</pre>');
}
});
geoLayer.addLayer(layer);
try { map.fitBounds(layer.getBounds(), { padding: [16,16] }); } catch {}
}
} catch (err) {
setError(err.message || String(err));
}
}
</script>
</body>
</html>