/** * kml2wkt does what its name implies : * it takes a kml file, parses it for linestrings and returns an equivalent WKT LINESTRING * * The returned WKT can be 2, 3 or 4D. * If does not really handle 4D parsing (beyond scope IMO) => KML import is restricted to routes (outings will only support GPX input) * If $dim = 4, then time field will be zero-padded. */ public static function kml2wkt($path, $dim = 3) { // FIXME: document structure is very user customizable, and thus this function is not very robust ... // a solution would be to extract points linked to a linestring (if these points are present), and to sort them by date to build the linestring. $xml = c2cTools::simplexmlLoadFile($path); // TODO: handle files with Document>NetworkLink>Url>href : wget content... (mymaps generates these ones) // TODO: handle files with one waypoint for point update ? // TODO : handle multilines geometries ? $i = 0; $wkta = array(); if ($pm = $xml->Document->Placemark) { // this is a simple kml // (for instance, one made with google "my maps") // in which these is no folder. c2cTools::log("ParseGeo::kml2wkt({$path}, {$dim}) with xml->Document->Placemark"); return 'LINESTRING(' . self::getKmlCoordinates($pm->LineString->coordinates, $dim) . ')'; } elseif ($geom_folders = $xml->Document->Folder) { // this is a "complex" KML with several folders (typical output of gpsbabel from gpx) $trk_coords = array(); // kml file may contain three geom folders : Waypoints, Tracks and Routes foreach ($geom_folders as $folder) { // we're just looking for tracks : if ($folder->name == 'Tracks') { // there might be subfolders if gps signal has been interrupted // we merge these folders together to build a single track. foreach ($folder->Folder as $track) { $trk_coords[] = self::getKmlCoordinates($track->Placemark->LineString->coordinates, $dim); } } } c2cTools::log("ParseGeo::kml2wkt({$path}, {$dim}) with xml->Document->Folder"); return 'LINESTRING(' . implode(', ', $trk_coords) . ')'; } else { c2cTools::log("kml2wkt : no track found"); return false; } }