Ejemplo n.º 1
0
function pbuild_make_link(&$p2p_links, $ends, $dist = FALSE)
{
    global $wpdb;
    $rec = reset($ends);
    $name = clb_val('', $rec, RF_NODES_TYPE) . ': ' . clb_val('', $rec, RF_NODES_NAME);
    $lat1 = clb_val(0, $rec, 'lat');
    $lng1 = clb_val(0, $rec, 'lng');
    $pnum1 = clb_val(0, $rec, RF_LINKS_KEY);
    $rec = next($ends);
    $name .= clb_val('', $rec, RF_NODES_TYPE) . ': ' . clb_val('', $rec, RF_NODES_NAME);
    $lat2 = clb_val(0, $rec, 'lat');
    $lng2 = clb_val(0, $rec, 'lng');
    $pnum2 = clb_val(0, $rec, RF_LINKS_KEY);
    //checking for existing links, and specifically any existing walk link
    $linkpnum = '';
    $makelink = TRUE;
    if (isset($p2p_links['nodes'][$pnum1]) && isset($p2p_links['nodes'][$pnum2])) {
        //get the list of link_pnums for both nodes and intersect to see if they are already connected
        $connections = array_intersect(explode(',', $p2p_links['nodes'][$pnum1]), explode(',', $p2p_links['nodes'][$pnum2]));
        $makelink = count($connections) <= 0;
        //dont make a link if there is already a direct link
        //if there are links see if one of them is a walk link in which case we may want to update it if the ends have moved.
        foreach ($connections as $ref) {
            if (isset($p2p_links['links'][$ref]['ptype']) && $p2p_links['links'][$ref]['ptype'] == 'walk') {
                $linkpnum = $ref;
                $makelink = TRUE;
                //comment this out if dont want to update walk links
                if (isset($p2p_links['links'][$ref]['pt_count']) && $p2p_links['links'][$ref]['pt_count'] > 2) {
                    $makelink = FALSE;
                }
                //if the walk link has more than 2 points then
                break;
            }
        }
        if (count($connections) > 1 && $linkpnum) {
            qlog(__LINE__, 'walk link and other link types', $pnum1, $pnum2, $connections);
        }
    }
    /*
    	$p2p_links may not include existing "walk" interchanges if there were no routes involving the stops
    	but to prevent creating duplicates do a search and do nothing if a walk exists
    */
    if ($makelink && !$linkpnum) {
        $pair = clb_join(array($pnum1, $pnum2), TRUE);
        $query = 'SELECT ' . RF_LINKS_KEY . ' FROM ' . RF_LINKS_FROM . ' WHERE ' . RF_LINKS_TYPE . '="walk" AND ' . RF_LINKS_END1 . ' IN ' . $pair . ' AND ' . RF_LINKS_END2 . ' IN ' . $pair;
        $check = $wpdb->get_results($query, ARRAY_A);
        if (is_array($check) && clb_val('', reset($check), RF_LINKS_KEY)) {
            $makelink = FALSE;
        }
    }
    if ($makelink) {
        if ($dist === FALSE) {
            $dist = pline_surface_dist($lat1, $lng1, $lat2, $lng2);
        }
        $processed = clb_now_utc();
        $query = '';
        $query .= ', ' . RF_LINKS_DIST . '=' . $dist * 2;
        //double the distance so this is not chosen over actual tracks
        $query .= ', ' . RF_LINKS_TIME . '=' . ceil($dist / 80);
        //80 meters per minute is about 3 miles per hour
        $query .= ', ' . RF_LINKS_MODIFIED . '=' . clb_escape($processed);
        $query .= ', ' . RF_LINKS_NAME . '=' . clb_escape($name);
        $query .= ', lat=' . round(($lat1 + $lat2) / 2, PT_PRECISION);
        $query .= ', lng=' . round(($lng1 + $lng2) / 2, PT_PRECISION);
        $points = array();
        $points[] = array($lat1, $lng1, 0);
        $points[] = array($lat2, $lng2, 0);
        if ($polyline = pline_make($points, array('color' => '#00FF00'))) {
            $query .= ', ' . RF_LINKS_LINE . '=' . clb_escape(clb_join($polyline, '', '&', '='));
            $query .= ', ' . RF_LINKS_POINTS . '=' . clb_escape(clb_blob_enc($points));
        }
        if ($linkpnum) {
            $query = 'UPDATE ' . RF_LINKS_FROM . ' SET ' . trim($query, ', ') . ' WHERE ' . RF_LINKS_KEY . '=' . clb_escape($linkpnum);
        } else {
            $linkpnum = pbuild_new_pnum(RF_LINKS_FROM, RF_LINKS_KEY);
            $query .= ', ' . RF_LINKS_KEY . '=' . clb_escape($linkpnum);
            $query .= ', ' . RF_LINKS_CREATED . '=' . clb_escape($processed);
            $query .= ', ' . RF_LINKS_END1 . '=' . clb_escape($pnum1);
            $query .= ', ' . RF_LINKS_END2 . '=' . clb_escape($pnum2);
            $query .= ', ' . RF_LINKS_TYPE . '=' . clb_escape('walk');
            $query = 'INSERT INTO ' . RF_LINKS_FROM . ' SET ' . trim($query, ', ');
        }
        // qlog(__LINE__, $query);
        $wpdb->query($query);
    }
    return $makelink;
}
Ejemplo n.º 2
0
         foreach ($sel as $row) {
             $name = pfind_unify_names($row[RF_NODES_NAME]);
             $words = preg_split('/\\W+/', $name, -1, PREG_SPLIT_NO_EMPTY);
             foreach ($words as $w) {
                 if ($w == '&') {
                     $w = 'and';
                 }
                 $sound = metaphone($w);
                 //sometimes the sound is nothing like a number or 'y'
                 if ($sound) {
                     $index[$sound][$row[RF_NODES_TYPE]][] = $row[RF_NODES_KEY];
                 }
                 // alternative $index[$sound][$row['ptype']]['pnum'] = $name;
             }
         }
         $blob = clb_blob_enc($index, TRUE);
         //TRUE=binary
         file_put_contents($metaphones, $blob);
         //, FILE_BINARY);
     }
     break;
 case 'near':
     //create mapping from raw segs to nodes
     $stopsegs = pbuild_stop2segs($node_types, $raw_segs, $near_path);
     break;
 case 'prim':
     //build array with end points of raw links so we can build node to node links
     $primitives = pbuild_primitives($raw_segs, $prim_path);
     break;
 case 'links':
     //build ACTUAL node to node link reocrds