|
|
@ -11,6 +11,7 @@ try { |
|
|
|
password: '123', |
|
|
|
database: 'highways4good', |
|
|
|
}) |
|
|
|
const godKey = '21c2d970e1646bb9a795900dd00093ce' |
|
|
|
|
|
|
|
function getDistance (lat1, lon1, lat2, lon2) { |
|
|
|
var R = 6371000; // 半径 of the earth in meters
|
|
|
@ -44,12 +45,24 @@ try { |
|
|
|
await client.query('BEGIN') |
|
|
|
console.log(`开始`); |
|
|
|
|
|
|
|
// 遍历南昌县所有村
|
|
|
|
const nanchangRes = await request.get(`https://restapi.amap.com/v3/config/district?key=${godKey}&keywords=360100&subdistrict=1`) |
|
|
|
console.log(nanchangRes.body); |
|
|
|
|
|
|
|
let townshipList = nanchangRes.body.districts[0].districts |
|
|
|
console.log(townshipList); |
|
|
|
|
|
|
|
for (let t of townshipList) { |
|
|
|
console.log(`${t.name}`); |
|
|
|
|
|
|
|
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®ion=360121&citylimit=true&page_size=25&page_num=${pageNum}&extensions=all`) |
|
|
|
// 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}`) |
|
|
|
|
|
|
|
await new Promise(resolve => setTimeout(() => resolve(), 2000)); |
|
|
|
|
|
|
|
// console.log(res.body);
|
|
|
|
const data = res.body |
|
|
@ -61,6 +74,7 @@ try { |
|
|
|
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, '') |
|
|
|
|
|
|
@ -81,19 +95,26 @@ try { |
|
|
|
vName = vName + '村' |
|
|
|
} |
|
|
|
|
|
|
|
console.log(`${vName}`); |
|
|
|
// console.log(`${vName}`);
|
|
|
|
let locationArr = p.location.split(',') |
|
|
|
|
|
|
|
// 查询镇名是否存在
|
|
|
|
const existRes = await client.query( |
|
|
|
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[0] |
|
|
|
let existV = existRes.rows[0] |
|
|
|
// console.log(existRes);
|
|
|
|
await client.query( |
|
|
|
`UPDATE village SET longitude=$1, latitude=$2 WHERE id=$3`, |
|
|
|
[locationArr[0], locationArr[1], existV.id] |
|
|
@ -102,6 +123,7 @@ try { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
const allVillagesRes = await client.query( |
|
|
|
`SELECT * FROM village` |
|
|
@ -121,7 +143,8 @@ try { |
|
|
|
continue |
|
|
|
} |
|
|
|
let distance = getDistance(v.latitude, v.longitude, vv.latitude, vv.longitude) |
|
|
|
console.log(`${v.name} 与 ${vv.name} 的距离为 ${distance} 米`); |
|
|
|
|
|
|
|
// console.log(`${v.name} 与 ${vv.name} 的距离为 ${distance} 米`);
|
|
|
|
|
|
|
|
distance = Math.round(distance) |
|
|
|
const existRes = await client.query( |
|
|
@ -134,8 +157,9 @@ try { |
|
|
|
[v.id, vv.id, distance] |
|
|
|
) |
|
|
|
} else { |
|
|
|
let upSql = `UPDATE village_distance SET distance=$1 WHERE origin_village=$2 AND calc_village=$3` |
|
|
|
await client.query( |
|
|
|
`UPDATE village_distance SET distance=$1 WHERE origin_village=$2 AND calc_village=$` |
|
|
|
upSql |
|
|
|
, [distance, v.id, vv.id] |
|
|
|
) |
|
|
|
} |
|
|
|