Skip to content

Commit

Permalink
Support both GPX export modes
Browse files Browse the repository at this point in the history
old mode with togpx plugin
and new mode with direct Api call

refers to #256
  • Loading branch information
TheGreatRefrigerator committed Nov 11, 2018
1 parent 69ef45c commit 1df0418
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,29 @@ <h1 ng-bind-html="('DOWNLOAD_ROUTE' | translate)">
</div>
<div ng-if="$ctrl.gpxOptShow">
<br>
<div class="ui checkbox">
<input type="checkbox" id="instructions" data-ng-model="$ctrl.instructions"
ng-click="$ctrl.instructions != $ctrl.instructions">
<label for="instructions" ng-bind-html="('INCLUDE_INSTRUCTIONS' | translate)"></label>
<div class="field ors-inline">
<div class="ui radio checkbox">
<input ng-model="$ctrl.toGpx"
ng-value=true type="radio">
<label ng-click="$ctrl.toGpx = true">togpx
</label>
</div>
</div>
<div class="field ors-inline">
<div class="ui radio checkbox">
<input ng-model="$ctrl.toGpx"
ng-value=false type="radio">
<label ng-click="$ctrl.toGpx = false">Ors API
</label>
</div>
</div>
<div ng-if="!$ctrl.toGpx">
<br>
<div class="ui checkbox">
<input type="checkbox" id="instructions" data-ng-model="$ctrl.instructions"
ng-click="$ctrl.instructions != $ctrl.instructions">
<label for="instructions" ng-bind-html="('INCLUDE_INSTRUCTIONS' | translate)"></label>
</div>
</div>
</div>
<div class="ui divider">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ angular
let ctrl = this;
ctrl.elevation = true;
ctrl.instructions = false;
ctrl.toGpx = true;
ctrl.filename = "ors-export-linestring";
ctrl.gpxOptShow = true;
ctrl.tcxOptShow = false;
Expand Down Expand Up @@ -88,10 +89,11 @@ angular
ctrl.exportRoute = () => {
let options = {
elevation: ctrl.elevation,
instructions: ctrl.instructions
instructions: ctrl.instructions,
toGpx: ctrl.toGpx
};
let currentRoute = null;
if (ctrl.currentFileFormat == "rawjson") {
if (ctrl.currentFileFormat === "rawjson") {
currentRoute =
orsRouteService.data.routes[orsRouteService.getCurrentRouteIdx()];
} else {
Expand Down
108 changes: 65 additions & 43 deletions app/infrastructure/ors-importexport-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,63 @@ angular
orsUtilsService,
orsRouteService
) => {
/**
* Creates the GPX data to export from the vector geometries on the map
* using togpx: https://github.com/tyrasd/togpx
* @param geometry - geometry to convert
* @param filename - filename of the export file
* @returns - the gpx data in xml format
*/
let gpxExportFromMap = (geometry, filename) => {
let geojsonData = L.polyline(geometry).toGeoJSON()
geojsonData.properties.name = filename // togpx is looking for name tag https://www.npmjs.com/package/togpx#gpx
return togpx(geojsonData, {
creator: "OpenRouteService.org"
})
};
/**
* Save GPX file by doing an additional api call
* using format=gpx with the same options
* @param options - route options
* @param filename - filename of the export file
*/
let saveGpxFromApi = (options, filename) => {
let userOptions = orsSettingsFactory.getUserOptions();
let settings = orsSettingsFactory.getSettings();
let payload = orsUtilsService.routingPayload(settings, userOptions);
payload.format = "gpx";
if (!options.instructions) payload.instructions = false;
const request = orsRouteService.fetchRoute(payload);
orsRouteService.routingRequests.requests.push(request);
request.promise.then(function (response) {
// parsing xml to js object using https://www.npmjs.com/package/xml-js
let exportData = xml2js(response);
let metadata = exportData.elements[0].elements[0].elements;
metadata[0].elements[0].text = filename;
// description node can be <desc></desc> so the parsed object wont have an elements array
if (metadata[1].elements == null) {
metadata[1].elements = [{text: "", type: "text"}];
}
// this will suppress the jshint error for linebreak before ternary
/*jshint -W014 */
metadata[1].elements[0].text = options.instruction
? "This route and instructions were generated from maps.openrouteservice"
: "This route was generated from maps.openrouteservice";
// parsing back to xml string + saving
exportData = js2xml(exportData);
exportData = new Blob([exportData], {
type: "application/xml;charset=utf-8"
});
FileSaver.saveAs(exportData, filename + '.gpx');
}, function (error) {
orsSettingsFactory.requestSubject.onNext(false);
alert("There was an error generating the gpx file: " + error);
});
}

let orsExportFactory = {};
/**
* Export any vector element on the map to GPX
* Export any vector element on the map to file
* @param {Object} geometry: the route object (leaflet object) to export
* @param {String} geomType: the type of geometry which is passed
* @param {Object} options: export options
Expand All @@ -43,43 +97,11 @@ angular
* Instructions can be included when needed.
*/
case "gpx":
let userOptions = orsSettingsFactory.getUserOptions();
let settings = orsSettingsFactory.getSettings();
let payload = orsUtilsService.routingPayload(settings, userOptions);
payload.format = "gpx";
if (!options.instructions) payload.instructions = false;
const request = orsRouteService.fetchRoute(payload);
orsRouteService.routingRequests.requests.push(request);
request.promise.then(
function(response) {
// parsing xml to js object using https://www.npmjs.com/package/xml-js
exportData = xml2js(response);
let metadata = exportData.elements[0].elements[0].elements;
metadata[0].elements[0].text = filename;
// description node can be <desc></desc> so the parsed object wont have an elements array
if (metadata[1].elements == null) {
metadata[1].elements = [{ text: "", type: "text" }];
} // this will suppress the jshint error for linebreak before ternary
/*jshint -W014 */ metadata[1].elements[0].text = options.instruction
? "This route and instructions were generated from maps.openrouteservice"
: "This route was generated from maps.openrouteservice";
// parsing back to xml string + saving
exportData = js2xml(exportData);
exportData = new Blob([exportData], {
type: "application/xml;charset=utf-8"
});
FileSaver.saveAs(exportData, filename + extension);
},
function(error) {
orsSettingsFactory.requestSubject.onNext(false);
alert("There was an error generating the gpx file: " + error);
}
);
// geojsonData = L.polyline(geometry).toGeoJSON();
// geojsonData.properties.name = filename; // togpx is looking for name tag https://www.npmjs.com/package/togpx#gpx
// exportData = togpx(geojsonData, {
// creator: "OpenRouteService.org"
// });
if (options.toGpx) {
exportData = gpxExportFromMap(geometry, filename)
} else {
saveGpxFromApi(options, filename, extension)
}
break;
case "kml":
geojsonData = L.polyline(geometry).toGeoJSON();
Expand All @@ -104,7 +126,7 @@ angular
case "geojson":
if (geomType === "linestring") {
if (!options.elevation) {
angular.forEach(geometry, function(value) {
angular.forEach(geometry, (value) => {
value = value.splice(2, 1);
});
}
Expand All @@ -120,15 +142,15 @@ angular
c[0][k][0] = c[0][k][1];
c[0][k][1] = store;
}
const geojsonpolygon = {
const geoJsonPolygon = {
type: "Feature",
properties: properties,
geometry: {
type: "Polygon",
coordinates: c
}
};
isochrones.push(geojsonpolygon);
isochrones.push(geoJsonPolygon);
}
exportData = JSON.stringify(isochrones);
exportData =
Expand All @@ -137,9 +159,9 @@ angular
break;
default:
}
// as gpx generation is async we have to save it upon returned promise
// as API gpx generation is async we have to save it upon returned promise
// therefore skipping saving for gpx here
if (format !== "gpx") {
if (!(format === 'gpx' && !options.toGpx)) {
exportData = new Blob([exportData], {
type: "application/xml;charset=utf-8"
});
Expand Down

0 comments on commit 1df0418

Please sign in to comment.