Skip to content

Commit 1d859a4

Browse files
authored
Merge pull request #25 from mbasa/develop
Meshcode Addition
2 parents 1c40186 + bc9797d commit 1d859a4

File tree

6 files changed

+129
-14
lines changed

6 files changed

+129
-14
lines changed

scripts/install.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,7 @@ psql -U ${DBROLE} -d ${DBNAME} -f ./sql/pgGeocoder.sql
1515
# Load reverse_geocoder function
1616
psql -U ${DBROLE} -d ${DBNAME} -f ./sql/pgReverseGeocoder.sql
1717

18+
# Load latlng2jpgridcode function
19+
psql -U ${DBROLE} -d ${DBNAME} -f ./sql/util/latlng2jpgridcode.sql
20+
1821
echo -e "\nDone!"

sql/createTables.sql

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ create table pggeocoder.address_t (
1515
lat float,
1616
lon float,
1717
ttable varchar(40),
18-
code varchar(2),
18+
code varchar(2),
1919
geog geography('POINT'),
20+
meshcode text,
2021
year text
2122
);
2223

@@ -30,6 +31,7 @@ create table pggeocoder.address_s (
3031
lon float,
3132
code varchar(5),
3233
geog geography('POINT'),
34+
meshcode text,
3335
year text
3436
);
3537

@@ -45,6 +47,7 @@ create table pggeocoder.address_o (
4547
lon float,
4648
code varchar(12),
4749
geog geography('POINT'),
50+
meshcode text,
4851
year text
4952
);
5053

@@ -60,6 +63,7 @@ create table pggeocoder.address_c (
6063
lat float,
6164
lon float,
6265
geog geography('POINT'),
66+
meshcode text,
6367
year text
6468
);
6569

@@ -75,7 +79,8 @@ create table pggeocoder.address_g (
7579
go varchar(60),
7680
lat float,
7781
lon float,
78-
geog geography('POINT')
82+
geog geography('POINT'),
83+
meshcode text
7984
);
8085

8186
--

sql/maintTables.sql

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,15 @@
88
--
99
update pggeocoder.address_t set ttable = 'pggeocoder.address_c';
1010

11+
--
12+
-- adding meshcode information
13+
--
14+
update pggeocoder.address_t set meshcode = latlng2jpgridcode(lat,lon,3);
15+
update pggeocoder.address_s set meshcode = latlng2jpgridcode(lat,lon,3);
16+
update pggeocoder.address_o set meshcode = latlng2jpgridcode(lat,lon,3);
17+
update pggeocoder.address_c set meshcode = latlng2jpgridcode(lat,lon,3);
18+
update pggeocoder.address_g set meshcode = latlng2jpgridcode(lat,lon,3);
19+
1120
--
1221
-- creating index for address_t
1322
--

sql/pgGeocoder.sql

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ CREATE TYPE geores AS (
4141
shikuchoson character varying,
4242
ooaza character varying,
4343
chiban character varying,
44-
go character varying
44+
go character varying,
45+
meshcode character varying
4546
);
4647

4748
--
@@ -134,8 +135,8 @@ DECLARE
134135
BEGIN
135136

136137
address := translate(paddress,
137-
'ヶケ?ー―‐−-ーのノ1234567890〇一二三四五六七八九十丁目',
138-
'がが---------12345678900123456789X-');
138+
'?ー―‐−-ーのノ1234567890〇一二三四五六七八九十丁目',
139+
'---------12345678900123456789X-');
139140

140141
IF strpos( address, 'X') <> 0 THEN
141142
tmparr := string_to_array( address,'X');
@@ -203,12 +204,19 @@ BEGIN
203204
address := regexp_replace(address, '^(大字|字)', '');
204205
END IF;
205206

207+
--
208+
-- Kanji correction.
209+
--
210+
address := translate( address,
211+
'淵壷蛍殼惠鴬靭ッ涛',
212+
'渕壺螢殻恵鶯靱ツ濤');
213+
206214
--
207215
-- Adding Kobayashi-san's rule set
208216
--
209217
address := translate( address,
210-
'榮之ノ治ヰヱ淵渕輿曽藪薮籠篭劔峯峰岡丘富冨祓桧檜莱洲冶治壇檀舘館斉斎竈竃朗鷆膳録嶋崎埼碕庄荘横橫鄕神塚塚都都德福朗郞嶽區溪縣廣斎眞槇槙莊藏龍瀧澤當邊舖萬豫禮茅礪砺',
211-
'-の冶いえ渕淵興曾薮藪篭籠剱峰峯丘岡冨富秡檜桧来州治冶檀壇館舘斎斉釜釜郎鷏善禄島埼崎崎荘庄橫横郷神塚塚都都徳福朗郎岳区渓県広斉真槙槇荘蔵竜滝沢当辺舗万予礼芽砺礪'
218+
'ヶケ榮之ノ治ヰヱ淵渕輿曽藪薮籠篭劔峯峰岡丘富冨祓桧檜莱洲冶治壇檀舘館斉斎竈竃朗鷆膳録嶋崎埼碕庄荘横橫鄕神塚塚都都德福朗郞嶽區溪縣廣斎眞槇槙莊藏龍瀧澤當邊舖萬豫禮茅礪砺',
219+
'がが栄-の冶いえ渕淵興曾薮藪篭籠剱峰峯丘岡冨富秡檜桧来州治冶檀壇館舘斎斉釜釜郎鷏善禄島埼崎崎荘庄橫横郷神塚塚都都徳福朗郎岳区渓県広斉真槙槇荘蔵竜滝沢当辺舗万予礼芽砺礪'
212220
);
213221

