-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #170 from etalab/communes-associees-deleguees
Communes associees deleguees
- Loading branch information
Showing
10 changed files
with
391 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
const {join} = require('path') | ||
const {keyBy} = require('lodash') | ||
const communes = require('@etalab/decoupage-administratif/data/communes.json') | ||
const epci = require('@etalab/decoupage-administratif/data/epci.json') | ||
const area = require('@turf/area').default | ||
const pointOnFeature = require('@turf/point-on-feature').default | ||
const truncate = require('@turf/truncate').default | ||
const bbox = require('@turf/bbox').default | ||
const bboxPolygon = require('@turf/bbox-polygon').default | ||
const {readGeoJSONFeatures, writeData, fixPrecision} = require('./util') | ||
|
||
const resolution = process.env.BUILD_LOW_RESOLUTION === '1' ? '50m' : '5m' | ||
|
||
const COMMUNES_ASSOCIEES_DELEGUEES_FEATURES_PATH = join(__dirname, '..', 'data', `communes-associees-deleguees-${resolution}.geojson.gz`) | ||
|
||
const COMMUNES_EPCI_MATCHING = epci.reduce((acc, curr) => { | ||
curr.membres.forEach(membre => { | ||
acc[membre.code] = curr.code | ||
}) | ||
|
||
return acc | ||
}, {}) | ||
|
||
async function buildCommunesAssocieesDeleguees() { | ||
const communesAssocieesDelegueesFeatures = await readGeoJSONFeatures(COMMUNES_ASSOCIEES_DELEGUEES_FEATURES_PATH) | ||
const communesAssocieesDelegueesFeaturesIndex = keyBy(communesAssocieesDelegueesFeatures, f => f.properties.code) | ||
|
||
const communesAssocieesDelegueesData = communes | ||
.filter(commune => { | ||
return ['commune-associee', 'commune-deleguee'].includes(commune.type) | ||
}) | ||
.map(commune => { | ||
const communeData = { | ||
code: commune.code, | ||
type: commune.type, | ||
nom: commune.nom, | ||
chefLieu: commune.chefLieu, | ||
codeDepartement: commune.departement, | ||
codeRegion: commune.region | ||
} | ||
|
||
if (commune.code in communesAssocieesDelegueesFeaturesIndex) { | ||
const contour = communesAssocieesDelegueesFeaturesIndex[commune.code].geometry | ||
communeData.contour = contour | ||
communeData.surface = fixPrecision(area(contour) / 10000, 2) | ||
communeData.centre = truncate(pointOnFeature(contour), {precision: 4}).geometry | ||
const bboxCommune = bbox(communesAssocieesDelegueesFeaturesIndex[commune.code]) | ||
const bboxPolygonCommune = bboxPolygon(bboxCommune) | ||
communeData.bbox = bboxPolygonCommune.geometry | ||
} | ||
|
||
if (commune.chefLieu in COMMUNES_EPCI_MATCHING) { | ||
communeData.codeEpci = COMMUNES_EPCI_MATCHING[commune.chefLieu] | ||
} | ||
|
||
return communeData | ||
}) | ||
|
||
await writeData('communes-associees-deleguees', communesAssocieesDelegueesData) | ||
} | ||
|
||
module.exports = {buildCommunesAssocieesDeleguees} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
const {initFields, initFormat} = require('./helpers') | ||
|
||
const initCommunesAssocieeDelegueeFields = initFields({ | ||
default: ['nom', 'code', 'type', 'chefLieu', 'codeDepartement', 'codeEpci', 'codeRegion', 'codesPostaux'], | ||
base: ['nom', 'code'] | ||
}) | ||
|
||
const initCommuneAssocieeDelegueeFormat = initFormat({ | ||
geometries: ['centre', 'contour', 'bbox'], | ||
defaultGeometry: 'centre' | ||
}) | ||
|
||
const communesAssocieesDelegueesDefaultQuery = {type: ['commune-associee', 'commune-deleguee']} | ||
|
||
module.exports = {initCommunesAssocieeDelegueeFields, initCommuneAssocieeDelegueeFormat, communesAssocieesDelegueesDefaultQuery} |
Oops, something went wrong.