OpenGeoClassByChristianFigge

OpenGeoDB & GISWiki - Das freie Portal für Geoinformatik (GIS)
Wechseln zu: Navigation, Suche
  1. <?php 
  2.  
  3. /**
  4.  * Yet another OpenGeo class
  5.  * version 0.1
  6.  * 
  7.  * @author Christian Figge
  8.  * @date 14.03.2012
  9.  * @email info@flazer.net
  10.  * @url http://flazer.net/
  11.  * @url http://opengeodb.org/
  12.  * This class uses the openGEO-Database
  13.  * and supports Germany, Austria and Switzerland
  14.  * (i didn't need more)
  15.  * so feel free to add more.
  16.  * 
  17.  * For better performance I used a selfwritten filecache.
  18.  * I commented this lines out, so for now everytime the database gets requested.
  19.  * This produces a unnecessary load on your db-machine.
  20.  * 
  21.  * I'm using ADOdb for databasehandling, so if you want to use something different,
  22.  * you have to implement your own handling. ($this->getDB())
  23.  * 
  24.  * example:
  25.  * 
  26.  * $oOpenGeo = new OpenGeo();
  27.  * $oOpenGeo->setCountry('de');
  28.  * $oOpenGeo->getProvinceByZipCode(20539);
  29.  * returns:  
  30.  * 	   array(
  31.  * 			[loc_id] => 114
  32.  * 			[text_val] => Hamburg
  33.  *		)
  34.  */
  35.  
  36. class OpenGeo {
  37.  
  38. 	private $oDB		= null;
  39. 	private $sCountry 	= 'de';
  40. 	private $aCountriesCfg = array('name'	 		=> 500100000,
  41. 									'layer'			=> 400200000,
  42. 									'province'		=> 100300000,
  43. 									'locale' 		=> 'de',
  44. 									'zipID'	 		=> 500300000,
  45. 									'countryLvl' 	=> 'ld_lvl1',
  46. 									'countries' 	=> array(
  47. 														'de'	=> 105,
  48. 														'at'	=> 106,
  49. 														'ch'	=> 107
  50. 														)
  51. 							);
  52.  
  53. 	/**
  54. 	 * Checks if the country is supported
  55. 	 * (de,at,ch)
  56. 	 * @param string $sCountry
  57. 	 * @return boolean
  58. 	 */
  59. 	public function checkSupportCountry($sCountry){
  60. 		if(in_array($sCountry, array_keys($this->aCountriesCfg['countries']))){
  61. 			return true;
  62. 		}
  63. 		return false;
  64. 	}
  65.  
  66.  
  67. 	/**
  68. 	 * Returns province for given zipCode
  69. 	 * @param integer $iZipCode
  70. 	 * @return array $aProvince
  71. 	 */
  72. 	public function getProvinceByZipCode($iZipCode){
  73.  
  74. 		$sTextType 		= $this->aCountriesCfg['name'];
  75. 		$sCountryID 	= $this->aCountriesCfg['countries'][$this->sCountry];
  76. 		$sZipID			= $this->aCountriesCfg['zipID'];
  77. 		$sProvinceID 	= $this->aCountriesCfg['province'];
  78. 		$aProvince		= array();
  79.  
  80. // 		$sCacheKey = 'province_'.$this->sCountry.'_'.$iZipCode;
  81. // 		$aProvince = System_Controller::getFilecache()->getData($sCacheKey);
  82.  
  83. 		if(!is_array($aProvince) || empty($aProvince)){
  84. 			$aProvince = $this->getDB()->getRow('SELECT geodb_textdata.loc_id, geodb_textdata.text_val FROM geodb_textdata
  85. 														WHERE  geodb_textdata.loc_id = (
  86. 															SELECT geodb_hierarchies.id_lvl3
  87. 															FROM geodb_hierarchies, geodb_textdata
  88. 																WHERE geodb_hierarchies.id_lvl2 = ?
  89. 																	AND  geodb_textdata.text_type = ?
  90. 																	AND geodb_textdata.text_val = ?
  91. 																	AND geodb_textdata.loc_id = geodb_hierarchies.loc_id
  92. 																	LIMIT 1
  93. 															)
  94. 															AND geodb_textdata.text_type = ?',
  95. 												array($sCountryID, $sZipID, $iZipCode, $sTextType));
  96.  
  97. // 			System_Controller::getFilecache()->setData($sCacheKey, $aProvince, 900);
  98. 		}
  99.  
  100. 		return $aProvince;
  101. 	}
  102.  
  103.  
  104. 	/**
  105. 	 * Returns city for given zipCode
  106. 	 * @param integer $iZipCode
  107. 	 * @return array $aCity
  108. 	 */
  109. 	public function getCityByZipCode($iZipCode){
  110.  
  111. 		$sTextType 		= $this->aCountriesCfg['name'];
  112. 		$sLayer 		= $this->aCountriesCfg['layer'];
  113. 		$sCountryID 	= $this->aCountriesCfg['countries'][$this->sCountry];
  114. 		$sZipID			= $this->aCountriesCfg['zipID'];
  115. 		$sLocale	 	= $this->aCountriesCfg['locale'];
  116. 		$aCity			= array();
  117.  
  118. // 		$sCacheKey = 'city'.$this->sCountry.'_'.$iZipCode;
  119. // 		$aCity = System_Controller::getFilecache()->getData($sCacheKey);
  120.  
  121. 		if(!is_array($aCity) || empty($aCity)){
  122. 			$aCity = $this->getDB()->getRow('SELECT geodb_textdata.loc_id, geodb_textdata.text_val FROM geodb_textdata WHERE loc_id = (
  123. 													SELECT geodb_textdata.loc_id FROM geodb_textdata
  124. 														WHERE  geodb_textdata.loc_id = (
  125. 															SELECT MIN(geodb_textdata.loc_id)
  126. 															FROM geodb_hierarchies, geodb_textdata
  127. 																WHERE geodb_hierarchies.id_lvl2 = ?
  128. 																	AND  geodb_textdata.text_type = ?
  129. 																	AND geodb_textdata.text_val = ?
  130. 																	AND geodb_textdata.loc_id = geodb_hierarchies.loc_id
  131. 														)
  132. 															AND geodb_textdata.text_type = ?
  133. 															LIMIT 1
  134. 													)
  135. 													AND geodb_textdata.text_type = ?
  136. 													AND geodb_textdata.text_locale = ?',
  137. 												array($sCountryID, $sZipID, $iZipCode, $sLayer, $sTextType,$sLocale));
  138.  
  139. // 			System_Controller::getFilecache()->setData($sCacheKey, $aCity, 900);
  140. 		}
  141. 		return $aCity;
  142. 	}
  143.  
  144.  
  145. 	/**
  146. 	 * Returns all possible provinces for selected country (set via $this->setCountry())
  147. 	 * @return array $aProvinces
  148. 	 */
  149. 	public function getProvinces(){
  150. 		$sTextType 		= $this->aCountriesCfg['name'];
  151. 		$sCountryID 	= $this->aCountriesCfg['countries'][$this->sCountry];
  152. 		$sProvinceID 	= $this->aCountriesCfg['province'];
  153.  
  154. // 		$sCacheKey = 'provinces_'.$this->sCountry;
  155. // 		$aProvinces = System_Controller::getFilecache()->getData($sCacheKey);
  156.  
  157. 		if(!is_array($aProvinces) || empty($aProvinces)){
  158. 			$aProvinces = $this->getDB()->getArray('SELECT DISTINCT geodb_textdata.loc_id, geodb_textdata.text_val 
  159. 														FROM geodb_locations, geodb_hierarchies, geodb_textdata
  160. 														WHERE geodb_locations.loc_type =100300000
  161. 															AND geodb_locations.loc_id = geodb_hierarchies.id_lvl3
  162. 															AND geodb_locations.loc_id = geodb_textdata.loc_id
  163. 															AND geodb_textdata.text_type = ?
  164. 															AND geodb_hierarchies.id_lvl2 = ?', 
  165. 													array($sTextType, $sCountryID));
  166.  
  167. // 			System_Controller::getFilecache()->setData($sCacheKey, $aProvinces, 900);
  168. 		}
  169.  
  170. 		return $aProvinces;
  171. 	}
  172.  
  173.  
  174. 	public function setCountry($sCountry){
  175. 		$this->sCountry = $sCountry;
  176. 	}
  177.  
  178. 	/*
  179. 	 * Databasehandler
  180. 	 * Put your own databasehandling in here.
  181. 	 */
  182. 	private function getDB(){
  183. 		if(!$this->oDB){
  184. 			$db = ADONewConnection('mysql');
  185. 			$db->Connect(_OPENGEO_DATABASE_HOST_, _OPENGEO_DATABASE_USER_, _OPENGEO_DATABASE_PASS_, _OPENGEO_DATABASE_NAME_);
  186. 			$db->SetFetchMode(ADODB_FETCH_ASSOC);
  187. 			$this->oDB = $db;
  188. 		}
  189. 		return $this->oDB;
  190. 	}
  191. }