Refactor common track loading parts (#312)

- common track style
- TracksLoader now also adds points as POIs
- RouteLoaderConverter does not add route points as POI
This commit is contained in:
Norbert Renner 2020-06-25 17:15:09 +02:00
parent fa59a44a5e
commit 5ad12a7c68
4 changed files with 98 additions and 35 deletions

View file

@ -312,7 +312,7 @@
BR.stravaSegments(map, layersControl);
}
BR.tracksLoader(map, layersControl, routing);
BR.tracksLoader(map, layersControl, routing, pois);
BR.routeLoader(map, layersControl, routing, pois);

View file

@ -257,11 +257,7 @@ BR.routeLoader = function(map, layersControl, routing, pois) {
},
addTrackOverlay: function(geoJSON) {
this._trackLayer = L.geoJSON(geoJSON, {
filter: function(geoJsonFeature) {
return !(geoJsonFeature.type == 'Feature' && geoJsonFeature.geometry.type == 'Point');
}
}).addTo(map);
this._trackLayer = L.geoJSON(geoJSON, BR.Track.getGeoJsonOptions(layersControl)).addTo(map);
layersControl.addOverlay(this._trackLayer, this._layerName);
@ -320,15 +316,7 @@ BR.routeLoader = function(map, layersControl, routing, pois) {
if (!this._bounds) map.fitBounds(this._bounds);
if (this._options.showPointAsPoi) {
turf.featureEach(this._currentGeoJSON, function(feature, idx) {
if (turf.getType(feature) == 'Point') {
var coord = turf.getCoord(feature);
var latlng = L.GeoJSON.coordsToLatLng(coord);
var name = '';
if (feature.properties && feature.properties.name) name = feature.properties.name;
pois.addMarker(latlng, name);
}
});
BR.Track.addPoiMarkers(pois, this._currentGeoJSON);
}
},

View file

@ -1,25 +1,16 @@
BR.tracksLoader = function(map, layersControl, routing) {
BR.tracksLoader = function(map, layersControl, routing, pois) {
// proxy to L.geoJSON factory function, to get hold of raw GeoJSON object
var createGeoJsonLayer = function(geojson, options) {
BR.Track.addPoiMarkers(pois, geojson);
return L.geoJSON(geojson, options);
};
TracksLoader = L.Control.FileLayerLoad.extend({
options: {
layer: L.geoJson,
layerOptions: {
style: function(geoJsonFeature) {
var currentLayerId = layersControl.getActiveBaseLayer().layer.id;
return {
color: currentLayerId === 'cyclosm' ? 'yellow' : 'blue',
weight: 4
};
},
interactive: false,
pointToLayer: function(geoJsonPoint, latlng) {
return L.marker(latlng, {
interactive: false,
opacity: 0.7,
// prevent being on top of route markers
zIndexOffset: -1000
});
}
},
// `layer` allows to use a customized version of `L.geoJson` with the same signature
layer: createGeoJsonLayer,
layerOptions: BR.Track.getGeoJsonOptions(layersControl),
addToMap: false,
// File size limit in kb (default: 1024) ?
fileSizeLimit: 1024,
@ -117,6 +108,7 @@ BR.tracksLoader = function(map, layersControl, routing) {
error: err && err.message ? err.message : err
})
);
console.error(err);
});
return tracksLoaderControl;

83
js/util/Track.js Normal file
View file

@ -0,0 +1,83 @@
/**
* Track loading commons
*/
BR.Track = {
/**
* Returns common options for styling and appearance of tracks
*
* @param {BR.ControlLayers} layersControl Layers control instance
*
* @returns {Object} to pass as `options` parameter to `L.geoJson`
*/
getGeoJsonOptions: function(layersControl) {
return {
style: function(geoJsonFeature) {
var currentLayerId = layersControl.getActiveBaseLayer().layer.id;
return {
color: currentLayerId === 'cyclosm' ? 'yellow' : 'blue',
weight: 4
};
},
interactive: false,
filter: function(geoJsonFeature) {
// remove POIs, added separately
return !BR.Track.isPoiPoint(geoJsonFeature);
},
pointToLayer: function(geoJsonPoint, latlng) {
// route waypoint (type=from/via/to)
return L.marker(latlng, {
interactive: false,
opacity: 0.7,
// prevent being on top of route markers
zIndexOffset: -1000
});
}
};
},
/**
* Add Points in the passed `geoJson` as POI markers, except route waypoints (type=from|via|to)
*
* @param {BR.PoiMarkers} pois POI control instance
* @param {Object} geoJson GeoJSON object
*/
addPoiMarkers: function(pois, geoJson) {
turf.featureEach(geoJson, function(feature, idx) {
if (BR.Track.isPoiPoint(feature)) {
var coord = turf.getCoord(feature);
var latlng = L.GeoJSON.coordsToLatLng(coord);
var name = '';
if (feature.properties && feature.properties.name) name = feature.properties.name;
pois.addMarker(latlng, name);
}
});
},
/**
* Checks if the passed GeoJSON Point feature is a route waypoint.
*
* Route points are exported e.g. as GPX `wpt` with a `type=from|via|to` property
* if the "waypoints" option is checked in the Export dialog.
*
* @param {Object} geoJsonPointFeature GeoJSON Point feature
*/
isRouteWaypoint: function(geoJsonPointFeature) {
var props = geoJsonPointFeature.properties;
if (props && props.type) {
var wptType = props.type;
if (wptType === 'from' || wptType === 'via' || wptType === 'to') {
return true;
}
}
return false;
},
/**
* Checks if the passed GeoJSON feature should be added as a POI
*
* @param {Object} geoJsonFeature GeoJSON feature
*/
isPoiPoint: function(geoJsonFeature) {
return turf.getType(geoJsonFeature) === 'Point' && !BR.Track.isRouteWaypoint(geoJsonFeature);
}
};