]> git.openstreetmap.org Git - nominatim.git/blob - utils/specialphrases.php
restrict detection of lat/lon pairs
[nominatim.git] / utils / specialphrases.php
1 #!/usr/bin/php -Cq
2 <?php
3
4         require_once(dirname(dirname(__FILE__)).'/lib/init-cmd.php');
5         ini_set('memory_limit', '800M');
6         ini_set('display_errors', 'stderr');
7
8         $aCMDOptions = array(
9                 "Import and export special phrases",
10                 array('help', 'h', 0, 1, 0, 0, false, 'Show Help'),
11                 array('quiet', 'q', 0, 1, 0, 0, 'bool', 'Quiet output'),
12                 array('verbose', 'v', 0, 1, 0, 0, 'bool', 'Verbose output'),
13                 array('countries', '', 0, 1, 0, 0, 'bool', 'Create import script for coutry codes and names'),
14                 array('wiki-import', '', 0, 1, 0, 0, 'bool', 'Create import script for search phrases '),
15         );
16         getCmdOpt($_SERVER['argv'], $aCMDOptions, $aCMDResult, true, true);
17
18         $aLanguageIn = array(
19                         'af',
20                         'ar',
21                         'br',
22                         'ca',
23                         'cs',
24                         'de',
25                         'en',
26                         'es',
27                         'et',
28                         'eu',
29                         'fa',
30                         'fi',
31                         'fr',
32                         'gl',
33                         'hr',
34                         'hu',
35                         'ia',
36                         'is',
37                         'it',
38                         'ja',
39                         'mk',
40                         'nl',
41                         'no',
42                         'pl',
43                         'ps',
44                         'pt',
45                         'ru',
46                         'sk',
47                         'sv',
48                         'uk',
49                         'vi',
50                 );
51
52     if ($aCMDResult['countries']) {
53         echo "select getorcreate_country(make_standard_name('uk'), 'gb');\n";
54         echo "select getorcreate_country(make_standard_name('united states'), 'us');\n";
55         echo "select count(*) from (select getorcreate_country(make_standard_name(country_code), country_code) from country_name where country_code is not null) as x;\n";
56
57         echo "select count(*) from (select getorcreate_country(make_standard_name(get_name_by_language(country_name.name,ARRAY['name'])), country_code) from country_name where get_name_by_language(country_name.name, ARRAY['name']) is not null) as x;\n";
58         foreach($aLanguageIn as $sLanguage)
59                 {
60             echo "select count(*) from (select getorcreate_country(make_standard_name(get_name_by_language(country_name.name,ARRAY['name:".$sLanguage."'])), country_code) from country_name where get_name_by_language(country_name.name, ARRAY['name:".$sLanguage."']) is not null) as x;\n";
61         }
62     }
63
64         if ($aCMDResult['wiki-import'])
65         {
66                 $aPairs = array();
67
68                 foreach($aLanguageIn as $sLanguage)
69                 {
70                         $sURL = 'http://wiki.openstreetmap.org/wiki/Special:Export/Nominatim/Special_Phrases/'.strtoupper($sLanguage);
71                         $sWikiPageXML = file_get_contents($sURL);
72                         if (preg_match_all('#\\| ([^|]+) \\|\\| ([^|]+) \\|\\| ([^|]+) \\|\\| ([^|]+) \\|\\| ([\\-YN])#', $sWikiPageXML, $aMatches, PREG_SET_ORDER))
73                         {
74                                 foreach($aMatches as $aMatch)
75                                 {
76                                         $sLabel = $aMatch[1];
77                                         $sClass = $aMatch[2];
78                                         $sType = $aMatch[3];
79                                         # hack around a bug where building=yes was imported with
80                                         # quotes into the wiki
81                                         $sType = preg_replace('/&quot;/', '', $sType);
82                                         # sanity check, in case somebody added garbage in the wiki
83                                         if (preg_match('/^\\w+$/', $sClass) < 1 ||
84                                                 preg_match('/^\\w+$/', $sType) < 1) {
85                                                 trigger_error("Bad class/type for language $sLanguage: $sClass=$sType");
86                                                 exit;
87                                         }       
88                                         $aPairs[$sClass.'|'.$sType] = array($sClass, $sType);
89
90                                         switch(trim($aMatch[4]))
91                                         {
92                                         case 'near':
93                                                 echo "select getorcreate_amenityoperator(make_standard_name('".pg_escape_string($sLabel)."'), '$sClass', '$sType', 'near');\n";
94                                                 break;
95                                         case 'in':
96                                                 echo "select getorcreate_amenityoperator(make_standard_name('".pg_escape_string($sLabel)."'), '$sClass', '$sType', 'in');\n";
97                                                 break;
98                                         default:
99                                                 echo "select getorcreate_amenity(make_standard_name('".pg_escape_string($sLabel)."'), '$sClass', '$sType');\n";
100                                                 break;
101                                         }
102                                 }
103                         }
104                 }
105
106         echo "create index idx_placex_classtype on placex (class, type);";
107
108                 foreach($aPairs as $aPair)
109                 {
110                         if ($aPair[1] == 'highway') continue;
111
112                         echo "create table place_classtype_".pg_escape_string($aPair[0])."_".pg_escape_string($aPair[1])." as ";
113                         echo "select place_id as place_id,st_centroid(geometry) as centroid from placex where ";
114                         echo "class = '".pg_escape_string($aPair[0])."' and type = '".pg_escape_string($aPair[1])."';\n";
115
116                         echo "CREATE INDEX idx_place_classtype_".pg_escape_string($aPair[0])."_".pg_escape_string($aPair[1])."_centroid ";
117                         echo "ON place_classtype_".pg_escape_string($aPair[0])."_".pg_escape_string($aPair[1])." USING GIST (centroid);\n";
118
119                         echo "CREATE INDEX idx_place_classtype_".pg_escape_string($aPair[0])."_".pg_escape_string($aPair[1])."_place_id ";
120                         echo "ON place_classtype_".pg_escape_string($aPair[0])."_".pg_escape_string($aPair[1])." USING btree(place_id);\n";
121
122             echo "GRANT SELECT ON place_classtype_".pg_escape_string($aPair[0])."_".pg_escape_string($aPair[1])." TO \"www-data\";";
123
124                 }
125
126         echo "drop index idx_placex_classtype;";
127         }