forked from opencaching/opencaching-pl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGetRegions.php
150 lines (130 loc) · 4.38 KB
/
GetRegions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
/** class GetRegions
*
* this class find Counrty and region (administation district, for exapmle Poland, woj. Małopolskie)
* returns array with names and codes. Compaibile also with table `cache_location`
*
* @params on input:
*
* float: $lat - latitude
* float: $lon - longitude
*
*
* Example returned
* Array
* (
* [adm1] => Polska
* [adm2] => Region Południowy
* [adm3] => Małopolskie
* [adm4] =>
* [code1] => PL
* [code2] => PL2
* [code3] => PL22
* [code4] =>
* )
*
* (adm4 and code4 is returned when coordinates are in known huge city area. Not suer if it is useful, so be carefull with that)
*
* Example of use:
* <?
* $region = new GetRegions();
* $regiony = $region->GetRegion($lat, $lon);
* print_r ($regiony);
* ?>
*
* @author Andrzej Łza Woźniak (some code I copied from former code.)
*/
class GetRegions
{
/**
*
* @@param array $opt - database accesing data from opencaching config
* @param float $lat geografical latitude (coordinates)
* @param float $lon geografical longitude (coordinates)
*
* @return array with code and names of regions selected from input geografical coordinates.void
*/
public function GetRegion($lat, $lon)
{
require_once(__DIR__ . '/lib/gis/gis.class.php');
$lat_float = (float) $lat;
$lon_float = (float) $lon;
$sCode = '';
$tmpqery = "SELECT `level`, `code`, AsText(`shape`) AS `geometry` FROM `nuts_layer` WHERE WITHIN(GeomFromText('POINT($lon $lat)'), `shape`) ORDER BY `level` DESC";
$db = new dataBase;
$db->simpleQuery($tmpqery);
$rsLayers = $db->dbResultFetchAll();
foreach ($rsLayers as $rLayers) {
if (gis::ptInLineRing($rLayers['geometry'], 'POINT(' . $lon . ' ' . $lat . ')')) {
$sCode = $rLayers['code'];
break;
}
}
if ($sCode != '') {
$adm1 = null;
$code1 = null;
$adm2 = null;
$code2 = null;
$adm3 = null;
$code3 = null;
$adm4 = null;
$code4 = null;
if (mb_strlen($sCode) > 5)
$sCode = mb_substr($sCode, 0, 5);
if (mb_strlen($sCode) == 5) {
$code4 = $sCode;
$q = "SELECT `name` FROM `nuts_codes` WHERE `code`='$sCode'";
$db->simpleQuery($q);
$re = $db->dbResultFetch();
$adm4 = $re["name"];
unset($re, $q);
$sCode = mb_substr($sCode, 0, 4);
}
if (mb_strlen($sCode) == 4) {
$code3 = $sCode;
$q = "SELECT `name` FROM `nuts_codes` WHERE `code`='$sCode'";
$db->simpleQuery($q);
$re = $db->dbResultFetch();
$adm3 = $re["name"];
unset($re, $q);
$sCode = mb_substr($sCode, 0, 3);
}
if (mb_strlen($sCode) == 3) {
$code2 = $sCode;
$q = "SELECT `name` FROM `nuts_codes` WHERE `code`='$sCode'";
$db->simpleQuery($q);
$re = $db->dbResultFetch();
$adm2 = $re["name"];
unset($re, $q);
$sCode = mb_substr($sCode, 0, 2);
}
if (mb_strlen($sCode) == 2) {
$code1 = $sCode;
// try to get localised name first
$q = "SELECT `countries`.`pl` FROM `countries` WHERE `countries`.`short`='$sCode'"; // TODO: country column should be localized
$db->simpleQuery($q);
$re = $db->dbResultFetch();
$adm1 = $re["pl"];
unset($re, $q);
if ($adm1 == null) {
$q = "SELECT `name` FROM `nuts_codes` WHERE `code`='$sCode'";
$db->simpleQuery($q);
$re = $db->dbResultFetch();
$adm1 = $re["name"];
unset($re, $q);
}
}
$wynik['adm1'] = $adm1;
$wynik['adm2'] = $adm2;
$wynik['adm3'] = $adm3;
$wynik['adm4'] = $adm4;
$wynik['code1'] = $code1;
$wynik['code2'] = $code2;
$wynik['code3'] = $code3;
$wynik['code4'] = $code4;
} else
$wynik = false;
return $wynik;
}
}
?>