-
Notifications
You must be signed in to change notification settings - Fork 16
/
index.js
162 lines (160 loc) · 5.88 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
const fs = require('fs');
const turf = require('@turf/union');
const GeojsonMinifier = require('geojson-minifier');
const NEPAL_GEOJSON = JSON.parse(
fs.readFileSync(`${__dirname}/assets/nepal.geojson.packed`),
);
const PROVINCE_DETAILS = JSON.parse(
fs.readFileSync(`${__dirname}/assets/province-details.json`),
).provinces;
const minifyr = new GeojsonMinifier({ precision: 6 });
const unpackedCoordinates = JSON.parse(minifyr.unpack(NEPAL_GEOJSON));
const numberOfDistricts = unpackedCoordinates.features.length;
const roundOff = value => Math.floor(value * 1000000) / 1000000;
for (let i = 0; i < numberOfDistricts; i += 1) {
unpackedCoordinates.features[i].geometry.coordinates[0][0][0] = roundOff(
unpackedCoordinates.features[i].geometry.coordinates[0][0][0],
);
unpackedCoordinates.features[i].geometry.coordinates[0][0][1] = roundOff(
unpackedCoordinates.features[i].geometry.coordinates[0][0][1],
);
}
const isInvalidProvinceInput = (provinceNo) => {
const prNo = Number(provinceNo);
if (!prNo) {
return true;
}
if (prNo < 1 || prNo > 7) {
return true;
}
return false;
};
const nepalGeojson = {
districts() {
/** get all districts geojson */
return unpackedCoordinates;
},
district(districtName) {
/** get desired district geojson */
const districts = this.districts().features;
if (districts.findIndex(
district => district.properties.DISTRICT === districtName.toUpperCase(),
) === -1) {
throw new Error(`District doesn't exist. Valid values are ${districts.map(district => district.properties.DISTRICT).join(', ')}`);
}
return {
type: 'FeatureCollection',
features: [
districts.find(
district => district.properties.DISTRICT === districtName.toUpperCase(),
),
],
};
},
province(provinceNo) {
/** get bounding coordinates of a province without district level boundaries */
const districts = this.districts().features;
const provinceDetails = Object.assign(districts
.filter(district => district.properties.PROVINCE === provinceNo)
.reduce((acc, district) => turf.default(acc, district), {
type: 'Feature',
properties: {},
geometry: { type: 'Polygon', coordinates: [] },
}), { properties: PROVINCE_DETAILS[provinceNo - 1] });
provinceDetails.geometry.coordinates = [provinceDetails.geometry.coordinates[0]];
return provinceDetails;
},
provinceWithDistricts(provinceNo) {
/** get bounding coordinates of a province with district level boundaries */
if (isInvalidProvinceInput(provinceNo)) {
throw new Error('Province should be between 1 and 7');
}
const districts = this.districts().features;
return {
type: 'FeatureCollection',
features: districts.filter(district => district.properties.PROVINCE === provinceNo),
};
},
country() {
/** get bounding coordinates of Nepal without province or district level boundaries */
const districts = this.districts().features;
const countryBoundaries = districts
.reduce((acc, district) => turf.default(acc, district), {
type: 'Feature',
properties: {},
geometry: { type: 'Polygon', coordinates: [] },
});
countryBoundaries.geometry.coordinates = [countryBoundaries.geometry.coordinates[0]];
return countryBoundaries;
},
countryWithProvinces() {
/** get bounding coordinates of Nepal with province level boundaries */
const districts = this.districts().features;
const provinces = [[], [], [], [], [], [], []];
districts.map(district => provinces[district.properties.PROVINCE - 1].push(district));
return {
type: 'FeatureCollection',
features: [
...provinces.map((province, idx) => {
const provinceDetails = Object.assign(province.reduce(
(acc, district) => turf.default(acc, district), {
type: 'Feature',
properties: {},
geometry: { type: 'Polygon', coordinates: [] },
},
), { properties: PROVINCE_DETAILS[idx] });
provinceDetails.geometry.coordinates = [provinceDetails.geometry.coordinates[0]];
return provinceDetails;
}),
],
};
},
countryWithDistricts() {
/** get bounding coordinates of Nepal with district level boundaries */
return unpackedCoordinates;
},
districtsInfo() {
/** get information regarding all districts */
const districts = this.districts().features;
return districts.map(district => ({
district: district.properties.DISTRICT,
headquarter: district.properties.HQ,
province: district.properties.PROVINCE,
}));
},
districtInfo(districtName) {
/** get information regarding selected district */
return this.districtsInfo().find(
place => place.district === districtName.toUpperCase(),
);
},
listDistricts() {
/** get all district names */
const districts = this.districts().features;
return districts.map(district => district.properties.DISTRICT);
},
listProvincesWithDistricts() {
/** list all provinces details with associated districts details */
const districts = this.districts().features;
const provinces = [[], [], [], [], [], [], []];
districts.map(district => provinces[district.properties.PROVINCE - 1].push({
district: district.properties.DISTRICT,
hq: district.properties.HQ,
}));
return provinces.map((provinceDistricts, pn) => ({
province: pn + 1,
details: PROVINCE_DETAILS[pn],
districts: provinceDistricts,
}));
},
listProvinceWithDistricts(provinceNumber) {
if (isInvalidProvinceInput(provinceNumber)) {
throw new Error('Province should be between 1 and 7');
}
/** list selected provinces details with associated districts details */
return this.listProvincesWithDistricts().find(
province => province.province === provinceNumber,
);
},
};
module.exports = nepalGeojson;