function num_of_neighbors($x, $y) { $neighbors = neighbors($x, $y); $num = 0; for ($i = 0; $i < 8; $i++) { if (isset($_SESSION["world"][$neighbors[$i]["x"]][$neighbors[$i]["y"]]) && $_SESSION["world"][$neighbors[$i]["x"]][$neighbors[$i]["y"]]) { $num++; } } return $num; }
<?php include "../../config/settings.inc.php"; include "../../include/database.inc.php"; include "setup.php"; include "../../include/myview.php"; $t = new MyView(); $t->thispage = "iem-sites"; $t->title = "Site Neighbors"; $t->sites_current = "neighbors"; function neighbors($station, $lat, $lon) { $con = iemdb("mesosite"); $rs = pg_prepare($con, "_SELECT", "SELECT *,\n ST_distance(ST_transform(geom,3857), \n ST_transform(ST_GeomFromEWKT('SRID=4326;POINT(" . $lon . " " . $lat . ")'), 3857)) /1000.0 as dist from stations \n WHERE ST_point_inside_circle(geom, " . $lon . ", " . $lat . ", 0.25) \n and id != \$1 ORDER by dist ASC"); $result = pg_execute($con, "_SELECT", array($station)); $s = "<table class=\"table table-striped\">\n <thead><tr><th>Distance [km]</th><th>Network</th><th>Station Name</th></tr></thead>"; for ($i = 0; $row = @pg_fetch_assoc($result, $i); $i++) { $s .= sprintf("<tr><td>%.3f</td><td><a href=\"locate.php?network=%s\">%s</a></td><td><a href=\"site.php?station=%s&network=%s\">%s</a></td></tr>", $row["dist"], $row["network"], $row["network"], $row["id"], $row["network"], $row["name"]); } $s .= "</table>"; return $s; } $n = neighbors($station, $metadata["lat"], $metadata["lon"]); $t->content = <<<EOF <h3 class="subtitle">Neighboring Stations</h3> <p>The following is a list of IEM tracked stations within roughly 25 kilometers from the site. Click on the site name for more information.</p> {$n} EOF; $t->render('sites.phtml');
function a_star($start, $target, $map) { $open_heap = array($start); // binary min-heap of indexes with values in $f $open = array($start => TRUE); // set of indexes $closed = array(); // set of indexes $g = []; $h = []; $f = []; $from = []; $g[$start] = 0; $h[$start] = heuristic($start, $target); $f[$start] = $g[$start] + $h[$start]; while ($open) { $i = heap_pop($open_heap, $f); //not yet supported //unset($open[$i]); $open[$i] = null; $closed[$i] = TRUE; if ($i == $target) { $path = array(); for (; $i != $start; $i = $from[$i]) { $path[myCount($path)] = $i; } return myArrayReverse($path); } foreach (neighbors($i, $map) as $j => $step) { if (!myArrayKeyExists($j, $closed)) { if (!myArrayKeyExists($j, $open) || $g[$i] + $step < $g[$j]) { $g[$j] = $g[$i] + $step; $h[$j] = heuristic($j, $target); $f[$j] = $g[$j] + $h[$j]; $from[$j] = $i; if (!myArrayKeyExists($j, $open)) { $open[$j] = TRUE; heap_push($open_heap, $f, $j); } else { heap_raise($open_heap, $f, $j); } } } } } return FALSE; }