214222
--
@@ -292,6 +300,7 @@ BEGIN
292300
output.code := 4;
293301
output.address := rec.todofuken;
294302
output.todofuken := rec.todofuken;
303+
output.meshcode := rec.meshcode;
295304
ELSE
296305
output.code := 5;
297306
END IF;
@@ -366,6 +375,7 @@ BEGIN
366375
output.address := rec.todofuken || rec.shikuchoson;
367376
output.todofuken := rec.todofuken;
368377
output.shikuchoson:= rec.shikuchoson;
378+
output.meshcode := rec.meshcode;
369379
END IF;
370380

371381
RETURN output;
@@ -447,6 +457,18 @@ BEGIN
447457
tr_shikuchoson = t_shikuchoson AND
448458
strpos(tmpaddr,tr_ooaza) = 1
449459
ORDER BY length DESC,year DESC LIMIT 1;
460+
461+
--
462+
-- 2nd Searching for correct District ('郡')
463+
--
464+
IF NOT FOUND AND t_shikuchoson ~ '' THEN
465+
SELECT INTO rec *,length(tr_ooaza) AS length
466+
FROM pggeocoder.address_o WHERE
467+
tr_shikuchoson LIKE '%郡'||split_part(t_shikuchoson,'',2) AND
468+
strpos(tmpaddr,tr_ooaza) = 1
469+
ORDER BY length DESC,year DESC LIMIT 1;
470+
END IF;
471+
450472
END IF;
451473

452474
IF FOUND THEN
@@ -456,7 +478,8 @@ BEGIN
456478
output.address := rec.todofuken||rec.shikuchoson||rec.ooaza;
457479
output.todofuken := rec.todofuken;
458480
output.shikuchoson:= rec.shikuchoson;
459-
output.ooaza := rec.ooaza;
481+
output.ooaza := rec.ooaza;
482+
output.meshcode := rec.meshcode;
460483
END IF;
461484

462485
RETURN output;
@@ -567,6 +590,7 @@ BEGIN
567590
output.shikuchoson:= rec.shikuchoson;
568591
output.ooaza := rec.ooaza;
569592
output.chiban := rec.chiban;
593+
output.meshcode := rec.meshcode;
570594
END IF;
571595

572596
RETURN output;
@@ -641,6 +665,7 @@ BEGIN
641665
output.ooaza := r_ooaza;
642666
output.chiban := rec.chiban;
643667
output.go := tmpstr3;
668+
output.meshcode := rec.meshcode;
644669
END IF;
645670

646671
RETURN output;

sql/pgReverseGeocoder.sql

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ CREATE TYPE geores AS (
4141
shikuchoson character varying,
4242
ooaza character varying,
4343
chiban character varying,
44-
go character varying
44+
go character varying,
45+
meshcode character varying
4546
);
4647

4748
--
@@ -90,7 +91,7 @@ BEGIN
9091
-- change, depending on the ABR dataset.
9192
--
9293
SELECT INTO record todofuken, shikuchoson, ooaza, chiban, go,
93-
lon, lat,
94+
meshcode,lon, lat,
9495
todofuken||shikuchoson||ooaza||chiban||'-'||go AS address
9596
FROM pggeocoder.address_g
9697
WHERE st_dwithin(point, geog,mDist)
@@ -106,13 +107,14 @@ BEGIN
106107
output.ooaza := record.ooaza;
107108
output.chiban := record.chiban;
108109
output.go := record.go;
110+
output.meshcode := record.meshcode;
109111
RETURN output;
110112
END IF;
111113

