caltopo-to-geojson
Advanced tools
Comparing version 0.0.4 to 0.1.0
@@ -6,16 +6,8 @@ 'use strict'; | ||
}); | ||
exports.metadata = metadata; | ||
exports.waypoints = waypoints; | ||
exports.routes = routes; | ||
var _parseDescription = require('./parse-description'); | ||
var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }(); | ||
var _parseDescription2 = _interopRequireDefault(_parseDescription); | ||
var _parseMetadata = require('./parse-metadata'); | ||
var _parseMetadata2 = _interopRequireDefault(_parseMetadata); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
exports.extractFolders = extractFolders; | ||
exports.extractMarkers = extractMarkers; | ||
exports.extractRoutes = extractRoutes; | ||
var FEATURE = 'Feature'; | ||
@@ -25,14 +17,10 @@ var POINT = 'Point'; | ||
function metadata(xml) { | ||
return xml.gpx.metadata.find(function (m) { | ||
return m.desc; | ||
}).desc[0]; | ||
function extractFolders(json) { | ||
return json.state.Folder; | ||
} | ||
function waypoints(xml, meta) { | ||
meta = meta || (0, _parseMetadata2.default)(metadata(xml)); | ||
function extractMarkers(json) { | ||
var folders = arguments.length <= 1 || arguments[1] === undefined ? extractFolders(json) : arguments[1]; | ||
return xml.gpx.wpt.map(function (w) { | ||
var description = (0, _parseDescription2.default)(w.desc[0]); | ||
return json.state.Marker.map(function (marker) { | ||
return { | ||
@@ -42,9 +30,9 @@ type: FEATURE, | ||
type: POINT, | ||
coordinates: [w.$.lon, w.$.lat].map(Number) | ||
coordinates: [marker.position.lng, marker.position.lat] | ||
}, | ||
properties: { | ||
name: w.name[0], | ||
folder: meta.folders[description.folderId], | ||
folderId: description.folderId, | ||
comments: description.comments | ||
name: marker.label, | ||
folder: folders[marker.folderId].label, | ||
folderId: marker.folderId, | ||
comments: marker.comments | ||
} | ||
@@ -55,8 +43,17 @@ }; | ||
function routes(xml, meta) { | ||
meta = meta || (0, _parseMetadata2.default)(metadata(xml)); | ||
function extractRoutes(json) { | ||
var folders = arguments.length <= 1 || arguments[1] === undefined ? extractFolders(json) : arguments[1]; | ||
return xml.gpx.rte.map(function (w) { | ||
var description = (0, _parseDescription2.default)(w.desc[0]); | ||
function coordinates(_ref) { | ||
var _ref2 = _slicedToArray(_ref, 2); | ||
var lat = _ref2[0]; | ||
var lon = _ref2[1]; | ||
return [lon, lat]; | ||
} | ||
return json.state.Shape.filter(function (shape) { | ||
return shape.way.type === 'ROUTE'; | ||
}).map(function (shape) { | ||
return { | ||
@@ -66,16 +63,12 @@ type: FEATURE, | ||
type: LINE_STRING, | ||
coordinates: w.rtept.map(points) | ||
coordinates: shape.way.coordinates.map(coordinates) | ||
}, | ||
properties: { | ||
name: w.name[0], | ||
folder: meta.folders[description.folderId], | ||
folderId: description.folderId, | ||
comments: description.comments | ||
name: shape.label, | ||
folder: folders[shape.folderId].label, | ||
folderId: shape.folderId, | ||
comments: shape.comments | ||
} | ||
}; | ||
}); | ||
function points(rtept) { | ||
return [rtept.$.lon, rtept.$.lat].map(Number); | ||
} | ||
} |
@@ -8,6 +8,2 @@ 'use strict'; | ||
var _xml2js = require('xml2js'); | ||
var _xml2js2 = _interopRequireDefault(_xml2js); | ||
var _extract = require('./extract'); | ||
@@ -17,37 +13,13 @@ | ||
var _parseMetadata = require('./parse-metadata'); | ||
var _parseMetadata2 = _interopRequireDefault(_parseMetadata); | ||
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } } | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
function importer(calJson) { | ||
var markers = extract.extractMarkers(calJson); | ||
var routes = extract.extractRoutes(calJson); | ||
function importer(xmlString) { | ||
return new Promise(function (resolve, reject) { | ||
_xml2js2.default.parseString(xmlString, function (err, xml) { | ||
console.log(xml); | ||
var rawMetadata = extract.metadata(xml); | ||
var metadata = (0, _parseMetadata2.default)(rawMetadata); | ||
var waypoints = extract.waypoints(xml); | ||
console.log(waypoints); | ||
var routes = extract.routes(xml); | ||
console.log(routes); | ||
var output = { | ||
type: 'FeatureCollection', | ||
features: [].concat(waypoints, routes) | ||
}; | ||
resolve(output); | ||
}); | ||
}).catch(function (e) { | ||
console.log('wtfffffffffffffff'); | ||
console.log(e); | ||
console.log(e.stack); | ||
}); | ||
var output = { | ||
type: 'FeatureCollection', | ||
features: [].concat(markers, routes) | ||
}; | ||
return output; | ||
} |
{ | ||
"name": "caltopo-to-geojson", | ||
"version": "0.0.4", | ||
"description": "Converts CalTopo GPX to GeoJSON objects.", | ||
"version": "0.1.0", | ||
"description": "Converts CalTopo JSON to GeoJSON objects.", | ||
"author": "Scott Hardy <scott.the.hardy@gmail.com>", | ||
@@ -13,5 +13,2 @@ "main": "lib/index.js", | ||
}, | ||
"dependencies": { | ||
"xml2js": "0.4.16" | ||
}, | ||
"devDependencies": { | ||
@@ -18,0 +15,0 @@ "babel": "6.5.2", |
@@ -8,3 +8,3 @@ # caltopo-to-geojson | ||
Converts [CalTopo](http://caltopo.com) GPX to [GeoJSON](http://geojson.org) objects. | ||
Converts [CalTopo](http://caltopo.com) JSON to [GeoJSON](http://geojson.org) objects. | ||
@@ -11,0 +11,0 @@ |
@@ -1,5 +0,1 @@ | ||
import parseDescription from './parse-description'; | ||
import parseMetadata from './parse-metadata'; | ||
const FEATURE = 'Feature'; | ||
@@ -10,56 +6,45 @@ const POINT = 'Point'; | ||
export function metadata(xml) { | ||
return xml.gpx.metadata.find(m => m.desc).desc[0]; | ||
export function extractFolders(json) { | ||
return json.state.Folder; | ||
} | ||
export function waypoints(xml, meta) { | ||
meta = meta || parseMetadata(metadata(xml)); | ||
return xml.gpx.wpt | ||
.map(w => { | ||
const description = parseDescription(w.desc[0]); | ||
return { | ||
export function extractMarkers(json, folders=extractFolders(json)) { | ||
return json.state.Marker | ||
.map(marker => ({ | ||
type: FEATURE, | ||
geometry: { | ||
type: POINT, | ||
coordinates: [ w.$.lon, w.$.lat ].map(Number), | ||
coordinates: [ marker.position.lng, marker.position.lat ], | ||
}, | ||
properties: { | ||
name: w.name[0], | ||
folder: meta.folders[description.folderId], | ||
folderId: description.folderId, | ||
comments: description.comments, | ||
name: marker.label, | ||
folder: folders[marker.folderId].label, | ||
folderId: marker.folderId, | ||
comments: marker.comments, | ||
}, | ||
}; | ||
}); | ||
})); | ||
} | ||
export function routes(xml, meta) { | ||
meta = meta || parseMetadata(metadata(xml)); | ||
export function extractRoutes(json, folders=extractFolders(json)) { | ||
function coordinates([lat, lon]) { | ||
return [ lon, lat ]; | ||
} | ||
return xml.gpx.rte | ||
.map(w => { | ||
const description = parseDescription(w.desc[0]); | ||
return { | ||
type: FEATURE, | ||
geometry: { | ||
type: LINE_STRING, | ||
coordinates: w.rtept.map(points), | ||
}, | ||
properties: { | ||
name: w.name[0], | ||
folder: meta.folders[description.folderId], | ||
folderId: description.folderId, | ||
comments: description.comments, | ||
}, | ||
}; | ||
}); | ||
function points(rtept) { | ||
return [ rtept.$.lon, rtept.$.lat ].map(Number); | ||
} | ||
return json.state.Shape | ||
.filter(shape => shape.way.type === 'ROUTE') | ||
.map(shape => ({ | ||
type: FEATURE, | ||
geometry: { | ||
type: LINE_STRING, | ||
coordinates: shape.way.coordinates.map(coordinates), | ||
}, | ||
properties: { | ||
name: shape.label, | ||
folder: folders[shape.folderId].label, | ||
folderId: shape.folderId, | ||
comments: shape.comments, | ||
}, | ||
})); | ||
} |
@@ -1,36 +0,13 @@ | ||
import xml2js from 'xml2js'; | ||
import * as extract from './extract'; | ||
import parseMetadata from './parse-metadata'; | ||
export default function importer(xmlString) { | ||
return new Promise((resolve, reject) => { | ||
export default function importer(calJson) { | ||
const markers = extract.extractMarkers(calJson); | ||
const routes = extract.extractRoutes(calJson); | ||
xml2js.parseString(xmlString, (err, xml) => { | ||
console.log(xml); | ||
const rawMetadata = extract.metadata(xml); | ||
const metadata = parseMetadata(rawMetadata); | ||
const waypoints = extract.waypoints(xml); | ||
console.log(waypoints); | ||
const routes = extract.routes(xml); | ||
console.log(routes); | ||
const output = { | ||
type: 'FeatureCollection', | ||
features: [].concat(waypoints, routes), | ||
}; | ||
resolve(output); | ||
}); | ||
}) | ||
.catch(e => { | ||
console.log('wtfffffffffffffff'); | ||
console.log(e); | ||
console.log(e.stack); | ||
}); | ||
const output = { | ||
type: 'FeatureCollection', | ||
features: [].concat(markers, routes), | ||
}; | ||
return output; | ||
} |
@@ -6,25 +6,18 @@ import fs from 'fs'; | ||
import importer from '../../lib'; | ||
import expected1 from './test-1.json'; | ||
import input1 from './test-1.json'; | ||
import expected1 from './test-1-expected.json'; | ||
const expecteds = [ | ||
expected1, | ||
const tests = [ | ||
[ input1, expected1 ], | ||
]; | ||
expecteds.forEach((expected, i) => { | ||
const n = i + 1; | ||
test(`integration test-${n}`, t => { | ||
const file = path.join(__dirname, `test-${n}.gpx`); | ||
const input = fs.readFileSync(file, 'utf8'); | ||
console.log(input); | ||
importer(input) | ||
.then(output => { | ||
console.log(output); | ||
t.deepEqual(output, expected); | ||
t.end(); | ||
}); | ||
tests.forEach(([ input, expected ], i) => { | ||
test(`integration test-${i + 1}`, t => { | ||
const output = importer(input); | ||
t.deepEqual(output, expected); | ||
t.end(); | ||
}); | ||
}); |
{ | ||
"type": "FeatureCollection", | ||
"features": [ | ||
{ | ||
"type": "Feature", | ||
"geometry": { | ||
"type": "Point", | ||
"coordinates": [ | ||
-123.02409587050437, | ||
40.88740538581878 | ||
] | ||
}, | ||
"properties": { | ||
"name": "Trailhead 1", | ||
"folder": "Trailheads", | ||
"folderId": "0", | ||
"comments": "Comment with <b>HTML</b> in it!" | ||
"state": { | ||
"timestamp": "1457850938316", | ||
"Shape": [ | ||
{ | ||
"color": "#FF0000", | ||
"comments": "Some comment...", | ||
"fill": 10, | ||
"folderId": 2, | ||
"id": 0, | ||
"label": "Trail 1", | ||
"lastUpdated": 1457850921000, | ||
"pattern": "solid", | ||
"updated": "1457850921000", | ||
"way": { | ||
"coordinates": [ | ||
[ | ||
40.88740648211728, | ||
-123.02409768104553 | ||
], | ||
[ | ||
40.88786475043473, | ||
-123.02412986755371 | ||
], | ||
[ | ||
40.88808780023742, | ||
-123.02364706993103 | ||
] | ||
], | ||
"id": 21798749, | ||
"polygon": false, | ||
"sourceDistance": 0, | ||
"type": "ROUTE" | ||
}, | ||
"weight": 2 | ||
} | ||
}, | ||
{ | ||
"type": "Feature", | ||
"geometry": { | ||
"type": "Point", | ||
"coordinates": [ | ||
-123.01609339935067, | ||
40.89255315925912 | ||
] | ||
], | ||
"Folder": [ | ||
{ | ||
"id": 0, | ||
"label": "Trailheads", | ||
"updated": "1457850800000" | ||
}, | ||
"properties": { | ||
"name": "Lake 1", | ||
"folder": "Water", | ||
"folderId": "1", | ||
"comments": "" | ||
{ | ||
"id": 1, | ||
"label": "Water", | ||
"updated": "1457850332000" | ||
}, | ||
{ | ||
"id": 2, | ||
"label": "Trails", | ||
"updated": "1457850921000" | ||
} | ||
}, | ||
{ | ||
"type": "Feature", | ||
"geometry": { | ||
"type": "LineString", | ||
"coordinates": [ | ||
[ | ||
-123.02409768104553, | ||
40.88740648211728 | ||
], | ||
[ | ||
-123.02412986755371, | ||
40.88786475043473 | ||
], | ||
[ | ||
-123.02364706993103, | ||
40.88808780023742 | ||
] | ||
] | ||
], | ||
"CustomLayer": [], | ||
"LocatorGroup": [], | ||
"Locator": [], | ||
"ConfiguredLayer": [], | ||
"GeoRef": [], | ||
"Marker": [ | ||
{ | ||
"comments": "", | ||
"folderId": 1, | ||
"id": 1, | ||
"label": "Lake 1", | ||
"position": { | ||
"id": 21798756, | ||
"lat": 40.89255315925912, | ||
"lng": -123.01609339935067 | ||
}, | ||
"updated": "1457850332000", | ||
"url": "#FF0000" | ||
}, | ||
"properties": { | ||
"name": "Trail 1", | ||
"folder": "Trails", | ||
"folderId": "2", | ||
"comments": "Some comment..." | ||
{ | ||
"comments": "Comment with <b>HTML<\/b> in it!", | ||
"folderId": 0, | ||
"id": 0, | ||
"label": "Trailhead 1", | ||
"position": { | ||
"id": 21798754, | ||
"lat": 40.88740538581878, | ||
"lng": -123.02409587050437 | ||
}, | ||
"updated": "1457850800000", | ||
"url": "#FF0000" | ||
} | ||
} | ||
] | ||
] | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
0
11926
18
297
1
- Removedxml2js@0.4.16
- Removedlodash@4.17.21(transitive)
- Removedsax@1.4.1(transitive)
- Removedxml2js@0.4.16(transitive)
- Removedxmlbuilder@4.2.1(transitive)