四好公路
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

162 lines
6.3 KiB

try {
const { Pool, Client } = require('pg')
const request = require('superagent')
const path = require('path')
const fs = require("fs");
const pool = new Pool({
host: '10.8.30.32',
port: 5432,
user: 'postgres',
password: '123',
database: 'highways4good',
})
function getDistance (lat1, lon1, lat2, lon2) {
var R = 6371000; // 半径 of the earth in meters
var dLat = deg2rad(lat2 - lat1); // deg2rad below
var dLon = deg2rad(lon2 - lon1);
var a =
Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) *
Math.sin(dLon / 2) * Math.sin(dLon / 2)
;
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
var d = R * c; // Distance in meters
return d;
}
function deg2rad (deg) {
return deg * (Math.PI / 180)
}
// 两个经纬度
let coord1 = { latitude: 31.2304, longitude: 121.4737 }; // 上海
let coord2 = { latitude: 30.5685, longitude: 114.3055 }; // 武汉
// let distance = getDistance(coord1.latitude, coord1.longitude, coord2.latitude, coord2.longitude);
const fun = async () => {
// note: we don't try/catch this because if connecting throws an exception
// we don't need to dispose of the client (it will be undefined)
const client = await pool.connect()
try {
await client.query('BEGIN')
console.log(`开始`);
// 遍历南昌县所有村
let pageNum = 1
let continueSearch = true
for (; continueSearch;) {
console.log(`查询第${pageNum}`);
const res = await request.get(`https://restapi.amap.com/v5/place/text?key=21c2d970e1646bb9a795900dd00093ce&keywords=%E6%9D%91%E5%A7%94%E4%BC%9A%7C%E6%9D%91%E6%B0%91%E5%A7%94%E5%91%98%E4%BC%9A&types=130106&region=360121&citylimit=true&page_size=25&page_num=${pageNum}&extensions=all`)
// console.log(res.body);
const data = res.body
console.log(`查得 ${data.pois.length}`);
if (data.count == 0) {
continueSearch = false
break;
} else {
pageNum++
}
for await (let p of data.pois) {
if (p.name.includes('村委会') || p.name.includes('村民委员会')) {
let vName = p.name.replace('村委会', '').replace('村民委员会', '').replace(/\(.*?\)/g, '')
if (vName.includes('乡')) {
let townKeyIndex = vName.indexOf('乡')
if (townKeyIndex >= 0) {
vName = vName.substring(townKeyIndex + 1)
}
}
if (vName.includes('镇')) {
let townKeyIndex = vName.indexOf('镇')
if (townKeyIndex >= 0) {
vName = vName.substring(townKeyIndex + 1)
}
}
if (!vName.endsWith('村')) {
vName = vName + '村'
}
console.log(`${vName}`);
let locationArr = p.location.split(',')
// 查询镇名是否存在
const existRes = await client.query(
`SELECT * FROM village WHERE name=$1`,
[vName]
)
if (existRes.rowCount == 0) {
} else {
let existV = existRes[0]
await client.query(
`UPDATE village SET longitude=$1, latitude=$2 WHERE id=$3`,
[locationArr[0], locationArr[1], existV.id]
)
}
}
}
}
const allVillagesRes = await client.query(
`SELECT * FROM village`
)
const allVillages = allVillagesRes.rows
for (let v of allVillages) {
if (!v.longitude || !v.latitude) {
continue
}
for (let vv of allVillages) {
if (
v.id != vv.id
// 只计算一个乡镇内的村
&& v.township_code == vv.township_code
) {
if (!vv.longitude || !vv.latitude) {
continue
}
let distance = getDistance(v.latitude, v.longitude, vv.latitude, vv.longitude)
console.log(`${v.name}${vv.name} 的距离为 ${distance}`);
distance = Math.round(distance)
const existRes = await client.query(
`SELECT * FROM village_distance WHERE origin_village=$1 AND calc_village=$2`,
[v.id, vv.id]
)
if (existRes.rowCount == 0) {
await client.query(
`INSERT INTO village_distance (origin_village, calc_village, distance) VALUES ($1, $2, $3)`,
[v.id, vv.id, distance]
)
} else {
await client.query(
`UPDATE village_distance SET distance=$1 WHERE origin_village=$2 AND calc_village=$`
, [distance, v.id, vv.id]
)
}
}
}
}
// await client.query('ROLLBACK')
await client.query('COMMIT')
console.log('执行完毕~')
} catch (e) {
await client.query('ROLLBACK')
console.log('执行错误~')
throw e
} finally {
client.release();
}
}
fun()
} catch (error) {
console.error(error)
}