Exemplo n.º 1
0
function inspect_data($path, $depth)
{
    $data = file_get_contents($path);
    //, FILE_BINARY);
    $map = clb_blob_dec($data);
    qpre(pfind_shallow($map, $depth));
}
Exemplo n.º 2
0
function pfind_service($prms, $pfind_defs, $type_names, $p2p_paths, $node_types = FALSE)
{
    clb_timing(__LINE__);
    pfind_def_tables($pfind_defs);
    $result = array('error' => 200, 'error_str' => 'no errors');
    //get the query or way points from the request
    $entries = array();
    if (isset($prms['q']) && $prms['q']) {
        $entries[] = $prms['q'];
    } else {
        $w = 0;
        while (isset($prms['w' . $w])) {
            $entries[] = $prms['w' . $w];
            $w++;
        }
    }
    $prop = array();
    $prop['service'] = strtolower(clb_val('json', $prms, 's'));
    //json, xml
    $prop['mode'] = strtolower(clb_val('', $prms, 'm'));
    $prop['units'] = strtolower(clb_val('mi', $prms, 'u'));
    //km, mi
    $prop['opto'] = strtolower(clb_val('best', $prms, 'o'));
    //dist, change, best
    $prop['opacity'] = strtolower(clb_val(0.5, $prms, 't'));
    $prop['color'] = strtolower(clb_val('#0000FF', $prms, 'c'));
    $prop['stroke'] = strtolower(clb_val(5, $prms, 'k'));
    $dist_only = FALSE != clb_val(0, $prms, 'gd');
    //get result from precalculated distances
    $prop['dist_only'] = $dist_only;
    $prop['getSteps'] = !$dist_only && FALSE != clb_val(0, $prms, 'gs');
    $prop['getPolyline'] = !$dist_only && FALSE != clb_val(0, $prms, 'gp');
    $path = FALSE;
    $mode = $prop['mode'];
    if (is_string($p2p_paths)) {
        $path = $p2p_paths;
    } else {
        if (!is_array($p2p_paths)) {
            $result = array('error' => 500, 'error_str' => 'server configuration error: route tables not specified');
        } else {
            if ($mode && isset($p2p_paths[$mode])) {
                $path = $p2p_paths[$mode];
            } else {
                if (isset($p2p_paths['rtm'])) {
                    $node_types = array('rail', 'tube', 'tram');
                    $path = $p2p_paths['rtm'];
                } else {
                    $path = reset($p2p_paths);
                }
            }
        }
    }
    if (is_array($path)) {
        if (!$node_types) {
            $node_types = clb_val(FALSE, $path, 'types');
        }
        //get the types
        $path = clb_val(FALSE, $path, 'path');
    }
    //if types not given as param or with paths, but the mode name is a node type use it as default.
    if (!$node_types && clb_val(FALSE, $type_names, $prop['mode'])) {
        $node_types = $prop['mode'];
    }
    if ($node_types && !is_array($node_types)) {
        $node_types = array($node_types);
    }
    //make types an array if just a single
    if (!$path || !is_string($path) || !is_file($path)) {
        $result = array('error' => 500, 'error_str' => 'server configuration error: route tables could not be loaded');
    } else {
        $waypoints = array();
        $result = pfind_interpret($entries, $mode, $waypoints, $path, $node_types);
        qlog(__LINE__, $mode, $entries, $result);
        if (IS_LOCAL) {
            foreach ($waypoints as $i => $stage) {
                foreach ($stage as $see) {
                    qlog(__LINE__, $i, join(', ', $see));
                }
            }
        }
        clb_timing('interpret');
    }
    if ($result['error'] == 200) {
        $links = FALSE;
        if (!$prop['dist_only'] && is_file($path)) {
            $data = file_get_contents($path);
            //, FILE_BINARY);	//need different p2p files for different combinations of modes
            $links = clb_blob_dec($data);
        }
        clb_timing('load array');
        if (!$links && !$prop['dist_only']) {
            $result = array('error' => 500, 'error_str' => 'point to point data file could not be found/loaded ' . $mode);
            qlog(__LINE__, $result, $mode, $path, $p2p_paths);
        } else {
            $result = pfind_routes($waypoints, $links, $prop, $type_names);
        }
        qlog(__LINE__, clb_timing('find path'));
    }
    //qlog(__LINE__,clb_xml($result, 'RouteFinder'));
    // qlog(__LINE__,clb_json($result, "'"));
    switch ($prop['service']) {
        case 'json':
            $return_data = json_encode($result);
            rs_response('application/json', $return_data, 'UTF-8');
            break;
        case 'javascript':
            $sid = clb_val('', $prms, 'sid');
            $func = clb_val('mfw_dir_result', $prms, 'callback');
            $return_data = clb_json($result, "'");
            $callback = $func . '(\'' . $sid . '\',' . $return_data . ');';
            clb_response('application/javascript', $callback, 'UTF-8');
            break;
        case 'xml':
            $return_data = clb_xml($result, 'RouteFinder');
            clb_response('xml', $return_data, 'UTF-8');
            break;
        case 'php':
            return $result;
            break;
    }
}
Exemplo n.º 3
0
function pbuild_p2p_links($stype, &$stops_xref, $path = '', $purge = FALSE)
{
    global $wpdb;
    if (!is_array($stype)) {
        $stype = array($stype);
    }
    $p2p_links = array();
    $unused = array();
    //rail stations with pltforms on different lines that converge need to be split into different platforms
    if ($do_plats = in_array('rail', $stype)) {
        $plats = pbuild_platforms();
        //incluse platforms within the main structure
        $p2p_links['stat2plat'] = $plats['stat2plat'];
        $p2p_links['plat2stat'] = $plats['plat2stat'];
    }
    if (!in_array('walk', $stype)) {
        $stype[] = 'walk';
    }
    //add in walking as a link type if not already in the list
    $query = 'SELECT ' . RF_LINKS_SELECT . ',' . RF_LINKS_POINTS . ' FROM ' . RF_LINKS_FROM . ' WHERE ptype IN ' . clb_join($stype, TRUE);
    $segs = $wpdb->get_results($query, ARRAY_A);
    $max_segs = clb_count($segs);
    if ($max_segs) {
        foreach ($segs as $seg_no => $seg) {
            //		qlog(__FUNCTION__, __LINE__, $seg_no, $max_segs);
            if ($seg_no % round($max_segs / 10) == 0) {
                echo round(100 * ($seg_no / $max_segs)) . '% complete' . "\n";
            }
            $data = array();
            $linkpnum = $seg[RF_LINKS_KEY];
            //remember the first/last points so we can do distance checking, omit other fields in the points and round decimal places to save space
            $pt_data = clb_blob_dec($seg[RF_LINKS_POINTS]);
            if (is_array($pt_data)) {
                $pt = reset($pt_data);
                $data['pt1'] = round($pt[0], PT_PRECISION) . ', ' . round($pt[1], PT_PRECISION);
                $pt = end($pt_data);
                $data['pt2'] = round($pt[0], PT_PRECISION) . ', ' . round($pt[1], PT_PRECISION);
            }
            $end1 = $seg[RF_LINKS_END1];
            $end2 = $seg[RF_LINKS_END2];
            //routes hold list of stations, so if these are platforms we need to convert to stations before looking up routes.
            if ($do_plats && isset($p2p_links['plat2stat'][$end1])) {
                $end1 = $p2p_links['plat2stat'][$end1];
            }
            if ($do_plats && isset($p2p_links['plat2stat'][$end2])) {
                $end2 = $p2p_links['plat2stat'][$end2];
            }
            if ($seg[RF_LINKS_TYPE] == 'walk') {
                if (!isset($stops_xref['stops'][$end1])) {
                    continue;
                }
                //if no routes using this stop no need to include the interchange to it
                if (!isset($stops_xref['stops'][$end2])) {
                    continue;
                }
                //if no routes using this stop no need to include the interchange to it
                $data['routes1'] = array();
                $data['routes2'] = array();
                /*
                	automatic walk links only have start and end points, handmade ones should have more
                	this is used when generating walk links to detect hand mades and leave them alone.
                */
                if (is_array($pt_data)) {
                    $data['pt_count'] = count($pt_data);
                }
            } else {
                //route numbers including dots in FORWARD direction
                $data['routes1'] = join(',', pbuild_common_routes($end1, $end2, $stops_xref));
                //common routes which can get from predecessor to the node on this leaf
                //route numbers including dots in REVERSE direction
                $data['routes2'] = join(',', pbuild_common_routes($end2, $end1, $stops_xref));
                //common routes which can get from predecessor to the node on this leaf
                if (empty($data['routes1']) && empty($data['routes2'])) {
                    qlog(__LINE__, 'link has no routes', $linkpnum);
                    $query = 'SELECT ' . RF_NODES_KEY . ' FROM ' . RF_NODES_FROM . ' WHERE ' . RF_NODES_KEY . ' IN ' . clb_join(array($seg[RF_LINKS_END1], $seg[RF_LINKS_END2]), TRUE);
                    $check = $wpdb->get_results($query, ARRAY_A);
                    if (clb_count($check) < 2) {
                        qlog(__FUNCTION__, __LINE__, 'deleting link with missing ends', $linkpnum, $seg[RF_LINKS_END1], $seg[RF_LINKS_END2]);
                        pbuild_del_rec(RF_LINKS_LINKS, RF_LINKS_KEY . '=' . clb_escape($linkpnum));
                    } else {
                        $unused[] = $linkpnum;
                    }
                    continue;
                    // do not include this segment if not on any routes.
                }
            }
            $data['dist'] = $seg[RF_LINKS_DIST];
            $data['weight'] = $seg[RF_LINKS_WEIGHT];
            $data['time'] = $seg[RF_LINKS_TIME];
            $data['end1'] = $end1 = $seg[RF_LINKS_END1];
            //these are pnums of the nodes at either end of the segment
            $data['end2'] = $end2 = $seg[RF_LINKS_END2];
            $data['ptype'] = $seg[RF_LINKS_TYPE];
            $data['reverse'] = $seg[RF_LINKS_REVERSE];
            $p2p_links['links'][$linkpnum] = http_build_query($data);
            $p2p_links['nodes'][$end1][] = $linkpnum;
            $p2p_links['nodes'][$end2][] = $linkpnum;
        }
    }
    //now turn the node lists into comma separated lists insteas of arrays
    foreach ($p2p_links['nodes'] as $key => $val) {
        $p2p_links['nodes'][$key] = join(',', $val);
    }
    if ($path && is_dir(dirname($path))) {
        $blob = clb_blob_enc($p2p_links, TRUE);
        //TRUE=binary
        file_put_contents($path, $blob);
        //, FILE_BINARY);
    }
    if (count($unused)) {
        qpre(__FUNCTION__, __LINE__, 'links without routes', count($unused), $unused);
    }
    if ($purge && count($unused)) {
        pbuild_del_rec(RF_LINKS_LINKS, RF_LINKS_KEY . '=' . clb_join($unused, TRUE));
    }
    return $p2p_links;
}
Exemplo n.º 4
0
function pline_pts_arr($points)
{
    $aoe_data = clb_blob_dec($points);
    if (is_array($aoe_data)) {
        return $aoe_data;
    }
    //if the above does not decode the contents, assume this is a simple list of points
    if (preg_match_all('/^([^\\s,]*)\\s*,\\s*([^\\s,]*)\\s*,\\s*([^\\s,]*)/m', $points, $raw_pts, PREG_SET_ORDER)) {
        $aoe_data = array();
        foreach ($raw_pts as $pt) {
            array_shift($pt);
            //remove full match from the reg_exp array leaving lng, lat, elev
            $aoe_data[] = $pt;
        }
    }
    return $aoe_data;
}
Exemplo n.º 5
0
            $result[] = $run;
        }
    }
    //qlog(__LINE__,count($complete), count($used), count($network_c), count($network_d));
    //qlog(__LINE__, clb_timing('shortest changes'));
    //qlog(__LINE__, $node2, $complete);
    return $result;
}
//get list of all stops sorted by latitude
$flat = 'wp_mfw_dists';
$query = 'SELECT ' . RF_NODES_KEY . ' FROM ' . RF_NODES_FROM . ' WHERE ' . RF_NODES_TYPE . ' IN ' . clb_join(array('rail'), TRUE) . ' ORDER BY ' . RF_NODES_KEY;
$sel1 = $wpdb->get_results($query, ARRAY_A);
if (is_array($sel1)) {
    //load p2p data so we can run shortest path and do our own lookups on link data
    $data = file_get_contents($p2p_path);
    $p2p_links = clb_blob_dec($data);
    $sel1 = clb_column($sel1, RF_NODES_KEY);
    //remove stations that dont have links to them
    foreach ($sel1 as $i => $pnum) {
        if (!isset($p2p_links['stat2plat'][$pnum]) && !isset($p2p_links['nodes'][$pnum])) {
            qpre('station not on network', $pnum);
            unset($sel1[$i]);
        }
    }
    $to_save = $sel1;
    sort($sel1);
    //renumber consecutively and make it easy to tell if we should create the pair
    $time = 0;
    //main double loop to permute station combinations inner loop starts from last station
    $stats = $base_stats = array('added' => 0, 'skipped' => 0, 'shortpaths' => 0);
    $chunks = max(1, (int) clb_val(1, $argv, 2));
Exemplo n.º 6
0
        $path = clb_dir(dirname($folder)) . 'metaphone.txt';
        $data = file_get_contents($path, FILE_BINARY);
        //need different p2p files for different combinations of modes
        $index = clb_blob_dec($data);
        ksort($index);
        qpre($index);
}
if (IS_CLI) {
    db_close();
    exit;
} else {
    if ($path) {
        clb_timing(__LINE__);
        $data = file_get_contents($path, FILE_BINARY);
        clb_timing('load');
        $map = clb_blob_dec($data);
        qpre(clb_timing('decode'), 'count', count($map, 1));
        qpre('file content length/array', $path, strlen($data), $map);
    }
}
db_close();
$base = clb_make_url() . '?m=';
echo clb_tag('p', '', clb_tag('a', 'near', '', array('href' => $base . 'near')));
echo clb_tag('p', '', clb_tag('a', 'raw', '', array('href' => $base . 'raw')));
echo clb_tag('p', '', clb_tag('a', 'stops', '', array('href' => $base . 'stops')));
echo clb_tag('p', '', clb_tag('a', 'links', '', array('href' => $base . 'links')));
echo clb_tag('p', '', clb_tag('a', 'p2p', '', array('href' => $base . 'p2p')));
echo clb_tag('p', '', clb_tag('a', 'interchange', '', array('href' => $base . 'interchange')));
echo clb_tag('p', '', clb_tag('a', 'check_raw_ends', '', array('href' => $base . '7')));
echo clb_tag('p', '', clb_tag('a', 'hardcoded test case', '', array('href' => $base . '8')));
echo clb_tag('p', '', clb_tag('a', 'audit of tyube stops', '', array('href' => $base . '9')));