]> git.openstreetmap.org Git - nominatim.git/blob - utils/specialphrases.php
Merge remote-tracking branch 'lonvia/address-order'
[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 country 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                 include(CONST_BasePath.'/settings/phrase_settings.php');
19
20
21     if ($aCMDResult['countries']) {
22         echo "select getorcreate_country(make_standard_name('uk'), 'gb');\n";
23         echo "select getorcreate_country(make_standard_name('united states'), 'us');\n";
24         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";
25
26         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";
27         foreach($aLanguageIn as $sLanguage)
28                 {
29             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";
30         }
31     }
32
33         if ($aCMDResult['wiki-import'])
34         {
35                 $aPairs = array();
36
37                 foreach($aLanguageIn as $sLanguage)
38                 {
39                         $sURL = 'http://wiki.openstreetmap.org/wiki/Special:Export/Nominatim/Special_Phrases/'.strtoupper($sLanguage);
40                         $sWikiPageXML = file_get_contents($sURL);
41                         if (preg_match_all('#\\| ([^|]+) \\|\\| ([^|]+) \\|\\| ([^|]+) \\|\\| ([^|]+) \\|\\| ([\\-YN])#', $sWikiPageXML, $aMatches, PREG_SET_ORDER))
42                         {
43                                 foreach($aMatches as $aMatch)
44                                 {
45                                         $sLabel = trim($aMatch[1]);
46                                         $sClass = trim($aMatch[2]);
47                                         $sType = trim($aMatch[3]);
48                                         # hack around a bug where building=yes was imported with
49                                         # quotes into the wiki
50                                         $sType = preg_replace('/&quot;/', '', $sType);
51                                         # sanity check, in case somebody added garbage in the wiki
52                                         if (preg_match('/^\\w+$/', $sClass) < 1 ||
53                                                 preg_match('/^\\w+$/', $sType) < 1) {
54                                                 trigger_error("Bad class/type for language $sLanguage: $sClass=$sType");
55                                                 exit;
56                                         }
57                                         # blacklisting: disallow certain class/type combinations
58                                         if (isset($aTagsBlacklist[$sClass]) && in_array($sType, $aTagsBlacklist[$sClass])) {
59                                                 # fwrite(STDERR, "Blacklisted: ".$sClass."/".$sType."\n");
60                                                 continue;
61                                         }
62                                         # whitelisting: if class is in whitelist, allow only tags in the list
63                                         if (isset($aTagsWhitelist[$sClass])     && !in_array($sType, $aTagsWhitelist[$sClass])) {
64                                                 # fwrite(STDERR, "Non-Whitelisted: ".$sClass."/".$sType."\n");
65                                                 continue;
66                                         }
67                                         $aPairs[$sClass.'|'.$sType] = array($sClass, $sType);
68
69                                         switch(trim($aMatch[4]))
70                                         {
71                                         case 'near':
72                                                 echo "select getorcreate_amenityoperator(make_standard_name('".pg_escape_string($sLabel)."'), '$sClass', '$sType', 'near');\n";
73                                                 break;
74                                         case 'in':
75                                                 echo "select getorcreate_amenityoperator(make_standard_name('".pg_escape_string($sLabel)."'), '$sClass', '$sType', 'in');\n";
76                                                 break;
77                                         default:
78                                                 echo "select getorcreate_amenity(make_standard_name('".pg_escape_string($sLabel)."'), '$sClass', '$sType');\n";
79                                                 break;
80                                         }
81                                 }
82                         }
83                 }
84
85         echo "create index idx_placex_classtype on placex (class, type);";
86
87                 foreach($aPairs as $aPair)
88                 {
89                         if ($aPair[0] == 'yes') continue;
90                         if ($aPair[1] == 'yes') continue;
91                         if ($aPair[0] == 'highway') continue;
92                         if ($aPair[1] == 'highway') continue;
93
94                         echo "create table place_classtype_".pg_escape_string($aPair[0])."_".pg_escape_string($aPair[1])." as ";
95                         echo "select place_id as place_id,st_centroid(geometry) as centroid from placex where ";
96                         echo "class = '".pg_escape_string($aPair[0])."' and type = '".pg_escape_string($aPair[1])."';\n";
97
98                         echo "CREATE INDEX idx_place_classtype_".pg_escape_string($aPair[0])."_".pg_escape_string($aPair[1])."_centroid ";
99                         echo "ON place_classtype_".pg_escape_string($aPair[0])."_".pg_escape_string($aPair[1])." USING GIST (centroid);\n";
100
101                         echo "CREATE INDEX idx_place_classtype_".pg_escape_string($aPair[0])."_".pg_escape_string($aPair[1])."_place_id ";
102                         echo "ON place_classtype_".pg_escape_string($aPair[0])."_".pg_escape_string($aPair[1])." USING btree(place_id);\n";
103
104             echo "GRANT SELECT ON place_classtype_".pg_escape_string($aPair[0])."_".pg_escape_string($aPair[1])." TO \"www-data\";";
105
106                 }
107
108         echo "drop index idx_placex_classtype;";
109         }