3 require_once(CONST_BasePath.'/lib/init-website.php');
 
   4 require_once(CONST_BasePath.'/lib/log.php');
 
   5 require_once(CONST_BasePath.'/lib/output.php');
 
   6 require_once(CONST_BasePath.'/lib/AddressDetails.php');
 
   7 ini_set('memory_limit', '200M');
 
   9 $oParams = new Nominatim\ParameterParser();
 
  11 $sOutputFormat = $oParams->getSet('format', array('html', 'json'), 'html');
 
  12 set_exception_handler_by_format($sOutputFormat);
 
  14 $aLangPrefOrder = $oParams->getPreferredLanguages();
 
  16 $sPlaceId = $oParams->getString('place_id');
 
  17 $sOsmType = $oParams->getSet('osmtype', array('N', 'W', 'R'));
 
  18 $iOsmId = $oParams->getInt('osmid', -1);
 
  19 $sClass = $oParams->getString('class');
 
  21 $bIncludeKeywords = $oParams->getBool('keywords', false);
 
  22 $bIncludeAddressDetails = $oParams->getBool('addressdetails', $sOutputFormat == 'html');
 
  23 $bIncludeLinkedPlaces = $oParams->getBool('linkedplaces', true);
 
  24 $bIncludeHierarchy = $oParams->getBool('hierarchy', $sOutputFormat == 'html');
 
  25 $bGroupHierarchy = $oParams->getBool('group_hierarchy', false);
 
  26 $bIncludePolygonAsGeoJSON = $oParams->getBool('polygon_geojson', $sOutputFormat == 'html');
 
  28 $oDB = new Nominatim\DB();
 
  31 $sLanguagePrefArraySQL = $oDB->getArraySQL($oDB->getDBQuotedList($aLangPrefOrder));
 
  33 if ($sOutputFormat == 'html' && !$sPlaceId && !$sOsmType) {
 
  34     include(CONST_BasePath.'/lib/template/details-index-html.php');
 
  38 if ($sOsmType && $iOsmId > 0) {
 
  39     $sSQL = 'SELECT place_id FROM placex WHERE osm_type = :type AND osm_id = :id';
 
  40     // osm_type and osm_id are not unique enough
 
  42         $sSQL .= " AND class='".$sClass."'";
 
  44     $sSQL .= ' ORDER BY class ASC';
 
  45     $sPlaceId = $oDB->getOne($sSQL, array(':type' => $sOsmType, ':id' => $iOsmId));
 
  47     // Be nice about our error messages for broken geometry
 
  51         $sSQL .= '    osm_type, ';
 
  53         $sSQL .= '    errormessage, ';
 
  56         $sSQL .= "    get_name_by_language(name,$sLanguagePrefArraySQL) AS localname,";
 
  57         $sSQL .= '    ST_AsText(prevgeometry) AS prevgeom, ';
 
  58         $sSQL .= '    ST_AsText(newgeometry) AS newgeom';
 
  59         $sSQL .= ' FROM import_polygon_error ';
 
  60         $sSQL .= ' WHERE osm_type = :type';
 
  61         $sSQL .= '   AND osm_id = :id';
 
  62         $sSQL .= ' ORDER BY updated DESC';
 
  64         $aPointDetails = $oDB->getRow($sSQL, array(':type' => $sOsmType, ':id' => $iOsmId));
 
  66             if (preg_match('/\[(-?\d+\.\d+) (-?\d+\.\d+)\]/', $aPointDetails['errormessage'], $aMatches)) {
 
  67                 $aPointDetails['error_x'] = $aMatches[1];
 
  68                 $aPointDetails['error_y'] = $aMatches[2];
 
  70                 $aPointDetails['error_x'] = 0;
 
  71                 $aPointDetails['error_y'] = 0;
 
  73             include(CONST_BasePath.'/lib/template/details-error-'.$sOutputFormat.'.php');
 
  80 if ($sPlaceId === false) userError('Please select a place id');
 
  82 $iPlaceID = (int)$sPlaceId;
 
  84 if (CONST_Use_US_Tiger_Data) {
 
  85     $iParentPlaceID = $oDB->getOne('SELECT parent_place_id FROM location_property_tiger WHERE place_id = '.$iPlaceID);
 
  86     if ($iParentPlaceID) $iPlaceID = $iParentPlaceID;
 
  89 // interpolated house numbers
 
  90 $iParentPlaceID = $oDB->getOne('SELECT parent_place_id FROM location_property_osmline WHERE place_id = '.$iPlaceID);
 
  91 if ($iParentPlaceID) $iPlaceID = $iParentPlaceID;
 
  93 // artificial postcodes
 
  94 $iParentPlaceID = $oDB->getOne('SELECT parent_place_id FROM location_postcode WHERE place_id = '.$iPlaceID);
 
  95 if ($iParentPlaceID) $iPlaceID = $iParentPlaceID;
 
  97 if (CONST_Use_Aux_Location_data) {
 
  98     $iParentPlaceID = $oDB->getOne('SELECT parent_place_id FROM location_property_aux WHERE place_id = '.$iPlaceID);
 
  99     if ($iParentPlaceID) $iPlaceID = $iParentPlaceID;
 
 102 $hLog = logStart($oDB, 'details', $_SERVER['QUERY_STRING'], $aLangPrefOrder);
 
 104 // Get the details for this point
 
 105 $sSQL = 'SELECT place_id, osm_type, osm_id, class, type, name, admin_level,';
 
 106 $sSQL .= '    housenumber, postcode, country_code,';
 
 107 $sSQL .= '    importance, wikipedia,';
 
 108 $sSQL .= '    ROUND(EXTRACT(epoch FROM indexed_date)) AS indexed_epoch,';
 
 109 $sSQL .= '    parent_place_id, ';
 
 110 $sSQL .= '    rank_address, ';
 
 111 $sSQL .= '    rank_search, ';
 
 112 $sSQL .= "    get_name_by_language(name,$sLanguagePrefArraySQL) AS localname, ";
 
 113 $sSQL .= "    ST_GeometryType(geometry) in ('ST_Polygon','ST_MultiPolygon') AS isarea, ";
 
 114 $sSQL .= '    ST_y(centroid) AS lat, ';
 
 115 $sSQL .= '    ST_x(centroid) AS lon, ';
 
 117 $sSQL .= '       WHEN importance = 0 OR importance IS NULL ';
 
 118 $sSQL .= '       THEN 0.75-(rank_search::float/40) ';
 
 119 $sSQL .= '       ELSE importance ';
 
 120 $sSQL .= '       END as calculated_importance, ';
 
 121 if ($bIncludePolygonAsGeoJSON) {
 
 122     $sSQL .= '    ST_AsGeoJSON(CASE ';
 
 123     $sSQL .= '                WHEN ST_NPoints(geometry) > 5000 ';
 
 124     $sSQL .= '                THEN ST_SimplifyPreserveTopology(geometry, 0.0001) ';
 
 125     $sSQL .= '                ELSE geometry ';
 
 126     $sSQL .= '                END) as asgeojson';
 
 128     $sSQL .= '    ST_AsGeoJSON(centroid) as asgeojson';
 
 130 $sSQL .= ' FROM placex ';
 
 131 $sSQL .= " WHERE place_id = $iPlaceID";
 
 133 $aPointDetails = $oDB->getRow($sSQL, null, 'Could not get details of place object.');
 
 135 if (!$aPointDetails) {
 
 136     userError('Unknown place id.');
 
 139 $aPointDetails['localname'] = $aPointDetails['localname']?$aPointDetails['localname']:$aPointDetails['housenumber'];
 
 140 $aPointDetails['icon'] = Nominatim\ClassTypes\getProperty($aPointDetails, 'icon', false);
 
 141 $aPointDetails['rank_search_label'] = getSearchRankLabel($aPointDetails['rank_search']); // only used in HTML format
 
 143 // Get all alternative names (languages, etc)
 
 144 $sSQL = 'SELECT (each(name)).key,(each(name)).value FROM placex ';
 
 145 $sSQL .= "WHERE place_id = $iPlaceID ORDER BY (each(name)).key";
 
 146 $aPointDetails['aNames'] = $oDB->getAssoc($sSQL);
 
 149 $sSQL = 'SELECT (each(address)).key as key,(each(address)).value FROM placex ';
 
 150 $sSQL .= "WHERE place_id = $iPlaceID ORDER BY key";
 
 151 $aPointDetails['aAddressTags'] = $oDB->getAssoc($sSQL);
 
 154 $sSQL = 'SELECT (each(extratags)).key,(each(extratags)).value FROM placex ';
 
 155 $sSQL .= "WHERE place_id = $iPlaceID ORDER BY (each(extratags)).key";
 
 156 $aPointDetails['aExtraTags'] = $oDB->getAssoc($sSQL);
 
 159 $aAddressLines = false;
 
 160 if ($bIncludeAddressDetails) {
 
 161     $oDetails = new Nominatim\AddressDetails($oDB, $iPlaceID, -1, $sLanguagePrefArraySQL);
 
 162     $aAddressLines = $oDetails->getAddressDetails(true);
 
 166 $aLinkedLines = false;
 
 167 if ($bIncludeLinkedPlaces) {
 
 168     $sSQL = 'SELECT placex.place_id, osm_type, osm_id, class, type, housenumber,';
 
 169     $sSQL .= ' admin_level, rank_address, ';
 
 170     $sSQL .= " ST_GeometryType(geometry) in ('ST_Polygon','ST_MultiPolygon') AS isarea,";
 
 171     $sSQL .= " ST_DistanceSpheroid(geometry, placegeometry, 'SPHEROID[\"WGS 84\",6378137,298.257223563, AUTHORITY[\"EPSG\",\"7030\"]]') AS distance, ";
 
 172     $sSQL .= " get_name_by_language(name,$sLanguagePrefArraySQL) AS localname, ";
 
 173     $sSQL .= ' length(name::text) AS namelength ';
 
 175     $sSQL .= '    placex, ';
 
 177     $sSQL .= '      SELECT centroid AS placegeometry ';
 
 178     $sSQL .= '      FROM placex ';
 
 179     $sSQL .= "      WHERE place_id = $iPlaceID ";
 
 181     $sSQL .= " WHERE linked_place_id = $iPlaceID";
 
 182     $sSQL .= ' ORDER BY ';
 
 183     $sSQL .= '   rank_address ASC, ';
 
 184     $sSQL .= '   rank_search ASC, ';
 
 185     $sSQL .= "   get_name_by_language(name, $sLanguagePrefArraySQL), ";
 
 186     $sSQL .= '   housenumber';
 
 187     $aLinkedLines = $oDB->getAll($sSQL);
 
 190 // All places this is an imediate parent of
 
 191 $aHierarchyLines = false;
 
 192 if ($bIncludeHierarchy) {
 
 193     $sSQL = 'SELECT obj.place_id, osm_type, osm_id, class, type, housenumber,';
 
 194     $sSQL .= " admin_level, rank_address, ST_GeometryType(geometry) in ('ST_Polygon','ST_MultiPolygon') AS isarea,";
 
 195     $sSQL .= " ST_DistanceSpheroid(geometry, placegeometry, 'SPHEROID[\"WGS 84\",6378137,298.257223563, AUTHORITY[\"EPSG\",\"7030\"]]') AS distance, ";
 
 196     $sSQL .= " get_name_by_language(name,$sLanguagePrefArraySQL) AS localname, ";
 
 197     $sSQL .= ' length(name::text) AS namelength ';
 
 200     $sSQL .= '      SELECT placex.place_id, osm_type, osm_id, class, type, housenumber, admin_level, rank_address, rank_search, geometry, name ';
 
 201     $sSQL .= '      FROM placex ';
 
 202     $sSQL .= "      WHERE parent_place_id = $iPlaceID ";
 
 203     $sSQL .= '      ORDER BY ';
 
 204     $sSQL .= '         rank_address ASC, ';
 
 205     $sSQL .= '         rank_search ASC ';
 
 206     $sSQL .= '      LIMIT 500 ';
 
 207     $sSQL .= '    ) AS obj,';
 
 209     $sSQL .= '      SELECT centroid AS placegeometry ';
 
 210     $sSQL .= '      FROM placex ';
 
 211     $sSQL .= "      WHERE place_id = $iPlaceID ";
 
 213     $sSQL .= ' ORDER BY ';
 
 214     $sSQL .= '    rank_address ASC, ';
 
 215     $sSQL .= '    rank_search ASC, ';
 
 216     $sSQL .= '    localname, ';
 
 217     $sSQL .= '    housenumber';
 
 218     $aHierarchyLines = $oDB->getAll($sSQL);
 
 221 $aPlaceSearchNameKeywords = false;
 
 222 $aPlaceSearchAddressKeywords = false;
 
 223 if ($bIncludeKeywords) {
 
 224     $sSQL = "SELECT * FROM search_name WHERE place_id = $iPlaceID";
 
 225     $aPlaceSearchName = $oDB->getRow($sSQL);
 
 227     if (!empty($aPlaceSearchName)) {
 
 228         $sWordIds = substr($aPlaceSearchName['name_vector'], 1, -1);
 
 229         if (!empty($sWordIds)) {
 
 230             $sSQL = 'SELECT * FROM word WHERE word_id in ('.$sWordIds.')';
 
 231             $aPlaceSearchNameKeywords = $oDB->getAll($sSQL);
 
 234         $sWordIds = substr($aPlaceSearchName['nameaddress_vector'], 1, -1);
 
 235         if (!empty($sWordIds)) {
 
 236             $sSQL = 'SELECT * FROM word WHERE word_id in ('.$sWordIds.')';
 
 237             $aPlaceSearchAddressKeywords = $oDB->getAll($sSQL);
 
 242 logEnd($oDB, $hLog, 1);
 
 244 if ($sOutputFormat=='html') {
 
 245     $sSQL = "SELECT TO_CHAR(lastimportdate,'YYYY/MM/DD HH24:MI')||' GMT' FROM import_status LIMIT 1";
 
 246     $sDataDate = $oDB->getOne($sSQL);
 
 247     $sTileURL = CONST_Map_Tile_URL;
 
 248     $sTileAttribution = CONST_Map_Tile_Attribution;
 
 251 include(CONST_BasePath.'/lib/template/details-'.$sOutputFormat.'.php');