]> git.openstreetmap.org Git - nominatim.git/blob - utils/specialphrases.php
189a7a3e00fef086a0d002dc69665e6a58c380c3
[nominatim.git] / utils / specialphrases.php
1 #!/usr/bin/php -Cq
2 <?php
3
4         require_once(dirname(dirname(__FILE__)).'/settings/settings.php');
5         require_once(CONST_BasePath.'/lib/init-cmd.php');
6         ini_set('memory_limit', '800M');
7         ini_set('display_errors', 'stderr');
8
9         $aCMDOptions = array(
10                 "Import and export special phrases",
11                 array('help', 'h', 0, 1, 0, 0, false, 'Show Help'),
12                 array('quiet', 'q', 0, 1, 0, 0, 'bool', 'Quiet output'),
13                 array('verbose', 'v', 0, 1, 0, 0, 'bool', 'Verbose output'),
14                 array('countries', '', 0, 1, 0, 0, 'bool', 'Create import script for country codes and names'),
15                 array('wiki-import', '', 0, 1, 0, 0, 'bool', 'Create import script for search phrases '),
16         );
17         getCmdOpt($_SERVER['argv'], $aCMDOptions, $aCMDResult, true, true);
18
19                 include(CONST_InstallPath.'/settings/phrase_settings.php');
20
21
22     if ($aCMDResult['countries']) {
23         echo "select getorcreate_country(make_standard_name('uk'), 'gb');\n";
24         echo "select getorcreate_country(make_standard_name('united states'), 'us');\n";
25         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";
26
27         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";
28         foreach($aLanguageIn as $sLanguage)
29                 {
30             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";
31         }
32     }
33
34         if ($aCMDResult['wiki-import'])
35         {
36                 $aPairs = array();
37
38                 foreach($aLanguageIn as $sLanguage)
39                 {
40                         $sURL = 'http://wiki.openstreetmap.org/wiki/Special:Export/Nominatim/Special_Phrases/'.strtoupper($sLanguage);
41                         $sWikiPageXML = file_get_contents($sURL);
42                         if (preg_match_all('#\\| ([^|]+) \\|\\| ([^|]+) \\|\\| ([^|]+) \\|\\| ([^|]+) \\|\\| ([\\-YN])#', $sWikiPageXML, $aMatches, PREG_SET_ORDER))
43                         {
44                                 foreach($aMatches as $aMatch)
45                                 {
46                                         $sLabel = trim($aMatch[1]);
47                                         $sClass = trim($aMatch[2]);
48                                         $sType = trim($aMatch[3]);
49                                         # hack around a bug where building=yes was imported with
50                                         # quotes into the wiki
51                                         $sType = preg_replace('/&quot;/', '', $sType);
52                                         # sanity check, in case somebody added garbage in the wiki
53                                         if (preg_match('/^\\w+$/', $sClass) < 1 ||
54                                                 preg_match('/^\\w+$/', $sType) < 1) {
55                                                 trigger_error("Bad class/type for language $sLanguage: $sClass=$sType");
56                                                 exit;
57                                         }
58                                         # blacklisting: disallow certain class/type combinations
59                                         if (isset($aTagsBlacklist[$sClass]) && in_array($sType, $aTagsBlacklist[$sClass])) {
60                                                 # fwrite(STDERR, "Blacklisted: ".$sClass."/".$sType."\n");
61                                                 continue;
62                                         }
63                                         # whitelisting: if class is in whitelist, allow only tags in the list
64                                         if (isset($aTagsWhitelist[$sClass])     && !in_array($sType, $aTagsWhitelist[$sClass])) {
65                                                 # fwrite(STDERR, "Non-Whitelisted: ".$sClass."/".$sType."\n");
66                                                 continue;
67                                         }
68                                         $aPairs[$sClass.'|'.$sType] = array($sClass, $sType);
69
70                                         switch(trim($aMatch[4]))
71                                         {
72                                         case 'near':
73                                                 echo "select getorcreate_amenityoperator(make_standard_name('".pg_escape_string($sLabel)."'), '$sClass', '$sType', 'near');\n";
74                                                 break;
75                                         case 'in':
76                                                 echo "select getorcreate_amenityoperator(make_standard_name('".pg_escape_string($sLabel)."'), '$sClass', '$sType', 'in');\n";
77                                                 break;
78                                         default:
79                                                 echo "select getorcreate_amenity(make_standard_name('".pg_escape_string($sLabel)."'), '$sClass', '$sType');\n";
80                                                 break;
81                                         }
82                                 }
83                         }
84                 }
85
86         echo "create index idx_placex_classtype on placex (class, type);";
87
88                 foreach($aPairs as $aPair)
89                 {
90                         echo "create table place_classtype_".pg_escape_string($aPair[0])."_".pg_escape_string($aPair[1]);
91                         if (CONST_Tablespace_Aux_Data)
92                                 echo " tablespace ".CONST_Tablespace_Aux_Data;
93                         echo " as select place_id as place_id,st_centroid(geometry) as centroid from placex where ";
94                         echo "class = '".pg_escape_string($aPair[0])."' and type = '".pg_escape_string($aPair[1])."'";
95                         echo ";\n";
96
97                         echo "CREATE INDEX idx_place_classtype_".pg_escape_string($aPair[0])."_".pg_escape_string($aPair[1])."_centroid ";
98                         echo "ON place_classtype_".pg_escape_string($aPair[0])."_".pg_escape_string($aPair[1])." USING GIST (centroid)";
99                         if (CONST_Tablespace_Aux_Index)
100                                 echo " tablespace ".CONST_Tablespace_Aux_Index;
101                         echo ";\n";
102
103                         echo "CREATE INDEX idx_place_classtype_".pg_escape_string($aPair[0])."_".pg_escape_string($aPair[1])."_place_id ";
104                         echo "ON place_classtype_".pg_escape_string($aPair[0])."_".pg_escape_string($aPair[1])." USING btree(place_id)";
105                         if (CONST_Tablespace_Aux_Index)
106                                 echo " tablespace ".CONST_Tablespace_Aux_Index;
107                         echo ";\n";
108
109             echo "GRANT SELECT ON place_classtype_".pg_escape_string($aPair[0])."_".pg_escape_string($aPair[1]).' TO "'.CONST_Database_Web_User."\";\n";
110
111                 }
112
113         echo "drop index idx_placex_classtype;";
114         }