mirror of
https://github.com/umap-project/umap.git
synced 2025-04-28 19:42:36 +02:00
chore: use GeoRSStoGeoJSON as ES module
This commit is contained in:
parent
a1b35ddff0
commit
2d905bd048
4 changed files with 119 additions and 89 deletions
|
@ -43,7 +43,7 @@
|
||||||
"colorbrewer": "^1.5.6",
|
"colorbrewer": "^1.5.6",
|
||||||
"csv2geojson": "5.1.2",
|
"csv2geojson": "5.1.2",
|
||||||
"dompurify": "^3.0.11",
|
"dompurify": "^3.0.11",
|
||||||
"georsstogeojson": "^0.1.0",
|
"georsstogeojson": "^0.2.0",
|
||||||
"jsdom": "^24.0.0",
|
"jsdom": "^24.0.0",
|
||||||
"leaflet": "1.9.4",
|
"leaflet": "1.9.4",
|
||||||
"leaflet-contextmenu": "^1.4.0",
|
"leaflet-contextmenu": "^1.4.0",
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
/* Uses globals for: csv2geojson, osmtogeojson, GeoRSSToGeoJSON (not available as ESM) */
|
/* Uses globals for: csv2geojson, osmtogeojson (not available as ESM) */
|
||||||
import { translate } from './i18n.js'
|
import { translate } from './i18n.js'
|
||||||
|
|
||||||
export const EXPORT_FORMATS = {
|
export const EXPORT_FORMATS = {
|
||||||
|
@ -115,7 +115,8 @@ export class Formatter {
|
||||||
}
|
}
|
||||||
|
|
||||||
async fromGeoRSS(str) {
|
async fromGeoRSS(str) {
|
||||||
return GeoRSSToGeoJSON(this.toDom(str))
|
const GeoRSSToGeoJSON = await import('../../vendors/georsstogeojson/GeoRSSToGeoJSON.js')
|
||||||
|
return GeoRSSToGeoJSON.parse(this.toDom(str))
|
||||||
}
|
}
|
||||||
|
|
||||||
toDom(x) {
|
toDom(x) {
|
||||||
|
|
|
@ -1,89 +1,120 @@
|
||||||
var GeoRSSToGeoJSON = function (dom, options) {
|
export function parse(dom, options) {
|
||||||
|
const g = {
|
||||||
|
type: 'FeatureCollection',
|
||||||
|
features: [],
|
||||||
|
}
|
||||||
|
|
||||||
function get(x, y) { return x.getElementsByTagName(y); }
|
const items = get(dom, 'item')
|
||||||
function get1(x, y) { var n = get(x, y); return n.length ? n[0] : null; }
|
for (const item of Array.from(items)) {
|
||||||
function norm(el) { if (el.normalize) { el.normalize(); } return el; }
|
const feature = processOne(item)
|
||||||
function nodeVal(x) { if (x) {norm(x);} return x && x.firstChild && x.firstChild.nodeValue; }
|
if (feature) {
|
||||||
function attr(x, y) { return x.getAttribute(y); }
|
g.features.push(feature)
|
||||||
|
|
||||||
var g = {
|
|
||||||
type: 'FeatureCollection',
|
|
||||||
features: []
|
|
||||||
};
|
|
||||||
|
|
||||||
function geom (node) {
|
|
||||||
|
|
||||||
function p(c) {return parseFloat(c);}
|
|
||||||
function r(c) {return c.reverse().map(p);} // we have latlon we want lonlat
|
|
||||||
function e(f) {var _=[]; for (var i=0; i<f.length; i+=2) {_.push(r(f.slice(i, i+2)));} return _;}
|
|
||||||
|
|
||||||
var type, coordinates;
|
|
||||||
|
|
||||||
NODE = node;
|
|
||||||
if (get1(node, 'geo:long')) {
|
|
||||||
type = 'Point';
|
|
||||||
coordinates = [p(nodeVal(get1(node, 'geo:long'))), p(nodeVal(get1(node, 'geo:lat')))];
|
|
||||||
} else if (get1(node, 'long')) {
|
|
||||||
type = 'Point';
|
|
||||||
coordinates = [p(nodeVal(get1(node, 'long'))), p(nodeVal(get1(node, 'lat')))];
|
|
||||||
} else if (get1(node, 'georss:point')) {
|
|
||||||
type = 'Point';
|
|
||||||
coordinates = r(nodeVal(get1(node, 'georss:point')).split(' '));
|
|
||||||
} else if (get1(node, 'point')) {
|
|
||||||
type = 'Point';
|
|
||||||
coordinates = r(nodeVal(get1(node, 'point')).split(' '));
|
|
||||||
} else {
|
|
||||||
var line = get1(node, 'georss:line'),
|
|
||||||
poly = get1(node, 'georss:polygon');
|
|
||||||
if (line || poly) {
|
|
||||||
type = line ? 'LineString' : 'Polygon';
|
|
||||||
var tag = line ? 'georss:line' : 'georss:polygon';
|
|
||||||
coordinates = nodeVal(get1(node, tag)).split(' ');
|
|
||||||
if (coordinates.length % 2 !== 0) return;
|
|
||||||
coordinates = e(coordinates);
|
|
||||||
if (poly) {
|
|
||||||
coordinates = [coordinates];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (type && coordinates) {
|
|
||||||
return {
|
|
||||||
type: type,
|
|
||||||
coordinates: coordinates
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
return g
|
||||||
|
}
|
||||||
|
|
||||||
function processOne (node) {
|
function get(x, y) {
|
||||||
var geometry = geom(node);
|
return x.getElementsByTagName(y)
|
||||||
// TODO collect and fire errors
|
}
|
||||||
if (!geometry) return;
|
function get1(x, y) {
|
||||||
var f = {
|
const n = get(x, y)
|
||||||
type: "Feature",
|
return n.length ? n[0] : null
|
||||||
geometry: geometry,
|
}
|
||||||
properties: {
|
function norm(el) {
|
||||||
title: nodeVal(get1(node, 'title')),
|
if (el.normalize) {
|
||||||
description: nodeVal(get1(node, 'description')),
|
el.normalize()
|
||||||
link: nodeVal(get1(node, 'link')),
|
}
|
||||||
}
|
return el
|
||||||
};
|
}
|
||||||
var media = get1(node, 'media:content'), mime;
|
function nodeVal(x) {
|
||||||
if (!media) {
|
if (x) {
|
||||||
media = get1(node, 'enclosure'), mime;
|
norm(x)
|
||||||
}
|
}
|
||||||
if (media) {
|
return x?.firstChild?.nodeValue
|
||||||
mime = attr(media, 'type');
|
}
|
||||||
if (mime.indexOf('image') !== -1) {
|
function attr(x, y) {
|
||||||
f.properties.img = attr(media, "url"); // How not to invent a key?
|
return x.getAttribute(y)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
g.features.push(f);
|
|
||||||
}
|
|
||||||
|
|
||||||
var items = get(dom, 'item');
|
function geom(node) {
|
||||||
for (var i = 0; i < items.length; i++) {
|
function p(c) {
|
||||||
processOne(items[i]);
|
return parseFloat(c)
|
||||||
|
}
|
||||||
|
function r(c) {
|
||||||
|
return c.reverse().map(p)
|
||||||
|
} // we have latlon we want lonlat
|
||||||
|
function e(f) {
|
||||||
|
const _ = []
|
||||||
|
for (let i = 0; i < f.length; i += 2) {
|
||||||
|
_.push(r(f.slice(i, i + 2)))
|
||||||
}
|
}
|
||||||
return g;
|
return _
|
||||||
};
|
}
|
||||||
if (typeof module !== 'undefined') module.exports = {GeoRSSToGeoJSON: GeoRSSToGeoJSON};
|
|
||||||
|
let type
|
||||||
|
let coordinates
|
||||||
|
|
||||||
|
if (get1(node, 'geo:long')) {
|
||||||
|
type = 'Point'
|
||||||
|
coordinates = [
|
||||||
|
p(nodeVal(get1(node, 'geo:long'))),
|
||||||
|
p(nodeVal(get1(node, 'geo:lat'))),
|
||||||
|
]
|
||||||
|
} else if (get1(node, 'long')) {
|
||||||
|
type = 'Point'
|
||||||
|
coordinates = [p(nodeVal(get1(node, 'long'))), p(nodeVal(get1(node, 'lat')))]
|
||||||
|
} else if (get1(node, 'georss:point')) {
|
||||||
|
type = 'Point'
|
||||||
|
coordinates = r(nodeVal(get1(node, 'georss:point')).split(' '))
|
||||||
|
} else if (get1(node, 'point')) {
|
||||||
|
type = 'Point'
|
||||||
|
coordinates = r(nodeVal(get1(node, 'point')).split(' '))
|
||||||
|
} else {
|
||||||
|
const line = get1(node, 'georss:line')
|
||||||
|
const poly = get1(node, 'georss:polygon')
|
||||||
|
if (line || poly) {
|
||||||
|
type = line ? 'LineString' : 'Polygon'
|
||||||
|
const tag = line ? 'georss:line' : 'georss:polygon'
|
||||||
|
coordinates = nodeVal(get1(node, tag)).split(' ')
|
||||||
|
if (coordinates.length % 2 !== 0) return
|
||||||
|
coordinates = e(coordinates)
|
||||||
|
if (poly) {
|
||||||
|
coordinates = [coordinates]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (type && coordinates) {
|
||||||
|
return {
|
||||||
|
type: type,
|
||||||
|
coordinates: coordinates,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function processOne(node) {
|
||||||
|
const geometry = geom(node)
|
||||||
|
// TODO collect and fire errors
|
||||||
|
if (!geometry) return
|
||||||
|
const f = {
|
||||||
|
type: 'Feature',
|
||||||
|
geometry: geometry,
|
||||||
|
properties: {
|
||||||
|
title: nodeVal(get1(node, 'title')),
|
||||||
|
description: nodeVal(get1(node, 'description')),
|
||||||
|
link: nodeVal(get1(node, 'link')),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
let media = get1(node, 'media:content')
|
||||||
|
let mime
|
||||||
|
if (!media) {
|
||||||
|
media = get1(node, 'enclosure')
|
||||||
|
}
|
||||||
|
if (media) {
|
||||||
|
mime = attr(media, 'type')
|
||||||
|
if (mime.indexOf('image') !== -1) {
|
||||||
|
f.properties.img = attr(media, 'url') // How not to invent a key?
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return f
|
||||||
|
}
|
||||||
|
|
|
@ -28,8 +28,6 @@
|
||||||
<script src="{% static 'umap/vendors/contextmenu/leaflet.contextmenu.min.js' %}"
|
<script src="{% static 'umap/vendors/contextmenu/leaflet.contextmenu.min.js' %}"
|
||||||
defer></script>
|
defer></script>
|
||||||
<script src="{% static 'umap/vendors/photon/leaflet.photon.js' %}" defer></script>
|
<script src="{% static 'umap/vendors/photon/leaflet.photon.js' %}" defer></script>
|
||||||
<script src="{% static 'umap/vendors/georsstogeojson/GeoRSSToGeoJSON.js' %}"
|
|
||||||
defer></script>
|
|
||||||
<script src="{% static 'umap/vendors/fullscreen/Leaflet.fullscreen.min.js' %}"
|
<script src="{% static 'umap/vendors/fullscreen/Leaflet.fullscreen.min.js' %}"
|
||||||
defer></script>
|
defer></script>
|
||||||
<script src="{% static 'umap/vendors/toolbar/leaflet.toolbar.js' %}" defer></script>
|
<script src="{% static 'umap/vendors/toolbar/leaflet.toolbar.js' %}" defer></script>
|
||||||
|
|
Loading…
Reference in a new issue