Ejemplo n.º 1
0
function compute($top, $bottom, $left, $right, $distance, $places, $tables, $tables_val)
{
    /////////////////
    function check_location($d, $lat, $lng, $places)
    {
        if (count($places) == 0) {
            return true;
        }
        $result3 = pg_query($GLOBALS['db_connection'], "select array_to_json(s.tagsarr) from nodes s where \n\t\t\t\t\t\tst_dwithin(ST_SetSRID(ST_Point(" . $lng . "," . $lat . "),4326)::geography,s.geom::geography," . $d . ")=true;");
        //$places=array();
        //array_push($places,array(array("amenity","school")),array(array("amenity","college"),array("amenity","university")),array(array("railway","station")));
        $p = $places;
        while ($row3 = pg_fetch_row($result3)) {
            $r = json_decode($row3[0]);
            for ($i = 0, $t = 1; $i < count($r); $i++, $t = 1) {
                $s = $r[$i];
                while ($a = current($p)) {
                    foreach ($a as $b) {
                        if ($s[0] == $b[0] && $s[1] == $b[1]) {
                            $t = 0;
                            break;
                        }
                    }
                    if ($t == 0) {
                        array_splice($p, key($p), 1);
                        break;
                    }
                    next($p);
                }
                reset($p);
            }
        }
        if (count($p) == 0) {
            return true;
        } else {
            return false;
        }
    }
    ///////////////////////
    $selected = array();
    /////////////////
    $deg_lat = 1.0 / 110574.0 * $GLOBALS['sq_len'];
    function deg_lng($lat)
    {
        $tem = 1.0 / (111320.0 * cos($lat)) * $GLOBALS['sq_len'];
        return $tem;
    }
    //////////////////
    log_time("before");
    ////////////////////////////////////////////////////////////////////
    $str = "insert into points values ";
    $str1 = "";
    for ($lat = $bottom; $lat <= $top; $lat = $lat + $deg_lat) {
        for ($lng = $left; $lng <= $right; $lng = $lng + deg_lng($lat)) {
            if (check_location($distance, $lat, $lng, $places)) {
                $str1 = $str1 . "(ST_SetSRID(ST_Point(" . $lng . "," . $lat . "),4326),array[" . $lat . "," . $lng . "]),";
            }
        }
    }
    log_time("check");
    ////////////////////////////////////////////////////////////////////
    if ($str1 == "") {
        return $selected;
    }
    $str = $str . $str1;
    $str[strlen($str) - 1] = ';';
    $result_pts = pg_query($GLOBALS['db_connection'], $str);
    if ($tables != "") {
        $result5 = pg_query($GLOBALS['db_connection'], "delete from points where chk_intersects_dwith_geom(geom," . $GLOBALS['buff'] . ")=true;\n\t\t\t\t\tdelete from points where chk_layers(array[" . $tables . "],array[" . $tables_val . "],geom)=false;\n\t\t\t\t\tselect latlng[1],latlng[2] from points;");
    } else {
        $result5 = pg_query($GLOBALS['db_connection'], "delete from points where chk_intersects_dwith_geom(geom," . $GLOBALS['buff'] . ")=true;\n\t\t\t\t\tselect latlng[1],latlng[2] from points;");
    }
    log_time("database");
    ////////////////////////////////////////////////////////////////////
    while ($row5 = pg_fetch_row($result5)) {
        $lat = floatval($row5[0]);
        $lng = floatval($row5[1]);
        array_push($selected, array("lat" => $lat, "lng" => $lng));
    }
    return $selected;
}
function compute($top, $bottom, $left, $right, $distance, $places, $tables, $tables_val)
{
    /////////////////
    function check_location($d, $lat, $lng, $places)
    {
        $result3 = pg_query($GLOBALS['db_connection'], "select array_to_json(s.tagsarr),st_asgeojson(s.geom)from nodes s where \n\t\t\t\t\t\tst_dwithin(ST_SetSRID(ST_Point(" . $lng . "," . $lat . "),4326)::geography,s.geom::geography," . $d . ")=true;");
        //$places=array();
        //array_push($places,array(array("amenity","school")),array(array("amenity","college"),array("amenity","university")),array(array("railway","station")));
        $p = $places;
        while ($row3 = pg_fetch_row($result3)) {
            $r = json_decode($row3[0]);
            for ($i = 0, $t = 1; $i < count($r); $i++, $t = 1) {
                $s = $r[$i];
                while ($a = current($p)) {
                    foreach ($a as $b) {
                        if ($s[0] == $b[0] && $s[1] == $b[1]) {
                            $t = 0;
                            break;
                        }
                    }
                    if ($t == 0) {
                        array_splice($p, key($p), 1);
                        break;
                    }
                    next($p);
                }
                reset($p);
            }
        }
        if (count($p) == 0) {
            return true;
        } else {
            return false;
        }
    }
    ///////////////////////
    $selected = array();
    /////////////////
    $deg_lat = 1.0 / 110574.0 * $GLOBALS['sq_len'];
    function deg_lng($lat)
    {
        $tem = 1.0 / (111320.0 * cos($lat)) * $GLOBALS['sq_len'];
        return $tem;
    }
    //////////////////
    for ($lat = $bottom; $lat <= $top; $lat = $lat + $deg_lat) {
        for ($lng = $left; $lng <= $right; $lng = $lng + deg_lng($lat)) {
            log_time("before database1");
            ////////////////////////////////////////////////////////////////////////////////////////////////
            $result = pg_query($GLOBALS['db_connection'], 'select chk_intersects_dwith(' . $lat . ',' . $lng . ',' . $GLOBALS['buff'] . ');');
            $bool = pg_fetch_row($result);
            if ($bool[0] == 'f') {
                $boolean = true;
                log_time("after database1");
                ////////////////////////////////////////////////////////////////////////////////////////////////
                for ($x = 0; $x < count($tables); $x = $x + 1) {
                    $result2 = pg_query($GLOBALS['db_connection'], "select st_dwithin(ST_SetSRID(ST_Point(" . $lng . "," . $lat . "),4326)::geography,geom::geography,0) from " . $tables[$x] . " where name=any(array[" . $tables_val[$x] . "]) and st_dwithin(ST_SetSRID(ST_Point(" . $lng . "," . $lat . "),4326)::geography,geom::geography,0)=true;");
                    $row2 = pg_fetch_row($result2);
                    if ($row2[0] != 't') {
                        $boolean = false;
                        break;
                    }
                }
                log_time("after database2");
                ////////////////////////////////////////////////////////////////////////////////////////////////
                if ($boolean) {
                    if (check_location($distance, $lat, $lng, $places)) {
                        array_push($selected, array("lat" => $lat, "lng" => $lng));
                    }
                    log_time("after check");
                    ////////////////////////////////////////////////////////////////////////////////////////////////
                }
            }
        }
    }
    return $selected;
}