2 // Script to extract structured city and street data
3 // from a running nominatim instance as CSV data
6 require_once(CONST_BasePath.'/lib/init-cmd.php');
7 require_once(CONST_BasePath.'/lib/ParameterParser.php');
8 ini_set('memory_limit', '800M');
11 'Export addresses as CSV file from a Nominatim database',
12 array('help', 'h', 0, 1, 0, 0, false, 'Show Help'),
13 array('quiet', 'q', 0, 1, 0, 0, 'bool', 'Quiet output'),
14 array('verbose', 'v', 0, 1, 0, 0, 'bool', 'Verbose output'),
16 array('output-type', '', 0, 1, 1, 1, 'str', 'Type of places to output (see below)'),
17 array('output-format', '', 0, 1, 1, 1, 'str', 'Column mapping (see below)'),
18 array('output-all-postcodes', '', 0, 1, 0, 0, 'bool', 'List all postcodes for address instead of just the most likely one'),
19 array('language', '', 0, 1, 1, 1, 'str', 'Preferred language for output (local name, if omitted)'),
20 array('restrict-to-country', '', 0, 1, 1, 1, 'str', 'Export only objects within country (country code)'),
21 array('restrict-to-osm-node', '', 0, 1, 1, 1, 'int', 'Export only objects that are children of this OSM node'),
22 array('restrict-to-osm-way', '', 0, 1, 1, 1, 'int', 'Export only objects that are children of this OSM way'),
23 array('restrict-to-osm-relation', '', 0, 1, 1, 1, 'int', 'Export only objects that are children of this OSM relation'),
24 "\nAddress ranks: continent, country, state, county, city, suburb, street, path",
25 'Additional output types: postcode, placeid (placeid for each object)',
26 "\noutput-format must be a semicolon-separated list of address ranks. Multiple ranks",
27 'can be merged into one column by simply using a comma-separated list.',
28 "\nDefault output-type: street",
29 'Default output format: street;suburb;city;county;state;country'
31 getCmdOpt($_SERVER['argv'], $aCMDOptions, $aCMDResult, true, true);
44 $oDB = new Nominatim\DB();
47 if (isset($aCMDResult['output-type'])) {
48 if (!isset($aRankmap[$aCMDResult['output-type']])) fail('unknown output-type: '.$aCMDResult['output-type']);
49 $iOutputRank = $aRankmap[$aCMDResult['output-type']];
51 $iOutputRank = $aRankmap['street'];
56 $oParams = new Nominatim\ParameterParser();
57 if (!isset($aCMDResult['language'])) $aCMDResult['language'] = 'xx';
58 $aLangPrefOrder = $oParams->getPreferredLanguages($aCMDResult['language']);
59 $sLanguagePrefArraySQL = $oDB->getArraySQL($oDB->getDBQuotedList($aLangPrefOrder));
61 // output formatting: build up a lookup table that maps address ranks to columns
62 $aColumnMapping = array();
64 if (!isset($aCMDResult['output-format'])) $aCMDResult['output-format'] = 'street;suburb;city;county;state;country';
65 foreach (preg_split('/\s*;\s*/', $aCMDResult['output-format']) as $sColumn) {
67 foreach (preg_split('/\s*,\s*/', $sColumn) as $sRank) {
68 if ($sRank == 'postcode' || $sRank == 'placeid') {
69 $aColumnMapping[$sRank] = $iNumCol;
71 } elseif (isset($aRankmap[$sRank])) {
72 $iRank = $aRankmap[$sRank];
73 if ($iRank <= $iOutputRank) {
74 $aColumnMapping[(string)$iRank] = $iNumCol;
79 if ($bHasData) $iNumCol++;
82 // build the query for objects
83 $sPlacexSQL = 'select min(place_id) as place_id, ';
84 $sPlacexSQL .= 'array_agg(place_id) as place_ids, ';
85 $sPlacexSQL .= 'country_code as cc, ';
86 $sPlacexSQL .= 'postcode, ';
87 // get the address places excluding postcodes
88 $sPlacexSQL .= 'array(select address_place_id from place_addressline a';
89 $sPlacexSQL .= ' where a.place_id = placex.place_id and isaddress';
90 $sPlacexSQL .= ' and address_place_id != placex.place_id';
91 $sPlacexSQL .= ' and not cached_rank_address in (5,11)';
92 $sPlacexSQL .= ' and cached_rank_address > 2 order by cached_rank_address)';
93 $sPlacexSQL .= ' as address';
94 $sPlacexSQL .= ' from placex where name is not null and linked_place_id is null';
96 $sPlacexSQL .= ' and rank_address = '.$iOutputRank;
98 if (isset($aCMDResult['restrict-to-country'])) {
99 $sPlacexSQL .= ' and country_code = '.$oDB->getDBQuoted($aCMDResult['restrict-to-country']);
102 // restriction to parent place id
106 if (isset($aCMDResult['restrict-to-osm-node'])) {
108 $sOsmId = $aCMDResult['restrict-to-osm-node'];
110 if (isset($aCMDResult['restrict-to-osm-way'])) {
112 $sOsmId = $aCMDResult['restrict-to-osm-way'];
114 if (isset($aCMDResult['restrict-to-osm-relation'])) {
116 $sOsmId = $aCMDResult['restrict-to-osm-relation'];
119 $sSQL = 'select place_id from placex where';
120 $sSQL .= ' osm_type = '.$oDB->getDBQuoted($sOsmType);
121 $sSQL .= ' and osm_id = '.$sOsmId;
122 $sParentId = $oDB->getOne($sSQL);
123 if (!$sParentId) fail('Could not find place '.$sOsmType.' '.$sOsmId);
126 $sPlacexSQL .= ' and place_id in (select place_id from place_addressline where address_place_id = '.$sParentId.' and isaddress)';
129 $sPlacexSQL .= " group by name->'name', address, postcode, country_code, placex.place_id";
131 // Iterate over placeids
132 // to get further hierarchical information
133 //var_dump($sPlacexSQL);
134 $aRes =& $oDB->query($sPlacexSQL);
135 $fOutstream = fopen('php://output', 'w');
136 while ($aRes->fetchInto($aRow)) {
138 $iPlaceID = $aRow['place_id'];
139 $sSQL = "select rank_address,get_name_by_language(name,$sLanguagePrefArraySQL) as localname from get_addressdata($iPlaceID, -1)";
140 $sSQL .= ' WHERE isaddress';
141 $sSQL .= ' order by rank_address desc,isaddress desc';
142 $aAddressLines = $oDB->getAll($sSQL);
144 $aOutput = array_fill(0, $iNumCol, '');
145 // output address parts
146 foreach ($aAddressLines as $aAddress) {
147 if (isset($aColumnMapping[$aAddress['rank_address']])) {
148 $aOutput[$aColumnMapping[$aAddress['rank_address']]] = $aAddress['localname'];
152 if (isset($aColumnMapping['postcode'])) {
153 if ($aCMDResult['output-all-postcodes']) {
154 $sSQL = 'select array_agg(px.postcode) from placex px join place_addressline pa ';
155 $sSQL .= 'on px.place_id = pa.address_place_id ';
156 $sSQL .= 'where pa.cached_rank_address in (5,11) ';
157 $sSQL .= 'and pa.place_id in (select place_id from place_addressline where address_place_id in ('.substr($aRow['place_ids'], 1, -1).')) ';
158 $sSQL .= 'group by postcode order by count(*) desc limit 1';
159 $sRes = $oDB->getOne($sSQL);
161 $aOutput[$aColumnMapping['postcode']] = substr($sRes, 1, -1);
163 $aOutput[$aColumnMapping['postcode']] = $aRow['postcode'];
166 if (isset($aColumnMapping['placeid'])) {
167 $aOutput[$aColumnMapping['placeid']] = substr($aRow['place_ids'], 1, -1);
169 fputcsv($fOutstream, $aOutput);