Page MenuHomeDevCentral

D2379.diff
No OneTemporary

D2379.diff

diff --git a/.gitignore b/.gitignore
--- a/.gitignore
+++ b/.gitignore
@@ -1 +1,2 @@
fantoir-doc/nature_voie.json
+fantoir-data/
diff --git a/Makefile b/Makefile
--- a/Makefile
+++ b/Makefile
@@ -1,9 +1,20 @@
RM=rm -f
-all: fantoir-doc/nature_voie.json
+FANTOIR_URL=https://www.data.gouv.fr/fr/datasets/r/66a699c0-90c4-4396-b738-cd02ee52e0b1
+FANTOIR_VERSION=FANTOIR1020
+
+all: fantoir-doc/nature_voie.json fantoir-data/fantoir.json
fantoir-doc/nature_voie.json: fantoir-doc/nature_voie.dat
utils/parse-kv-spaces.py fantoir-doc/nature_voie.dat > fantoir-doc/nature_voie.json
+fantoir-data/${FANTOIR_VERSION}:
+ wget -O /tmp/fantoir.zip ${FANTOIR_URL}
+ mkdir -p fantoir-data && cd fantoir-data && unzip /tmp/fantoir.zip
+ ${RM} /tmp/fantoir.zip
+
+fantoir-data/fantoir.json: fantoir-data/${FANTOIR_VERSION}
+ utils/parse-fantoir.py fantoir-data/${FANTOIR_VERSION} > fantoir-data/fantoir.json
+
clean:
- ${RM} fantoir-doc/nature_voie.json
+ ${RM} fantoir-doc/nature_voie.json fantoir-data/${FANTOIR_VERSION} fantoir-data/fantoir.json
diff --git a/utils/parse-fantoir.py b/utils/parse-fantoir.py
new file mode 100755
--- /dev/null
+++ b/utils/parse-fantoir.py
@@ -0,0 +1,179 @@
+#!/usr/bin/env python3
+
+import sys
+import json
+
+
+# -------------------------------------------------------------
+# External data
+#
+# Used to provide enrichment for FANTOIR data
+# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+
+
+def provide_external_data_paths():
+ return {
+ "natures_voie": "fantoir-doc/nature_voie.json",
+ }
+
+
+def fetch_external_data():
+ paths = provide_external_data_paths()
+
+ return {k: load_json(v) for k, v in paths.items()}
+
+
+def load_json(filename):
+ with open(filename) as fd:
+ return json.load(fd)
+
+
+# -------------------------------------------------------------
+# Parse FANTOIR
+# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+
+
+class FantoirParser:
+ """Parse a FANTOIR database"""
+
+ def __init__(self, data):
+ self.data = data
+
+ @staticmethod
+ def parse_departement(code_departement, code_direction):
+ dpt = code_departement
+
+ # Départements d'outre-mer (DOM)
+ if dpt == "97":
+ dpt += code_direction
+
+ return dpt
+
+ @staticmethod
+ def parse_type_commune(type):
+ try:
+ return {
+ "N": "rurale",
+ "R": "recensée",
+ }[type]
+ except KeyError:
+ return type
+
+ @staticmethod
+ def get_type_name(type):
+ try:
+ return {
+ 1: "voie",
+ 2: "ensemble immobilier",
+ 3: "lieu-dit",
+ 4: "pseudo-voie",
+ 5: "voie provisoire",
+ }[type]
+ except KeyError:
+ return ""
+
+ def get_nature_voie(self, code_nature_voie):
+ try:
+ return self.data["natures_voie"][code_nature_voie]
+ except KeyError:
+ return code_nature_voie
+
+ def get_department_name(self, department):
+ return ""
+
+ def get_commune_name(self, code_insee):
+ return ""
+
+ def parse_line(self, line):
+ department = self.parse_departement(line[0:2], line[2:3])
+ code_insee = line[0:2] + line[3:6]
+ code_nature_voie = line[11:15].strip()
+ type = int(line[108:109])
+
+ voie = {
+ "commune": {
+ "departement": department,
+ "code_commune": line[3:6],
+ "code_insee": code_insee,
+ "nom_departement": self.get_department_name(department),
+ "nom_commune": self.get_commune_name(code_insee),
+ "type_commune": self.parse_type_commune(line[42:43]),
+ },
+ "voie": {
+ "identifiant_communal_voie": line[6:10],
+ "cle_rivoli": line[10:11],
+ "code_nature_voie": code_nature_voie,
+ "nature_voie": self.get_nature_voie(code_nature_voie),
+ "libelle_voie": line[15:41].strip(),
+
+ "RUR": "pseudo-recensée" if line[45:46] == "3" else "",
+
+ "type": type,
+ "type_description": self.get_type_name(type),
+ "public": line[48:49] == "0",
+ },
+ "population": {
+ "is_large": line[49:50] == "*",
+ "à part": int(line[59:66]),
+ "fictive": int(line[66:73]),
+ },
+ "metadata": {
+ "cancelled": line[73:74] != " ",
+ "cancel_date": line[74:81],
+ "creation_date": line[81:88],
+ "MAJIC": line[103:108],
+ "last_alpha_word": line[112:120],
+ }
+ }
+
+ # Extra data for lieu-dit
+ if type == 3:
+ voie["lieu-dit"] = {
+ "built": line[109:110] == "1",
+ }
+
+ return json.dumps(voie, indent=4)
+
+ @staticmethod
+ def is_relevant_voie_record(line):
+ try:
+ return int(line[108]) in range(1, 6)
+ except IndexError:
+ return False
+
+ def parse(self, filename):
+ print("[")
+
+ separator = ""
+ with open(filename) as fd:
+ for line in fd:
+ if not self.is_relevant_voie_record(line):
+ continue
+
+ print(separator + self.parse_line(line.strip()))
+
+ if separator == "":
+ separator = ","
+
+ print("]")
+
+
+# -------------------------------------------------------------
+# Application entry point
+# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+
+
+def run(filename):
+ data = fetch_external_data()
+ parser = FantoirParser(data)
+ parser.parse(filename)
+
+
+if __name__ == "__main__":
+ try:
+ file_to_parse = sys.argv[1]
+ except IndexError:
+ print("Usage:", sys.argv[0], "<filename>", file=sys.stderr)
+ sys.exit(1)
+
+ run(file_to_parse)

File Metadata

Mime Type
text/plain
Expires
Sat, Nov 23, 07:50 (17 h, 31 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2256243
Default Alt Text
D2379.diff (5 KB)

Event Timeline