-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate-grid.js
95 lines (78 loc) · 2.98 KB
/
create-grid.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
import { readFileSync, writeFile } from "fs";
import { resolve } from "path";
const CELL_SIZE = 0.1; // Define the grid cell size in degrees
// Helper function to get the grid cell index for a given latitude and longitude
function getGridCell(lat, lon) {
const latIndex = Math.floor(lat / CELL_SIZE);
const lonIndex = Math.floor(lon / CELL_SIZE);
return `${latIndex},${lonIndex}`;
}
// Parse the TSV file
async function parseTSVFile(filePath) {
const grid = {};
const fileContent = readFileSync(filePath, "utf-8");
const lines = fileContent.split("\n");
lines.forEach((line, lineNumber) => {
if (lineNumber === 0 || line.trim() === "") return; // skip header line
const [, , , latitude, longitude] = line.split("\t");
const lat = parseFloat(latitude);
const lon = parseFloat(longitude);
const cell = getGridCell(lat, lon);
if (!grid[cell]) grid[cell] = [];
grid[cell].push([lat, lon, lineNumber]);
});
return grid;
}
function writeGridToJSON(grid, outputFilePath) {
return new Promise((resolve, reject) => {
writeFile(outputFilePath, JSON.stringify(grid), "utf-8", (err) => {
if (err) reject(err);
else resolve(outputFilePath);
});
});
}
// Calculate the Euclidean distance between two points (lat1, lon1) and (lat2, lon2)
function calculateDistance(lat1, lon1, lat2, lon2) {
return Math.sqrt(Math.pow(lat1 - lat2, 2) + Math.pow(lon1 - lon2, 2));
}
// Find the top 10 closest settlements
function findClosestSettlements(grid, targetLat, targetLon, k = 10) {
const targetCell = getGridCell(targetLat, targetLon);
const neighbors = [];
const [latIndex, lonIndex] = targetCell.split(",").map(Number);
// Check the current cell and its neighbors
for (let i = latIndex - 1; i <= latIndex + 1; i++) {
for (let j = lonIndex - 1; j <= lonIndex + 1; j++) {
const cellKey = `${i},${j}`;
if (grid[cellKey]) {
for (const settlement of grid[cellKey]) {
const distance = calculateDistance(
targetLat,
targetLon,
settlement[0],
settlement[1]
);
neighbors.push({ ...settlement, distance });
}
}
}
}
// Sort by distance and return the top k results
neighbors.sort((a, b) => a.distance - b.distance);
return neighbors.slice(0, k);
}
// Main function
async function main() {
const filePath = resolve("generated_data/db.tsv");
const t1 = new Date().getTime();
const grid = await parseTSVFile(filePath);
const t2 = new Date().getTime();
console.log("parse TSV in ", t2 - t1, " ms");
const outputFilePath = resolve("generated_data/grid.json");
await writeGridToJSON(grid, outputFilePath);
// const targetLat = 40.02106; // Replace with your target latitude
// const targetLon = 32.83102; // Replace with your target longitude
// const closestSettlements = findClosestSettlements(grid, targetLat, targetLon);
// console.log("Top 10 closest settlements:", closestSettlements);
}
main().catch((err) => console.error(err));