From d2e55124697bd0fa0afba62894ee442f5ee87902 Mon Sep 17 00:00:00 2001 From: Johannes Schriewer Date: Thu, 14 Jul 2022 20:33:48 +0200 Subject: [PATCH] Add county and state to addresses --- .../sql/geocoder/003-forward_geocoding.sql | 24 +++++++++++++++++++ .../sql/geocoder/004-reverse_geocoding.sql | 6 +++++ .../data/sql/prepare/001-custom_types.sql | 2 ++ 3 files changed, 32 insertions(+) diff --git a/osmgeocoder/data/sql/geocoder/003-forward_geocoding.sql b/osmgeocoder/data/sql/geocoder/003-forward_geocoding.sql index 9801875..0a6395e 100644 --- a/osmgeocoder/data/sql/geocoder/003-forward_geocoding.sql +++ b/osmgeocoder/data/sql/geocoder/003-forward_geocoding.sql @@ -41,6 +41,8 @@ AS $$ h.house_number::text, c.postcode::text, NULLIF(c.name, '')::text as city, + NULLIF(a6.name, '')::text as county, + NULLIF(a4.name, '')::text as "state", h.geometry::gis.geometry(point, 3857), gis.ST_Distance(h.geometry, center) as distance, '00000000-0000-0000-0000-000000000000'::uuid as license_id @@ -48,6 +50,8 @@ AS $$ public.osm_struct_streets s JOIN public.osm_struct_cities c ON s.city_id = c.id JOIN public.osm_struct_house h ON h.street_id = s.id + LEFT JOIN public.osm_admin a4 ON gis.ST_Contains(a4.geometry, h.geometry::gis.geometry(point, 3857)) and a4.admin_level = 4 + LEFT JOIN public.osm_admin a6 ON gis.ST_Contains(a6.geometry, h.geometry::gis.geometry(point, 3857)) and a6.admin_level = 6 WHERE (center IS NULL OR gis.ST_DWithin(h.geometry, center, radius)) -- only search around center if center is not null AND s.name % search_term @@ -92,6 +96,8 @@ BEGIN h.house_number::text, c.postcode::text, NULLIF(c.name, '')::text as city, + NULLIF(a6.name, '')::text as county, + NULLIF(a4.name, '')::text as "state", h.geometry::gis.geometry(point, 3857), gis.ST_Distance(h.geometry, center) as distance, '00000000-0000-0000-0000-000000000000'::uuid as license_id @@ -99,6 +105,8 @@ BEGIN public.osm_struct_streets s JOIN public.osm_struct_cities c ON s.city_id = c.id JOIN public.osm_struct_house h ON h.street_id = s.id + LEFT JOIN public.osm_admin a4 ON gis.ST_Contains(a4.geometry, h.geometry::gis.geometry(point, 3857)) and a4.admin_level = 4 + LEFT JOIN public.osm_admin a6 ON gis.ST_Contains(a6.geometry, h.geometry::gis.geometry(point, 3857)) and a6.admin_level = 6 WHERE (center IS NULL OR gis.ST_DWithin(h.geometry, center, radius)) -- only search around center if center is not null AND gis.ST_Within(gis.ST_Centroid(h.geometry), country_poly) -- intersect with country polygon @@ -177,6 +185,8 @@ $$ h.house_number::text, c.postcode::text, NULLIF(c.name, '')::text as city, + NULLIF(a6.name, '')::text as county, + NULLIF(a4.name, '')::text as "state", h.geometry::gis.geometry(point, 3857), gis.ST_Distance(h.geometry, center) as distance, '00000000-0000-0000-0000-000000000000'::uuid as license_id @@ -184,6 +194,8 @@ $$ public.osm_struct_streets s JOIN public.osm_struct_cities c ON s.city_id = c.id JOIN public.osm_struct_house h ON h.street_id = s.id + LEFT JOIN public.osm_admin a4 ON gis.ST_Contains(a4.geometry, h.geometry::gis.geometry(point, 3857)) and a4.admin_level = 4 + LEFT JOIN public.osm_admin a6 ON gis.ST_Contains(a6.geometry, h.geometry::gis.geometry(point, 3857)) and a6.admin_level = 6 WHERE (center IS NULL OR gis.ST_DWithin(h.geometry, center, radius)) -- only search around center if center is not null AND c.name % search_city @@ -232,6 +244,8 @@ BEGIN h.house_number::text, c.postcode::text, NULLIF(c.name, '')::text as city, + NULLIF(a6.name, '')::text as county, + NULLIF(a4.name, '')::text as "state", h.geometry::gis.geometry(point, 3857), gis.ST_Distance(h.geometry, center) as distance, '00000000-0000-0000-0000-000000000000'::uuid as license_id @@ -239,6 +253,8 @@ BEGIN public.osm_struct_streets s JOIN public.osm_struct_cities c ON s.city_id = c.id JOIN public.osm_struct_house h ON h.street_id = s.id + LEFT JOIN public.osm_admin a4 ON gis.ST_Contains(a4.geometry, h.geometry::gis.geometry(point, 3857)) and a4.admin_level = 4 + LEFT JOIN public.osm_admin a6 ON gis.ST_Contains(a6.geometry, h.geometry::gis.geometry(point, 3857)) and a6.admin_level = 6 WHERE (center IS NULL OR gis.ST_DWithin(h.geometry, center, radius)) -- only search around center if center is not null AND gis.ST_Within(gis.ST_Centroid(h.geometry), country_poly) -- intersect with country polygon @@ -314,6 +330,8 @@ $$ h.house_number::text, c.postcode::text, NULLIF(c.name, '')::text as city, + NULLIF(a6.name, '')::text as county, + NULLIF(a4.name, '')::text as "state", h.geometry::gis.geometry(point, 3857), gis.ST_Distance(h.geometry, center) as distance, '00000000-0000-0000-0000-000000000000'::uuid as license_id @@ -321,6 +339,8 @@ $$ public.osm_struct_streets s JOIN public.osm_struct_cities c ON s.city_id = c.id JOIN public.osm_struct_house h ON h.street_id = s.id + LEFT JOIN public.osm_admin a4 ON gis.ST_Contains(a4.geometry, h.geometry::gis.geometry(point, 3857)) and a4.admin_level = 4 + LEFT JOIN public.osm_admin a6 ON gis.ST_Contains(a6.geometry, h.geometry::gis.geometry(point, 3857)) and a6.admin_level = 6 WHERE (center IS NULL OR gis.ST_DWithin(h.geometry, center, radius)) -- only search around center if center is not null AND s.name % search_term @@ -369,6 +389,8 @@ BEGIN h.house_number::text, c.postcode::text, NULLIF(c.name, '')::text as city, + NULLIF(a6.name, '')::text as county, + NULLIF(a4.name, '')::text as "state", h.geometry::gis.geometry(point, 3857), gis.ST_Distance(h.geometry, center) as distance, '00000000-0000-0000-0000-000000000000'::uuid as license_id @@ -376,6 +398,8 @@ BEGIN public.osm_struct_streets s JOIN public.osm_struct_cities c ON s.city_id = c.id JOIN public.osm_struct_house h ON h.street_id = s.id + LEFT JOIN public.osm_admin a4 ON gis.ST_Contains(a4.geometry, h.geometry::gis.geometry(point, 3857)) and a4.admin_level = 4 + LEFT JOIN public.osm_admin a6 ON gis.ST_Contains(a6.geometry, h.geometry::gis.geometry(point, 3857)) and a6.admin_level = 6 WHERE (center IS NULL OR gis.ST_DWithin(h.geometry, center, radius)) -- only search around center if center is not null AND gis.ST_Within(gis.ST_Centroid(h.geometry), country_poly) -- intersect with country polygon diff --git a/osmgeocoder/data/sql/geocoder/004-reverse_geocoding.sql b/osmgeocoder/data/sql/geocoder/004-reverse_geocoding.sql index dd802f4..dfc82b3 100644 --- a/osmgeocoder/data/sql/geocoder/004-reverse_geocoding.sql +++ b/osmgeocoder/data/sql/geocoder/004-reverse_geocoding.sql @@ -33,6 +33,8 @@ BEGIN h.housenumber as house_number, c.postcode, c.city, + NULL as county, + NULL as "state", location, gis.ST_Distance(location, point) as distance, c.license_id @@ -69,12 +71,16 @@ BEGIN h.house_number, c.postcode, c.name as city, + NULLIF(a6.name, '')::text as county, + NULLIF(a4.name, '')::text as "state", h.geometry as location, gis.ST_Distance(h.geometry, point) as distance, '00000000-0000-0000-0000-000000000000'::uuid as license_id FROM public.osm_struct_house h JOIN public.osm_struct_streets s ON h.street_id = s.id JOIN public.osm_struct_cities c ON s.city_id = c.id + LEFT JOIN public.osm_admin a4 ON gis.ST_Contains(a4.geometry, h.geometry::gis.geometry(point, 3857)) and a4.admin_level = 4 + LEFT JOIN public.osm_admin a6 ON gis.ST_Contains(a6.geometry, h.geometry::gis.geometry(point, 3857)) and a6.admin_level = 6 WHERE gis.ST_X(h.geometry) >= gis.ST_X(point) - radius AND gis.ST_X(h.geometry) <= gis.ST_X(point) + radius diff --git a/osmgeocoder/data/sql/prepare/001-custom_types.sql b/osmgeocoder/data/sql/prepare/001-custom_types.sql index 6999c18..bb158c2 100644 --- a/osmgeocoder/data/sql/prepare/001-custom_types.sql +++ b/osmgeocoder/data/sql/prepare/001-custom_types.sql @@ -12,6 +12,8 @@ BEGIN house_number text, postcode text, city text, + county text, + "state" text, location gis.geometry(point, 3857), distance float, license_id uuid