Browse Source

更正错别字

dev
CODE 1 year ago
parent
commit
371bfb5a40
  1. 4
      api/app/lib/controllers/report/index.js
  2. 110
      api/app/lib/models/village_distance.js
  3. 2
      scripts/1.4.0/data/1_calc_village_distance/Dockerfilenew
  4. 122
      scripts/1.4.0/data/1_calc_village_distance/index.js
  5. 4
      scripts/1.4.0/schema/2.create_village_table.sql

4
api/app/lib/controllers/report/index.js

@ -698,9 +698,9 @@ async function roadSpotPrepare (ctx) {
} }
const villageNearRes = await sequelize.query(` const villageNearRes = await sequelize.query(`
SELECT id,calc_village,diatance FROM village_distance SELECT id,calc_village,distance FROM village_distance
WHERE origin_village = ${spotFirstVillageId} WHERE origin_village = ${spotFirstVillageId}
ORDER BY diatance ASC ORDER BY distance ASC
`, { type: QueryTypes.SELECT }) `, { type: QueryTypes.SELECT })
let villageCheckIdList = villageNearRes.map(item => item.calc_village) let villageCheckIdList = villageNearRes.map(item => item.calc_village)

110
api/app/lib/models/village_distance.js

@ -2,59 +2,59 @@
'use strict'; 'use strict';
module.exports = dc => { module.exports = dc => {
const DataTypes = dc.ORM; const DataTypes = dc.ORM;
const sequelize = dc.orm; const sequelize = dc.orm;
const VillageDistance = sequelize.define("villageDistance", { const VillageDistance = sequelize.define("villageDistance", {
id: { id: {
type: DataTypes.INTEGER, type: DataTypes.INTEGER,
allowNull: false, allowNull: false,
defaultValue: null, defaultValue: null,
comment: null, comment: null,
primaryKey: true, primaryKey: true,
field: "id", field: "id",
autoIncrement: true, autoIncrement: true,
unique: "village_distance_id_uindex" unique: "village_distance_id_uindex"
}, },
originVillage: { originVillage: {
type: DataTypes.INTEGER, type: DataTypes.INTEGER,
allowNull: false, allowNull: false,
defaultValue: null, defaultValue: null,
comment: "原点村庄", comment: "原点村庄",
primaryKey: false, primaryKey: false,
field: "origin_village", field: "origin_village",
autoIncrement: false, autoIncrement: false,
references: { references: {
key: "id", key: "id",
model: "village" model: "village"
} }
}, },
calcVillage: { calcVillage: {
type: DataTypes.INTEGER, type: DataTypes.INTEGER,
allowNull: true, allowNull: true,
defaultValue: null, defaultValue: null,
comment: "相对计算村庄", comment: "相对计算村庄",
primaryKey: false, primaryKey: false,
field: "calc_village", field: "calc_village",
autoIncrement: false, autoIncrement: false,
references: { references: {
key: "id", key: "id",
model: "village" model: "village"
} }
}, },
diatance: { distance: {
type: DataTypes.INTEGER, type: DataTypes.INTEGER,
allowNull: true, allowNull: true,
defaultValue: null, defaultValue: null,
comment: "距离 m", comment: "距离 m",
primaryKey: false, primaryKey: false,
field: "diatance", field: "distance",
autoIncrement: false autoIncrement: false
} }
}, { }, {
tableName: "village_distance", tableName: "village_distance",
comment: "", comment: "",
indexes: [] indexes: []
}); });
dc.models.VillageDistance = VillageDistance; dc.models.VillageDistance = VillageDistance;
return VillageDistance; return VillageDistance;
}; };

2
scripts/1.4.0/data/1_calc_village_distance/Dockerfilenew

@ -1,5 +1,5 @@
FROM repository.anxinyun.cn/base-images/nodejs12:20.10.12.2 FROM repository.anxinyun.cn/base-images/nodejs12:20.10.12.2
COPY ./scripts/1.3.1/data/1_insert_report_data /var/app COPY ./scripts/1.4.0/data/1_calc_village_distance /var/app
WORKDIR /var/app WORKDIR /var/app
RUN npm cache clean -f RUN npm cache clean -f
RUN rm -rf package-lock.json RUN rm -rf package-lock.json

122
scripts/1.4.0/data/1_calc_village_distance/index.js

