Teryt.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. <?php
  2. class Teryt {
  3. const SIMC_TABLE = "TERYT_SIMC";
  4. const TERC_TABLE = "TERYT_TERC";
  5. const ULIC_TABLE = "TERYT_ULIC";
  6. const PNA_TABLE = "PNA";
  7. private static function checkArgs($args, $argsAvailable) {
  8. if ($args == null) throw new Exception("Missing argument");
  9. if (!is_array($args)) throw new Exception("Armument must be an array");
  10. if (!count($args)) throw new Exception("Argument list cannot be empty");
  11. foreach ($args as $arg => $value) {
  12. if (!in_array($arg, $argsAvailable)) throw new Exception("Unknown argument {$arg}");
  13. }
  14. }
  15. private static function throwException($msg, $sql) {
  16. throw new Exception(json_encode(['msg' => $msg, 'sql' => $sql]));
  17. }
  18. public static function search($args = null) {
  19. $argsAvailable = ["kodPocztowy", "wojewodztwo", "powiat", "gmina", "miejscowosc", "ulica"];
  20. self::checkArgs($args, $argsAvailable);
  21. $args = array_map('trim', $args);
  22. $where = "";
  23. $return = null;
  24. if (isset($args['kodPocztowy'])) {
  25. $args['kodPocztowy'] = str_replace(' ', '', $args['kodPocztowy']);
  26. if (isset($args['miejscowosc'])) $whereKod = "and miejscowosc = " . DB::getPDO()->quote($args['miejscowosc']);
  27. else $whereKod = "";
  28. $query = "select wojewodztwo, powiat, gmina from `" . self::PNA_TABLE . "` where kodPocztowy = " . DB::getPDO()->quote($args['kodPocztowy']) . " $whereKod and nazwa = '' group by wojewodztwo, powiat, gmina";
  29. $result = DB::getPDO()->fetchall($query);
  30. if (count($result) == 1) {
  31. $kodPocztowy['wojewodztwo'] = $result[0]['wojewodztwo'];
  32. $kodPocztowy['powiat'] = $result[0]['powiat'];
  33. $kodPocztowy['gmina'] = $result[0]['gmina'];
  34. }
  35. }
  36. try {
  37. if (isset($args['wojewodztwo'])) {
  38. $query = "select WOJ from `" . self::TERC_TABLE . "` where NAZWA = " . DB::getPDO()->quote($args['wojewodztwo']) . " and POW = 0";
  39. if (!($return['WOJ'] = DB::getPDO()->fetchValue($query))) throw new Exception("wojewodztwo not found");
  40. $where = "and WOJ = '{$return['WOJ']}'";
  41. }
  42. if (isset($args['powiat'])) {
  43. $args['powiat'] = preg_replace('/^M. /i', '', $args['powiat']);
  44. $query = "select WOJ, POW from `" . self::TERC_TABLE . "` where NAZWA = " . DB::getPDO()->quote($args['powiat']) . " and POW > 0 and GMI = 0 {$where}";
  45. if (!($result = DB::getPDO()->fetchall($query))) throw new Exception("powiat not found");
  46. if (count($result) > 1) throw new Exception("Found too many powiats");
  47. $return['WOJ'] = $result[0]['WOJ'];
  48. $return['POW'] = $result[0]['POW'];
  49. $where = "and WOJ = '{$return['WOJ']}' and POW = '{$return['POW']}'";
  50. }
  51. if (isset($args['gmina'])) {
  52. $args['gmina'] = preg_replace('/^M. /i', '', $args['gmina']);
  53. $query = "select WOJ, POW, GMI from `" . self::TERC_TABLE. "` where NAZWA = " . DB::getPDO()->quote($args['gmina']) . " and GMI > 0 {$where} group by WOJ, POW, GMI";
  54. if (!($result = DB::getPDO()->fetchall($query))) throw new Exception("gmina not found");
  55. if (count($result) == 1) {// throw new Exception("Found too many gminas ({$query})");
  56. $return['WOJ'] = $result[0]['WOJ'];
  57. $return['POW'] = $result[0]['POW'];
  58. $return['GMI'] = $result[0]['GMI'];
  59. $where = "and WOJ = '{$return['WOJ']}' and POW = '{$return['POW']}' and GMI = '{$return['GMI']}'";
  60. }
  61. }
  62. if (!isset($return['WOJ'])) throw new Exception("Try with data from kodPocztowy");
  63. if (isset($args['miejscowosc'])) {
  64. $args['miejscowosc'] = preg_replace('/^M. /i', '', $args['miejscowosc']);
  65. $args['miejscowosc'] = str_replace(' - ', '-', $args['miejscowosc']);
  66. $args['miejscowosc'] = str_replace(' ', '%', $args['miejscowosc']);
  67. $query = "select WOJ, POW, GMI, SYM from `" . self::SIMC_TABLE . "` where NAZWA like " . DB::getPDO()->quote($args['miejscowosc']) . " {$where}";
  68. if (!($result = DB::getPDO()->fetchall($query))) self::throwException("miejscowosc not found", $query);
  69. $return['WOJ'] = $result[0]['WOJ'];
  70. $return['POW'] = $result[0]['POW'];
  71. $return['GMI'] = $result[0]['GMI'];
  72. if (count($result) > 1) {//self::throwException("Found too many miejscowoscs", $query);
  73. $SYMarr = array_map(function ($a) {return $a['SYM'];}, $result);
  74. $where = "and WOJ = '{$return['WOJ']}' and POW = '{$return['POW']}' and ((RODZ_GMI<8 and GMI = '{$return['GMI']}' and SYM in (" . implode(",", $SYMarr) . ")) or RODZ_GMI>=8)";
  75. } else {
  76. $return['SYM'] = $result[0]['SYM'];
  77. $where = "and WOJ = '{$return['WOJ']}' and POW = '{$return['POW']}' and ((RODZ_GMI<8 and GMI = '{$return['GMI']}' and SYM = '{$return['SYM']}') or RODZ_GMI>=8)";
  78. }
  79. }
  80. } catch (Exception $e) {
  81. if (!isset($kodPocztowy)) throw new Exception($e->getMessage());
  82. $query = "select WOJ from `" . self::TERC_TABLE . "` where NAZWA = " . DB::getPDO()->quote($kodPocztowy['wojewodztwo']) . " and POW = 0";
  83. if (!($return['WOJ'] = DB::getPDO()->fetchValue($query))) throw new Exception("wojewodztwo not found");
  84. $where = "and WOJ = '{$return['WOJ']}'";
  85. $query = "select WOJ, POW from `" . self::TERC_TABLE . "` where NAZWA = " . DB::getPDO()->quote($kodPocztowy['powiat']) . " and POW > 0 and GMI = 0 {$where}";
  86. if (!($result = DB::getPDO()->fetchall($query))) throw new Exception("powiat not found");
  87. if (count($result) > 1) throw new Exception("Found too many powiats");
  88. $return['WOJ'] = $result[0]['WOJ'];
  89. $return['POW'] = $result[0]['POW'];
  90. $where = "and WOJ = '{$return['WOJ']}' and POW = '{$return['POW']}'";
  91. $query = "select WOJ, POW, GMI from `" . self::TERC_TABLE. "` where NAZWA = " . DB::getPDO()->quote($kodPocztowy['gmina']) . " and GMI > 0 {$where} group by WOJ, POW, GMI";
  92. if (!($result = DB::getPDO()->fetchall($query))) throw new Exception("gmina not found");
  93. if (count($result) == 1) {
  94. $return['WOJ'] = $result[0]['WOJ'];
  95. $return['POW'] = $result[0]['POW'];
  96. $return['GMI'] = $result[0]['GMI'];
  97. $where = "and WOJ = '{$return['WOJ']}' and POW = '{$return['POW']}' and GMI = '{$return['GMI']}'";
  98. }
  99. if (isset($args['miejscowosc'])) {
  100. $args['miejscowosc'] = preg_replace('/^M. /i', '', $args['miejscowosc']);
  101. $args['miejscowosc'] = str_replace(' - ', '-', $args['miejscowosc']);
  102. $args['miejscowosc'] = str_replace(' ', '%', $args['miejscowosc']);
  103. $query = "select WOJ, POW, GMI, SYM from `" . self::SIMC_TABLE . "` where NAZWA like " . DB::getPDO()->quote($args['miejscowosc']) . " {$where}";
  104. if (!($result = DB::getPDO()->fetchall($query))) self::throwException("miejscowosc not found", $query);
  105. $return['WOJ'] = $result[0]['WOJ'];
  106. $return['POW'] = $result[0]['POW'];
  107. $return['GMI'] = $result[0]['GMI'];
  108. if (count($result) > 1) {//self::throwException("Found too many miejscowoscs", $query);
  109. $SYMarr = array_map(function ($a) {return $a['SYM'];}, $result);
  110. $where = "and WOJ = '{$return['WOJ']}' and POW = '{$return['POW']}' and ((RODZ_GMI<8 and GMI = '{$return['GMI']}' and SYM in (" . implode(",", $SYMarr) . ")) or RODZ_GMI>=8)";
  111. } else {
  112. $return['SYM'] = $result[0]['SYM'];
  113. $where = "and WOJ = '{$return['WOJ']}' and POW = '{$return['POW']}' and ((RODZ_GMI<8 and GMI = '{$return['GMI']}' and SYM = '{$return['SYM']}') or RODZ_GMI>=8)";
  114. }
  115. }
  116. }
  117. if (isset($args['ulica'])) {
  118. $args['ulica'] = trim(preg_replace('/osiedle/i', 'os.', $args['ulica']));
  119. $args['ulica'] = trim(preg_replace('/aleja/i', 'al.', $args['ulica']));
  120. $args['ulica'] = trim(preg_replace('/plac/i', 'pl.', $args['ulica']));
  121. $args['ulica'] = trim(preg_replace('/kardynała/i', 'kard', $args['ulica']));
  122. $args['ulica'] = trim(preg_replace('/\(.*\)/', '', $args['ulica']));
  123. $args['ulica'] = trim(preg_replace('/-go/i', '', $args['ulica']));
  124. $args['ulica'] = trim(preg_replace('/\.$/', '%', $args['ulica']));
  125. $search = [' ', '..'];
  126. $replace = ['%', '.'];
  127. $args['ulica'] = str_replace($search, $replace, $args['ulica']);
  128. $ulica = DB::getPDO()->quote("{$args['ulica']}");
  129. $query = "select WOJ, POW, GMI, SYM, SYM_UL from `" . self::ULIC_TABLE . "` where (concat(CECHA, ' ', NAZWA_1) like {$ulica} or concat(CECHA, ' ', NAZWA_2, ' ', NAZWA_1) like {$ulica} " .
  130. "or concat(CECHA, NAZWA_1) like {$ulica} or concat(CECHA, NAZWA_2, ' ', NAZWA_1) like {$ulica}) {$where} group by WOJ, POW, SYM_UL";
  131. $result = DB::getPDO()->fetchall($query);
  132. if (!$result) {
  133. $ulica = DB::getPDO()->quote("ul. {$args['ulica']}");
  134. $query = "select WOJ, POW, GMI, SYM, SYM_UL from `" . self::ULIC_TABLE . "` where (concat(CECHA, ' ', NAZWA_1) like {$ulica} or concat(CECHA, ' ', NAZWA_2, ' ', NAZWA_1) like {$ulica}) " .
  135. "{$where} group by WOJ, POW, SYM_UL";
  136. $result = DB::getPDO()->fetchall($query);
  137. }
  138. if (!$result) {
  139. $ulica = DB::getPDO()->quote("ul. {$args['ulica']}%");
  140. $query = "select WOJ, POW, GMI, SYM, SYM_UL from `" . self::ULIC_TABLE . "` where (concat(CECHA, ' ', NAZWA_1) like {$ulica} or concat(CECHA, ' ', NAZWA_2, ' ', NAZWA_1) like {$ulica} " .
  141. "or concat(CECHA, NAZWA_1) like {$ulica} or concat(CECHA, NAZWA_2, ' ', NAZWA_1) like {$ulica}) {$where} group by WOJ, POW, SYM_UL";
  142. $result = DB::getPDO()->fetchall($query);
  143. }
  144. if (!$result) {
  145. $ulica = DB::getPDO()->quote("{$args['ulica']}%");
  146. $query = "select WOJ, POW, GMI, SYM, SYM_UL from `" . self::ULIC_TABLE . "` where (concat(CECHA, ' ', NAZWA_1) like {$ulica} or concat(CECHA, ' ', NAZWA_2, ' ', NAZWA_1) like {$ulica} " .
  147. "or concat(CECHA, NAZWA_1) like {$ulica} or concat(CECHA, NAZWA_2, ' ', NAZWA_1) like {$ulica}) {$where} group by WOJ, POW, SYM_UL";
  148. $result = DB::getPDO()->fetchall($query);
  149. }
  150. if (!$result) {
  151. $ulica = DB::getPDO()->quote("{$args['ulica']}%");
  152. $query = "select WOJ, POW, GMI, SYM, SYM_UL from `" . self::ULIC_TABLE . "` where (NAZWA_1 like {$ulica} or concat(NAZWA_2, ' ', NAZWA_1) like {$ulica}) {$where} group by WOJ, POW, SYM_UL";
  153. $result = DB::getPDO()->fetchall($query);
  154. }
  155. if (!$result) {
  156. $ulica = DB::getPDO()->quote("%{$args['ulica']}");
  157. $query = "select WOJ, POW, GMI, SYM, SYM_UL from `" . self::ULIC_TABLE . "` where (NAZWA_1 like {$ulica} or concat(NAZWA_2, ' ', NAZWA_1) like {$ulica}) {$where} group by WOJ, POW, SYM_UL";
  158. $result = DB::getPDO()->fetchall($query);
  159. }
  160. if (!$result) {
  161. $ulica = DB::getPDO()->quote("%{$args['ulica']}%");
  162. $query = "select WOJ, POW, GMI, SYM, SYM_UL from `" . self::ULIC_TABLE . "` where (NAZWA_1 like {$ulica} or concat(NAZWA_2, ' ', NAZWA_1) like {$ulica}) {$where} group by WOJ, POW, SYM_UL";
  163. $result = DB::getPDO()->fetchall($query);
  164. }
  165. if (!$result) {
  166. preg_match('/^([^\.]*)\.([^\.]*)\.(.*)$/', $args['ulica'], $matches);
  167. if (count($matches) == 4) {
  168. $ulica = DB::getPDO()->quote(trim($matches[1]) . ". " . trim($matches[2]) . "% " . trim($matches[3]) . "%");
  169. $query = "select WOJ, POW, GMI, SYM, SYM_UL from `" . self::ULIC_TABLE . "` where (concat(CECHA, ' ', NAZWA_1) like {$ulica} " .
  170. "or concat(CECHA, ' ', NAZWA_2, ' ', NAZWA_1) like {$ulica}) {$where} group by WOJ, POW, SYM_UL";
  171. $result = DB::getPDO()->fetchall($query);
  172. if (!$result) {
  173. $ulica = DB::getPDO()->quote(trim($matches[1]) . ". " . trim($matches[3]) . "%");
  174. $query = "select WOJ, POW, GMI, SYM, SYM_UL from `" . self::ULIC_TABLE . "` where concat(CECHA, ' ', NAZWA_1) like {$ulica} {$where} group by WOJ, POW, SYM_UL";
  175. $result = DB::getPDO()->fetchall($query);
  176. }
  177. if (!$result) {
  178. $ulica = DB::getPDO()->quote("ul. " . trim($matches[3]) . "%");
  179. $query = "select WOJ, POW, GMI, SYM, SYM_UL from `" . self::ULIC_TABLE . "` where concat(CECHA, ' ', NAZWA_1) like {$ulica} {$where} group by WOJ, POW, SYM_UL";
  180. $result = DB::getPDO()->fetchall($query);
  181. }
  182. }
  183. }
  184. if (!$result) {
  185. preg_match('/^([^\.]*)\.(.*)$/', $args['ulica'], $matches);
  186. if (count($matches) == 3) {
  187. $ulica = DB::getPDO()->quote("ul. " . trim($matches[1]) . "% " . trim($matches[2]) . "%");
  188. $query = "select WOJ, POW, GMI, SYM, SYM_UL from `" . self::ULIC_TABLE . "` where (concat(CECHA, ' ', NAZWA_1) like {$ulica} " .
  189. "or concat(CECHA, ' ', NAZWA_2, ' ', NAZWA_1) like {$ulica}) {$where} group by WOJ, POW, SYM_UL";
  190. $result = DB::getPDO()->fetchall($query);
  191. if (!$result) {
  192. $ulica = DB::getPDO()->quote("ul. " . trim($matches[2]) . "%");
  193. $query = "select WOJ, POW, GMI, SYM, SYM_UL from `" . self::ULIC_TABLE . "` where concat(CECHA, ' ', NAZWA_1) like {$ulica} {$where} group by WOJ, POW, SYM_UL";
  194. $result = DB::getPDO()->fetchall($query);
  195. }
  196. if (!$result) {
  197. $ulica = DB::getPDO()->quote(trim($matches[1]) . ". " . trim($matches[2]) . "%");
  198. $query = "select WOJ, POW, GMI, SYM, SYM_UL from `" . self::ULIC_TABLE . "` where concat(CECHA, ' ', NAZWA_1) like {$ulica} {$where} group by WOJ, POW, SYM_UL";
  199. $result = DB::getPDO()->fetchall($query);
  200. }
  201. if (!$result) {
  202. $ulica = DB::getPDO()->quote(trim($matches[1]) . ". %" . trim($matches[2]));
  203. $query = "select WOJ, POW, GMI, SYM, SYM_UL from `" . self::ULIC_TABLE . "` where concat(CECHA, ' ', NAZWA_1) like {$ulica} {$where} group by WOJ, POW, SYM_UL";
  204. $result = DB::getPDO()->fetchall($query);
  205. }
  206. }
  207. }
  208. if (!$result) {
  209. $where = "and WOJ = '{$return['WOJ']}' and POW = '{$return['POW']}' and GMI = '{$return['GMI']}'";
  210. $query = "select WOJ, POW, GMI, SYM from `" . self::SIMC_TABLE . "` where NAZWA like " . DB::getPDO()->quote($args['ulica']) . " {$where}";
  211. $result = DB::getPDO()->fetchall($query);
  212. }
  213. if (!$result) {
  214. if (isset($return['SYM'])) $where2 = "and SYM = '{$return['SYM']}'";
  215. else $where2 = "";
  216. $where = "and WOJ = '{$return['WOJ']}' and POW = '{$return['POW']}' and ((RODZ_GMI<8 and GMI = '{$return['GMI']}' {$where2}) or RODZ_GMI>=8)";
  217. $query = "select count(*) from `" . self::ULIC_TABLE . "` where 1 = 1 {$where}";
  218. if ($liczbaUlic = DB::getPDO()->fetchValue($query)) throw new Exception(json_encode(['msg' => "ulica not found", 'sql' => $query]));
  219. } else {
  220. if (count($result) > 1) self::throwException("Found too many ulicas", $query);
  221. $return['WOJ'] = $result[0]['WOJ'];
  222. $return['POW'] = $result[0]['POW'];
  223. $return['GMI'] = $result[0]['GMI'];
  224. $return['SYM'] = $result[0]['SYM'];
  225. if (isset($result[0]['SYM_UL'])) $return['SYM_UL'] = $result[0]['SYM_UL'];
  226. }
  227. if (!isset($return['SYM'])) {
  228. if (isset($SYMarr)) $return['SYM'] = $SYMarr[0];
  229. else throw new Exception("miejscowosc not found");
  230. }
  231. } elseif (isset($return['SYM'])) {
  232. $query = "select count(*) from `" . self::ULIC_TABLE . "` where `SYM` = '{$return['SYM']}'";
  233. if (DB::getPDO()->fetchValue($query)) throw new Exception(json_encode(['msg' => "missing required parameter - ulica", 'sql' => $query]));
  234. }
  235. return $return;
  236. }
  237. public static function verify($args) {
  238. $argsAvailable = ["WOJ", "POW", "GMI", "SYM", "SYM_UL"];
  239. self::checkArgs($args, $argsAvailable);
  240. if (!(isset($args['SYM']) && isset($args['SYM_UL']))) throw new Exception("Missing SYM or/and SYM_UL");
  241. if ($args['SYM_UL']) {
  242. $query = "select WOJ, POW, GMI, CECHA, NAZWA_1, NAZWA_2 from `" . self::ULIC_TABLE . "` where SYM = " . DB::getPDO()->quote($args['SYM']) . " and SYM_UL = " . DB::getPDO()->quote($args['SYM_UL']);
  243. if (!($result = DB::getPDO()->fetchall($query))) throw new Exception("ulica not found");
  244. if (count($result) > 1) throw new Exception("Found too many ulicas");
  245. } else {
  246. $query = "select count(*) from `" . self::ULIC_TABLE . "` where `SYM` = '{$args['SYM']}'";
  247. if (DB::getPDO()->fetchValue($query)) throw new Exception("Invalid SYM_UL");
  248. $query = "select `WOJ`, `POW`, `GMI` from `" . self::SIMC_TABLE . "` where SYM = " . DB::getPDO()->quote($args['SYM']);
  249. if (!($result = DB::getPDO()->fetchall($query))) throw new Exception("miejscowosc not found");
  250. }
  251. return $result[0];
  252. }
  253. public static function getNames($args) {
  254. $result = self::verify($args);
  255. if ($args['SYM_UL']) {
  256. $return['ulica_cecha'] = $result['CECHA'];
  257. $return['ulica_nazwa_1'] = $result['NAZWA_1'];
  258. $return['ulica_nazwa_2'] = $result['NAZWA_2'];
  259. }
  260. if (isset($args['WOJ'])) {
  261. if ($args['WOJ'] != $result['WOJ']) throw new Exception("Invalid WOJ");
  262. } else $args['WOJ'] = $result['WOJ'];
  263. if (isset($args['POW'])) {
  264. if ($args['POW'] != $result['POW']) throw new Exception("Invalid POW");
  265. } else $args['POW'] = $result['POW'];
  266. if (isset($args['GMI'])) {
  267. if ($args['GMI'] != $result['GMI']) throw new Exception("Invalid POW");
  268. } else $args['GMI'] = $result['GMI'];
  269. $query = "select NAZWA from `" . self::TERC_TABLE . "` where WOJ = '{$args['WOJ']}' and POW = 0";
  270. if (!($return['wojewodztwo'] = DB::getPDO()->fetchValue($query))) throw new Exception("wojewodztwo not found");
  271. $query = "select NAZWA, NAZWA_DOD from `" . self::TERC_TABLE . "` where WOJ = '{$args['WOJ']}' and POW = '{$args['POW']}' and GMI = 0";
  272. if (!($result = DB::getPDO()->fetchall($query))) throw new Exception("powiat not found");
  273. if (count($result) > 1) throw new Exception("Found too many powiats");
  274. $return['powiat'] = $result[0]['NAZWA'];
  275. $return['powiat_rodzaj'] = $result[0]['NAZWA_DOD'];
  276. $query = "select NAZWA, NAZWA_DOD from `" . self::TERC_TABLE . "` where WOJ = '{$args['WOJ']}' and POW = '{$args['POW']}' and GMI = '{$args['GMI']}' group by WOJ, POW, GMI, NAZWA";
  277. if (!($result = DB::getPDO()->fetchall($query))) throw new Exception("gmina not found");
  278. if (count($result) > 1) throw new Exception("Found too many gminas");
  279. $return['gmina'] = $result[0]['NAZWA'];
  280. $return['gmina_rodzaj'] = $result[0]['NAZWA_DOD'];
  281. $query = "select NAZWA from `" . self::SIMC_TABLE . "` where WOJ = '{$args['WOJ']}' and POW = '{$args['POW']}' and GMI = '{$args['GMI']}' and SYM = '{$args['SYM']}'";
  282. if (!($result = DB::getPDO()->fetchall($query))) throw new Exception("miejscowosc not found");
  283. if (count($result) > 1) throw new Exception("Found too many miejscowoscs");
  284. $return['miejscowosc'] = $result[0]['NAZWA'];
  285. return $return;
  286. }
  287. }