|
|
|
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',
|
|
|
|
})
|
|
|
|
const godKey = '21c2d970e1646bb9a795900dd00093ce'
|
|
|
|
|
|
|
|
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 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(`开始`);
|
|
|
|
|
|
|
|
// const noTownshipLoop = false
|
|
|
|
const noTownshipLoop = true
|
|
|
|
|
|
|
|
// // 查南昌市下的区县
|
|
|
|
// const nanchangRes =
|
|
|
|
// noTownshipLoop ? [] : await request.get(`https://restapi.amap.com/v3/config/district?key=${godKey}&keywords=360100&subdistrict=1`)
|
|
|
|
|
|
|
|
// let townshipList =
|
|
|
|
// noTownshipLoop ? [] : nanchangRes.body.districts[0].districts
|
|
|
|
|
|
|
|
// // 获取各村位置
|
|
|
|
|
|
|
|
// for (let t of (noTownshipLoop ? [{}] : townshipList)) {
|
|
|
|
// console.log(`${t.name}`);
|
|
|
|
|
|
|
|
// // 划分 20 个矩形查询
|
|
|
|
// let leftTop = [115.792048, 28.976526]
|
|
|
|
// let rightBottom = [116.324981, 28.262191]
|
|
|
|
|
|
|
|
// for (let i = 0; i < 5; i++) { // 纵向
|
|
|
|
|
|
|
|
// let top = leftTop[1] + (rightBottom[1] - leftTop[1]) / 5 * i
|
|
|
|
// let bottom = leftTop[1] + (rightBottom[1] - leftTop[1]) / 5 * (i + 1)
|
|
|
|
|
|
|
|
// for (let j = 0; j < 5; j++) { // 横向
|
|
|
|
// let left = leftTop[0] + (rightBottom[0] - leftTop[0]) / 5 * j
|
|
|
|
// let right = leftTop[0] + (rightBottom[0] - leftTop[0]) / 5 * (j + 1)
|
|
|
|
|
|
|
|
// console.log(`矩形:${left},${top},${right},${bottom}`);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// let pageNum = 1
|
|
|
|
// let continueSearch = true
|
|
|
|
// for (; continueSearch;) {
|
|
|
|
// console.log(`查询第${pageNum}页`);
|
|
|
|
// let pageSize = 10
|
|
|
|
// /** 关键词查询 */
|
|
|
|
// // const res = await request.get(`https://restapi.amap.com/v5/place/text?key=${godKey}&types=130106®ion=360000&citylimit=true&page_size=25&page_num=${pageNum}&extensions=all`)
|
|
|
|
|
|
|
|
// /** 区县查询 */
|
|
|
|
// // const res = await request.get(`https://restapi.amap.com/v3/place/text?key=${godKey}&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&page_size=25&types=130106&citylimit=true&city=${t.adcode}&page=${pageNum}`)
|
|
|
|
|
|
|
|
// /** 矩形查询 */
|
|
|
|
// pageSize = 50
|
|
|
|
// // const res = await request.get(`https://restapi.amap.com/v3/place/polygon?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&offset=${pageSize}&page=${pageNum}&polygon=${left},${top}|${right},${bottom}`)
|
|
|
|
|
|
|
|
// /** 矩形查询 V2 */
|
|
|
|
// pageSize = 25
|
|
|
|
// const res = await request.get(`https://restapi.amap.com/v5/place/polygon?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&page_size=${pageSize}&page_num=${pageNum}&polygon=${left},${top}|${right},${bottom}`)
|
|
|
|
|
|
|
|
// await new Promise(resolve => setTimeout(() => resolve(), 2000));
|
|
|
|
|
|
|
|
// // console.log(res.body);
|
|
|
|
// const data = res.body
|
|
|
|
// // console.log(data);
|
|
|
|
// console.log(`查得 ${data.pois.length} 条`);
|
|
|
|
// if (data.count == 0) {
|
|
|
|
// continueSearch = false
|
|
|
|
// break;
|
|
|
|
// } else {
|
|
|
|
// pageNum++
|
|
|
|
// }
|
|
|
|
// for await (let p of data.pois) {
|
|
|
|
// console.log(p.name);
|
|
|
|
// 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(',')
|
|
|
|
|
|
|
|
// // 查询镇名是否存在
|
|
|
|
// let existRes = await client.query(
|
|
|
|
// `SELECT * FROM village WHERE name=$1`,
|
|
|
|
// [vName]
|
|
|
|
// )
|
|
|
|
// if (existRes.rowCount == 0) {
|
|
|
|
// existRes = await client.query(
|
|
|
|
// `SELECT * FROM village WHERE name=$1`,
|
|
|
|
// [vName + '委会']
|
|
|
|
// )
|
|
|
|
// }
|
|
|
|
|
|
|
|
// if (existRes.rowCount == 0) {
|
|
|
|
|
|
|
|
// } else {
|
|
|
|
// let existV = existRes.rows[0]
|
|
|
|
|
|
|
|
// if (existV.id < 219) {
|
|
|
|
// continue
|
|
|
|
// }
|
|
|
|
// // console.log(existRes);
|
|
|
|
// await client.query(
|
|
|
|
// `UPDATE village SET longitude=$1, latitude=$2 WHERE id=$3`,
|
|
|
|
// [locationArr[0], locationArr[1], existV.id]
|
|
|
|
// )
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
// if (data.pois.length < pageSize) {
|
|
|
|
// continueSearch = false
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
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 {
|
|
|
|
if (!noTownshipLoop) {
|
|
|
|
let upSql = `UPDATE village_distance SET distance=$1 WHERE origin_village=$2 AND calc_village=$3`
|
|
|
|
await client.query(
|
|
|
|
upSql
|
|
|
|
, [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)
|
|
|
|
}
|