-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
67 lines (60 loc) · 2.15 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
var query_overpass = require('query-overpass');
var turf = require('turf');
var fc = require('turf-featurecollection');
module.exports = function(id, callback) {
query_overpass('[out:json];relation('+id+');>;out;', function(error, data){
if(!error){
linestringArray = [];
pointPolygon = [];
data.features.forEach(function(entry) {
if(entry.geometry.type == 'LineString'){
linestringArray.push(entry.geometry.coordinates);
}
});
var n = linestringArray.length;
var igeo = 0;
for (var i = 0; i < n; i++) {
if(!pointPolygon[igeo]){
pointPolygon[igeo] = linestringArray[igeo];
linestringArray.splice(0,1);
}
var pointtofind = pointPolygon[igeo][pointPolygon[igeo].length-1];
linestringArray.forEach(function(entry,index) {
if(entry[0][0] == pointtofind[0] && entry[0][1] == pointtofind[1]){
var arraytoadd = entry;
arraytoadd.splice(0,1);
pointPolygon[igeo] = pointPolygon[igeo].concat(arraytoadd);
linestringArray.splice(index,1);
}
if(entry[entry.length-1][0] == pointtofind[0] && entry[entry.length-1][1] == pointtofind[1]){
var arraytoadd = entry.reverse();
arraytoadd.splice(0,1);
pointPolygon[igeo] = pointPolygon[igeo].concat(arraytoadd);
linestringArray.splice(index,1);
}
});
if(
pointPolygon[igeo][0][0] == pointPolygon[igeo][pointPolygon[igeo].length-1][0]
&& pointPolygon[igeo][0][1] == pointPolygon[igeo][pointPolygon[igeo].length-1][1]
&& linestringArray.length != 0
){
igeo++;
}
}
if(linestringArray.length == 0){
try {
var poly1 = turf.polygon(pointPolygon);
var poly2 = turf.polygon(pointPolygon, {bbox:turf.bbox(poly1)});
callback(fc([poly2]));
}
catch (e) {
callback(false);
}
}else{
callback(false);
}
}else{
callback(false);
}
}, {flatProperties:true});
};