Ejemplo n.º 1
0
function extract_table($table_text)
{
    $rows = explode("\n", trim($table_text));
    $format = 'csv';
    if (preg_match('/\\t/', $rows[0])) {
        $format = 'tsv';
    }
    // Headings
    $headings = array();
    switch ($format) {
        case 'tsv':
            $headings = explode("\t", $rows[0]);
            break;
        case 'csv':
            $headings = str_getcsv($rows[0]);
            break;
    }
    // Contents
    $content = array();
    $nrows = count($rows);
    for ($i = 1; $i < $nrows; $i++) {
        switch ($format) {
            case 'tsv':
                $cells = explode("\t", $rows[$i]);
                break;
            case 'csv':
                $cells = str_getcsv($rows[$i]);
                break;
        }
        $content[] = $cells;
    }
    // Classify columns
    $column_types = array();
    $ncols = count($headings);
    for ($i = 0; $i < $ncols; $i++) {
        $column_types[$i] = TYPE_UNKNOWN;
        // Locality
        if (preg_match('/coordinates/i', $headings[$i])) {
            $column_types[$i] = TYPE_LATITUDE_LONGITUDE;
        }
        if (preg_match('/latlong/i', $headings[$i])) {
            $column_types[$i] = TYPE_LATITUDE_LONGITUDE;
        }
        if (preg_match('/latitude/i', $headings[$i])) {
            $column_types[$i] = TYPE_LATITUDE;
        }
        if (preg_match('/longitude/i', $headings[$i])) {
            $column_types[$i] = TYPE_LONGITUDE;
        }
    }
    // By assumption
    $column_types[0] = TYPE_OTU;
    if (0) {
        echo '<pre>';
        print_r($headings);
        print_r($column_types);
        print_r($content);
        echo '</pre>';
    }
    $data = array();
    foreach ($content as $row) {
        $otu = new stdclass();
        $otu->label = $row[0];
        $n = count($row);
        for ($i = 1; $i < $n; $i++) {
            //echo "i=" . $i . ' ' . $column_types[$i] . "<br/>";
            switch ($column_types[$i]) {
                case TYPE_LATITUDE_LONGITUDE:
                    if (IsLatLong($row[$i], $latlong)) {
                        $otu->latlong = $latlong;
                    }
                    break;
                case TYPE_LATITUDE:
                    if (IsLatitude($row[$i], $latitude)) {
                        if (!isset($otu->latlong)) {
                            $otu->latlong = array();
                        }
                        $otu->latlong['latitude'] = $latitude;
                    }
                    break;
                case TYPE_LONGITUDE:
                    if (IsLongitude($row[$i], $longitude)) {
                        if (!isset($otu->latlong)) {
                            $otu->latlong = array();
                        }
                        $otu->latlong['longitude'] = $longitude;
                    }
                    break;
                default:
                    break;
            }
        }
        $data[] = $otu;
    }
    /*
    echo '<pre>';
    print_r($data);
    echo '</pre>';
    */
    return $data;
}
Ejemplo n.º 2
0
    	
    //	array_push($tests, '4°54.87\'S, 29°35.85\'E');
    
    	array_push($tests, '5° 67\'');
    	array_push($tests, '14°39′ S, 145°27′ E');
    	
    	array_push($tests, '054°03′N–112°46′W');
    	array_push($tests, "38° 02' N, 32° 51' E");
    	
    	array_push($tests, '42°20′ N/9°10′ E');*/
    //	array_push($tests, 'N 0°6\'41"; W 77°22\'28"');
    //array_push($tests,'35° 56.218′ N');
    //array_push($tests,'117° 54.343′ W');
    //array_push($tests, 'S 01°06.18');
    //array_push($tests, 'W 77°35.67');
    array_push($tests, '29°55\'21.62"N');
    $ok = 0;
    foreach ($tests as $str) {
        $latlong = array();
        if (IsLatitude($str, $latlong)) {
            print_r($latlong);
            $ok++;
        } else {
            array_push($failed, $str);
        }
    }
    // report
    echo "--------------------------\n";
    echo count($tests) . ' strings, ' . (count($tests) - $ok) . ' failed' . "\n";
    print_r($failed);
}