-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
72 lines (56 loc) · 2.3 KB
/
script.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
68
69
70
71
72
// way(25865560); damar
// node(548617400); wurlali
// node(1070177665); wuwarlali
var request = require('request'),
osmtogeojson = require('osmtogeojson'),
kmltogeojson = require('togeojson'),
DOMParser = require('xmldom').DOMParser,
tj = require('togeojson'),
fs = require('fs');
var overpassApiUrl = 'http://overpass-api.de/api/interpreter',
damarIslandQuery = '[out:json];area[name="Indonesia"];way(25865560);(._;>;);out body;',
damarMountainQuery = '[out:json];area[name="Indonesia"];(node(548617400);node(1070177665););(._;>;);out body;',
dephutUrl = 'http://appgis.dephut.go.id/appgis/download.aspx',
damarCriticallandQuery = 'status=view&filename=Maluku.kml&fileFullName=E:\\webgis1\\Peta Tematik Kehutanan1\\KML\\Lahan Kritis\\Maluku.kml';
function saveFile(filename, data) {
fs.writeFile(filename, data, function(err) {
if(err) {
console.log(err);
}
console.log("The file " + filename + " was saved!");
});
}
function printError(code, error) {
console.log("Error found!");
console.log("Status Code : " + code);
console.log("Error Message : " + error);
};
function saveOverpassGeoJson(query, filename) {
request(overpassApiUrl+'?data='+query, function (error, response, body) {
if (!error && response.statusCode == 200) {
var osm = JSON.parse(body);
var geojson = osmtogeojson(osm);
saveFile(filename, JSON.stringify(geojson));
} else {
printError(response.statusCode, error);
}
});
}
function saveDephutKml(query, filename) {
request(dephutUrl+'?'+query, function (error, response, body) {
if (!error && response.statusCode == 200) {
saveFile(filename, body);
} else {
printError(response.statusCode, error);
}
});
}
function convertKmlToGeojson(srcFilename, destFilename) {
var kml = new DOMParser().parseFromString(fs.readFileSync(srcFilename, 'utf8'));
var geojson = tj.kml(kml, { styles: true });
saveFile(destFilename, JSON.stringify(geojson));
}
saveOverpassGeoJson(damarIslandQuery, 'damar_island.geojson');
saveOverpassGeoJson(damarMountainQuery, 'damar_mountain.geojson');
saveDephutKml(damarCriticallandQuery, 'damar_criticalland.kml');
convertKmlToGeojson('damar_criticalland.kml', 'damar_criticalland.geojson');