]> git.openstreetmap.org Git - nominatim.git/blob - lib/ReverseGeocode.php
adapt to code style conventions
[nominatim.git] / lib / ReverseGeocode.php
1 <?php
2         class ReverseGeocode
3         {
4                 protected $oDB;
5
6                 protected $fLat;
7                 protected $fLon;
8                 protected $iMaxRank = 28;
9
10                 protected $aLangPrefOrder = array();
11
12                 function ReverseGeocode(&$oDB)
13                 {
14                         $this->oDB =& $oDB;
15                 }
16
17                 function setLanguagePreference($aLangPref)
18                 {
19                         $this->aLangPrefOrder = $aLangPref;
20                 }
21
22                 function setLatLon($fLat, $fLon)
23                 {
24                         $this->fLat = (float)$fLat;
25                         $this->fLon = (float)$fLon;
26                 }
27
28                 function setRank($iRank)
29                 {
30                         $this->iMaxRank = $iRank;
31                 }
32
33                 function setZoom($iZoom)
34                 {
35                         // Zoom to rank, this could probably be calculated but a lookup gives fine control
36                         $aZoomRank = array(
37                                 0 => 2, // Continent / Sea
38                                 1 => 2,
39                                 2 => 2,
40                                 3 => 4, // Country
41                                 4 => 4,
42                                 5 => 8, // State
43                                 6 => 10, // Region
44                                 7 => 10,
45                                 8 => 12, // County
46                                 9 => 12,
47                                 10 => 17, // City
48                                 11 => 17,
49                                 12 => 18, // Town / Village
50                                 13 => 18,
51                                 14 => 22, // Suburb
52                                 15 => 22,
53                                 16 => 26, // Street, TODO: major street?
54                                 17 => 26,
55                                 18 => 30, // or >, Building
56                                 19 => 30, // or >, Building
57                                 );
58                         $this->iMaxRank = (isset($iZoom) && isset($aZoomRank[$iZoom]))?$aZoomRank[$iZoom]:28;
59                 }
60
61                 function lookup()
62                 {
63                         $sPointSQL = 'ST_SetSRID(ST_Point('.$this->fLon.','.$this->fLat.'),4326)';
64                         $iMaxRank = $this->iMaxRank;
65                         $iMaxRank_orig = $this->iMaxRank;
66
67                         // Find the nearest point
68                         $fSearchDiam = 0.0004;
69                         $iPlaceID = null;
70                         $aArea = false;
71                         $fMaxAreaDistance = 1;
72                         $bIsInUnitedStates = false;
73                         $bPlaceIsTiger = false;
74                         while(!$iPlaceID && $fSearchDiam < $fMaxAreaDistance)
75                         {
76                                 $fSearchDiam = $fSearchDiam * 2;
77
78                                 // If we have to expand the search area by a large amount then we need a larger feature
79                                 // then there is a limit to how small the feature should be
80                                 if ($fSearchDiam > 2 && $iMaxRank > 4) $iMaxRank = 4;
81                                 if ($fSearchDiam > 1 && $iMaxRank > 9) $iMaxRank = 8;
82                                 if ($fSearchDiam > 0.8 && $iMaxRank > 10) $iMaxRank = 10;
83                                 if ($fSearchDiam > 0.6 && $iMaxRank > 12) $iMaxRank = 12;
84                                 if ($fSearchDiam > 0.2 && $iMaxRank > 17) $iMaxRank = 17;
85                                 if ($fSearchDiam > 0.1 && $iMaxRank > 18) $iMaxRank = 18;
86                                 if ($fSearchDiam > 0.008 && $iMaxRank > 22) $iMaxRank = 22;
87                                 if ($fSearchDiam > 0.001 && $iMaxRank > 26) $iMaxRank = 26;
88
89                                 $sSQL = 'select place_id,parent_place_id,rank_search,calculated_country_code from placex';
90                                 $sSQL .= ' WHERE ST_DWithin('.$sPointSQL.', geometry, '.$fSearchDiam.')';
91                                 $sSQL .= ' and rank_search != 28 and rank_search >= '.$iMaxRank;
92                                 $sSQL .= ' and (name is not null or housenumber is not null)';
93                                 $sSQL .= ' and class not in (\'waterway\',\'railway\',\'tunnel\',\'bridge\',\'man_made\')';
94                                 $sSQL .= ' and indexed_status = 0 ';
95                                 $sSQL .= ' and (ST_GeometryType(geometry) not in (\'ST_Polygon\',\'ST_MultiPolygon\') ';
96                                 $sSQL .= ' OR ST_DWithin('.$sPointSQL.', centroid, '.$fSearchDiam.'))';
97                                 $sSQL .= ' ORDER BY ST_distance('.$sPointSQL.', geometry) ASC limit 1';
98                                 if (CONST_Debug) var_dump($sSQL);
99                                 $aPlace = $this->oDB->getRow($sSQL);
100                                 if (PEAR::IsError($aPlace))
101                                 {
102                                         failInternalError("Could not determine closest place.", $sSQL, $aPlace);
103                                 }
104                                 $iPlaceID = $aPlace['place_id'];
105                                 $iParentPlaceID = $aPlace['parent_place_id'];
106                                 $bIsInUnitedStates = ($aPlace['calculated_country_code'] == 'us');
107                         }
108
109                         // Only street found? If it's in the US we can check TIGER data for nearest housenumber
110                         if ($bIsInUnitedStates && $iMaxRank_orig >= 28 && $iPlaceID && ($aPlace['rank_search'] == 26 || $aPlace['rank_search'] == 27 ))
111                         {
112                                 $fSearchDiam = 0.001;
113                                 $sSQL = 'SELECT place_id,parent_place_id,30 as rank_search, ST_line_locate_point(linegeo,'.$sPointSQL.') as fraction';
114                                 //if (CONST_Debug) { $sSQL .= ', housenumber, ST_distance('.$sPointSQL.', centroid) as distance, st_y(centroid) as lat, st_x(centroid) as lon'; }
115                                 $sSQL .= ' FROM location_property_tiger WHERE parent_place_id = '.$iPlaceID;
116                                 $sSQL .= ' AND ST_DWithin('.$sPointSQL.', linegeo, '.$fSearchDiam.')';  //no centroid anymore in Tiger data, now we have lines
117                                 $sSQL .= ' ORDER BY ST_distance('.$sPointSQL.', linegeo) ASC limit 1';
118
119
120                                 // print all house numbers in the parent (street)
121                                 /*if (CONST_Debug)
122                                 {
123                                         $sSQL = preg_replace('/limit 1/', 'limit 100', $sSQL);
124                                         var_dump($sSQL);
125
126                                         $aAllHouses = $this->oDB->getAll($sSQL);
127                                         foreach($aAllHouses as $i)
128                                         {
129                                                 echo $i['housenumber'] . ' | ' . $i['distance'] * 1000 . ' | ' . $i['lat'] . ' | ' . $i['lon']. ' | '. "<br>\n";
130                                         }
131                                 }*/
132
133                                 $aPlaceTiger = $this->oDB->getRow($sSQL);
134                                 if (PEAR::IsError($aPlace))
135                                 {
136                                         failInternalError("Could not determine closest Tiger place.", $sSQL, $aPlaceTiger);
137                                 }
138                                 if ($aPlaceTiger)
139                                 {
140                                         if (CONST_Debug) var_dump('found Tiger place', $aPlaceTiger);
141                                         $bPlaceIsTiger = true;
142                                         $aPlace = $aPlaceTiger;
143                                         $iPlaceID = $aPlaceTiger['place_id'];
144                                         $iParentPlaceID = $aPlaceTiger['parent_place_id']; // the street
145                                         $iFraction = $aPlaceTiger['fraction'];
146                                 }
147                         }
148
149                         // The point we found might be too small - use the address to find what it is a child of
150                         if ($iPlaceID && $iMaxRank < 28)
151                         {
152                                 if ($aPlace['rank_search'] > 28 && $iParentPlaceID && !$bPlaceIsTiger)
153                                 {
154                                         $iPlaceID = $iParentPlaceID;
155                                 }
156                                 $sSQL = "select address_place_id from place_addressline where place_id = $iPlaceID order by abs(cached_rank_address - $iMaxRank) asc,cached_rank_address desc,isaddress desc,distance desc limit 1";
157                                 $iPlaceID = $this->oDB->getOne($sSQL);
158                                 if (PEAR::IsError($iPlaceID))
159                                 {
160                                         failInternalError("Could not get parent for place.", $sSQL, $iPlaceID);
161                                 }
162                                 if (!$iPlaceID)
163                                 {
164                                         $iPlaceID = $aPlace['place_id'];
165                                 }
166                         }
167
168                         return array('place_id' => $iPlaceID,
169                                                  'type' => $bPlaceIsTiger ? 'tiger' : 'osm',
170                                                  'fraction' => $bPlaceIsTiger ? $iFraction : -1 );
171                 }
172         }
173 ?>