Skip to content

Commit

Permalink
Merge pull request #160 from etalab/feat-mairie-bbox
Browse files Browse the repository at this point in the history
Add mairie and bbox output
  • Loading branch information
ThomasG77 authored Aug 18, 2022
2 parents e9fcbc9 + d64f0f9 commit e0e0746
Show file tree
Hide file tree
Showing 6 changed files with 645 additions and 613 deletions.
14 changes: 14 additions & 0 deletions build/communes.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,22 @@ const communes = require('@etalab/decoupage-administratif/data/communes.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_FEATURES_PATH = join(__dirname, '..', 'data', `communes-${resolution}.geojson.gz`)
const COMMUNES_FEATURES_MAIRIE_PATH = join(__dirname, '..', 'data', 'chflieux-communes-arrondissements-municipaux.geojson.gz')

const MORTES_POUR_LA_FRANCE = ['55189', '55039', '55050', '55239', '55307', '55139']

async function buildCommunes() {
const communesFeatures = await readGeoJSONFeatures(COMMUNES_FEATURES_PATH)
const communesFeaturesIndex = keyBy(communesFeatures, f => f.properties.code)
const communesFeaturesMairie = await readGeoJSONFeatures(COMMUNES_FEATURES_MAIRIE_PATH)
const communesFeaturesMairieIndex = keyBy(communesFeaturesMairie, f => f.properties.insee_com)

const communesData = communes
.filter(commune => {
Expand All @@ -37,6 +42,15 @@ async function buildCommunes() {
communeData.contour = contour
communeData.surface = fixPrecision(area(contour) / 10000, 2)
communeData.centre = truncate(pointOnFeature(contour), {precision: 4}).geometry
const bboxCommune = bbox(communesFeaturesIndex[commune.code])
const bboxPolygonCommune = bboxPolygon(bboxCommune)
communeData.bbox = bboxPolygonCommune.geometry
if (commune.code in communesFeaturesMairieIndex) {
communeData.mairie = truncate(communesFeaturesMairieIndex[commune.code], {precision: 4}).geometry
} else {
console.log('Pas de mairie associée à', commune.code)
communeData.mairie = communeData.centre
}
}

if (MORTES_POUR_LA_FRANCE.includes(commune.code)) {
Expand Down
10 changes: 9 additions & 1 deletion definition.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ parameters:
- centre
- surface
- contour
- mairie
- bbox
- codeDepartement
- departement
- codeRegion
Expand Down Expand Up @@ -78,7 +80,7 @@ parameters:
in: query
description: Géométrie à utiliser pour la sortie géographique
type: string
enum: ['centre', 'contour']
enum: ['centre', 'contour', 'mairie', 'bbox']
default: centre
limitParam:
name: limit
Expand Down Expand Up @@ -388,6 +390,12 @@ definitions:
contour:
type: object
description: Contour de la commune (Polygon GeoJSON)
mairie:
type: object
description: Mairie principale de la commune (Point GeoJSON). Pour les COM et les communes mortes pour la France, on retourne le centre.
bbox:
type: object
description: Rectangle englobant la commune (Polygon GeoJSON)
Departement:
type: object
properties:
Expand Down
1 change: 1 addition & 0 deletions download-sources.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ mkdir -p data
echo "Retrieve datasets"
wget -N -P data/ http://etalab-datasets.geo.data.gouv.fr/contours-administratifs/2022/geojson/communes-5m.geojson.gz
wget -N -P data/ http://etalab-datasets.geo.data.gouv.fr/contours-administratifs/2022/geojson/communes-50m.geojson.gz
wget -N -P data/ http://etalab-datasets.geo.data.gouv.fr/contours-administratifs/2022/geojson/chflieux-communes-arrondissements-municipaux.geojson.gz
echo "Completed"
2 changes: 1 addition & 1 deletion lib/communeHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const initCommuneFields = initFields({
})

const initCommuneFormat = initFormat({
geometries: ['centre', 'contour'],
geometries: ['centre', 'contour', 'mairie', 'bbox'],
defaultGeometry: 'centre'
})

Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
"access": "public"
},
"dependencies": {
"@turf/bbox": "^6.5.0",
"@turf/bbox-polygon": "^6.5.0",
"cors": "^2.8.5",
"express": "^4.17.1",
"lodash": "^4.17.11",
Expand Down
Loading

0 comments on commit e0e0746

Please sign in to comment.