kopia lustrzana https://github.com/FacilMap/facilmap
Move file-import code to separate file
rodzic
934c7dbdf5
commit
e26e588fb6
|
@ -0,0 +1,83 @@
|
|||
import toGeoJSON from '@mapbox/togeojson';
|
||||
import osmtogeojson from 'osmtogeojson';
|
||||
|
||||
import fm from '../../app';
|
||||
|
||||
|
||||
fm.app.factory("fmMapSearchFiles", function($rootScope, $compile, fmUtils) {
|
||||
return function(map, fmMapSearch) {
|
||||
const fmMapSearchFiles = {
|
||||
parseFiles(files) {
|
||||
var ret = {features: [ ]};
|
||||
var errors = false;
|
||||
files.forEach(function(file) {
|
||||
var geojson = null;
|
||||
|
||||
if(file.match(/^\s*</)) {
|
||||
var doc = $.parseXML(file);
|
||||
var xml = $(doc).find(":root");
|
||||
|
||||
if(xml.is("gpx"))
|
||||
geojson = toGeoJSON.gpx(xml[0]);
|
||||
else if(xml.is("kml"))
|
||||
geojson = toGeoJSON.kml(xml[0]);
|
||||
else if(xml.is("osm"))
|
||||
geojson = osmtogeojson(doc);
|
||||
} else if(file.match(/^\s*\{/)) {
|
||||
var content = JSON.parse(file);
|
||||
if(content.type)
|
||||
geojson = content;
|
||||
}
|
||||
|
||||
if(geojson == null)
|
||||
return errors = true;
|
||||
|
||||
var features;
|
||||
if(geojson.type == "FeatureCollection")
|
||||
features = geojson.features || [ ];
|
||||
else if(geojson.type == "Feature")
|
||||
features = [ geojson ];
|
||||
else
|
||||
features = [ { type: "Feature", geometry: geojson, properties: { } } ];
|
||||
|
||||
features.forEach(function(feature) {
|
||||
var name;
|
||||
|
||||
if(typeof feature.properties != "object")
|
||||
feature.properties = { };
|
||||
|
||||
if(feature.properties.name)
|
||||
name = feature.properties.name;
|
||||
else if(feature.properties.tags.name)
|
||||
name = feature.properties.tags.name;
|
||||
else if(feature.properties.type)
|
||||
name = feature.properties.type + " " + feature.properties.id;
|
||||
else if([ "Polygon", "MultiPolygon" ].indexOf(feature.geometry.type) != -1)
|
||||
name = "Polygon";
|
||||
else if([ "LineString", "MultiLineString" ].indexOf(feature.geometry.type) != -1)
|
||||
name = "Line";
|
||||
else if([ "Point", "MultiPoint" ].indexOf(feature.geometry.type) != -1)
|
||||
name = "Point";
|
||||
else
|
||||
name = feature.geometry.type || "Object";
|
||||
|
||||
ret.features.push({
|
||||
short_name: name,
|
||||
display_name: name,
|
||||
extratags: feature.properties.data || feature.properties.tags || fmUtils.flattenObject(feature.properties),
|
||||
geojson: feature.geometry,
|
||||
type: feature.properties.type || feature.geometry.type
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
if(errors)
|
||||
return map.messages.showMessage("danger", "Some files could not be parsed.");
|
||||
|
||||
return ret;
|
||||
}
|
||||
};
|
||||
|
||||
return fmMapSearchFiles;
|
||||
};
|
||||
});
|
|
@ -2,13 +2,11 @@ import fm from '../../app';
|
|||
import $ from 'jquery';
|
||||
import L from 'leaflet';
|
||||
import ng from 'angular';
|
||||
import toGeoJSON from '@mapbox/togeojson';
|
||||
import osmtogeojson from 'osmtogeojson';
|
||||
import '../../../assets/font/fontello.css';
|
||||
import 'jquery-ui';
|
||||
import 'jquery-ui/ui/widgets/resizable';
|
||||
|
||||
fm.app.factory("fmMapSearch", function($rootScope, $compile, fmUtils, $timeout, $q, fmMapSearchRoute) {
|
||||
fm.app.factory("fmMapSearch", function($rootScope, $compile, fmUtils, $timeout, $q, fmMapSearchRoute, fmMapSearchFiles) {
|
||||
return function(map) {
|
||||
var iconSuffix = ".n.32.png";
|
||||
|
||||
|
@ -43,9 +41,9 @@ fm.app.factory("fmMapSearch", function($rootScope, $compile, fmUtils, $timeout,
|
|||
scope.searchString = q = results[0].display_name;
|
||||
|
||||
if(typeof results == "string")
|
||||
loadSearchResults(parseFiles([ results ]), noZoom);
|
||||
loadSearchResults(filesUi.parseFiles([ results ]), noZoom);
|
||||
else
|
||||
loadSearchResults(results, noZoom);
|
||||
loadSearchResults({features: results}, noZoom);
|
||||
}).catch(function(err) {
|
||||
map.messages.showMessage("danger", err);
|
||||
});
|
||||
|
@ -156,80 +154,10 @@ fm.app.factory("fmMapSearch", function($rootScope, $compile, fmUtils, $timeout,
|
|||
function loadSearchResults(results, noZoom) {
|
||||
clearRenders();
|
||||
|
||||
scope.searchResults = results;
|
||||
scope.searchResults = results.features;
|
||||
|
||||
if(results && results.length > 0)
|
||||
(scope.showAll && results.length > 1) ? scope.showAllResults(noZoom) : scope.showResult(scope.searchResults[0], noZoom);
|
||||
}
|
||||
|
||||
function parseFiles(files) {
|
||||
var ret = [ ];
|
||||
var errors = false;
|
||||
files.forEach(function(file) {
|
||||
var geojson = null;
|
||||
|
||||
if(file.match(/^\s*</)) {
|
||||
var doc = $.parseXML(file);
|
||||
var xml = $(doc).find(":root");
|
||||
|
||||
if(xml.is("gpx"))
|
||||
geojson = toGeoJSON.gpx(xml[0]);
|
||||
else if(xml.is("kml"))
|
||||
geojson = toGeoJSON.kml(xml[0]);
|
||||
else if(xml.is("osm"))
|
||||
geojson = osmtogeojson(doc);
|
||||
} else if(file.match(/^\s*\{/)) {
|
||||
var content = JSON.parse(file);
|
||||
if(content.type)
|
||||
geojson = content;
|
||||
}
|
||||
|
||||
if(geojson == null)
|
||||
return errors = true;
|
||||
|
||||
var features;
|
||||
if(geojson.type == "FeatureCollection")
|
||||
features = geojson.features || [ ];
|
||||
else if(geojson.type == "Feature")
|
||||
features = [ geojson ];
|
||||
else
|
||||
features = [ { type: "Feature", geometry: geojson, properties: { } } ];
|
||||
|
||||
features.forEach(function(feature) {
|
||||
var name;
|
||||
|
||||
if(typeof feature.properties != "object")
|
||||
feature.properties = { };
|
||||
|
||||
if(feature.properties.name)
|
||||
name = feature.properties.name;
|
||||
else if(feature.properties.tags.name)
|
||||
name = feature.properties.tags.name;
|
||||
else if(feature.properties.type)
|
||||
name = feature.properties.type + " " + feature.properties.id;
|
||||
else if([ "Polygon", "MultiPolygon" ].indexOf(feature.geometry.type) != -1)
|
||||
name = "Polygon";
|
||||
else if([ "LineString", "MultiLineString" ].indexOf(feature.geometry.type) != -1)
|
||||
name = "Line";
|
||||
else if([ "Point", "MultiPoint" ].indexOf(feature.geometry.type) != -1)
|
||||
name = "Point";
|
||||
else
|
||||
name = feature.geometry.type || "Object";
|
||||
|
||||
ret.push({
|
||||
short_name: name,
|
||||
display_name: name,
|
||||
extratags: feature.properties.data || feature.properties.tags || fmUtils.flattenObject(feature.properties),
|
||||
geojson: feature.geometry,
|
||||
type: feature.properties.type || feature.geometry.type
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
if(errors)
|
||||
return map.messages.showMessage("danger", "Some files could not be parsed.");
|
||||
|
||||
return ret;
|
||||
if(scope.searchResults && scope.searchResults.length > 0)
|
||||
(scope.showAll && scope.searchResults.length > 1) ? scope.showAllResults(noZoom) : scope.showResult(scope.searchResults[0], noZoom);
|
||||
}
|
||||
|
||||
function _lineStringToTrackPoints(geometry) {
|
||||
|
@ -389,7 +317,7 @@ fm.app.factory("fmMapSearch", function($rootScope, $compile, fmUtils, $timeout,
|
|||
},
|
||||
|
||||
showFiles: function(files) {
|
||||
loadSearchResults(parseFiles(files));
|
||||
loadSearchResults(filesUi.parseFiles(files));
|
||||
},
|
||||
|
||||
route: function(destinations, mode, noZoom) {
|
||||
|
@ -418,6 +346,7 @@ fm.app.factory("fmMapSearch", function($rootScope, $compile, fmUtils, $timeout,
|
|||
};
|
||||
|
||||
var routeUi = fmMapSearchRoute(map, searchUi);
|
||||
var filesUi = fmMapSearchFiles(map, searchUi);
|
||||
|
||||
el.find(".fm-search-results").resizable({
|
||||
handles: {
|
||||
|
|
Ładowanie…
Reference in New Issue