forked from OSGeo/spatialreference.org
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase.js
More file actions
345 lines (322 loc) · 12.8 KB
/
base.js
File metadata and controls
345 lines (322 loc) · 12.8 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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
async function init_map(area_of_use, crs_type, id) {
if (!area_of_use) return;
let map = L.map('map');
let osm = L.tileLayer('https://tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors',
maxZoom: 18,
}).addTo(map);
let rect = makeRectangle(area_of_use, 'green').addTo(map);
map.fitBounds(rect.getBounds());
if (crs_type === 'PROJECTED_CRS' || crs_type === 'GEOGRAPHIC_2D_CRS') {
const decimals = crs_type === 'PROJECTED_CRS' ? 1 : 6;
const proj = new Proj();
await proj.init();
const transformer = proj.create_transformer_from_crs({source_crs: 'EPSG:4326', target_crs: id});
const axes = proj.crs_axes({crs: id});
document.querySelector('#coordinates table tr:nth-child(1) .name').innerText = axes[0].name + ': ';
document.querySelector('#coordinates table tr:nth-child(2) .name').innerText = axes[1].name + ': ';
const marker = L.marker(rect.getCenter(), {draggable: true}).addTo(map);
marker.on('move', (e) => {
let res = [[NaN, NaN]];
try {
res = transformer.transform({points: [[e.latlng.lat, e.latlng.lng]]});
} catch (_e) {}
document.querySelector('#coordinates table tr:nth-child(1) .number').innerText = res[0][0].toFixed(decimals);
document.querySelector('#coordinates table tr:nth-child(2) .number').innerText = res[0][1].toFixed(decimals);
});
marker.setLatLng(rect.getCenter());
marker.once('move', (e) => {
console.log(proj.proj_info());
document.getElementById('drag_marker').classList.add('hidden');
});
}
}
function makeRectangle (area_of_use, color) {
let bounds = null;
let off0 = 0, off2 = 0;
if (area_of_use[0] < area_of_use[2]) {
} else if (Math.abs(area_of_use[0]) < Math.abs(area_of_use[2])) {
off2 = 360
} else {
off0 = 360
}
bounds = [[area_of_use[1], area_of_use[0]-off0], [area_of_use[3], area_of_use[2]+off2]];
return L.rectangle(bounds, {color: color});
}
function type_abbr(type) {
if (type == 'PROJECTED_CRS') {
return 'P';
} else if (type == 'GEOGRAPHIC_2D_CRS') {
return 'G2D';
} else if (type == 'GEOGRAPHIC_3D_CRS') {
return 'G3D';
} else if (type == 'GEOGRAPHIC_CRS') {
return 'G';
} else if (type == 'GEODETIC_CRS') {
return 'GD';
} else if (type == 'GEOCENTRIC_CRS') {
return 'GC';
} else if (type == 'VERTICAL_CRS') {
return 'V';
} else if (type == 'COMPOUND_CRS') {
return 'C';
}
return '';
}
function generate_entries(data, home_dir, from, number, container) {
for (let i = from; i < from + number; i++) {
if (i >= data.length)
break;
const crs = data[i]
let li = document.createElement('li');
let a = document.createElement('a');
a.href = `${home_dir}/ref/${crs.auth_name.toLowerCase()}/${crs.code}/`;
a.innerText = `${crs.auth_name}:${crs.code}`;
li.appendChild(a);
name_broken = crs.name.replaceAll('_', '<wbr>_')
li.innerHTML += `: ${name_broken}`;
const t = type_abbr(crs.type);
if (t) {
li.innerHTML += ` <small class="type_abbr" title="${crs.type}">[${t}]</small>`;
}
if (crs.deprecated) {
li.innerHTML += ' <span class="deprecated_in_list">(deprecated)</span>';
}
container.appendChild(li);
}
}
function update_pages_links(page, search, max_pages) {
let s = search ? `&search=${search}` : '';
function doit (page_number, class_name, show) {
let prev = document.querySelectorAll(class_name);
Array.from(prev).forEach(e => {
if (!show) {
e.classList.add('is-disabled');
} else {
e.classList.remove('is-disabled');
e.href = `?page=${page_number}${s}`;
}
});
}
page = Number(page)
doit(page - 1, '.prev_page', page > 1)
doit(page + 1, '.next_page', page < max_pages)
Array.from(document.querySelectorAll('.next_page')).forEach(e => e.href = `?page=${page + 1}${s}`);
}
function paramsToDic(location) {
const url = new URL(location);
let dic = {};
for (let k of url.searchParams.keys()) {
dic[k] = url.searchParams.get(k);
}
return dic;
}
function filter_data(data, search, authority) {
if (!search || !search.trim()) {
search = '';
}
let s = search.toUpperCase().split(' ');
let no_s = s.filter(elem => elem[0] == '-').map(elem => elem.substring(1));
s = s.filter(elem => elem[0] != '-');
let auth_code = [null, s.length ? s[0] : null];
if (s.length == 1 && s[0].split(':').length == 2) {
auth_code = s[0].split(':');
}
let r = data.filter(d => {
let name = d.name.toUpperCase();
let outlier = s.find(elem => !name.includes(elem));
if (outlier == "WGS84") {
// many people searches WGS84, however the EPSG name has a space: "WGS 84"
const s2 = s.map(elem => elem == "WGS84" ? "WGS 84" : elem);
outlier = s2.find(elem => !name.includes(elem));
}
let valid = (outlier == undefined);
if (no_s.find(elem => name.includes(elem)) != undefined) {
valid = false
}
if (!isNaN(auth_code[1]) && d.code === auth_code[1] && (!auth_code[0] || d.auth_name === auth_code[0])) {
// filter by code or auth:code
valid = true;
}
if (authority && authority !== d.auth_name) {
valid = false;
}
return valid;
});
return r;
}
function init_ref(home_dir, authority) {
fetch(home_dir + '/crslist.json', {
method: "GET",
})
.then(response => response.json())
.then(data => {
let entries_per_page = 50;
let params = paramsToDic(window.location);
let page = params.page || 1;
data = filter_data(data, params.search, authority);
document.querySelector('#found').innerText = data.length;
if(params.search && params.search.trim()) {
document.querySelector('#searched_text_span').classList.remove("hidden")
document.querySelector('#searched_text').textContent = params.search;
const forms = document.querySelectorAll('form')
Array.from(forms).forEach(form => form.elements['search'].value = params.search);
only_searching = document.querySelector('.only_searching')
if (only_searching) {
only_searching.href += params.search.trim();
}
}
let container = document.querySelector('#list1 ul');
generate_entries(data, home_dir, (page - 1) * entries_per_page, entries_per_page/2, container);
container = document.querySelector('#list2 ul');
generate_entries(data, home_dir, (page - 0.5) * entries_per_page, entries_per_page/2, container);
update_pages_links(page, params.search, Math.ceil(data.length / entries_per_page))
});
}
function not_found(home_dir) {
// This function is called in case of a 404, trying to redirect old links.
// something like https://spatialreference.org/ref/epsg/anguilla-1957-british-west-indies-grid/
const loc = window.location;
const match = loc.pathname.match(/.*?\/ref\/(.*?)\/(.*)/);
if (!match || match.length != 3) {
return;
}
const auth_in_url = match[1];
const name_in_url = match[2].endsWith('/') ? match[2].slice(0, -1) : match[2];
// Not supported anymore. Give the user at least the JSON file with the data.
if (auth_in_url == "sr-org" || auth_in_url == "iau2000") {
const url = `https://github.com/OSGeo/spatialreference.org/blob/master/scripts/${auth_in_url}.json`;
const text = `Sorry, ${auth_in_url} is not supported anymore. You can get old data from file<br>` +
`<a href="${url}" target="_blank">${url}</a>`;
document.querySelector('#not_found_cont').innerHTML = text;
return;
}
// It could be an old link. Let's analyze it
fetch(home_dir + '/crslist.json', {
method: "GET",
})
.then(response => response.json())
.then(data => {
let r = data.filter(d => {
if (d.auth_name.toLowerCase() != auth_in_url)
return false;
const scaped_name = d.name.replace(/[^0-9a-zA-Z]+/g, '-').toLowerCase();
return scaped_name == name_in_url;
});
if (r.length == 1) {
const d = r[0];
const url = `${loc.origin}/ref/${auth_in_url}/${d.code}`;
const text = `However, apparently you want to visit<br><br>` +
`${d.auth_name}:${d.code}<br>${d.name}<br><br>` +
`Redirecting to <a href="${url}">${url}</a>`;
document.querySelector('#not_found_cont').innerHTML = text;
setTimeout(() => location.assign(url), 5000);
}
});
}
function download_prj(name, file) {
if (name && name !=='') {
var link = document.createElement('a');
link.download = name;
link.href = file;
link.click();
}
}
function lexer_from_python() {
const Error = 'err';
const Text = '';
const Punctuation = 'p';
const Generic = {Heading: 'gh'};
const Keyword = {Declaration: 'kd', Constant: 'kc'}; // Keyword='k'
const Name = {Class: 'nc', Builtin: 'nb', Other: 'no'};
const Literal = {String: 's'};
const Number = {Float: 'mf', Hex: 'mh', Integer: 'mi'};
const String = {Double: 's2', Single: 's1'};
function r(rx, type) {
if (type == Keyword)
type = 'k';
let res = {cls:type};
res.regex = rx instanceof RegExp ? rx : new RegExp(rx.replaceAll('\b', '\\b'), 'y');
return res;
}
// just removed the initial "r" from every line, and add a few extra \ in Text and Punctuation
const lexer = [
r('^This CRS cannot be written.*', Error),
r('\\s+', Text),
r('[{}\\[\\]();,-.]+', Punctuation),
r('^(PROJCS|GEOGCS|GEOCCS|VERT_CS|COMPD_CS)\b', Generic.Heading),
r('^(PROJCRS|GEOGCRS|GEODCRS|VERTCRS|COMPOUNDCRS)\b', Generic.Heading),
r('(PROJCS|GEOGCS|GEOCCS|VERT_CS)\b', Keyword.Declaration),
r('(PROJCRS|GEOGCRS|GEODCRS|VERTCRS)\b', Keyword.Declaration),
r('(PARAMETER|PROJECTION|SPHEROID|DATUM|GEOGCS|AXIS|VERT_DATUM)\b', Keyword),
r('(ELLIPSOID)\b', Keyword),
r('(METHOD)\b', Keyword),
r('(PRIMEM|UNIT|TOWGS84)\b', Keyword.Constant),
r('([A-Z]+UNIT)\b', Name.Class),
r('(east|west|north|south|up|down|geocentric[XYZ])\b', Literal.String),
r('(EAST|WEST|NORTH|SOUTH|UP|DOWN)\b', Literal.String),
r('(ORDER|SCOPE|AREA|BBOX)\b', Keyword.Constant),
r('(BASEGEOGCRS|CONVERSION|CS|USAGE|VDATUM)\b', Keyword.Declaration),
r('([Cc]artesian|[Ee]llipsoidal|[Vv]ertical)\b', Literal.String),
r('(AUTHORITY)\b', Name.Builtin),
r('(ID)\b', Name.Builtin),
r('[$a-zA-Z_][a-zA-Z0-9_]*', Name.Other),
r('[0-9][0-9]*\.[0-9]+([eE][0-9]+)?[fd]?', Number.Float),
r('0x[0-9a-fA-F]+', Number.Hex),
r('[0-9]+', Number.Integer),
r('"(\\\\|\\"|[^"])*"', String.Double),
r("'(\\\\|\\'|[^'])*'", String.Single),
];
return lexer
}
function format(txt) {
let lexer = lexer_from_python();
const lines = txt.split('\n');
let res = ''
lines.forEach(line => {
let pos = 0;
while (line.length > 0 && pos < line.length) {
let found = false;
for(let l = 0; l < lexer.length; l++) {
let lx = lexer[l];
lx.regex.lastIndex = pos;
const match = lx.regex.exec(line);
if (match && match.index == pos) {
found = true;
const matched = match[0];
if (lexer[l].cls == '') {
res += matched;
} else {
res += `<span class="${lx.cls}">${matched}</span>`;
}
pos += matched.length
break;
}
}
if (!found) {
res += line[pos];
pos += 1
}
}
res += '\n';
});
let syntax = document.createElement('div');
syntax.classList.add('syntax');
let pre = document.createElement('pre');
pre.innerHTML = res;
syntax.appendChild(pre);
return syntax;
}
function fill_prettywkt(filename) {
let wkt = document.getElementById('wkt');
if (!wkt.innerText) {
fetch(filename, {
method: "GET",
})
.then(response => response.text())
.then(data => {
let syntax = format(data);
wkt.appendChild(syntax);
})
}
}