-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
103 lines (98 loc) · 3.06 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
const fs = require('fs');
const gju = require('geojson-utils');
var waterfall = require('async-waterfall');
const nepal = require('./geo_data/nepal.json');
const getCountry = () => {
return nepal;
};
const getProvince = (province, callback) => {
fs.readFile(require.resolve(`./geo_data/province/${province}.json`), 'utf8', function (err, data) {
if (err) {
return callback(err);
}
data = JSON.parse(data);
return callback(null, data);
});
};
const getDistrict = (district, callback) => {
fs.readFile(require.resolve(`./geo_data/districts/${district}.json`), 'utf8', function (err, data) {
if (err) {
return callback(err);
}
data = JSON.parse(data);
return callback(null, data);
});
};
exports.getCountry = getCountry;
exports.getProvince = getProvince;
exports.getDistrict = getDistrict;
exports.geoLocate = (latitude, longitude) => {
return new Promise((resolve, reject) => {
waterfall([
//find province
function(callback) {
let province = getCountry().features.find((province) => {
let check = gju.pointInPolygon(
{"type":"Point","coordinates": [ longitude, latitude]},
province.geometry
);
return check;
});
if(province){
callback(null, province);
} else {
callback(new Error('Could not determine province'));
}
},
//find district
function(province, callback) {
getProvince(province.properties.TARGET, (err, data) => {
if(err) {
return callback(err);
}
let district = data.features.find((district) => {
let check = gju.pointInPolygon(
{"type":"Point","coordinates": [longitude, latitude]},
district.geometry
);
return check;
});
if(district){
callback(null, province, district);
} else {
callback(new Error('Could not determine District'));
}
});
},
//find local body
function(province, district, callback) {
let districtTarget = district.properties.TARGET.substring( 0, 1 ).toUpperCase()+district.properties.TARGET.substring( 1 ).toLowerCase();
getDistrict(districtTarget, (err, data) => {
let localBody = data.features.find((localBody) => {
let check = gju.pointInPolygon(
{"type":"Point","coordinates": [longitude, latitude]},
localBody.geometry
);
return check;
});
if(localBody){
callback(null, {province, district, localBody});
} else {
callback(new Error('Could not determine Local Body'));
}
});
}
], function (err, result) {
if(err) {
reject(err);
}
console.log(result.localBody)
result = {
province: result.province.properties.TARGET,
district: result.district.properties.TARGET,
localBody: result.localBody.properties.FIRST_GaPa
};
resolve(result);
});
});
}