Skip to main content
100%

Code pour passer des id de parcelles Etalab en commune, prefixe, section, numero et inversement

✓ Published0🌍 Public
TThomasG77
Last edited Nov 10, 2023
Created on Nov 10, 2023

This example demonstrates how to convert between French land parcel identifiers from the Etalab system and their constituent components: municipality code, prefix, section, and number. It shows the parsing of a composite key, such as `323190000A0133`, into its semantic parts, and the reverse operation to reconstruct the identifier. Implemented in both JavaScript and Python, the code uses basic string manipulation methods including `substring`, `replace` with a regular expression, `padStart`, and `zfill` for handling leading zeros. The JavaScript version runs directly in the console, while the Python version uses f-string formatting for output.

AI-generated description

Code pour passer des id de parcelles Etalab en commune, prefixe, section, numero et inversement

Par exemple, pour l'id de parcelle 323190000A0133

Il s'agit d'un clé composite (une clé constituée de plusieurs clés)

  • 5 premiers caractères = code INSEE sur 5 caractères soit 32319
  • 3 caractères suivants = prefixe, ancien code INSEE sur 3 caractères (on ne mentionne pas à nouveau le département). 000 si inchangé
  • 2 caractères suivants = lettre ou numéro de section préfixé de 0 si une seul caractère. Ainsi A deviens 0A
  • 4 derniers caractères = numéro de parcelle dans la section préfixé de 0 si nécessaire 133 devant 0133

Javascript

function removeLeadingZeros(myString) {
  let regex = /^0+/g;
  return myString.replace(regex, "");
}

let id_parcel = '323190000A0133'

let commune = id_parcel.substring(0, 5)
let prefixe = id_parcel.substring(5, 8)
let section = removeLeadingZeros(id_parcel.substring(8, 10))
let numero = removeLeadingZeros(id_parcel.substring(10, 14))

console.log(commune, prefixe, section, numero)
// Pour revenir à l'id de parcelle
console.log(`${commune}${prefixe}${section.padStart(2, '0')}${numero.padStart(4, '0')}`)

Python

id_parcel = '323190000A0133'

commune = id_parcel[0:5]
prefixe = id_parcel[5:8]
section = id_parcel[8:10].lstrip('0')
numero = id_parcel[10:14].lstrip('0')

print(commune, prefixe, section, numero)

# Pour revenir à l'id de parcelle
print(f'{commune}{prefixe}{section.zfill(2)}{numero.zfill(4)}')

Similar vizzes