@ -11,6 +11,7 @@ try {
password: '123', password: '123',
database: 'highways4good', database: 'highways4good',
}) })
const godKey = '21c2d970e1646bb9a795900dd00093ce'
function getDistance (lat1, lon1, lat2, lon2) { function getDistance (lat1, lon1, lat2, lon2) {
var R = 6371000; // 半径 of the earth in meters var R = 6371000; // 半径 of the earth in meters
@ -44,60 +45,81 @@ try {
await client.query('BEGIN') await client.query('BEGIN')
console.log(`开始`); console.log(`开始`);
// 遍历南昌县所有村 const nanchangRes = await request.get(`https://restapi.amap.com/v3/config/district?key=${godKey}&keywords=360100&subdistrict=1`)
let pageNum = 1 console.log(nanchangRes.body);
let continueSearch = true
for (; continueSearch;) { let townshipList = nanchangRes.body.districts[0].districts
console.log(`查询第${pageNum}`); console.log(townshipList);
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`)
for (let t of townshipList) {
// console.log(res.body); console.log(`${t.name}`);
const data = res.body
console.log(`查得 ${data.pois.length}`); let pageNum = 1
if (data.count == 0) { let continueSearch = true
continueSearch = false for (; continueSearch;) {
break; console.log(`查询第${pageNum}`);
} else { // const res = await request.get(`https://restapi.amap.com/v5/place/text?key=${godKey}&types=130106&region=360000&citylimit=true&page_size=25&page_num=${pageNum}&extensions=all`)
pageNum++
} 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}`)
for await (let p of data.pois) {
if (p.name.includes('村委会') || p.name.includes('村民委员会')) { await new Promise(resolve => setTimeout(() => resolve(), 2000));
let vName = p.name.replace('村委会', '').replace('村民委员会', '').replace(/\(.*?\)/g, '')
// console.log(res.body);
if (vName.includes('乡')) { const data = res.body
let townKeyIndex = vName.indexOf('乡') console.log(`查得 ${data.pois.length}`);
if (townKeyIndex >= 0) { if (data.count == 0) {
vName = vName.substring(townKeyIndex + 1) 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('镇')) {
if (vName.includes('镇')) { let townKeyIndex = vName.indexOf('镇')
let townKeyIndex = vName.indexOf('镇') if (townKeyIndex >= 0) {
if (townKeyIndex >= 0) { vName = vName.substring(townKeyIndex + 1)
vName = vName.substring(townKeyIndex + 1) }
} }
}
if (!vName.endsWith('村')) { if (!vName.endsWith('村')) {
vName = vName + '村' vName = vName + '村'
} }
console.log(`${vName}`); // console.log(`${vName}`);
let locationArr = p.location.split(',') let locationArr = p.location.split(',')
// 查询镇名是否存在 // 查询镇名是否存在
const existRes = await client.query( let existRes = await client.query(
`SELECT * FROM village WHERE name=$1`, `SELECT * FROM village WHERE name=$1`,
[vName] [vName]
) )
if (existRes.rowCount == 0) {
existRes = await client.query(
`SELECT * FROM village WHERE name=$1`,
[vName + '委会']
)
}
if (existRes.rowCount == 0) { if (existRes.rowCount == 0) {
} else { } else {
let existV = existRes[0] let existV = existRes.rows[0]
await client.query( // console.log(existRes);
`UPDATE village SET longitude=$1, latitude=$2 WHERE id=$3`, await client.query(
[locationArr[0], locationArr[1], existV.id] `UPDATE village SET longitude=$1, latitude=$2 WHERE id=$3`,
) [locationArr[0], locationArr[1], existV.id]
)
}
} }
} }
} }
@ -121,7 +143,8 @@ try {
continue continue
} }
let distance = getDistance(v.latitude, v.longitude, vv.latitude, vv.longitude) 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) distance = Math.round(distance)
const existRes = await client.query( const existRes = await client.query(
@ -134,8 +157,9 @@ try {
[v.id, vv.id, distance] [v.id, vv.id, distance]
) )
} else { } else {
let upSql = `UPDATE village_distance SET distance=$1 WHERE origin_village=$2 AND calc_village=$3`
await client.query( await client.query(
`UPDATE village_distance SET distance=$1 WHERE origin_village=$2 AND calc_village=$` upSql
, [distance, v.id, vv.id] , [distance, v.id, vv.id]
) )
} }

4
scripts/1.4.0/schema/2.create_village_table.sql

@ -34,7 +34,7 @@ create table if not exists village_distance
calc_village integer calc_village integer
constraint village_distance_village_id_fk_2 constraint village_distance_village_id_fk_2
references village, references village,
diatance integer distance integer
); );
comment on table village_distance is '村之间的距离'; comment on table village_distance is '村之间的距离';
@ -43,7 +43,7 @@ comment on column village_distance.origin_village is '原点村庄';
comment on column village_distance.calc_village is '相对计算村庄'; comment on column village_distance.calc_village is '相对计算村庄';
comment on column village_distance.diatance is '距离 m'; comment on column village_distance.distance is '距离 m';
create unique index if not exists village_distance_id_uindex create unique index if not exists village_distance_id_uindex
on village_distance (id); on village_distance (id);

Loading…
Cancel
Save