]> git.openstreetmap.org Git - nominatim.git/blobdiff - nominatim/tools/refresh.py
Enhanced the implementation of OSM views GeoTIFF import functionality
[nominatim.git] / nominatim / tools / refresh.py
index 9c5b7b085e50582202a117528d87bc0ca7ff117a..1bb801f569aa9db4b3c105323db2cc6ca79987f7 100644 (file)
@@ -9,6 +9,7 @@ Functions for bringing auxiliary data in the database up-to-date.
 """
 from typing import MutableSequence, Tuple, Any, Type, Mapping, Sequence, List, cast
 import logging
+import subprocess
 from textwrap import dedent
 from pathlib import Path
 
@@ -126,7 +127,7 @@ PHP_CONST_DEFS = (
 def import_wikipedia_articles(dsn: str, data_path: Path, ignore_errors: bool = False) -> int:
     """ Replaces the wikipedia importance tables with new data.
         The import is run in a single transaction so that the new data
-        is replace seemlessly.
+        is replace seamlessly.
 
         Returns 0 if all was well and 1 if the importance file could not
         be found. Throws an exception if there was an error reading the file.
@@ -146,6 +147,30 @@ def import_wikipedia_articles(dsn: str, data_path: Path, ignore_errors: bool = F
 
     return 0
 
+def import_osm_views_geotiff(conn: Connection, data_path: Path) -> int:
+    """ Replaces the OSM views table with new data.
+
+        Returns 0 if all was well and 1 if the OSM views GeoTIFF file could not
+        be found. Throws an exception if there was an error reading the file.
+    """
+    datafile = data_path / 'osmviews.tiff'
+
+    if not datafile.exists():
+        return 1
+
+    postgis_version = conn.postgis_version_tuple()
+    if postgis_version[0] < 3:
+        return 2
+
+    with conn.cursor() as cur:
+        cur.execute('DROP TABLE IF EXISTS "osm_views"')
+        conn.commit()
+
+        cmd = f"raster2pgsql -s 4326 -I -C -t 100x100 {datafile} \
+            public.osm_views | psql nominatim > /dev/null"
+        subprocess.run(["/bin/bash", "-c" , cmd], check=True)
+
+    return 0
 
 def recompute_importance(conn: Connection) -> None:
     """ Recompute wikipedia links and importance for all entries in placex.