biale_plamy.php 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. $_SERVER['SERVER_NAME'] = 'biuro.biall-net.pl';
  3. require("../../se-lib/bootstrap.php");
  4. Lib::loadClass("Vendor_Geophp");
  5. //
  6. $query = "select astext(the_geom) as multiway from Rozdzielcza_test_bzyk_drogi where numpoints(the_geom)>1";
  7. $result = DB::getPDO()->fetchall($query);
  8. $joins = array();
  9. foreach ($result as $row) {
  10. $multiway = Vendor_Geophp::load($row['multiway'], 'wkt');
  11. //echo $multiway->length();
  12. for ($i = 1; $i < $n = $multiway->numPoints(); $i++) {
  13. $ways[] = new LineString(array($multiway->pointN($i), $multiway->pointN($i+1)));
  14. if (!in_array($multiway->pointN($i)->asText(), $joins)) $joins[] = $multiway->pointN($i)->asText();
  15. }
  16. if (!in_array($multiway->pointN($n)->asText(), $joins)) $joins[] = $multiway->pointN($n)->asText();
  17. }
  18. // todo relacje ways <-> joins
  19. $query = "select ID, astext(the_geom) as point, astext(the_geom_nearest_way) as way, astext(the_geom_xpoint) as xpoint from Rozdzielcza_test_bzyk_punkty_adresowe";
  20. $result = DB::getPDO()->fetchall($query);
  21. $xpoints = array();
  22. foreach ($result as $row) {
  23. $points[$row['ID']] = Vendor_Geophp::load($row['point'], 'wkt');
  24. $xpoint = Vendor_Geophp::load($row['xpoint'], 'wkt');
  25. if (!($key = array_search($xpoint->asText(), $xpoints))) $key = array_push($xpoints, $xpoint->asText())-1;
  26. $xpoints2points[$key][] = $row['ID'];
  27. $points2xpoints[$row['ID']] = $key;
  28. $way = Vendor_Geophp::load($row['way'], 'wkt');
  29. if (!(in_array($xpoint->asText(), $joins))) {
  30. if (@!in_array($key, $ways2cut[array_search($way, $ways)])) {
  31. $ways2cut[array_search($way, $ways)][] = $key;
  32. }
  33. };
  34. }
  35. function pointsDistance($a, $b) {
  36. $line = new LineString(array($a, $b));
  37. return $line->length();
  38. }
  39. foreach ($ways2cut as $way_key => $xpoint_keys) {
  40. $distances = array();
  41. foreach ($xpoint_keys as $xpoint_key) {
  42. $xpoint = Vendor_Geophp::load($xpoints[$xpoint_key], 'wkt');
  43. $joins[] = $xpoint;
  44. $distances[$xpoint_key] = pointsDistance($ways[$way_key]->startPoint(), $xpoint);
  45. }
  46. asort($distances);
  47. $sub_xpoints = array();
  48. foreach (array_keys($distances) as $xpoint_key) {
  49. $sub_xpoints[] = Vendor_Geophp::load($xpoints[$xpoint_key]);
  50. }
  51. $startPoint = $ways[$way_key]->startPoint();
  52. $endPoint = $ways[$way_key]->endPoint();
  53. $ways[$way_key] = new LineString(array($startPoint, $sub_xpoints[0]));
  54. for ($i = 0; $i < $n = count($sub_xpoints) - 1; $i++) {
  55. $ways[] = new LineString(array($sub_xpoints[$i], $sub_xpoints[$i+1]));
  56. }
  57. $ways[] = new LineString(array($sub_xpoints[$n], $endPoint));
  58. }
  59. //print_r($ways);
  60. /*
  61. $db = DB::getDB();
  62. $db->query("truncate table Rozdzielcza_test_bzyk_ways");
  63. $db->query("truncate table Rozdzielcza_test_bzyk_joins");
  64. foreach ($ways as $way) {
  65. $query = "insert into Rozdzielcza_test_bzyk_ways (the_geom) values (linefromtext('".$way->asText()."'))";
  66. $db->query($query);
  67. }
  68. foreach ($xpoints as $xpoint) {
  69. $xpoint2 = Vendor_Geophp::load($xpoint, 'wkt');
  70. $query = "insert into Rozdzielcza_test_bzyk_joins (the_geom) values (pointfromtext('".$xpoint2->asText()."'))";
  71. $db->query($query);
  72. }
  73. */