3 namespace Nominatim\Setup;
6 * Parses an address level description.
8 class AddressLevelParser
12 public function __construct(string $sDescriptionFile)
14 $sJson = file_get_contents($sDescriptionFile);
15 $this->aLevels = json_decode($sJson, true);
16 if (!$this->aLevels) {
17 switch (json_last_error()) {
20 case JSON_ERROR_DEPTH:
21 fail('JSON error - Maximum stack depth exceeded');
23 case JSON_ERROR_STATE_MISMATCH:
24 fail('JSON error - Underflow or the modes mismatch');
26 case JSON_ERROR_CTRL_CHAR:
27 fail('JSON error - Unexpected control character found');
29 case JSON_ERROR_SYNTAX:
30 fail('JSON error - Syntax error, malformed JSON');
33 fail('JSON error - Malformed UTF-8 characters, possibly incorrectly encoded');
36 fail('JSON error - Unknown error');
43 * Dump the description into a database table.
45 * @param object $oDB Database conneciton to use.
46 * @param string $sTable Name of table to create.
50 * A new table is created. Any previously existing table is dropped.
51 * The table has the following columns:
52 * country, class, type, rank_search, rank_address.
54 public function createTable($oDB, $sTable)
56 chksql($oDB->query('DROP TABLE IF EXISTS '.$sTable));
57 $sSql = 'CREATE TABLE '.$sTable;
58 $sSql .= '(country_code varchar(2), class TEXT, type TEXT,';
59 $sSql .= ' rank_search SMALLINT, rank_address SMALLINT)';
60 chksql($oDB->query($sSql));
62 $sSql = 'CREATE UNIQUE INDEX ON '.$sTable.'(country_code, class, type)';
63 chksql($oDB->query($sSql));
65 $sSql = 'INSERT INTO '.$sTable.' VALUES ';
66 foreach ($this->aLevels as $aLevel) {
67 $aCountries = array();
68 if (isset($aLevel['countries'])) {
69 foreach ($aLevel['countries'] as $sCountry) {
70 $aCountries[$sCountry] = getDBQuoted($sCountry);
73 $aCountries['NULL'] = 'NULL';
75 foreach ($aLevel['tags'] as $sKey => $aValues) {
76 foreach ($aValues as $sValue => $mRanks) {
79 $sValue ? getDBQuoted($sValue) : 'NULL'
81 if (is_array($mRanks)) {
82 $aFields[] = (string) $mRanks[0];
83 $aFields[] = (string) $mRanks[1];
85 $aFields[] = (string) $mRanks;
86 $aFields[] = (string) $mRanks;
88 $sLine = ','.join(',', $aFields).'),';
90 foreach ($aCountries as $sCountries) {
91 $sSql .= '('.$sCountries.$sLine;
96 chksql($oDB->query(rtrim($sSql, ',')));