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 ($sOsmType && $iOsmId > 0) {
 
  35         "SELECT place_id FROM placex WHERE osm_type='%s' AND osm_id=%d",
 
  39     // osm_type and osm_id are not unique enough
 
  41         $sSQL .= " AND class='".$sClass."'";
 
  43     $sSQL .= ' ORDER BY class ASC';
 
  44     $sPlaceId = chksql($oDB->getOne($sSQL));
 
  46     // Be nice about our error messages for broken geometry
 
  50         $sSQL .= '    osm_type, ';
 
  52         $sSQL .= '    errormessage, ';
 
  55         $sSQL .= "    get_name_by_language(name,$sLanguagePrefArraySQL) AS localname,";
 
  56         $sSQL .= '    ST_AsText(prevgeometry) AS prevgeom, ';
 
  57         $sSQL .= '    ST_AsText(newgeometry) AS newgeom';
 
  58         $sSQL .= ' FROM import_polygon_error ';
 
  59         $sSQL .= " WHERE osm_type = '".$sOsmType."'";
 
  60         $sSQL .= '  AND osm_id = '.$iOsmId;
 
  61         $sSQL .= ' ORDER BY updated DESC';
 
  63         $aPointDetails = chksql($oDB->getRow($sSQL));
 
  65             if (preg_match('/\[(-?\d+\.\d+) (-?\d+\.\d+)\]/', $aPointDetails['errormessage'], $aMatches)) {
 
  66                 $aPointDetails['error_x'] = $aMatches[1];
 
  67                 $aPointDetails['error_y'] = $aMatches[2];
 
  69                 $aPointDetails['error_x'] = 0;
 
  70                 $aPointDetails['error_y'] = 0;
 
  72             include(CONST_BasePath.'/lib/template/details-error-'.$sOutputFormat.'.php');
 
  79 if ($sPlaceId === false) userError('Please select a place id');
 
  81 $iPlaceID = (int)$sPlaceId;
 
  83 if (CONST_Use_US_Tiger_Data) {
 
  84     $iParentPlaceID = chksql($oDB->getOne('SELECT parent_place_id FROM location_property_tiger WHERE place_id = '.$iPlaceID));
 
  85     if ($iParentPlaceID) $iPlaceID = $iParentPlaceID;
 
  88 // interpolated house numbers
 
  89 $iParentPlaceID = chksql($oDB->getOne('SELECT parent_place_id FROM location_property_osmline WHERE place_id = '.$iPlaceID));
 
  90 if ($iParentPlaceID) $iPlaceID = $iParentPlaceID;
 
  92 // artificial postcodes
 
  93 $iParentPlaceID = chksql($oDB->getOne('SELECT parent_place_id FROM location_postcode WHERE place_id = '.$iPlaceID));
 
  94 if ($iParentPlaceID) $iPlaceID = $iParentPlaceID;
 
  96 if (CONST_Use_Aux_Location_data) {
 
  97     $iParentPlaceID = chksql($oDB->getOne('SELECT parent_place_id FROM location_property_aux WHERE place_id = '.$iPlaceID));
 
  98     if ($iParentPlaceID) $iPlaceID = $iParentPlaceID;
 
 101 $hLog = logStart($oDB, 'details', $_SERVER['QUERY_STRING'], $aLangPrefOrder);
 
 103 // Get the details for this point
 
 104 $sSQL = 'SELECT place_id, osm_type, osm_id, class, type, name, admin_level,';
 
 105 $sSQL .= '    housenumber, postcode, country_code,';
 
 106 $sSQL .= '    importance, wikipedia,';
 
 107 $sSQL .= '    ROUND(EXTRACT(epoch FROM indexed_date)) AS indexed_epoch,';
 
 108 $sSQL .= '    parent_place_id, ';
 
 109 $sSQL .= '    rank_address, ';
 
 110 $sSQL .= '    rank_search, ';
 
 111 $sSQL .= "    get_name_by_language(name,$sLanguagePrefArraySQL) AS localname, ";
 
 112 $sSQL .= "    ST_GeometryType(geometry) in ('ST_Polygon','ST_MultiPolygon') AS isarea, ";
 
 113 $sSQL .= '    ST_y(centroid) AS lat, ';
 
 114 $sSQL .= '    ST_x(centroid) AS lon, ';
 
 116 $sSQL .= '       WHEN importance = 0 OR importance IS NULL ';
 
 117 $sSQL .= '       THEN 0.75-(rank_search::float/40) ';
 
 118 $sSQL .= '       ELSE importance ';
 
 119 $sSQL .= '       END as calculated_importance, ';
 
 120 if ($bIncludePolygonAsGeoJSON) {
 
 121     $sSQL .= '    ST_AsGeoJSON(CASE ';
 
 122     $sSQL .= '                WHEN ST_NPoints(geometry) > 5000 ';
 
 123     $sSQL .= '                THEN ST_SimplifyPreserveTopology(geometry, 0.0001) ';
 
 124     $sSQL .= '                ELSE geometry ';
 
 125     $sSQL .= '                END) as asgeojson';
 
 127     $sSQL .= '    ST_AsGeoJSON(centroid) as asgeojson';
 
 129 $sSQL .= ' FROM placex ';
 
 130 $sSQL .= " WHERE place_id = $iPlaceID";
 
 132 $aPointDetails = chksql($oDB->getRow($sSQL), 'Could not get details of place object.');
 
 134 if (!$aPointDetails) {
 
 135     userError('Unknown place id.');
 
 138 $aPointDetails['localname'] = $aPointDetails['localname']?$aPointDetails['localname']:$aPointDetails['housenumber'];
 
 139 $aPointDetails['icon'] = Nominatim\ClassTypes\getProperty($aPointDetails, 'icon', false);
 
 140 $aPointDetails['rank_search_label'] = getSearchRankLabel($aPointDetails['rank_search']); // only used in HTML format
 
 142 // Get all alternative names (languages, etc)
 
 143 $sSQL = 'SELECT (each(name)).key,(each(name)).value FROM placex ';
 
 144 $sSQL .= "WHERE place_id = $iPlaceID ORDER BY (each(name)).key";
 
 145 $aPointDetails['aNames'] = $oDB->getAssoc($sSQL);
 
 148 $sSQL = 'SELECT (each(address)).key as key,(each(address)).value FROM placex ';
 
 149 $sSQL .= "WHERE place_id = $iPlaceID ORDER BY key";
 
 150 $aPointDetails['aAddressTags'] = $oDB->getAssoc($sSQL);
 
 153 $sSQL = 'SELECT (each(extratags)).key,(each(extratags)).value FROM placex ';
 
 154 $sSQL .= "WHERE place_id = $iPlaceID ORDER BY (each(extratags)).key";
 
 155 $aPointDetails['aExtraTags'] = $oDB->getAssoc($sSQL);
 
 158 $aAddressLines = false;
 
 159 if ($bIncludeAddressDetails) {
 
 160     $oDetails = new Nominatim\AddressDetails($oDB, $iPlaceID, -1, $sLanguagePrefArraySQL);
 
 161     $aAddressLines = $oDetails->getAddressDetails(true);
 
 165 $aLinkedLines = false;
 
 166 if ($bIncludeLinkedPlaces) {
 
 167     $sSQL = 'SELECT placex.place_id, osm_type, osm_id, class, type, housenumber,';
 
 168     $sSQL .= ' admin_level, rank_address, ';
 
 169     $sSQL .= " ST_GeometryType(geometry) in ('ST_Polygon','ST_MultiPolygon') AS isarea,";
 
 170     $sSQL .= " ST_DistanceSpheroid(geometry, placegeometry, 'SPHEROID[\"WGS 84\",6378137,298.257223563, AUTHORITY[\"EPSG\",\"7030\"]]') AS distance, ";
 
 171     $sSQL .= " get_name_by_language(name,$sLanguagePrefArraySQL) AS localname, ";
 
 172     $sSQL .= ' length(name::text) AS namelength ';
 
 174     $sSQL .= '    placex, ';
 
 176     $sSQL .= '      SELECT centroid AS placegeometry ';
 
 177     $sSQL .= '      FROM placex ';
 
 178     $sSQL .= "      WHERE place_id = $iPlaceID ";
 
 180     $sSQL .= " WHERE linked_place_id = $iPlaceID";
 
 181     $sSQL .= ' ORDER BY ';
 
 182     $sSQL .= '   rank_address ASC, ';
 
 183     $sSQL .= '   rank_search ASC, ';
 
 184     $sSQL .= "   get_name_by_language(name, $sLanguagePrefArraySQL), ";
 
 185     $sSQL .= '   housenumber';
 
 186     $aLinkedLines = $oDB->getAll($sSQL);
 
 189 // All places this is an imediate parent of
 
 190 $aHierarchyLines = false;
 
 191 if ($bIncludeHierarchy) {
 
 192     $sSQL = 'SELECT obj.place_id, osm_type, osm_id, class, type, housenumber,';
 
 193     $sSQL .= " admin_level, rank_address, ST_GeometryType(geometry) in ('ST_Polygon','ST_MultiPolygon') AS isarea,";
 
 194     $sSQL .= " ST_DistanceSpheroid(geometry, placegeometry, 'SPHEROID[\"WGS 84\",6378137,298.257223563, AUTHORITY[\"EPSG\",\"7030\"]]') AS distance, ";
 
 195     $sSQL .= " get_name_by_language(name,$sLanguagePrefArraySQL) AS localname, ";
 
 196     $sSQL .= ' length(name::text) AS namelength ';
 
 199     $sSQL .= '      SELECT placex.place_id, osm_type, osm_id, class, type, housenumber, admin_level, rank_address, rank_search, geometry, name ';
 
 200     $sSQL .= '      FROM placex ';
 
 201     $sSQL .= "      WHERE parent_place_id = $iPlaceID ";
 
 202     $sSQL .= '      ORDER BY ';
 
 203     $sSQL .= '         rank_address ASC, ';
 
 204     $sSQL .= '         rank_search ASC ';
 
 205     $sSQL .= '      LIMIT 500 ';
 
 206     $sSQL .= '    ) AS obj,';
 
 208     $sSQL .= '      SELECT centroid AS placegeometry ';
 
 209     $sSQL .= '      FROM placex ';
 
 210     $sSQL .= "      WHERE place_id = $iPlaceID ";
 
 212     $sSQL .= ' ORDER BY ';
 
 213     $sSQL .= '    rank_address ASC, ';
 
 214     $sSQL .= '    rank_search ASC, ';
 
 215     $sSQL .= '    localname, ';
 
 216     $sSQL .= '    housenumber';
 
 217     $aHierarchyLines = $oDB->getAll($sSQL);
 
 220 $aPlaceSearchNameKeywords = false;
 
 221 $aPlaceSearchAddressKeywords = false;
 
 222 if ($bIncludeKeywords) {
 
 223     $sSQL = "SELECT * FROM search_name WHERE place_id = $iPlaceID";
 
 224     $aPlaceSearchName = $oDB->getRow($sSQL);
 
 226     if (!empty($aPlaceSearchName)) {
 
 227         $sSQL = 'SELECT * FROM word WHERE word_id in ('.substr($aPlaceSearchName['name_vector'], 1, -1).')';
 
 228         $aPlaceSearchNameKeywords = $oDB->getAll($sSQL);
 
 230         $sSQL = 'SELECT * FROM word WHERE word_id in ('.substr($aPlaceSearchName['nameaddress_vector'], 1, -1).')';
 
 231         $aPlaceSearchAddressKeywords = $oDB->getAll($sSQL);
 
 235 logEnd($oDB, $hLog, 1);
 
 237 if ($sOutputFormat=='html') {
 
 238     $sSQL = "SELECT TO_CHAR(lastimportdate,'YYYY/MM/DD HH24:MI')||' GMT' FROM import_status LIMIT 1";
 
 239     $sDataDate = chksql($oDB->getOne($sSQL));
 
 240     $sTileURL = CONST_Map_Tile_URL;
 
 241     $sTileAttribution = CONST_Map_Tile_Attribution;
 
 244 include(CONST_BasePath.'/lib/template/details-'.$sOutputFormat.'.php');