Skip to content

Commit d2e5512

Browse files
committed
Add county and state to addresses
1 parent 22a6cd5 commit d2e5512

3 files changed

Lines changed: 32 additions & 0 deletions

File tree

osmgeocoder/data/sql/geocoder/003-forward_geocoding.sql

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,17 @@ AS $$
4141
h.house_number::text,
4242
c.postcode::text,
4343
NULLIF(c.name, '')::text as city,
44+
NULLIF(a6.name, '')::text as county,
45+
NULLIF(a4.name, '')::text as "state",
4446
h.geometry::gis.geometry(point, 3857),
4547
gis.ST_Distance(h.geometry, center) as distance,
4648
'00000000-0000-0000-0000-000000000000'::uuid as license_id
4749
FROM
4850
public.osm_struct_streets s
4951
JOIN public.osm_struct_cities c ON s.city_id = c.id
5052
JOIN public.osm_struct_house h ON h.street_id = s.id
53+
LEFT JOIN public.osm_admin a4 ON gis.ST_Contains(a4.geometry, h.geometry::gis.geometry(point, 3857)) and a4.admin_level = 4
54+
LEFT JOIN public.osm_admin a6 ON gis.ST_Contains(a6.geometry, h.geometry::gis.geometry(point, 3857)) and a6.admin_level = 6
5155
WHERE
5256
(center IS NULL OR gis.ST_DWithin(h.geometry, center, radius)) -- only search around center if center is not null
5357
AND s.name % search_term
@@ -92,13 +96,17 @@ BEGIN
9296
h.house_number::text,
9397
c.postcode::text,
9498
NULLIF(c.name, '')::text as city,
99+
NULLIF(a6.name, '')::text as county,
100+
NULLIF(a4.name, '')::text as "state",
95101
h.geometry::gis.geometry(point, 3857),
96102
gis.ST_Distance(h.geometry, center) as distance,
97103
'00000000-0000-0000-0000-000000000000'::uuid as license_id
98104
FROM
99105
public.osm_struct_streets s
100106
JOIN public.osm_struct_cities c ON s.city_id = c.id
101107
JOIN public.osm_struct_house h ON h.street_id = s.id
108+
LEFT JOIN public.osm_admin a4 ON gis.ST_Contains(a4.geometry, h.geometry::gis.geometry(point, 3857)) and a4.admin_level = 4
109+
LEFT JOIN public.osm_admin a6 ON gis.ST_Contains(a6.geometry, h.geometry::gis.geometry(point, 3857)) and a6.admin_level = 6
102110
WHERE
103111
(center IS NULL OR gis.ST_DWithin(h.geometry, center, radius)) -- only search around center if center is not null
104112
AND gis.ST_Within(gis.ST_Centroid(h.geometry), country_poly) -- intersect with country polygon
@@ -177,13 +185,17 @@ $$
177185
h.house_number::text,
178186
c.postcode::text,
179187
NULLIF(c.name, '')::text as city,
188+
NULLIF(a6.name, '')::text as county,
189+
NULLIF(a4.name, '')::text as "state",
180190
h.geometry::gis.geometry(point, 3857),
181191
gis.ST_Distance(h.geometry, center) as distance,
182192
'00000000-0000-0000-0000-000000000000'::uuid as license_id
183193
FROM
184194
public.osm_struct_streets s
185195
JOIN public.osm_struct_cities c ON s.city_id = c.id
186196
JOIN public.osm_struct_house h ON h.street_id = s.id
197+
LEFT JOIN public.osm_admin a4 ON gis.ST_Contains(a4.geometry, h.geometry::gis.geometry(point, 3857)) and a4.admin_level = 4
198+
LEFT JOIN public.osm_admin a6 ON gis.ST_Contains(a6.geometry, h.geometry::gis.geometry(point, 3857)) and a6.admin_level = 6
187199
WHERE
188200
(center IS NULL OR gis.ST_DWithin(h.geometry, center, radius)) -- only search around center if center is not null
189201
AND c.name % search_city
@@ -232,13 +244,17 @@ BEGIN
232244
h.house_number::text,
233245
c.postcode::text,
234246
NULLIF(c.name, '')::text as city,
247+
NULLIF(a6.name, '')::text as county,
248+
NULLIF(a4.name, '')::text as "state",
235249
h.geometry::gis.geometry(point, 3857),
236250
gis.ST_Distance(h.geometry, center) as distance,
237251
'00000000-0000-0000-0000-000000000000'::uuid as license_id
238252
FROM
239253
public.osm_struct_streets s
240254
JOIN public.osm_struct_cities c ON s.city_id = c.id
241255
JOIN public.osm_struct_house h ON h.street_id = s.id
256+
LEFT JOIN public.osm_admin a4 ON gis.ST_Contains(a4.geometry, h.geometry::gis.geometry(point, 3857)) and a4.admin_level = 4
257+
LEFT JOIN public.osm_admin a6 ON gis.ST_Contains(a6.geometry, h.geometry::gis.geometry(point, 3857)) and a6.admin_level = 6
242258
WHERE
243259
(center IS NULL OR gis.ST_DWithin(h.geometry, center, radius)) -- only search around center if center is not null
244260
AND gis.ST_Within(gis.ST_Centroid(h.geometry), country_poly) -- intersect with country polygon
@@ -314,13 +330,17 @@ $$
314330
h.house_number::text,
315331
c.postcode::text,
316332
NULLIF(c.name, '')::text as city,
333+
NULLIF(a6.name, '')::text as county,
334+
NULLIF(a4.name, '')::text as "state",
317335
h.geometry::gis.geometry(point, 3857),
318336
gis.ST_Distance(h.geometry, center) as distance,
319337
'00000000-0000-0000-0000-000000000000'::uuid as license_id
320338
FROM
321339
public.osm_struct_streets s
322340
JOIN public.osm_struct_cities c ON s.city_id = c.id
323341
JOIN public.osm_struct_house h ON h.street_id = s.id
342+
LEFT JOIN public.osm_admin a4 ON gis.ST_Contains(a4.geometry, h.geometry::gis.geometry(point, 3857)) and a4.admin_level = 4
343+
LEFT JOIN public.osm_admin a6 ON gis.ST_Contains(a6.geometry, h.geometry::gis.geometry(point, 3857)) and a6.admin_level = 6
324344
WHERE
325345
(center IS NULL OR gis.ST_DWithin(h.geometry, center, radius)) -- only search around center if center is not null
326346
AND s.name % search_term
@@ -369,13 +389,17 @@ BEGIN
369389
h.house_number::text,
370390
c.postcode::text,
371391
NULLIF(c.name, '')::text as city,
392+
NULLIF(a6.name, '')::text as county,
393+
NULLIF(a4.name, '')::text as "state",
372394
h.geometry::gis.geometry(point, 3857),
373395
gis.ST_Distance(h.geometry, center) as distance,
374396
'00000000-0000-0000-0000-000000000000'::uuid as license_id
375397
FROM
376398
public.osm_struct_streets s
377399
JOIN public.osm_struct_cities c ON s.city_id = c.id
378400
JOIN public.osm_struct_house h ON h.street_id = s.id
401+
LEFT JOIN public.osm_admin a4 ON gis.ST_Contains(a4.geometry, h.geometry::gis.geometry(point, 3857)) and a4.admin_level = 4
402+
LEFT JOIN public.osm_admin a6 ON gis.ST_Contains(a6.geometry, h.geometry::gis.geometry(point, 3857)) and a6.admin_level = 6
379403
WHERE
380404
(center IS NULL OR gis.ST_DWithin(h.geometry, center, radius)) -- only search around center if center is not null
381405
AND gis.ST_Within(gis.ST_Centroid(h.geometry), country_poly) -- intersect with country polygon

