function gpx_parse_fh($fh, $more = array()) { $data = fread($fh, filesize($more['file']['path'])); fclose($fh); libxml_use_internal_errors(true); $gpx = simplexml_load_string($data); if (!$gpx) { $errors = array(); foreach (libxml_get_errors() as $error) { $errors[] = $error->message; } return array('ok' => 0, 'error' => 'failed to parse XML: ' . implode(";", $errors)); } $label = (string) $gpx->trk->name; $label = import_scrub($label); $simplify = $GLOBALS['cfg']['import_do_simplification']['gpx'] && $more['simplify'] ? 1 : 0; $data = array(); $errors = array(); $record = 1; foreach ($gpx->trk->trkseg->trkpt as $pt) { $record++; if ($more['max_records'] && $record > $more['max_records'] && !$simplify) { break; } $attrs = $pt->attributes(); $lat = (string) $attrs['lat']; $lon = (string) $attrs['lon']; list($lat, $lon) = import_ensure_valid_latlon($lat, $lon); if (!$lat) { $errors[] = array('record' => $record, 'error' => 'invalid latitude', 'column' => 'latitude'); continue; } if (!$lon) { $errors[] = array('record' => $record, 'error' => 'invalid longitude', 'column' => 'longitude'); continue; } $tmp = array(); $tmp['latitude'] = $lat; $tmp['longitude'] = $lon; $created = (string) $pt->time; $elevation = (string) $pt->ele; $tmp['created'] = import_scrub($created); $tmp['elevation'] = import_scrub($elevation); $data[] = $tmp; } if ($simplify) { $data = _gpx_simplify($data); if ($more['max_records'] && count($data) > $more['max_records']) { $data = array_slice($data, 0, $more['max_records']); } } return array('ok' => 1, 'label' => $label, 'data' => &$data, 'errors' => &$errors, 'simplified' => $simplify); }
function kml_parse_fh($fh, $more = array()) { $fsize = filesize($more['file']['path']); $data = fread($fh, $fsize); fclose($fh); libxml_use_internal_errors(true); $xml = simplexml_load_string($data); if (!$xml) { $errors = array(); foreach (libxml_get_errors() as $error) { $errors[] = $error->message; } return array('ok' => 0, 'error' => 'failed to parse XML: ' . implode(";", $errors)); } # Please KML, die in a fire... $nl = null; if ($xml->NetworkLink) { $nl = $xml->NetworkLink; } else { if ($xml->Document->NetworkLink) { $nl = $xml->Document->NetworkLink; } else { } } # if ($nl) { if (!$GLOBALS['cfg']['import_kml_resolve_network_links']) { return array('ok' => 0, 'error' => 'Network linked KML files are not currently supported.'); } $url = $nl->Url; $link = (string) $url->href; $rsp = http_get($link); if (!$rsp['ok']) { return $rsp; } $xml = new SimpleXMLElement($rsp['body']); } $ctx = $xml->Document ? $xml->Document : $xml->Folder; if (!$ctx) { return array('ok' => 0, 'error' => 'Failed to locate any placemarks'); } if ($ctx->Folder) { $ctx = $ctx->Folder; } $label = (string) $ctx->name; $label = import_scrub($label); $data = array(); $errors = array(); $record = 1; # I hate KML... $placemarks = array(); if ($ctx->Folder) { foreach ($ctx->Folder as $f) { foreach ($f->Placemark as $p) { $placemarks[] = $p; } } } else { $placemarks = $ctx->Placemark; } # foreach ($placemarks as $p) { $record++; if ($more['max_records'] && $record > $more['max_records']) { break; } $tmp = array(); # do everything but the geo bits first in case we run in to a big # bag of points like we might find in a KML file from Google's # mytracks (20110111/straup) $title = (string) $p->name; $desc = (string) $p->description; # foursquare-isms if (preg_match("/^foursquare /", $label) && ($a = $p->description->a)) { $attrs = $a->attributes(); $href = (string) $attrs['href']; if (preg_match("/venue\\/(\\d+)\\/?\$/", $href, $m)) { $tmp['foursquare:venue'] = $m[1]; $desc = (string) $a; } } # random other stuff if ($pub = $p->published) { $pub = (string) $pub; $pub = import_scrub($pub); $tmp['created'] = $pub; } if ($vis = $p->visibility) { if ($vis = (string) $vis) { $tmp['perms'] = 'private'; } } $title = import_scrub($title); $desc = import_scrub($desc); $tmp['title'] = $title; $tmp['description'] = $desc; # sigh... if ($coords = (string) $p->Point->coordinates) { $coords = trim($coords); list($lon, $lat, $altitude) = explode(",", $coords, 3); list($lat, $lon) = import_ensure_valid_latlon($lat, $lon); if (!$lat) { $errors[] = array('record' => $record, 'error' => 'invalid latitude', 'column' => 'latitude'); continue; } if (!$lon) { $errors[] = array('record' => $record, 'error' => 'invalid longitude', 'column' => 'longitude'); continue; } $tmp['altitude'] = import_scrub($altitude); $tmp['latitude'] = $lat; $tmp['longitude'] = $lon; } else { if (($coords = $p->MultiGeometry->LineString->coordinates) || ($coords = $p->LineString->coordinates)) { $simplify = $GLOBALS['cfg']['import_do_simplification']['kml'] && $more['simplify'] ? 1 : 0; # We're going to keep our own counter below $record -= 2; $coords = (string) $coords; $coords = trim($coords); $coords = preg_split("/[\\s]+/", $coords); # foreach ($coords as $coord) { $record++; if ($more['max_records'] && $record > $more['max_records'] && !$simplify) { break; } list($lon, $lat, $altitude) = explode(",", $coord, 3); list($lat, $lon) = import_ensure_valid_latlon($lat, $lon); if (!$lat) { $errors[] = array('record' => $record, 'error' => "invalid latitude", 'column' => 'latitude'); continue; } if (!$lon) { $errors[] = array('record' => $record, 'error' => "invalid longitude", 'column' => 'longitude'); continue; } $data[] = array('latitude' => $lat, 'longitude' => $lon, 'altitude' => import_scrub($altitude)); } # if ($simplify) { $data = _kml_simplify($data); if ($more['max_records'] && count($data) > $more['max_records']) { $data = array_slice($data, 0, $more['max_records']); } } continue; } else { # to be revisited maybe; this really just makes things # more complicated than not (20110415/straup) continue; $errors[] = array('record' => $record, 'error' => 'Unable to find any location information (that Dotspotting knows about).'); } } $data[] = $tmp; } return array('ok' => 1, 'label' => $label, 'data' => &$data, 'errors' => &$errors, 'simplified' => $simplify); }
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); }
function json_parse_fh($fh, $more = array()) { $raw = fread($fh, filesize($more['file']['path'])); $json = json_decode($raw, "as a hash"); if (!isset($json['features'])) { return array('ok' => 0, 'error' => 'Missing features'); } $data = array(); $errors = array(); $record = 1; foreach ($json['features'] as $f) { $record++; if ($more['max_records'] && $record > $more['max_records']) { break; } if (!isset($f['geometry'])) { $errors[] = array('error' => 'missing geometry', 'record' => $record); continue; } if (!preg_match("/^(?:Multi)?Point\$/", $f['geometry']['type'])) { $errors[] = array('error' => 'not a supported geometry', 'record' => $record); continue; } $tmp = array(); if (isset($f['properties'])) { foreach ($f['properties'] as $key => $value) { $key = import_scrub($key); $value = import_scrub($value); $tmp[$key] = $value; } } # MultiPoints (get their own counter) if ($f['geometry']['type'] == 'MultiPoint') { $counter = $record - 1; foreach ($f['geometry']['coordinates'] as $coords) { $counter++; if ($more['max_records'] && $counter > $more['max_records']) { break; } list($lon, $lat) = $coords; list($lat, $lon) = import_ensure_valid_latlon($lat, $lon); if (!$lat) { $errors[] = array('error' => 'invalid latitude', 'record' => $record, 'column' => 'latitude'); } if (!$lon) { $errors[] = array('error' => 'longitude', 'record' => $record, 'column' => 'longitude'); } $tmp['latitude'] = $lat; $tmp['longitude'] = $lon; $data[] = $tmp; } continue; } # plain old Points list($lon, $lat) = $f['geometry']['coordinates']; list($lat, $lon) = import_ensure_valid_latlon($lat, $lon); if (!$lat) { $errors[] = array('error' => 'invalid latitude', 'record' => $record, 'column' => 'latitude'); continue; } if (!$lon) { $errors[] = array('error' => 'invalid longitude', 'record' => $record, 'column' => 'longitude'); continue; } $tmp['latitude'] = $lat; $tmp['longitude'] = $lon; $data[] = $tmp; } return array('ok' => 1, 'data' => &$data, 'errors' => &$errors); }