Example #1
0
function shp_parse_fh($fh, $more)
{
    # See also:
    # http://vis4.net/blog/de/2010/04/reading-esri-shapefiles-in-php/
    fclose($fh);
    $args = array('noparts' => true);
    $shp = new ShapeFile($more['file']['path'], $args);
    if (!$shp) {
        return array('ok' => 0, 'error' => 'Failed to parse shapefile');
    }
    $data = array();
    $errors = array();
    $record = 0;
    while ($record = $shp->getNext()) {
        # This is mostly here if/when we break in $parts loop
        if ($more['max_records'] && $record > $more['max_records']) {
            break;
        }
        # What to do about file specific metadata?
        $shp_data = $record->getShpData();
        $parts = isset($shp_data['parts']) ? $shp_data['parts'] : array($shp_data);
        foreach ($parts as $pt) {
            $record++;
            if ($more['max_records'] && $record > $more['max_records']) {
                break;
            }
            $lat = $pt['y'];
            $lon = $pt['x'];
            if (!$lat || !$lon) {
                continue;
            }
            # check $more for reprojection nonsense here
            # the projection transformation stuff is very
            # much bleeding edge still and may turn in to
            # a yak (20110216/straup)
            # loadlib("geo_proj");
            # $from = 'EPSG:26943';
            # $pt = array('latitude' => $lat, 'longitude' => $lon);
            # $pt = geo_proj_transform($pt, $from, 'EPSG:4326');
            if (!geo_utils_is_valid_latitude($lat)) {
                $errors[] = array('record' => $record, 'column' => 'latitude', 'error' => 'Invalid latitude');
                continue;
            }
            if (!geo_utils_is_valid_longitude($lon)) {
                $errors[] = array('record' => $record, 'column' => 'longitude', 'error' => 'Invalid longitude');
                continue;
            }
            $tmp = array('latitude' => $lat, 'longitude' => $lon);
            $data[] = $tmp;
        }
    }
    if (!count($data)) {
        return array('ok' => 0, 'error' => '');
    }
    return array('ok' => 1, 'errors' => &$errors, 'data' => &$data);
}
Example #2
0
function csv_parse_fh($fh, $more = array())
{
    $keys = array();
    $data = array();
    $checked_fieldnames = 0;
    # see below
    $field_names = is_array($more['field_names']) ? $more['field_names'] : null;
    $errors = array();
    $record = 0;
    while (!feof($fh)) {
        $record++;
        if ($more['max_records'] && $record > $more['max_records']) {
            break;
        }
        $ln = trim(fgets($fh));
        if (!$ln) {
            continue;
        }
        if (preg_match("/^#/", $ln)) {
            continue;
        }
        $row = str_getcsv($ln);
        if (!$row) {
            continue;
        }
        // lets do away with any row, whose combined string length is empty (seanc | 20110926)
        $row2str = implode("", $row);
        if (strlen($row2str) < 1) {
            continue;
        }
        if ($record === 1 && !$field_names) {
            $has_latitude = in_array('latitude', $row) ? 1 : 0;
            $has_longitude = in_array('longitude', $row) ? 1 : 0;
            $possible_lat = $GLOBALS['cfg']['import_fields_mightbe_latitude'];
            $possible_lon = $GLOBALS['cfg']['import_fields_mightbe_longitude'];
            foreach ($row as $col) {
                $col = strtolower($col);
                // do away with columns that are empty  (seanc | 20110926)
                if (strlen(trim($col)) < 1) {
                    continue;
                }
                if (!$has_latitude && in_array($col, $possible_lat)) {
                    $col = 'latitude';
                }
                if (!$has_longitude && in_array($col, $possible_lon)) {
                    $col = 'longitude';
                }
                $field_names[] = $col;
            }
            continue;
        }
        #
        # Okay, first check to make sure that we have some kind
        # of remotely sane input as column names - if we don't
        # even have that then there's not much point in going any
        # further.
        #
        if (!$checked_fieldnames) {
            for ($i = 0; $i < count($field_names); $i++) {
                $raw = $field_names[$i];
                $clean = sanitize($raw, 'str');
                if (!$clean) {
                    return array('ok' => 0, 'error' => "invalid column name", 'column' => $raw);
                }
                $field_names[$i] = $clean;
            }
            $checked_fieldnames = 1;
        }
        #
        # Okay, go!
        #
        $tmp = array();
        for ($i = 0; $i < count($field_names); $i++) {
            $raw = trim($row[$i]);
            $clean = sanitize($raw, 'str');
            $tmp[$field_names[$i]] = $clean;
            if ($raw && !$clean) {
                $errors[] = array('record' => $record, 'error' => "invalid input", 'column' => $field_names[$i]);
            }
        }
        # ensure valid latitude/longitude
        if (isset($tmp['latitude']) && !geo_utils_is_valid_latitude($tmp['latitude'])) {
            $errors[] = array('record' => $record, 'error' => 'invalid latitude', 'column' => 'latitude');
        }
        if (isset($tmp['longitude']) && !geo_utils_is_valid_longitude($tmp['longitude'])) {
            $errors[] = array('record' => $record, 'error' => 'invalid longitude', 'column' => 'longitude');
        } else {
        }
        # done...
        $data[] = $tmp;
    }
    fclose($fh);
    return array('ok' => 1, 'data' => &$data, 'errors' => &$errors);
}
function api_foursquare_venues_search()
{
    $lat = request_float('latitude');
    $lon = request_float('longitude');
    $alt = request_float('altitude');
    $query = request_str('query');
    # See this? It's a quick and dirty shim until I can figure
    # out how to pass 'sort' flags via the UI (20120201/straup)
    # $sort = request_float('sort');
    $sort = $GLOBALS['cfg']['foursquare_venues_sort'];
    $sort_func = "_api_foursquare_venues_sort_by_name";
    if ($sort == 'distance') {
        $sort_func = "_api_foursquare_venues_sort_by_distance";
    }
    if (!$lat || !geo_utils_is_valid_latitude($lat)) {
        api_output_error(999, "Missing or invalid latitude");
    }
    if (!$lat || !geo_utils_is_valid_longitude($lon)) {
        api_output_error(999, "Missing or invalid longitude");
    }
    $checkin_crumb = crumb_generate("api", "privatesquare.venues.checkin");
    $fsq_user = foursquare_users_get_by_user_id($GLOBALS['cfg']['user']['id']);
    $method = 'venues/search';
    if ($query) {
        $args = array('oauth_token' => $fsq_user['oauth_token'], 'll' => "{$lat},{$lon}", 'radius' => 1200, 'limit' => 30, 'intent' => 'match', 'query' => $query);
        $rsp = foursquare_api_call($method, $args);
        if (!$rsp['ok']) {
            _api_foursquare_error($rsp);
        }
        $venues = $rsp['rsp']['venues'];
        usort($venues, $sort_func);
        $out = array('venues' => $venues, 'query' => $query, 'latitude' => $lat, 'longitude' => $lon, 'crumb' => $checkin_crumb);
        api_output_ok($out);
    }
    $random_user = foursquare_users_random_user();
    if (!$random_user) {
        $random_user = $fsq_user;
    }
    # https://developer.foursquare.com/docs/venues/search
    # TO DO: api_call_multi
    # first get stuff scoped to the current user
    $args = array('oauth_token' => $fsq_user['oauth_token'], 'll' => "{$lat},{$lon}", 'limit' => 30, 'intent' => 'checkin');
    $rsp = foursquare_api_call($method, $args);
    if (!$rsp['ok']) {
        _api_foursquare_error($rsp);
    }
    $venues = array();
    $seen = array();
    foreach ($rsp['rsp']['venues'] as $v) {
        $venues[] = $v;
        $seen[] = $v['id'];
    }
    # now just get whatever
    $args = array('oauth_token' => $random_user['oauth_token'], 'll' => "{$lat},{$lon}", 'limit' => 30, 'radius' => 800, 'intent' => 'browse');
    $rsp = foursquare_api_call($method, $args);
    if (!$rsp['ok']) {
        _api_foursquare_error($rsp);
    }
    foreach ($rsp['rsp']['venues'] as $v) {
        if (!in_array($v['id'], $seen)) {
            $venues[] = $v;
        }
    }
    usort($venues, $sort_func);
    # go!
    $out = array('venues' => $venues, 'latitude' => $lat, 'longitude' => $lon, 'crumb' => $checkin_crumb);
    api_output_ok($out);
}
Example #4
0
function import_ensure_valid_latlon($lat, $lon)
{
    $lat = $lat && geo_utils_is_valid_latitude($lat) ? $lat : null;
    $lon = $lon && geo_utils_is_valid_longitude($lon) ? $lon : null;
    return array($lat, $lon);
}
Example #5
0
function xls_parse_fh($fh, $more = array())
{
    loadpear("Spreadsheet/Excel/Reader");
    fclose($fh);
    $xls = new Spreadsheet_Excel_Reader($more['file']['path'], false);
    $rows = $xls->rowcount();
    $cols = $xls->colcount(0);
    if (!$rows || !$cols) {
        return array('ok' => 0, 'error' => 'Unable to locate any rows or columns with data. Perhaps the spreadsheet is old or has been corrupted?');
    }
    $fields = array();
    for ($i = 1; $i < $cols; $i++) {
        $raw = $xls->val(1, $i);
        $fields[] = strtolower($raw);
    }
    #
    $possible_lat = $GLOBALS['cfg']['import_fields_mightbe_latitude'];
    $possible_lon = $GLOBALS['cfg']['import_fields_mightbe_longitude'];
    $lat_field = 'latitude';
    $lon_field = 'longitude';
    if (!in_array($lat_field, $fields)) {
        foreach ($fields as $f) {
            if (in_array($f, $possible_lat)) {
                $lat_field = $f;
                break;
            }
        }
    }
    if (!in_array($lon_field, $fields)) {
        foreach ($fields as $f) {
            if (in_array($f, $possible_lon)) {
                $lon_field = $f;
                break;
            }
        }
    }
    #
    $data = array();
    $errors = array();
    $record = 0;
    for ($i = 2; $i < $rows; $i++) {
        $record++;
        if ($more['max_records'] && $record > $more['max_records']) {
            break;
        }
        $tmp = array();
        for ($j = 1; $j < $cols; $j++) {
            $label = $fields[$j - 1];
            $value = $xls->val($i, $j);
            if ($label == $lat_field) {
                if (!geo_utils_is_valid_latitude($value)) {
                    $errors[] = array('record' => $record, 'error' => 'invalid latitude', 'column' => 'latitude');
                    continue;
                }
                $label = 'latitude';
            }
            if ($label == $lon_field) {
                if (!geo_utils_is_valid_longitude($value)) {
                    $errors[] = array('record' => $record, 'error' => 'invalid longitude', 'column' => 'longitude');
                    continue;
                }
                $label = 'longitude';
            }
            # TO DO : dates and times (they seem to be always be weird)
            $tmp[$label] = import_scrub($value);
        }
        $data[] = $tmp;
    }
    return array('ok' => 1, 'data' => &$data, 'errors' => &$errors);
}
Example #6
0
function dots_ensure_valid_data(&$data)
{
    if (!isset($data['latitude'])) {
        return array('ok' => 0, 'error' => 'Missing latitude.');
    }
    if (!isset($data['longitude'])) {
        return array('ok' => 0, 'error' => 'Missing longitude.');
    }
    if (!geo_utils_is_valid_latitude($data['latitude'])) {
        return array('ok' => 0, 'error' => "Invalid latitude: '{$data['latitude']}'");
    }
    if (!geo_utils_is_valid_longitude($data['longitude'])) {
        return array('ok' => 0, 'error' => "Invalid longitude: '{$data['longitude']}'");
    }
    return array('ok' => 1);
}