osmgeocoder/data/sql/geocoder/004-reverse_geocoding.sql

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ BEGIN
3333
h.housenumber as house_number,
3434
c.postcode,
3535
c.city,
36+
NULL as county,
37+
NULL as "state",
3638
location,
3739
gis.ST_Distance(location, point) as distance,
3840
c.license_id
@@ -69,12 +71,16 @@ BEGIN
6971
h.house_number,
7072
c.postcode,
7173
c.name as city,
74+
NULLIF(a6.name, '')::text as county,
75+
NULLIF(a4.name, '')::text as "state",
7276
h.geometry as location,
7377
gis.ST_Distance(h.geometry, point) as distance,
7478
'00000000-0000-0000-0000-000000000000'::uuid as license_id
7579
FROM public.osm_struct_house h
7680
JOIN public.osm_struct_streets s ON h.street_id = s.id
7781
JOIN public.osm_struct_cities c ON s.city_id = c.id
82+
LEFT JOIN public.osm_admin a4 ON gis.ST_Contains(a4.geometry, h.geometry::gis.geometry(point, 3857)) and a4.admin_level = 4
83+
LEFT JOIN public.osm_admin a6 ON gis.ST_Contains(a6.geometry, h.geometry::gis.geometry(point, 3857)) and a6.admin_level = 6
7884
WHERE
7985
gis.ST_X(h.geometry) >= gis.ST_X(point) - radius
8086
AND gis.ST_X(h.geometry) <= gis.ST_X(point) + radius

osmgeocoder/data/sql/prepare/001-custom_types.sql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ BEGIN
1212
house_number text,
1313
postcode text,
1414
city text,
15+
county text,
16+
"state" text,
1517
location gis.geometry(point, 3857),
1618
distance float,
1719
license_id uuid

0 commit comments

Comments
 (0)