-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
50 lines (42 loc) · 1.52 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
const transformation = require('transform-coordinates');
const fs = require('fs');
// import geojson file
const fileName = "KyleCityLimits";
const geojson = require(`./${fileName}.json`);
// Set from and to coordinate systems, WGS 84 (EPSG:4326) is standard
const transform = transformation('EPSG:2278', '4326');
function replaceCoordinates(coordinateSet) {
return coordinateSet.map(coordinatePair => {
let lat = coordinatePair[0];
let lng = coordinatePair[1];
let transformedCoordinate = transform.forward({ x: lat, y: lng });
let newCoord = [transformedCoordinate.x, transformedCoordinate.y];
return newCoord;
});
}
function convertCoordinates(geojson) {
delete geojson.crs;
geojson.features.forEach(feature => {
let {coordinates, type} = feature.geometry;
if (type === "Polygon") {
coordinates.forEach((coordinateSet, index) => {
coordinates[index] = replaceCoordinates(coordinateSet);
})
} else if (type === "MultiPolygon") {
coordinates.forEach(polygonCoordinates => {
polygonCoordinates.forEach((coordinateSet, index) => {
polygonCoordinates[index] = replaceCoordinates(coordinateSet);
});
})
}
});
}
convertCoordinates(geojson)
const jsonContent = JSON.stringify(geojson);
fs.writeFile(`${fileName}-converted.json`, jsonContent, 'utf8', function (err) {
if (err) {
console.log("An error occured while writing JSON Object to File.");
return console.log(err);
}
console.log("JSON file has been saved.");
});