]> git.openstreetmap.org Git - nominatim.git/blob - utils/specialphrases.php
hack around yes quotes in wiki and add a simple sanity check against wiki accidents
[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
14                 array('wiki-import', '', 0, 1, 0, 0, 'bool', 'Create nominatim db'),
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['wiki-import'])
53         {
54                 $aPairs = array();
55
56                 foreach($aLanguageIn as $sLanguage)
57                 {
58                         $sURL = 'http://wiki.openstreetmap.org/wiki/Special:Export/Nominatim/Special_Phrases/'.strtoupper($sLanguage);
59                         $sWikiPageXML = file_get_contents($sURL);
60                         if (preg_match_all('#\\| ([^|]+) \\|\\| ([^|]+) \\|\\| ([^|]+) \\|\\| ([^|]+) \\|\\| ([\\-YN])#', $sWikiPageXML, $aMatches, PREG_SET_ORDER))
61                         {
62                                 foreach($aMatches as $aMatch)
63                                 {
64                                         $sLabel = $aMatch[1];
65                                         $sClass = $aMatch[2];
66                                         $sType = $aMatch[3];
67                                         # hack around a bug where building=yes was imported with
68                                         # quotes into the wiki
69                                         $sType = preg_replace('/&quot;/', '', $sType);
70                                         # sanity check, in case somebody added garbage in the wiki
71                                         if (preg_match('/^\\w+$/', $sClass) < 1 ||
72                                                 preg_match('/^\\w+$/', $sType) < 1) {
73                                                 trigger_error("Bad class/type for language $sLanguage: $sClass=$sType");
74                                                 exit;
75                                         }       
76                                         $aPairs[$sClass.'|'.$sType] = array($sClass, $sType);
77
78                                         switch(trim($aMatch[4]))
79                                         {
80                                         case 'near':
81                                                 echo "select getorcreate_amenityoperator(make_standard_name('".pg_escape_string($sLabel)."'), '$sClass', '$sType', 'near');\n";
82                                                 break;
83                                         case 'in':
84                                                 echo "select getorcreate_amenityoperator(make_standard_name('".pg_escape_string($sLabel)."'), '$sClass', '$sType', 'in');\n";
85                                                 break;
86                                         default:
87                                                 echo "select getorcreate_amenity(make_standard_name('".pg_escape_string($sLabel)."'), '$sClass', '$sType');\n";
88                                                 break;
89                                         }
90                                 }
91                         }
92                 }
93
94         echo "create index idx_placex_classtype on placex (class, type);";
95
96                 foreach($aPairs as $aPair)
97                 {
98                         if ($aPair[1] == 'highway') continue;
99
100                         echo "create table place_classtype_".pg_escape_string($aPair[0])."_".pg_escape_string($aPair[1])." as ";
101                         echo "select place_id as place_id,st_centroid(geometry) as centroid from placex where ";
102                         echo "class = '".pg_escape_string($aPair[0])."' and type = '".pg_escape_string($aPair[1])."';\n";
103
104                         echo "CREATE INDEX idx_place_classtype_".pg_escape_string($aPair[0])."_".pg_escape_string($aPair[1])."_centroid ";
105                         echo "ON place_classtype_".pg_escape_string($aPair[0])."_".pg_escape_string($aPair[1])." USING GIST (centroid);\n";
106
107                         echo "CREATE INDEX idx_place_classtype_".pg_escape_string($aPair[0])."_".pg_escape_string($aPair[1])."_place_id ";
108                         echo "ON place_classtype_".pg_escape_string($aPair[0])."_".pg_escape_string($aPair[1])." USING btree(place_id);\n";
109
110             echo "GRANT SELECT ON place_classtype_".pg_escape_string($aPair[0])."_".pg_escape_string($aPair[1])." TO \"www-data\";";
111
112                 }
113
114         echo "drop index idx_placex_classtype;";
115         }