mirror of
https://github.com/osm-search/Nominatim.git
synced 2026-02-26 11:08:13 +00:00
port function to compute initial postcodes to Python
This commit is contained in:
@@ -6,7 +6,6 @@ from pathlib import Path
|
|||||||
|
|
||||||
import psutil
|
import psutil
|
||||||
|
|
||||||
from nominatim.tools.exec_utils import run_legacy_script
|
|
||||||
from nominatim.db.connection import connect
|
from nominatim.db.connection import connect
|
||||||
from nominatim.db import status, properties
|
from nominatim.db import status, properties
|
||||||
from nominatim.version import NOMINATIM_VERSION
|
from nominatim.version import NOMINATIM_VERSION
|
||||||
@@ -56,6 +55,7 @@ class SetupAll:
|
|||||||
from ..tools import database_import
|
from ..tools import database_import
|
||||||
from ..tools import refresh
|
from ..tools import refresh
|
||||||
from ..indexer.indexer import Indexer
|
from ..indexer.indexer import Indexer
|
||||||
|
from ..tools import postcodes
|
||||||
|
|
||||||
if args.osm_file and not Path(args.osm_file).is_file():
|
if args.osm_file and not Path(args.osm_file).is_file():
|
||||||
LOG.fatal("OSM file '%s' does not exist.", args.osm_file)
|
LOG.fatal("OSM file '%s' does not exist.", args.osm_file)
|
||||||
@@ -116,8 +116,7 @@ class SetupAll:
|
|||||||
args.threads or psutil.cpu_count() or 1)
|
args.threads or psutil.cpu_count() or 1)
|
||||||
|
|
||||||
LOG.warning('Calculate postcodes')
|
LOG.warning('Calculate postcodes')
|
||||||
run_legacy_script('setup.php', '--calculate-postcodes',
|
postcodes.import_postcodes(args.config.get_libpq_dsn(), args.project_dir)
|
||||||
nominatim_env=args, throw_on_fail=not args.ignore_errors)
|
|
||||||
|
|
||||||
if args.continue_at is None or args.continue_at in ('load-data', 'indexing'):
|
if args.continue_at is None or args.continue_at in ('load-data', 'indexing'):
|
||||||
LOG.warning('Indexing places')
|
LOG.warning('Indexing places')
|
||||||
|
|||||||
80
nominatim/tools/postcodes.py
Normal file
80
nominatim/tools/postcodes.py
Normal file
@@ -0,0 +1,80 @@
|
|||||||
|
"""
|
||||||
|
Functions for importing, updating and otherwise maintaining the table
|
||||||
|
of artificial postcode centroids.
|
||||||
|
"""
|
||||||
|
|
||||||
|
from nominatim.db.utils import execute_file
|
||||||
|
from nominatim.db.connection import connect
|
||||||
|
|
||||||
|
def import_postcodes(dsn, project_dir):
|
||||||
|
""" Set up the initial list of postcodes.
|
||||||
|
"""
|
||||||
|
|
||||||
|
with connect(dsn) as conn:
|
||||||
|
conn.drop_table('gb_postcode')
|
||||||
|
conn.drop_table('us_postcode')
|
||||||
|
|
||||||
|
with conn.cursor() as cur:
|
||||||
|
cur.execute("""CREATE TABLE gb_postcode (
|
||||||
|
id integer,
|
||||||
|
postcode character varying(9),
|
||||||
|
geometry GEOMETRY(Point, 4326))""")
|
||||||
|
|
||||||
|
with conn.cursor() as cur:
|
||||||
|
cur.execute("""CREATE TABLE us_postcode (
|
||||||
|
postcode text,
|
||||||
|
x double precision,
|
||||||
|
y double precision)""")
|
||||||
|
conn.commit()
|
||||||
|
|
||||||
|
gb_postcodes = project_dir / 'gb_postcode_data.sql.gz'
|
||||||
|
if gb_postcodes.is_file():
|
||||||
|
execute_file(dsn, gb_postcodes)
|
||||||
|
|
||||||
|
us_postcodes = project_dir / 'us_postcode_data.sql.gz'
|
||||||
|
if us_postcodes.is_file():
|
||||||
|
execute_file(dsn, us_postcodes)
|
||||||
|
|
||||||
|
with conn.cursor() as cur:
|
||||||
|
cur.execute("TRUNCATE location_postcode")
|
||||||
|
cur.execute("""
|
||||||
|
INSERT INTO location_postcode
|
||||||
|
(place_id, indexed_status, country_code, postcode, geometry)
|
||||||
|
SELECT nextval('seq_place'), 1, country_code,
|
||||||
|
upper(trim (both ' ' from address->'postcode')) as pc,
|
||||||
|
ST_Centroid(ST_Collect(ST_Centroid(geometry)))
|
||||||
|
FROM placex
|
||||||
|
WHERE address ? 'postcode' AND address->'postcode' NOT SIMILAR TO '%(,|;)%'
|
||||||
|
AND geometry IS NOT null
|
||||||
|
GROUP BY country_code, pc
|
||||||
|
""")
|
||||||
|
|
||||||
|
cur.execute("""
|
||||||
|
INSERT INTO location_postcode
|
||||||
|
(place_id, indexed_status, country_code, postcode, geometry)
|
||||||
|
SELECT nextval('seq_place'), 1, 'us', postcode,
|
||||||
|
ST_SetSRID(ST_Point(x,y),4326)
|
||||||
|
FROM us_postcode WHERE postcode NOT IN
|
||||||
|
(SELECT postcode FROM location_postcode
|
||||||
|
WHERE country_code = 'us')
|
||||||
|
""")
|
||||||
|
|
||||||
|
cur.execute("""
|
||||||
|
INSERT INTO location_postcode
|
||||||
|
(place_id, indexed_status, country_code, postcode, geometry)
|
||||||
|
SELECT nextval('seq_place'), 1, 'gb', postcode, geometry
|
||||||
|
FROM gb_postcode WHERE postcode NOT IN
|
||||||
|
(SELECT postcode FROM location_postcode
|
||||||
|
WHERE country_code = 'gb')
|
||||||
|
""")
|
||||||
|
|
||||||
|
cur.execute("""
|
||||||
|
DELETE FROM word WHERE class='place' and type='postcode'
|
||||||
|
and word NOT IN (SELECT postcode FROM location_postcode)
|
||||||
|
""")
|
||||||
|
|
||||||
|
cur.execute("""
|
||||||
|
SELECT count(getorcreate_postcode_id(v)) FROM
|
||||||
|
(SELECT distinct(postcode) as v FROM location_postcode) p
|
||||||
|
""")
|
||||||
|
conn.commit()
|
||||||
Reference in New Issue
Block a user