commit 4d90013d9eaf802ff25bc1d29add6ea01f2f025b
parent 2abc4c481c3195d2925178d761a5941b14585ab2
Author: myadzel <myadzel@gmail.com>
Date: Wed, 28 Dec 2022 19:48:38 +0300
add geojson parser (issue #1096)
Diffstat:
3 files changed, 88 insertions(+), 3 deletions(-)
diff --git a/src/lib/leaflet.control.track-list/lib/parsers/geojson.js b/src/lib/leaflet.control.track-list/lib/parsers/geojson.js
@@ -0,0 +1,83 @@
+import {decode} from 'utf8';
+
+function parseGeojson(txt, fileName) {
+ let error;
+
+ const tracks = [];
+ const points = [];
+
+ function parsePoints(features) {
+ features.forEach((feature) => {
+ let pointName = '';
+
+ if ('name' in feature.properties) {
+ pointName = decode(feature.properties.name.toString());
+ }
+
+ const [lng, lat] = feature.geometry.coordinates;
+
+ if (typeof lng !== 'number' || typeof lat !== 'number') {
+ error = 'CORRUPT';
+
+ return;
+ }
+
+ points.push({
+ lng,
+ lat,
+ name: pointName,
+ });
+ });
+ }
+
+ function parseTracks(features) {
+ features.forEach((feature) => {
+ const segment = [];
+
+ feature.geometry.coordinates.forEach((coordinate) => {
+ const [lng, lat] = coordinate;
+
+ if (typeof lng !== 'number' || typeof lat !== 'number') {
+ error = 'CORRUPT';
+
+ return;
+ }
+
+ segment.push({
+ lng,
+ lat,
+ });
+ });
+
+ if (segment.length < 2) {
+ error = 'CORRUPT';
+
+ return;
+ }
+
+ tracks.push(segment);
+ });
+ }
+
+ try {
+ const json = JSON.parse(txt);
+
+ const features = json.features;
+
+ parsePoints(features.filter((feature) => feature.geometry.type === 'Point'));
+ parseTracks(features.filter((feature) => feature.geometry.type === 'LineString'));
+ } catch (err) {
+ error = 'CORRUPT';
+ }
+
+ return [
+ {
+ name: fileName,
+ tracks,
+ points,
+ error,
+ },
+ ];
+}
+
+export {parseGeojson};
diff --git a/src/lib/leaflet.control.track-list/lib/parsers/index.js b/src/lib/leaflet.control.track-list/lib/parsers/index.js
@@ -2,6 +2,7 @@ import parseGpx from './gpx';
import parseZip from './zip';
import {parseKmz, parseKml} from './kml';
import {parseOziPlt, parseOziRte, parseOziWpt} from './ozi';
+import {parseGeojson} from './geojson';
const parsers = [
parseKmz,
@@ -11,6 +12,7 @@ const parsers = [
parseOziPlt,
parseOziWpt,
parseKml,
+ parseGeojson,
];
export default parsers;
diff --git a/src/lib/leaflet.control.track-list/track-list.js b/src/lib/leaflet.control.track-list/track-list.js
@@ -92,7 +92,7 @@ L.Control.TrackList = L.Control.extend({
lineCursorStyle: {interactive: false, weight: 1.5, opacity: 1, dashArray: '7,7'},
lineCursorValidStyle: {color: 'green'},
lineCursorInvalidStyle: {color: 'red'},
- splitExtensions: ['gpx', 'kml', 'kmz', 'wpt', 'rte', 'plt', 'fit', 'tmp', 'jpg', 'crdownload'],
+ splitExtensions: ['gpx', 'kml', 'geojson', 'kmz', 'wpt', 'rte', 'plt', 'fit', 'tmp', 'jpg', 'crdownload'],
splitExtensionsFirstStage: ['xml', 'txt', 'html', 'php', 'tmp', 'gz'],
trackHighlightStyle: {
color: 'yellow',
@@ -147,8 +147,8 @@ L.Control.TrackList = L.Control.extend({
<div class="leaflet-control-content">
<div class="header">
<div class="hint"
- title="gpx kml Ozi zip YandexMaps Strava GPSLib Etomesto GarminConnect SportsTracker OSM Tracedetrail OpenStreetMap.ru Wikiloc">
- gpx kml Ozi zip YandexMaps Strava
+ title="gpx kml Ozi geojson zip YandexMaps Strava GPSLib Etomesto GarminConnect SportsTracker OSM Tracedetrail OpenStreetMap.ru Wikiloc">
+ gpx kml Ozi geojson zip YandexMaps Strava
<span class="formats-hint-more">…</span>
</div>
<div class="button-minimize" data-bind="click: setMinimized"></div>