112114
SELECT INTO o_bdry geom FROM pggeocoder.boundary_o WHERE st_intersects(point,geom);
113115
IF FOUND THEN
114116
SELECT INTO record todofuken, shikuchoson, ooaza, chiban,
115-
lon, lat,
117+
meshcode,lon, lat,
116118
todofuken||shikuchoson||ooaza||chiban AS address,
117119
st_distance(point::geography,geog) AS dist
118120
FROM pggeocoder.address_c
@@ -128,11 +130,11 @@ BEGIN
128130
output.shikuchoson:= record.shikuchoson;
129131
output.ooaza := record.ooaza;
130132
output.chiban := record.chiban;
131-
133+
output.meshcode := record.meshcode;
132134
RETURN output;
133135
ELSE
134136
SELECT INTO record todofuken, shikuchoson, ooaza, NULL as chiban,
135-
lon, lat,
137+
meshcode,lon, lat,
136138
todofuken||shikuchoson||ooaza AS address,
137139
st_distance(point::geography,geog) AS dist
138140
FROM pggeocoder.address_o
@@ -148,6 +150,7 @@ BEGIN
148150
output.shikuchoson:= record.shikuchoson;
149151
output.ooaza := record.ooaza;
150152
output.chiban := record.chiban;
153+
output.meshcode := record.meshcode;
151154
RETURN output;
152155
ELSE
153156
s_flag := TRUE;
@@ -161,7 +164,7 @@ BEGIN
161164
SELECT INTO s_bdry geom FROM pggeocoder.boundary_s WHERE st_intersects(point,geom);
162165
IF FOUND THEN
163166
SELECT INTO record todofuken, shikuchoson, NULL as ooaza, NULL as chiban,
164-
lon, lat,
167+
meshcode,lon, lat,
165168
todofuken||shikuchoson AS address, 0 AS dist
166169
FROM pggeocoder.address_s AS a
167170
WHERE st_intersects(a.geog, s_bdry.geom::geography);
@@ -174,6 +177,7 @@ BEGIN
174177
output.shikuchoson:= record.shikuchoson;
175178
output.ooaza := record.ooaza;
176179
output.chiban := record.chiban;
180+
output.meshcode := record.meshcode;
177181
RETURN output;
178182
END IF;
179183
END IF;

sql/util/latlng2jpgridcode.sql

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
-- ***********************
2+
-- * Source: https://github.com/boiledorange73/pg_jpgrid/blob/main/latlng2jpgridcode.sql
3+
-- * License: BSD
4+
-- ***********************
5+
6+
CREATE OR REPLACE FUNCTION latlng2jpgridcode(lat DOUBLE PRECISION, lng DOUBLE PRECISION, level INTEGER) RETURNS TEXT AS $$
7+
DECLARE
8+
lats INTEGER;
9+
lngs INTEGER;
10+
ret TEXT;
11+
dlatsh DOUBLE PRECISION;
12+
dlngsh DOUBLE PRECISION;
13+
c INTEGER;
14+
n INTEGER;
15+
BEGIN
16+
IF lat < 20 OR lat > 46 OR lng < 122 OR lng > 155 THEN
17+
RETURN NULL;
18+
END IF;
19+
-- 1st
20+
lats := 3600 * lat;
21+
lngs := 3600 * lng - 360000;
22+
ret := LPAD((lats / 2400)::TEXT, 2, '0') || LPAD((lngs / 3600)::TEXT, 2, '0');
23+
lats := lats % 2400;
24+
lngs := lngs % 3600;
25+
-- 2nd
26+
IF level >= 2 THEN
27+
ret := ret || LPAD((lats / 300)::TEXT, 1, '0') || LPAD((lngs / 450)::TEXT, 1, '0');
28+
lats := lats % 300;
29+
lngs := lngs % 450;
30+
END IF;
31+
-- 3rd
32+
IF level >= 3 THEN
33+
ret := ret || LPAD((lats / 30)::TEXT, 1, '0') || LPAD((lngs / 45)::TEXT, 1, '0');
34+
lats := lats % 30;
35+
lngs := lngs % 45;
36+
END IF;
37+
-- 4th and beyond
38+
dlatsh := 30;
39+
dlngsh := 45;
40+
FOR n IN 4..level LOOP
41+
dlatsh := 0.5 * dlatsh;
42+
dlngsh := 0.5 * dlngsh;
43+
c := 1;
44+
IF lats > dlatsh THEN
45+
c := 3;
46+
lats := lats - dlatsh;
47+
END IF;
48+
IF lngs > dlngsh THEN
49+
c := c + 1;
50+
lngs := lngs - dlngsh;
51+
END IF;
52+
ret := ret || c::TEXT;
53+
END LOOP;
54+
-- fin
55+
RETURN ret;
56+
END;
57+
$$ LANGUAGE plpgsql;
58+
59+
CREATE OR REPLACE FUNCTION point2jpgridcode(p GEOMETRY(POINT), level INTEGER) RETURNS TEXT AS $$
60+
DECLARE
61+
lat DOUBLE PRECISION;
62+
lng DOUBLE PRECISION;
63+
BEGIN
64+
IF p IS NULL THEN
65+
RETURN NULL;
66+
END IF;
67+
RETURN latlng2jpgridcode(ST_Y(p), ST_X(p), level);
68+
END;
69+
$$ LANGUAGE plpgsql;

0 commit comments

Comments
 (0)