]> 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 8c1e9d9bbb24882601e4d0d8bed6c3c54a41f498..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
 
@@ -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.