Skip to content

Commit

Permalink
add_placename_data db build script now automatically fetches and extr…
Browse files Browse the repository at this point in the history
…acts IS50V_ORNEFNI (placename) data
  • Loading branch information
sveinbjornt committed Sep 19, 2023
1 parent 37fc259 commit f40f8c4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
27 changes: 27 additions & 0 deletions add_placename_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,20 @@
from pprint import pprint # type: ignore
import sqlite3
from pathlib import Path
from io import BytesIO
import zipfile

import fiona # type: ignore
import requests

from iceaddr.dist import in_iceland


ORNEFNI_DATA_FILE = "is_50v_ornefni_wgs_84.gpkg"
ORNEFNI_DATA_URL = (
"https://atlas.lmi.is/heikir/downloadData/is_50v_ornefni_wgs_84_gpkg.zip"
)

# Remote URL for latest IS50V data:
# https://atlas.lmi.is/heikir/downloadData/is_50v_ornefni_wgs_84_gpkg.zip
# This file should be placed in repo root and renamed before running this program
Expand All @@ -34,6 +43,21 @@
DEFAULT_DBNAME = "iceaddr.db"


def fetch_ornefni_data() -> None:
"""Fetch IS50V placename data from remote URL, unzip
from memory to current directory and rename file."""

r = requests.get(ORNEFNI_DATA_URL, allow_redirects=True)
if r.status_code != 200:
print(f"Failed to download {ORNEFNI_DATA_URL}")
exit(1)

z = zipfile.ZipFile(BytesIO(r.content))
z.extractall()

Path(ORNEFNI_DATA_FILE).rename(GPKG_FILE)


def center_point(coords: List[Tuple[float, float]]) -> Tuple[float, float]:
"""Find the center point of a given set of coordinates."""
x: float = 0
Expand Down Expand Up @@ -192,6 +216,9 @@ def add_placenames_from_is50v(dbc: sqlite3.Connection) -> None:


def main() -> None:
# Fetch placename data from remote URL
fetch_ornefni_data()

# Delete any existing table
delete_table(DEFAULT_DBNAME)

Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ dev = [
"types-setuptools>=68.2.0",
"ruff>=0.0.285",
"coverage[toml]>=7.3.1",
"requests>=2.26.0",
]

# *** Configuration of tools ***
Expand Down

0 comments on commit f40f8c4

Please sign in to comment.