-
Notifications
You must be signed in to change notification settings - Fork 5
/
pgGeocoder.sql
651 lines (539 loc) · 18.7 KB
/
pgGeocoder.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
--
-- pgGeocoder.ja : Japanese Geocoder for PostgreSQL
-- Copyright (C) 2007 Mario Basa
--
-- This program is free software; you can redistribute it and/or
-- modify it under the terms of the GNU General Public License
-- as published by the Free Software Foundation; either version 2
-- of the License, or (at your option) any later version.
--
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License
-- along with this program; if not, write to the Free Software
-- Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
--
-- このプログラムはフリーソフトウェアです。あなたはこれを、フリーソフトウェ
-- ア財団によって発行された GNU 一般公衆利用許諾契約書(バージョン2か、希
-- 望によってはそれ以降のバージョンのうちどれか)の定める条件の下で再頒布
-- または改変することができます。
--
-- このプログラムは有用であることを願って頒布されますが、*全くの無保証*
-- です。商業可能性の保証や特定の目的への適合性は、言外に示されたものも含
-- め全く存在しません。詳しくはGNU 一般公衆利用許諾契約書をご覧ください。
--
-- あなたはこのプログラムと共に、GNU 一般公衆利用許諾契約書の複製物を一部
-- 受け取ったはずです。もし受け取っていなければ、フリーソフトウェア財団ま
-- で請求してください(宛先は the Free Software Foundation, Inc., 59
-- Temple Place, Suite 330, Boston, MA 02111-1307 USA)。
DROP TYPE IF EXISTS geores CASCADE;
CREATE TYPE geores AS (
code integer,
x double precision,
y double precision,
address character varying,
todofuken character varying,
shikuchoson character varying,
ooaza character varying,
chiban character varying,
go character varying
);
--
-- Main Geocoder function.
--
CREATE OR REPLACE FUNCTION geocoder(character varying)
RETURNS geores AS $$
DECLARE
address ALIAS FOR $1;
gc geores;
output geores;
matching_nomatch integer;
matching_todofuken integer;
matching_shikuchoson integer;
matching_ooaza integer;
matching_chiban integer;
matching_pinpnt integer;
BEGIN
matching_nomatch := 0;
matching_todofuken := 1;
matching_shikuchoson := 2;
matching_ooaza := 3;
matching_chiban := 4;
matching_pinpnt := 5;
output := searchTodofuken( address );
IF output.address <> 'なし' THEN
output.code := matching_todofuken;
gc := searchShikuchoson( address,output.todofuken);
ELSE
output.code := matching_nomatch;
gc := searchShikuchoson( address,'');
END IF;
IF gc.address <> 'なし' THEN
output := gc;
output.code := matching_shikuchoson;
gc := searchOoaza( address,output.todofuken,output.shikuchoson );
ELSE
RETURN output;
END IF;
IF gc.address <> 'なし' THEN
output := gc;
output.code := matching_ooaza;
gc := searchChiban( address,output.todofuken,output.shikuchoson,
output.ooaza );
ELSE
RETURN output;
END IF;
IF gc.address <> 'なし' THEN
output := gc;
output.code := matching_chiban;
gc := searchPinpoint( address,output.todofuken,output.shikuchoson,
output.ooaza,output.chiban );
ELSE
RETURN output;
END IF;
IF gc.address <> 'なし' THEN
output := gc;
output.code := matching_pinpnt;
END IF;
RETURN output;
END;
$$ LANGUAGE plpgsql;
--
-- Function to normalize Address for easier matches
--
CREATE OR REPLACE FUNCTION normalizeAddr(character varying)
RETURNS varchar AS $$
DECLARE
paddress ALIAS FOR $1;
address varchar;
tmpstr varchar;
tmparr text[];
st integer;
en integer;
arrc integer;
arrl integer;
BEGIN
address := translate(paddress,
'ヶケ?ー―‐−-ーのノ1234567890〇一二三四五六七八九十丁目',
'がが---------12345678900123456789X-');
IF strpos( address, 'X') <> 0 THEN
tmparr := string_to_array( address,'X');
address := '';
arrl := array_upper( tmparr, 1 );
arrc := 1;
WHILE arrc < arrl LOOP
st := ascii(substr(tmparr[arrc],length(tmparr[arrc]),1));
en := ascii(substr(tmparr[ arrc+1 ],1,1));
--
-- For cases like '十九'
--
IF (st < 48 OR st > 57) AND (en >= 48 AND en <= 57) THEN
IF arrc = 1 THEN
address := address || tmparr[arrc] || '1' || tmparr[arrc+1];
ELSE
address := address || '1' || tmparr[arrc+1];
END IF;
END IF;
--
-- For cases like '二十九'
--
IF (st >= 48 AND st <= 57) AND (en >= 48 AND en <= 57) THEN
IF arrc = 1 THEN
address := address || tmparr[arrc] || tmparr[arrc+1];
ELSE
address := address || tmparr[arrc+1];
END IF;
END IF;
--
-- For cases like '二十'
--
IF (st >= 48 AND st <= 57) AND (en < 48 OR en > 57) THEN
IF arrc = 1 THEN
address := address || tmparr[arrc] || '0' || tmparr[arrc+1];
ELSE
address := address || '0' || tmparr[arrc+1];
END IF;
END IF;
--
-- For cases like '十'
--
IF (st < 48 OR st > 57) AND (en < 48 OR en > 57) THEN
IF arrc = 1 THEN
address := address || tmparr[arrc] || '10' || tmparr[arrc+1];
ELSE
address := address || '10' || tmparr[arrc+1];
END IF;
END IF;
arrc := arrc + 1;
END LOOP;
END IF;
--
-- Removing 大字 OR 字 since addresses sometimes omit this
--
IF address ~ '^大字' OR address ~ '^字' THEN
address := regexp_replace(address, '^(大字|字)', '');
END IF;
--
-- Adding Kobayashi-san's rule set
--
address := translate( address,
'榮之ノ治ヰヱ淵渕輿曽藪薮籠篭劔峯峰岡丘富冨祓桧檜莱洲冶治壇檀舘館斉斎竈竃朗鷆膳録嶋崎埼碕庄荘横橫鄕神塚塚都都德福朗郞嶽區溪縣廣斎眞槇槙莊藏龍瀧澤當邊舖萬豫禮茅礪砺',
'栄-の冶いえ渕淵興曾薮藪篭籠剱峰峯丘岡冨富秡檜桧来州治冶檀壇館舘斎斉釜釜郎鷏善禄島埼崎崎荘庄橫横郷神塚塚都都徳福朗郎岳区渓県広斉真槙槇荘蔵竜滝沢当辺舗万予礼芽砺礪'
);
--
-- For addresses like 清水一丁目−3番−14号
--
address := replace(address,'--','-');
--
-- Replacing 通り with 通. Common address mistake.
--
address := replace(address,'通り','通');
--
-- Replacing floor num in address ie 清水1-3-14 3F
--
address := regexp_replace(address,' \d*F' ,' nF');
address := regexp_replace(address,' \d*F' ,' nF');
address := regexp_replace(address,' \W*F' ,' nF');
address := regexp_replace(address,' \W*F',' nF');
address := regexp_replace(address,' \W*階' ,' nF');
address := regexp_replace(address,' \W*階',' nF');
address := regexp_replace(address,' \d*階' ,' nF');
address := regexp_replace(address,' \d*階',' nF');
--
-- For addresses like 北海道札幌市白石区本通1北3
--
tmpstr := ( regexp_matches(address,'\d[東西南北]\d'))[1];
IF tmpstr IS NOT NULL THEN
tmparr := string_to_array( tmpstr,NULL );
address := regexp_replace(address,tmpstr,tmparr[1]||'-'||tmparr[2]||tmparr[3]);
END IF;
--
-- For addresses like 北海道札幌市白石区南郷通6北-4
--
tmpstr := ( regexp_matches(address,'\d[東西南北]-\d'))[1];
IF tmpstr IS NOT NULL THEN
tmparr := string_to_array( tmpstr,NULL );
address := regexp_replace(address,tmpstr,tmparr[1]||'-'||tmparr[2]||tmparr[4]);
END IF;
RETURN address;
END;
$$ LANGUAGE plpgsql;
--
-- Function to search Todofuken level of an address
-- parameters: address
--
CREATE OR REPLACE FUNCTION searchTodofuken(character varying)
RETURNS geores AS $$
DECLARE
paddress ALIAS FOR $1;
address varchar;
rec RECORD;
output geores;
BEGIN
-- RAISE NOTICE 'Todofuken Parameter passed is : %',paddress;
output.x := -999;
output.y := -999;
output.address := 'なし';
output.todofuken := '';
address := replace(paddress,' ','');
address := replace(address,' ','');
SELECT INTO rec * FROM pggeocoder.address_t WHERE address
LIKE todofuken||'%';
IF FOUND THEN
output.x := rec.lon;
output.y := rec.lat;
output.code := 4;
output.address := rec.todofuken;
output.todofuken := rec.todofuken;
ELSE
output.code := 5;
END IF;
RETURN output;
END;
$$ LANGUAGE plpgsql;
--
-- Function to search Shikuchoson level of an address
-- parameters: address,todofuken (may be blank)
--
CREATE OR REPLACE FUNCTION searchShikuchoson( character varying,
character varying )
RETURNS geores AS $$
DECLARE
paddress ALIAS FOR $1;
r_todofuken ALIAS FOR $2;
address varchar;
tmpstr varchar;
rec RECORD;
output geores;
BEGIN
-- RAISE NOTICE 'Shikuchoson Parameters passed are : % and %',
-- paddress,r_todofuken;
output.x := -999;
output.y := -999;
output.address := 'なし';
address := replace(paddress,' ','');
address := replace(address,' ','');
IF r_todofuken <> '' THEN
tmpstr := split_part(address,r_todofuken,2);
SELECT INTO rec * FROM pggeocoder.address_s WHERE
todofuken = r_todofuken AND
normalizeAddr(tmpstr) LIKE tr_shikuchoson||'%'
ORDER BY length(tr_shikuchoson) DESC;
ELSE
tmpstr := normalizeAddr(address);
SELECT INTO rec * FROM pggeocoder.address_s WHERE
tmpstr LIKE tr_shikuchoson||'%'
ORDER BY length(tr_shikuchoson) DESC;
END IF;
--
-- Removing District ('郡') since it is not
-- normally written in the address
--
IF NOT FOUND THEN
tmpstr := normalizeAddr(address);
IF r_todofuken <> '' THEN
SELECT INTO rec * FROM pggeocoder.address_s WHERE
todofuken = r_todofuken AND
tmpstr LIKE '%'||substr(tr_shikuchoson,strpos(tr_shikuchoson,'郡')+1)||'%'
ORDER BY length(tr_shikuchoson) DESC;
ELSE
SELECT INTO rec * FROM pggeocoder.address_s WHERE
tmpstr LIKE substr(tr_shikuchoson,strpos(tr_shikuchoson,'郡')+1)||'%'
ORDER BY length(tr_shikuchoson) DESC;
END IF;
END IF;
IF FOUND THEN
output.x := rec.lon;
output.y := rec.lat;
output.code := 3;
output.address := rec.todofuken || rec.shikuchoson;
output.todofuken := rec.todofuken;
output.shikuchoson:= rec.shikuchoson;
END IF;
RETURN output;
END;
$$ LANGUAGE plpgsql;
--
-- Function to search Ooaza level of an address
-- parameters: address, shikuchoson
--
CREATE OR REPLACE FUNCTION searchOoaza( character varying, character varying,
character varying )
RETURNS geores AS $$
DECLARE
paddress ALIAS FOR $1;
r_todofuken ALIAS FOR $2;
r_shikuchoson ALIAS FOR $3;
address varchar;
t_shikuchoson varchar;
tmpstr varchar;
tmpaddr varchar;
rec RECORD;
output geores;
BEGIN
-- RAISE NOTICE 'Ooaza Parameters passed are : % and %',
-- paddress,r_shikuchoson;
output.x := -999;
output.y := -999;
output.address := 'なし';
address := replace(paddress,' ','');
address := replace(address,' ','');
t_shikuchoson := normalizeAddr( r_shikuchoson );
tmpstr := split_part(normalizeAddr(address),t_shikuchoson,2);
IF tmpstr = '' THEN
tmpstr := split_part(normalizeAddr(address),
substr(t_shikuchoson,strpos(t_shikuchoson,'郡')+1),2);
END IF;
--tmpstr := tmpstr || '-';
--tmpaddr := normalizeAddr( tmpstr );
tmpaddr := tmpstr || '-'; -- to match addresses like 杉並区清水1
--
-- Removing 大字 OR 字 since addresses sometimes omit this
--
IF tmpaddr ~ '^大字' OR tmpaddr ~ '^字' THEN
tmpaddr := regexp_replace(tmpaddr, '^(大字|字)', '');
END IF;
--
-- Trying to parse Kyoto Addresses which contains Directions
--
IF r_todofuken = '京都府' THEN
SELECT INTO rec *,
strpos(address,ooaza) AS pos,
length(tr_ooaza) AS length
FROM pggeocoder.address_o WHERE
todofuken = r_todofuken AND
tr_shikuchoson = t_shikuchoson AND
strpos(tmpaddr,tr_ooaza) >= 1
ORDER BY length DESC,pos DESC,year DESC LIMIT 1;
ELSE
--
-- the 'Order By length' slows down the operation a bit
-- but produces more accurate matches.
--
SELECT INTO rec *,length(tr_ooaza) AS length FROM pggeocoder.address_o WHERE
todofuken = r_todofuken AND
tr_shikuchoson = t_shikuchoson AND
strpos(tmpaddr,tr_ooaza) = 1
ORDER BY length DESC,year DESC LIMIT 1;
END IF;
IF FOUND THEN
output.x := rec.lon;
output.y := rec.lat;
output.code := 2;
output.address := rec.todofuken||rec.shikuchoson||rec.ooaza;
output.todofuken := rec.todofuken;
output.shikuchoson:= rec.shikuchoson;
output.ooaza := rec.ooaza;
END IF;
RETURN output;
END;
$$ LANGUAGE plpgsql;
--
-- Function to search Chiban level of an address
-- parameters: address, todofuken, shikuchoson, ooza
--
CREATE OR REPLACE FUNCTION searchChiban( character varying,character varying,
character varying,character varying )
RETURNS geores AS $$
DECLARE
paddress ALIAS FOR $1;
r_todofuken ALIAS FOR $2;
r_shikuchoson ALIAS FOR $3;
r_ooaza ALIAS FOR $4;
address varchar;
ooaza varchar;
shikuchoson varchar;
preftab varchar;
tmpstr1 varchar;
tmpstr2 varchar;
tmpstr3 varchar;
tmpcnt integer;
tmpflag integer;
rec RECORD;
output geores;
BEGIN
output.x := -999;
output.y := -999;
output.address := 'なし';
preftab := '';
IF r_todofuken <> '' THEN
SELECT INTO rec * FROM pggeocoder.address_t where todofuken = r_todofuken;
preftab := rec.ttable;
END IF;
IF preftab = '' THEN
RETURN output;
END IF;
address := normalizeAddr( paddress );
address := replace(address,' ','');
address := replace(address,' ','');
ooaza := replace(r_ooaza,' ','');
ooaza := replace(ooaza,' ','');
ooaza := normalizeAddr( ooaza );
shikuchoson := replace(r_shikuchoson,' ','');
shikuchoson := replace(shikuchoson,' ','');
shikuchoson := normalizeAddr( shikuchoson );
-- RAISE NOTICE 'Chiban Parameters passed are : % and %',
-- address,ooaza;
-- Test if ooaza string is contained in shikuchoson
-- i.e. address='和気郡和気町和気681'' ooaza='和気'
-- FOR counter IN reverse 5..2 LOOP
-- tmpstr1 := split_part( address,ooaza,counter);
-- EXIT WHEN tmpstr1 <> '';
-- END LOOP;
tmpstr1 := split_part(address,ooaza,2);
tmpstr1 := replace(tmpstr1,'X','10');
tmpcnt := 1;
tmpflag := length( tmpstr1 );
tmpstr2 := '';
tmpstr3 := '';
WHILE tmpcnt <= tmpflag LOOP
tmpstr2 := substr(tmpstr1,tmpcnt,1);
IF ascii( tmpstr2 ) >= 48 AND ascii( tmpstr2 ) <= 57 THEN
tmpstr3 := tmpstr3 || tmpstr2;
ELSE
EXIT;
END IF;
tmpcnt := tmpcnt + 1;
END LOOP;
tmpstr1 := 'SELECT * FROM '|| preftab ||' WHERE ' ||
'tr_shikuchoson = ' || quote_literal(shikuchoson) || ' AND ' ||
'tr_ooaza = ' || quote_literal(ooaza) || ' AND ' ||
'chiban = ' || quote_literal(tmpstr3) ||
' ORDER BY year DESC';
EXECUTE tmpstr1 into rec;
IF rec.lon IS NOT NULL AND rec.lat IS NOT NULL THEN
output.code := 1;
output.x := rec.lon;
output.y := rec.lat;
output.address := rec.todofuken||rec.shikuchoson||
rec.ooaza||rec.chiban;
output.todofuken := rec.todofuken;
output.shikuchoson:= rec.shikuchoson;
output.ooaza := rec.ooaza;
output.chiban := rec.chiban;
END IF;
RETURN output;
END;
$$ LANGUAGE plpgsql;
--
-- Function to search Pinpoint level of an address
-- parameters: address, todofuken, shikuchoson, ooza, chiban
--
CREATE OR REPLACE FUNCTION searchPinpoint( character varying,character varying,
character varying,character varying,character varying )
RETURNS geores AS $$
DECLARE
paddress ALIAS FOR $1;
r_todofuken ALIAS FOR $2;
r_shikuchoson ALIAS FOR $3;
r_ooaza ALIAS FOR $4;
r_chiban ALIAS FOR $5;
address varchar;
ooaza varchar;
tmpstr1 varchar;
tmpstr2 varchar;
tmpstr3 varchar;
rec RECORD;
output geores;
BEGIN
output.x := -999;
output.y := -999;
output.address := 'なし';
address := replace(paddress,'番地','-');
address := replace(address,'番','-');
address := normalizeAddr( address );
address := replace(address,' ','');
address := replace(address,' ','');
ooaza := replace(r_ooaza,'番','-');
ooaza := normalizeAddr(ooaza);
tmpstr2 := substring( split_part(address,ooaza,2) from '\d*\-\d*');
IF tmpstr2 IS NOT NULL THEN
tmpstr3 := split_part(tmpstr2,'-',2);
ELSE
RETURN output;
END IF;
tmpstr1 := 'SELECT * FROM pggeocoder.address_g WHERE ' ||
'tr_shikuchoson = ' || quote_literal(normalizeAddr(r_shikuchoson)) || ' AND ' ||
'tr_ooaza = ' || quote_literal(normalizeAddr(r_ooaza)) || ' AND ' ||
'chiban = ' || quote_literal(r_chiban) || ' AND ' ||
'go = ' || quote_literal(tmpstr3);
--RAISE NOTICE 'tmpstr1 %',tmpstr1;
EXECUTE tmpstr1 into rec;
IF rec.lon IS NOT NULL AND rec.lat IS NOT NULL THEN
output.code := 1;
output.x := rec.lon;
output.y := rec.lat;
output.address := rec.todofuken||r_shikuchoson||
r_ooaza||rec.chiban||'-'||tmpstr3;
output.todofuken := rec.todofuken;
output.shikuchoson:= r_shikuchoson;
output.ooaza := r_ooaza;
output.chiban := rec.chiban;
output.go := tmpstr3;
END IF;
RETURN output;
END;
$$ LANGUAGE plpgsql;