forked from mbasa/pgGeocoder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pgGeocoder.sql
538 lines (445 loc) · 14.9 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
--
-- 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)。
--
-- 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_eki integer;
matching_todofuken integer;
matching_shikuchoson integer;
matching_ooaza integer;
matching_chiban integer;
matching_pinpnt integer;
BEGIN
matching_nomatch := -99;
matching_eki := 30;
matching_todofuken := 5;
matching_shikuchoson := 4;
matching_ooaza := 3;
matching_chiban := 2;
matching_pinpnt := 1;
--
-- Address Search
--
output := searchTodofuken( address );
IF output.address <> 'なし' THEN
output.code := matching_todofuken;
IF LENGTH( address ) <= 4 THEN
RETURN output;
END IF;
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
--
-- Places Search (not an address)
--
gc := searchPlaces( address );
IF gc.address <> 'なし' THEN
output :=gc;
END IF;
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;
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一二三四五六七八九十丁目',
'kk----1234567890123456789X-');
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;
--
-- Adding Kobayashi-san's rule set
--
address := translate( address,
'之ノ治ヰヱ淵渕輿曽藪薮籠篭劔峯峰岡丘富冨祓桧檜莱洲冶治壇檀舘館斉斎竈竃朗鷆膳録嶋崎埼碕庄荘横橫鄕神塚塚都都德福朗郞嶽區溪縣廣斎眞槇槙莊藏龍瀧澤當邊舖萬豫禮茅礪砺',
'のの冶いえ渕淵興曾薮藪篭籠剱峰峯丘岡冨富秡檜桧来州治冶檀壇館舘斎斉釜釜郎鷏善禄島埼崎崎荘庄橫横郷神塚塚都都徳福朗郎岳区渓県広斉真槙槇荘蔵竜滝沢当辺舗万予礼芽砺礪'
);
--
-- 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 POIs in the places table
-- parameters: address
--
CREATE OR REPLACE FUNCTION searchPlaces(character varying)
RETURNS geores AS $$
DECLARE
paddress ALIAS FOR $1;
address varchar;
rec RECORD;
output geores;
BEGIN
output.x := -999;
output.y := -999;
output.code := -99;
output.address := 'なし';
address := replace(paddress,' ','');
address := replace(address,' ','');
SELECT INTO rec * FROM places WHERE name IS NOT NULL AND name LIKE address||'%'
ORDER BY name LIMIT 1;
IF FOUND THEN
output.x := rec.lon;
output.y := rec.lat;
output.address := rec.name;
output.code := 6;
END IF;
RETURN output;
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 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 address_s WHERE
todofuken = r_todofuken AND
tmpstr LIKE shikuchoson||'%'
ORDER BY length(shikuchoson) DESC;
ELSE
SELECT INTO rec * FROM address_s WHERE
address LIKE shikuchoson||'%'
ORDER BY length(shikuchoson) DESC;
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;
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,' ','');
tmpstr := split_part(address,r_shikuchoson,2);
tmpstr := tmpstr || '-'; -- to match addresses like 杉並区清水1
tmpaddr := normalizeAddr( tmpstr );
--
-- Trying to parse Kyoto Addresses which contains Directions
--
IF r_todofuken = '京都府' THEN
--
-- For Kyoto Addresses which adds an extra '字'
--
address := replace(address,'市字','市');
SELECT INTO rec *,length(tr_ooaza) AS length FROM address_o WHERE
todofuken = r_todofuken AND
shikuchoson = r_shikuchoson AND
strpos(tmpaddr,tr_ooaza) > 1 ORDER BY length DESC LIMIT 1;
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;
RETURN output;
END IF;
END IF;
--
-- the 'Order By length' slows down the operation a bit
-- but produces more accurate matches.
--
SELECT INTO rec *,length(tr_ooaza) AS length FROM address_o WHERE
todofuken = r_todofuken AND
shikuchoson = r_shikuchoson AND
strpos(tmpaddr,tr_ooaza) = 1 ORDER BY length DESC LIMIT 1;
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;
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 address_t where todofuken = r_todofuken;
preftab := rec.ttable;
END IF;
IF preftab = '' THEN
RETURN output;
END IF;
address := replace(paddress,' ','');
address := replace(address,' ','');
address := normalizeAddr( address );
ooaza := replace(r_ooaza,' ','');
ooaza := replace(ooaza,' ','');
ooaza := normalizeAddr( ooaza );
-- RAISE NOTICE 'Chiban Parameters passed are : % and %',
-- address,ooaza;
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 ' ||
'shikuchoson = ' || quote_literal(r_shikuchoson) || ' AND ' ||
'ooaza = ' || quote_literal(r_ooaza) || ' AND ' ||
'chiban = ' || quote_literal(tmpstr3);
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;