-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmigration-import-regions.js
70 lines (66 loc) · 2.04 KB
/
migration-import-regions.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
const fs = require('fs')
const statesGeojsons = JSON.parse(fs.readFileSync('data/states.json', 'utf8'))
const municipalitiesGeojsons = JSON.parse(fs.readFileSync('data/municipalities.json', 'utf8'))
const MongoClient = require('mongodb').MongoClient
const cuid = require('cuid')
const turf = require('@turf/turf')
const URL_PREFIX = process.env.URL_PREFIX || 'http://localhost:3000'
const MONGO_URL = process.env.MONGO_URL || 'mongodb://localhost:27017'
const MONGO_DB_NAME = process.env.MONGO_DB_NAME || 'vientos-dev'
let codesMap = {}
function getCenter (geometry) {
let shape = geometry.type === 'Polygon'
? turf.polygon(geometry.coordinates)
: turf.multiPolygon(geometry.coordinates)
return turf.centroid(shape).geometry.coordinates
}
let states = statesGeojsons.features.map(feature => {
let url = `${URL_PREFIX}/places/${cuid()}`
let name = feature.properties.state_name
codesMap[feature.properties.state_code] = url
let [longitude, latitude] = getCenter(feature.geometry)
let bbox = turf.bbox(feature.geometry)
return {
_id: url,
type: 'Place',
name,
address: name,
latitude,
longitude,
bbox: {
west: bbox[0],
south: bbox[1],
east: bbox[2],
north: bbox[3]
},
level: 'state'
}
})
let municipalities = municipalitiesGeojsons.features.map(feature => {
let state = states.find(s => s._id === codesMap[feature.properties.state_code])
let name = feature.properties.mun_name
let [longitude, latitude] = getCenter(feature.geometry)
let bbox = turf.bbox(feature.geometry)
return {
_id: `${URL_PREFIX}/places/${cuid()}`,
type: 'Place',
name,
address: `${name}, ${state.name}`,
state: state._id,
latitude,
longitude,
bbox: {
west: bbox[0],
south: bbox[1],
east: bbox[2],
north: bbox[3]
},
level: 'municipality'
}
})
MongoClient.connect(MONGO_URL)
.then(client => {
client.db(MONGO_DB_NAME).collection('states').insert(states)
client.db(MONGO_DB_NAME).collection('municipalities').insert(municipalities)
client.close()
})