Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for .poly files #65

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 47 additions & 4 deletions src/leaflet.filelayer.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Load files *locally* (GeoJSON, KML, GPX) into the map
* Load files *locally* (GeoJSON, KML, GPX, POLY) into the map
* using the HTML5 File API.
*
* Requires Mapbox's togeojson.js to be in global scope
Expand Down Expand Up @@ -54,7 +54,8 @@
geojson: this._loadGeoJSON,
json: this._loadGeoJSON,
gpx: this._convertToGeoJSON,
kml: this._convertToGeoJSON
kml: this._convertToGeoJSON,
poly: this._loadPoly
};
},

Expand Down Expand Up @@ -209,6 +210,48 @@
}
return layer;
},

/* Convert .poly into geojson and load it
* The Osmosis polygon filter file format is supported by Osmosis, mapsplit,
* osmconvert, osmchange and pbftoosm as a way of defining extraction polygons.
* https://wiki.openstreetmap.org/wiki/Osmosis/Polygon_Filter_File_Format
*/
kikiminou marked this conversation as resolved.
Show resolved Hide resolved
_loadPoly: function _loadPoly(content) {
var lines = content.split("\n");
var json = {
"type": "Feature",
"name": $.trim(lines[0]),
leplatrem marked this conversation as resolved.
Show resolved Hide resolved
"geometry": {
"type": "MultiPolygon",
"coordinates": [],
"crs":{"type":"name","properties":{"name":"EPSG:4326"}}
}
};
lines.splice(0, 1);
lines.splice(lines.length-1, 1);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do those two lines? Please add a comment :)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comments added


var begin = true;
var current = [];
lines.forEach(function (elt) {
var line =$.trim(elt);
line = line.replace(/\s+/g, ' ');
if (begin === true) {
leplatrem marked this conversation as resolved.
Show resolved Hide resolved
begin = false;
return;
} else {
var coords = line.split(' ');
if (coords.length >= 2) {
current.push([Number(coords[0]), Number(coords[1])]);
}
}
if (line === 'END') {
json.geometry.coordinates.push([current]);
current = [];
begin = true;
}
});
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest that you create a dedicated function poly2geojson() with some unit tests :)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, it seems to be for me the most complex part... Do I really need to install nodejs and all thoses things called chai mocha simon happen ? I tryed npm, but I get various sorts of errors I don't understand : like "Unsupported platform for [email protected]: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})"... I'm surprised that Debian Linux 9.6 on x64 is not supported by mocha... and I don't have time to search why all those tangled things don't work. Maybe you have a simple procedure?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also I'm using a virtualized file system and it doesn't seem compatible with npm. It says:

npm ERR! rofs EROFS: read-only file system, symlink '../acorn/bin/acorn' -> '/media/sf_www/kikiminou/Leaflet.FileLayer/node_modules/.bin/acorn'
npm ERR! rofs Often virtualized file systems, or other file systems
npm ERR! rofs that don't support symlinks, give this error.

Copy link
Author

@kikiminou kikiminou Feb 21, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe I can simply create the entry poly2geojson in test.filelayer.js and let someone else test it ?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have time to search why all those tangled things don't work

I know exactly what you mean. And me neither :)

Maybe you have a simple procedure?

Using nvm is usually helpful to get a recent version of npm working.

Maybe I can simply create the entry poly2geojson in test.filelayer.js and let someone else test it ?

You could write the test and let the Continous Integration run it for you when you push to your branch :) Look, that's the output of your last commit https://travis-ci.org/makinacorpus/Leaflet.FileLayer/builds/496427902

Merging without tests is possible too, if folks from @makinacorpus approve it :)

return this._loadGeoJSON(json);
},

_convertToGeoJSON: function _convertToGeoJSON(content, format) {
var geojson;
Expand All @@ -223,7 +266,7 @@

var FileLayerLoad = L.Control.extend({
statics: {
TITLE: 'Load local file (GPX, KML, GeoJSON)',
TITLE: 'Load local file (GPX, KML, GeoJSON, POLY)',
LABEL: '⌅'
},
options: {
Expand Down Expand Up @@ -308,7 +351,7 @@
fileInput.type = 'file';
fileInput.multiple = 'multiple';
if (!this.options.formats) {
fileInput.accept = '.gpx,.kml,.json,.geojson';
fileInput.accept = '.gpx,.kml,.json,.geojson,.poly';
} else {
fileInput.accept = this.options.formats.join(',');
}
Expand Down