2 Functions for importing, updating and otherwise maintaining the table
3 of artificial postcode centroids.
6 from nominatim.db.utils import execute_file
7 from nominatim.db.connection import connect
9 def import_postcodes(dsn, project_dir):
10 """ Set up the initial list of postcodes.
13 with connect(dsn) as conn:
14 conn.drop_table('gb_postcode')
15 conn.drop_table('us_postcode')
17 with conn.cursor() as cur:
18 cur.execute("""CREATE TABLE gb_postcode (
20 postcode character varying(9),
21 geometry GEOMETRY(Point, 4326))""")
23 with conn.cursor() as cur:
24 cur.execute("""CREATE TABLE us_postcode (
27 y double precision)""")
30 gb_postcodes = project_dir / 'gb_postcode_data.sql.gz'
31 if gb_postcodes.is_file():
32 execute_file(dsn, gb_postcodes)
34 us_postcodes = project_dir / 'us_postcode_data.sql.gz'
35 if us_postcodes.is_file():
36 execute_file(dsn, us_postcodes)
38 with conn.cursor() as cur:
39 cur.execute("TRUNCATE location_postcode")
41 INSERT INTO location_postcode
42 (place_id, indexed_status, country_code, postcode, geometry)
43 SELECT nextval('seq_place'), 1, country_code,
44 upper(trim (both ' ' from address->'postcode')) as pc,
45 ST_Centroid(ST_Collect(ST_Centroid(geometry)))
47 WHERE address ? 'postcode' AND address->'postcode' NOT SIMILAR TO '%(,|;)%'
48 AND geometry IS NOT null
49 GROUP BY country_code, pc
53 INSERT INTO location_postcode
54 (place_id, indexed_status, country_code, postcode, geometry)
55 SELECT nextval('seq_place'), 1, 'us', postcode,
56 ST_SetSRID(ST_Point(x,y),4326)
57 FROM us_postcode WHERE postcode NOT IN
58 (SELECT postcode FROM location_postcode
59 WHERE country_code = 'us')
63 INSERT INTO location_postcode
64 (place_id, indexed_status, country_code, postcode, geometry)
65 SELECT nextval('seq_place'), 1, 'gb', postcode, geometry
66 FROM gb_postcode WHERE postcode NOT IN
67 (SELECT postcode FROM location_postcode
68 WHERE country_code = 'gb')
72 DELETE FROM word WHERE class='place' and type='postcode'
73 and word NOT IN (SELECT postcode FROM location_postcode)
77 SELECT count(getorcreate_postcode_id(v)) FROM
78 (SELECT distinct(postcode) as v FROM location_postcode) p