1 | <?php
|
---|
2 | require("config.php");
|
---|
3 | $lat=$_REQUEST['lat'];
|
---|
4 | $lon=$_REQUEST['lon'];
|
---|
5 | if ( !is_numeric($lat) or !is_numeric($lon) ) die;
|
---|
6 |
|
---|
7 |
|
---|
8 | // From: http://www.sitepoint.com/forums/showthread.php?656315-Adding-Distance-To-GPS-Coordinates-To-Get-Bounding-Box&p=4519646&viewfull=1#post4519646
|
---|
9 | function new_coords($lat, $lon, $bearing, $distance)
|
---|
10 | {
|
---|
11 | // Radius of Earth in meters
|
---|
12 | $radius = 6371000;
|
---|
13 |
|
---|
14 | // New latitude in degrees.
|
---|
15 | $new_lat = rad2deg(asin(sin(deg2rad($lat)) * cos($distance / $radius) + cos(deg2rad($lat)) * sin($distance / $radius) * cos(deg2rad($bearing))));
|
---|
16 |
|
---|
17 | // New longitude in degrees.
|
---|
18 | $new_lon = rad2deg(deg2rad($lon) + atan2(sin(deg2rad($bearing)) * sin($distance / $radius) * cos(deg2rad($lat)), cos($distance / $radius) - sin(deg2rad($lat)) * sin(deg2rad($new_lat))));
|
---|
19 |
|
---|
20 | // Assign new latitude and longitude to an array to be returned to the caller.
|
---|
21 | $coord['lat'] = $new_lat;
|
---|
22 | $coord['lon'] = $new_lon;
|
---|
23 |
|
---|
24 | return $coord;
|
---|
25 | }
|
---|
26 |
|
---|
27 | // Boundary box 20x20 meters around the point
|
---|
28 | $x1_coor = new_coords($lat, $lon, 315, 10); // Left upper point
|
---|
29 | $x2_coor = new_coords($lat, $lon, 135, 10); // Right down point
|
---|
30 |
|
---|
31 | $x1_lon = $x1_coor['lon'];
|
---|
32 | $x2_lon = $x2_coor['lon'];
|
---|
33 | $x1_lat = $x1_coor['lat'];
|
---|
34 | $x2_lat = $x2_coor['lat'];;
|
---|
35 |
|
---|
36 | $boundary_polygon=($x1_lon) . " " . ($x1_lat) . ", " . ($x1_lon) . " " . ($x2_lat) . ", " . ($x2_lon) . " " . ($x2_lat) . ", " . ($x2_lon) . " " . ($x1_lat) . ", " . ($x1_lon) . " " . ($x1_lat);
|
---|
37 |
|
---|
38 | header('Content-Type: application/json');
|
---|
39 |
|
---|
40 | $data = array();
|
---|
41 |
|
---|
42 | $data["coordinates"] = array( "lat" => "$lat", "lon" => "$lon");
|
---|
43 | $data["source"] = "cuzk:ruian";
|
---|
44 |
|
---|
45 |
|
---|
46 | // building
|
---|
47 | $query="
|
---|
48 | select s.kod,
|
---|
49 | s.pocet_podlazi, a.nazev zpusob_vyuziti, s.plati_od, s.pocet_bytu, s.dokonceni,
|
---|
50 | s.zpusob_vyuziti_kod, a.osmtag_k, a.osmtag_v
|
---|
51 | from rn_stavebni_objekt s
|
---|
52 | left outer join osmtables.zpusob_vyuziti_objektu a on s.zpusob_vyuziti_kod = a.kod
|
---|
53 | where st_contains(s.hranice,st_transform(st_geomfromtext('POINT(".$lon." ".$lat.")',4326),900913))
|
---|
54 | and not s.deleted
|
---|
55 | limit 1;
|
---|
56 | ";
|
---|
57 | $result=pg_query($CONNECT,$query);
|
---|
58 |
|
---|
59 | if (pg_num_rows($result) > 0)
|
---|
60 | {
|
---|
61 | $row = pg_fetch_array($result, 0);
|
---|
62 |
|
---|
63 |
|
---|
64 | $data["stavebni_objekt"] =
|
---|
65 | array( "ruian_id" => $row["kod"],
|
---|
66 | "pocet_podlazi" => $row["pocet_podlazi"],
|
---|
67 | "zpusob_vyuziti" => $row["zpusob_vyuziti"],
|
---|
68 | "zpusob_vyuziti_kod" => $row["zpusob_vyuziti_kod"],
|
---|
69 | "zpusob_vyuziti_key" => $row["osmtag_k"],
|
---|
70 | "zpusob_vyuziti_val" => $row["osmtag_v"],
|
---|
71 | "pocet_bytu" => $row["pocet_bytu"],
|
---|
72 | "dokonceni" => $row["dokonceni"],
|
---|
73 | "plati_od" => $row["plati_od"]
|
---|
74 | );
|
---|
75 | } else
|
---|
76 | $data["stavebni_objekt"] = array();
|
---|
77 |
|
---|
78 | // Ghosts: Buildings without geometry in close neighbourhood
|
---|
79 | $query="
|
---|
80 | select * from (
|
---|
81 | select s.kod,
|
---|
82 | s.pocet_podlazi, a.nazev zpusob_vyuziti, s.plati_od, s.pocet_bytu, s.dokonceni,
|
---|
83 | s.zpusob_vyuziti_kod, a.osmtag_k, a.osmtag_v,
|
---|
84 | s.definicni_bod,
|
---|
85 | st_distance( (st_transform(s.definicni_bod,4326))::geography, (st_setsrid(st_makepoint(".$lon.",".$lat."),4326))::geography ) dist
|
---|
86 | from rn_stavebni_objekt s
|
---|
87 | left outer join osmtables.zpusob_vyuziti_objektu a on s.zpusob_vyuziti_kod = a.kod
|
---|
88 | where st_intersects(s.definicni_bod, st_transform(st_geometryfromtext(
|
---|
89 | 'POLYGON (( $boundary_polygon ))' ,4326),900913))
|
---|
90 | and not s.deleted
|
---|
91 | and s.hranice is null
|
---|
92 | order by definicni_bod <->
|
---|
93 | st_transform(st_setsrid(st_makepoint(".$lon.",".$lat."),4326),900913)
|
---|
94 | limit 5) as x
|
---|
95 | order by dist;
|
---|
96 | ";
|
---|
97 | $result=pg_query($CONNECT,$query);
|
---|
98 |
|
---|
99 | if (pg_num_rows($result) > 0)
|
---|
100 | {
|
---|
101 | $so = array();
|
---|
102 | for ($i = 0; $i < pg_num_rows($result); $i++)
|
---|
103 | {
|
---|
104 | $row = pg_fetch_array($result, $i);
|
---|
105 | array_push($so,
|
---|
106 | array( "ruian_id" => $row["kod"],
|
---|
107 | "pocet_podlazi" => $row["pocet_podlazi"],
|
---|
108 | "zpusob_vyuziti" => $row["zpusob_vyuziti"],
|
---|
109 | "zpusob_vyuziti_kod" => $row["zpusob_vyuziti_kod"],
|
---|
110 | "zpusob_vyuziti_key" => $row["osmtag_k"],
|
---|
111 | "zpusob_vyuziti_val" => $row["osmtag_v"],
|
---|
112 | "pocet_bytu" => $row["pocet_bytu"],
|
---|
113 | "dokonceni" => $row["dokonceni"],
|
---|
114 | "plati_od" => $row["plati_od"],
|
---|
115 | "vzdalenost" => $row["dist"]
|
---|
116 | ));
|
---|
117 | }
|
---|
118 | $data["so_bez_geometrie"] = $so;
|
---|
119 | } else
|
---|
120 | $data["so_bez_geometrie"] = array();
|
---|
121 |
|
---|
122 | // Addresses
|
---|
123 | if ($data["stavebni_objekt"]["ruian_id"] > 0)
|
---|
124 | {
|
---|
125 | $query="
|
---|
126 | select am.kod as adresni_misto_kod,
|
---|
127 | am.stavobj_kod,
|
---|
128 | st_asgeojson(st_transform(am.definicni_bod, 4326)) as pozice,
|
---|
129 | CASE
|
---|
130 | WHEN s.typ_kod = 1 THEN 'Číslo popisné'
|
---|
131 | WHEN s.typ_kod = 2 THEN 'Číslo evidenční'
|
---|
132 | WHEN s.typ_kod = 3 THEN 'bez č.p./č.e.'
|
---|
133 | ELSE ''
|
---|
134 | END cislo_typ,
|
---|
135 | am.cislo_domovni,
|
---|
136 | am.cislo_orientacni_hodnota || coalesce(am.cislo_orientacni_pismeno, '') cislo_orientacni,
|
---|
137 | am.adrp_psc psc,
|
---|
138 | ul.kod ulice_kod, ul.nazev ulice,
|
---|
139 | c.kod cast_obce_kod, c.nazev cast_obce,
|
---|
140 | momc.kod mestska_cast_kod, momc.nazev mestska_cast,
|
---|
141 | ob.kod obec_kod, ob.nazev obec,
|
---|
142 | ok.kod okres_kod, ok.nazev okres,
|
---|
143 | vu.kod kraj_kod, vu.nazev kraj
|
---|
144 | from ruian.rn_adresni_misto am
|
---|
145 | left outer join rn_stavebni_objekt s on am.stavobj_kod = s.kod and not s.deleted
|
---|
146 | left outer join osmtables.zpusob_vyuziti_objektu a on s.zpusob_vyuziti_kod = a.kod
|
---|
147 | left outer join rn_ulice ul on am.ulice_kod = ul.kod and not ul.deleted
|
---|
148 | left outer join rn_cast_obce c on c.kod = s.cobce_kod and not c.deleted
|
---|
149 | left outer join rn_momc momc on momc.kod = s.momc_kod and not momc.deleted
|
---|
150 | left outer join rn_obec ob on coalesce(ul.obec_kod, c.obec_kod) = ob.kod and not ob.deleted
|
---|
151 | left outer join rn_okres ok on ob.okres_kod = ok.kod and not ok.deleted
|
---|
152 | left outer join rn_vusc vu on ok.vusc_kod = vu.kod and not vu.deleted
|
---|
153 | where am.stavobj_kod = ".$data["stavebni_objekt"]["ruian_id"]."
|
---|
154 | and not am.deleted
|
---|
155 | order by st_distance( (st_transform(am.definicni_bod,4326))::geography,
|
---|
156 | (st_setsrid(st_makepoint(".$lon.",".$lat."),4326))::geography)
|
---|
157 | ;
|
---|
158 | ";
|
---|
159 |
|
---|
160 | $result=pg_query($CONNECT,$query);
|
---|
161 | $error= pg_last_error($CONNECT);
|
---|
162 | if (pg_num_rows($result) > 0)
|
---|
163 | {
|
---|
164 | $am = array();
|
---|
165 | for ($i = 0; $i < pg_num_rows($result); $i++)
|
---|
166 | {
|
---|
167 | $row = pg_fetch_array($result, $i);
|
---|
168 | $geometry=json_decode($row["pozice"], true);
|
---|
169 | array_push($am,
|
---|
170 | array("ruian_id" => $row["adresni_misto_kod"],
|
---|
171 | "pozice" => $geometry['coordinates'],
|
---|
172 | "budova_kod" => $row["stavobj_kod"],
|
---|
173 | "cislo_typ" => $row["cislo_typ"],
|
---|
174 | "cislo_domovni" => $row["cislo_domovni"],
|
---|
175 | "cislo_orientacni" => $row["cislo_orientacni"],
|
---|
176 | "ulice_kod" => $row["ulice_kod"],
|
---|
177 | "ulice" => $row["ulice"],
|
---|
178 | "cast_obce_kod" => $row["cast_obce_kod"],
|
---|
179 | "cast_obce" => $row["cast_obce"],
|
---|
180 | "mestska_cast_kod" => $row["mestska_cast_kod"],
|
---|
181 | "mestska_cast" => $row["mestska_cast"],
|
---|
182 | "obec_kod" => $row["obec_kod"],
|
---|
183 | "obec" => $row["obec"],
|
---|
184 | "okres_kod" => $row["okres_kod"],
|
---|
185 | "okres" => $row["okres"],
|
---|
186 | "kraj_kod" => $row["kraj_kod"],
|
---|
187 | "kraj" => $row["kraj"],
|
---|
188 | "psc" => $row["psc"]
|
---|
189 | ));
|
---|
190 | }
|
---|
191 | $data["adresni_mista"] = $am;
|
---|
192 | } else
|
---|
193 | {
|
---|
194 | // echo "error: $error\n";
|
---|
195 | $data["adresni_mista"] = array();
|
---|
196 | }
|
---|
197 | }
|
---|
198 | else
|
---|
199 | {
|
---|
200 | $query="
|
---|
201 | select am.kod as adresni_misto_kod,
|
---|
202 | am.stavobj_kod,
|
---|
203 | st_asgeojson(st_transform(am.definicni_bod, 4326)) as pozice,
|
---|
204 | CASE
|
---|
205 | WHEN s.typ_kod = 1 THEN 'Číslo popisné'
|
---|
206 | WHEN s.typ_kod = 2 THEN 'Číslo evidenční'
|
---|
207 | WHEN s.typ_kod = 3 THEN 'bez č.p./č.e.'
|
---|
208 | ELSE ''
|
---|
209 | END cislo_typ,
|
---|
210 | am.cislo_domovni,
|
---|
211 | am.cislo_orientacni_hodnota || coalesce(am.cislo_orientacni_pismeno, '') cislo_orientacni,
|
---|
212 | am.adrp_psc psc, ul.nazev ulice, c.nazev cast_obce,
|
---|
213 | momc.nazev mestska_cast,
|
---|
214 | ob.nazev obec, ok.nazev okres, vu.nazev kraj,
|
---|
215 | st_distance( (st_transform(am.definicni_bod,4326))::geography, (st_setsrid(st_makepoint(".$lon.", ".$lat."),4326))::geography ) dist
|
---|
216 | from ( select kod, stavobj_kod,
|
---|
217 | cislo_domovni, cislo_orientacni_hodnota, cislo_orientacni_pismeno,
|
---|
218 | ulice_kod, adrp_psc,
|
---|
219 | definicni_bod
|
---|
220 | from ruian.rn_adresni_misto
|
---|
221 | where not deleted
|
---|
222 | order by definicni_bod <->
|
---|
223 | st_transform(st_setsrid(st_makepoint(".$lon.", ".$lat."),4326),900913)
|
---|
224 | limit 100) as am
|
---|
225 | left outer join rn_stavebni_objekt s on am.stavobj_kod = s.kod and not s.deleted
|
---|
226 | left outer join osmtables.zpusob_vyuziti_objektu a on s.zpusob_vyuziti_kod = a.kod
|
---|
227 | left outer join rn_ulice ul on am.ulice_kod = ul.kod and not ul.deleted
|
---|
228 | left outer join rn_cast_obce c on c.kod = s.cobce_kod and not c.deleted
|
---|
229 | left outer join rn_momc momc on momc.kod = s.momc_kod and not momc.deleted
|
---|
230 | left outer join rn_obec ob on coalesce(ul.obec_kod, c.obec_kod) = ob.kod and not ob.deleted
|
---|
231 | left outer join rn_okres ok on ob.okres_kod = ok.kod and not ok.deleted
|
---|
232 | left outer join rn_vusc vu on ok.vusc_kod = vu.kod and not vu.deleted
|
---|
233 | where st_distance( (st_transform(am.definicni_bod,4326))::geography, (st_setsrid(st_makepoint(".$lon.", ".$lat."),4326))::geography ) < 100
|
---|
234 | order by dist
|
---|
235 | limit 5
|
---|
236 | ;
|
---|
237 | ";
|
---|
238 |
|
---|
239 | $result=pg_query($CONNECT,$query);
|
---|
240 | $error= pg_last_error($CONNECT);
|
---|
241 | if (pg_num_rows($result) > 0)
|
---|
242 | {
|
---|
243 | $am = array();
|
---|
244 | for ($i = 0; $i < pg_num_rows($result); $i++)
|
---|
245 | {
|
---|
246 | $row = pg_fetch_array($result, $i);
|
---|
247 | $geometry=json_decode($row["pozice"], true);
|
---|
248 | array_push($am,
|
---|
249 | array("ruian_id" => $row["adresni_misto_kod"],
|
---|
250 | "pozice" => $geometry['coordinates'],
|
---|
251 | "budova_kod" => $row["stavobj_kod"],
|
---|
252 | "cislo_typ" => $row["cislo_typ"],
|
---|
253 | "cislo_domovni" => $row["cislo_domovni"],
|
---|
254 | "cislo_orientacni" => $row["cislo_orientacni"],
|
---|
255 | "ulice" => $row["ulice"],
|
---|
256 | "cast_obce" => $row["cast_obce"],
|
---|
257 | "mestska_cast" => $row["mestska_cast"],
|
---|
258 | "obec" => $row["obec"],
|
---|
259 | "okres" => $row["okres"],
|
---|
260 | "kraj" => $row["kraj"],
|
---|
261 | "psc" => $row["psc"],
|
---|
262 | "vzdalenost" => $row["dist"]
|
---|
263 | ));
|
---|
264 | }
|
---|
265 | $data["adresni_mista"] = $am;
|
---|
266 | } else
|
---|
267 | {
|
---|
268 | // echo "error: $error\n";
|
---|
269 | $data["adresni_mista"] = array();
|
---|
270 | }
|
---|
271 |
|
---|
272 | }
|
---|
273 |
|
---|
274 | // land
|
---|
275 | $query="
|
---|
276 | select s.id, a.nazev as druh_pozemku, b.nazev as zpusob_vyuziti, s.plati_od
|
---|
277 | from rn_parcela s
|
---|
278 | left outer join osmtables.druh_pozemku a on s.druh_pozemku_kod = a.kod
|
---|
279 | left outer join osmtables.zpusob_vyuziti_pozemku b on s.zpusob_vyu_poz_kod = b.kod
|
---|
280 | where st_contains(s.hranice,st_transform(st_geomfromtext('POINT(".$lon." ".$lat.")',4326),900913))
|
---|
281 | and not s.deleted
|
---|
282 | limit 1;
|
---|
283 | ";
|
---|
284 |
|
---|
285 | $result=pg_query($CONNECT,$query);
|
---|
286 | $error= pg_last_error($CONNECT);
|
---|
287 | if (pg_num_rows($result) > 0)
|
---|
288 | {
|
---|
289 | $row = pg_fetch_array($result, 0);
|
---|
290 |
|
---|
291 | $data["parcela"] =
|
---|
292 | array( "ruian_id" => $row["id"],
|
---|
293 | "druh_pozemku" => $row["druh_pozemku"],
|
---|
294 | "zpusob_vyuziti" => $row["zpusob_vyuziti"],
|
---|
295 | "plati_od" => $row["plati_od"]
|
---|
296 | );
|
---|
297 | } else
|
---|
298 | {
|
---|
299 | // echo "error: $error\n";
|
---|
300 | $data["parcela"] = array();
|
---|
301 | }
|
---|
302 |
|
---|
303 | // ulice
|
---|
304 | $query="
|
---|
305 | select u.kod, u.nazev as jmeno
|
---|
306 | from ( select kod, nazev, definicni_cara
|
---|
307 | from ruian.rn_ulice
|
---|
308 | where not deleted
|
---|
309 | order by definicni_cara <->
|
---|
310 | st_transform(st_setsrid(st_makepoint(".$lon.",".$lat."),4326),900913)
|
---|
311 | limit 500) as u
|
---|
312 | where st_distance( (st_transform(u.definicni_cara,4326))::geography, (st_setsrid(st_makepoint(".$lon.",".$lat."),4326))::geography ) < 10
|
---|
313 | order by st_distance( (st_transform(u.definicni_cara,4326))::geography,
|
---|
314 | (st_setsrid(st_makepoint(".$lon.",".$lat."),4326))::geography)
|
---|
315 | limit 1
|
---|
316 | ;
|
---|
317 | ";
|
---|
318 |
|
---|
319 | $result=pg_query($CONNECT,$query);
|
---|
320 | $error= pg_last_error($CONNECT);
|
---|
321 | if (pg_num_rows($result) > 0)
|
---|
322 | {
|
---|
323 | $row = pg_fetch_array($result, 0);
|
---|
324 |
|
---|
325 | $data["ulice"] =
|
---|
326 | array( "ruian_id" => $row["kod"],
|
---|
327 | "jmeno" => $row["jmeno"]);
|
---|
328 | } else
|
---|
329 | {
|
---|
330 | // echo "error: $error\n";
|
---|
331 | $data["ulice"] = array();
|
---|
332 | }
|
---|
333 |
|
---|
334 | // cadastral area
|
---|
335 | $query="
|
---|
336 | select ku.kod, ku.nazev,
|
---|
337 | ob.kod obec_kod, ob.nazev obec,
|
---|
338 | ok.kod okres_kod, ok.nazev okres,
|
---|
339 | vu.kod kraj_kod, vu.nazev kraj
|
---|
340 | from rn_katastralni_uzemi ku
|
---|
341 | left outer join rn_obec ob on ku.obec_kod = ob.kod and not ob.deleted
|
---|
342 | left outer join rn_okres ok on ob.okres_kod = ok.kod and not ok.deleted
|
---|
343 | left outer join rn_vusc vu on ok.vusc_kod = vu.kod and not vu.deleted
|
---|
344 | where st_contains(ku.hranice,st_transform(st_geomfromtext('POINT(".$lon." ".$lat.")',4326),900913))
|
---|
345 | and not ku.deleted
|
---|
346 | limit 1;
|
---|
347 | ";
|
---|
348 |
|
---|
349 | $result=pg_query($CONNECT,$query);
|
---|
350 | $error= pg_last_error($CONNECT);
|
---|
351 | if (pg_num_rows($result) > 0)
|
---|
352 | {
|
---|
353 | $row = pg_fetch_array($result, 0);
|
---|
354 |
|
---|
355 | $data["katastr"] =
|
---|
356 | array( "ruian_id" => $row["kod"],
|
---|
357 | "nazev" => $row["nazev"],
|
---|
358 | "obec_kod" => $row["obec_kod"],
|
---|
359 | "obec" => $row["obec"],
|
---|
360 | "okres_kod" => $row["okres_kod"],
|
---|
361 | "okres" => $row["okres"],
|
---|
362 | "kraj_kod" => $row["kraj_kod"],
|
---|
363 | "kraj" => $row["kraj"],
|
---|
364 | );
|
---|
365 | } else
|
---|
366 | {
|
---|
367 | $data["katastr"] = array();
|
---|
368 | }
|
---|
369 |
|
---|
370 | echo json_encode($data);
|
---|
371 |
|
---|
372 | ?>
|
---|