[gpx] Extract track & category names from file name
Signed-off-by: cyber-toad <the.cyber.toad@proton.me>
This commit is contained in:
parent
8843e70eee
commit
a567e6eb11
5 changed files with 98 additions and 5 deletions
24
data/gpx_test_data/empty_names1.gpx
Normal file
24
data/gpx_test_data/empty_names1.gpx
Normal file
|
@ -0,0 +1,24 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<gpx xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.topografix.com/GPX/1/1" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd http://www.garmin.com/xmlschemas/GpxExtensions/v3 http://www.garmin.com/xmlschemas/GpxExtensionsv3.xsd http://www.garmin.com/xmlschemas/TrackPointExtension/v1 http://www.garmin.com/xmlschemas/TrackPointExtensionv1.xsd http://www.topografix.com/GPX/gpx_style/0/2 http://www.topografix.com/GPX/gpx_style/0/2/gpx_style.xsd" xmlns:gpxtpx="http://www.garmin.com/xmlschemas/TrackPointExtension/v1" xmlns:gpxx="http://www.garmin.com/xmlschemas/GpxExtensions/v3" xmlns:gpx_style="http://www.topografix.com/GPX/gpx_style/0/2" version="1.1" creator="https://gpx.studio">
|
||||
<trk>
|
||||
<trkseg>
|
||||
<trkpt lat="48.208378" lon="16.374915">
|
||||
<ele>184.8</ele>
|
||||
</trkpt>
|
||||
<trkpt lat="48.208407" lon="16.374831">
|
||||
<ele>184.8</ele>
|
||||
</trkpt>
|
||||
</trkseg>
|
||||
</trk>
|
||||
|
||||
<trk>
|
||||
<trkseg>
|
||||
<trkpt lat="58.208378" lon="26.374915">
|
||||
<ele>184.8</ele>
|
||||
</trkpt>
|
||||
<trkpt lat="58.208407" lon="26.374831">
|
||||
<ele>184.8</ele>
|
||||
</trkpt>
|
||||
</trkseg>
|
||||
</trk>
|
||||
</gpx>
|
13
data/gpx_test_data/empty_names2.gpx
Normal file
13
data/gpx_test_data/empty_names2.gpx
Normal file
|
@ -0,0 +1,13 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<gpx xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.topografix.com/GPX/1/1" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd http://www.garmin.com/xmlschemas/GpxExtensions/v3 http://www.garmin.com/xmlschemas/GpxExtensionsv3.xsd http://www.garmin.com/xmlschemas/TrackPointExtension/v1 http://www.garmin.com/xmlschemas/TrackPointExtensionv1.xsd http://www.topografix.com/GPX/gpx_style/0/2 http://www.topografix.com/GPX/gpx_style/0/2/gpx_style.xsd" xmlns:gpxtpx="http://www.garmin.com/xmlschemas/TrackPointExtension/v1" xmlns:gpxx="http://www.garmin.com/xmlschemas/GpxExtensions/v3" xmlns:gpx_style="http://www.topografix.com/GPX/gpx_style/0/2" version="1.1" creator="https://gpx.studio">
|
||||
<trk>
|
||||
<trkseg>
|
||||
<trkpt lat="48.208378" lon="16.374915">
|
||||
<ele>184.8</ele>
|
||||
</trkpt>
|
||||
<trkpt lat="48.208407" lon="16.374831">
|
||||
<ele>184.8</ele>
|
||||
</trkpt>
|
||||
</trkseg>
|
||||
</trk>
|
||||
</gpx>
|
|
@ -273,12 +273,55 @@ std::string const kKmbExtension = ".kmb";
|
|||
std::string const kGpxExtension = ".gpx";
|
||||
std::string const kDefaultBookmarksFileName = "Bookmarks";
|
||||
|
||||
// Populate empty category & track names based on file name: assign file name to category name,
|
||||
// if there is only one unnamed track - assign file name to it, otherwise add numbers 1, 2, 3...
|
||||
// to file name to build names for all unnamed tracks
|
||||
void FillEmptyNames(std::unique_ptr<kml::FileData> & kmlData, const std::string & file)
|
||||
{
|
||||
auto start = file.find_last_of('/') + 1;
|
||||
auto end = file.find_last_of('.');
|
||||
if (end == std::string::npos)
|
||||
end = file.size();
|
||||
auto name = file.substr(start, end - start);
|
||||
|
||||
if (kmlData->m_categoryData.m_name.empty())
|
||||
kmlData->m_categoryData.m_name[kml::kDefaultLang] = name;
|
||||
|
||||
if (kmlData->m_tracksData.empty())
|
||||
return;
|
||||
|
||||
auto emptyNames = std::count_if(kmlData->m_tracksData.begin(), kmlData->m_tracksData.end(),
|
||||
[](const kml::TrackData & t) { return t.m_name.empty(); });
|
||||
if (emptyNames == 0)
|
||||
return;
|
||||
|
||||
auto emptyTrackNum = 1;
|
||||
for (auto & track : kmlData->m_tracksData)
|
||||
{
|
||||
if (track.m_name.empty())
|
||||
{
|
||||
if (emptyNames == 1)
|
||||
{
|
||||
track.m_name[kml::kDefaultLang] = name;
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
track.m_name[kml::kDefaultLang] = name + " " + std::to_string(emptyTrackNum);
|
||||
emptyTrackNum++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::unique_ptr<kml::FileData> LoadKmlFile(std::string const & file, KmlFileType fileType)
|
||||
{
|
||||
std::unique_ptr<kml::FileData> kmlData;
|
||||
try
|
||||
{
|
||||
kmlData = LoadKmlData(FileReader(file), fileType);
|
||||
if (kmlData != nullptr)
|
||||
FillEmptyNames(kmlData, file);
|
||||
}
|
||||
catch (std::exception const & e)
|
||||
{
|
||||
|
|
|
@ -1874,14 +1874,10 @@ void BookmarkManager::LoadBookmarkRoutine(std::string const & filePath, bool isT
|
|||
auto const ext = base::GetFileExtension(filePath);
|
||||
std::unique_ptr<kml::FileData> kmlData;
|
||||
if (ext == ".gpx")
|
||||
{
|
||||
kmlData = LoadKmlFile(fileSavePath, KmlFileType::Gpx);
|
||||
}
|
||||
else
|
||||
{
|
||||
kmlData = LoadKmlFile(fileSavePath, KmlFileType::Text);
|
||||
}
|
||||
|
||||
|
||||
if (m_needTeardown)
|
||||
return;
|
||||
|
||||
|
|
|
@ -1121,6 +1121,23 @@ UNIT_CLASS_TEST(Runner, TrackParsingTest_1)
|
|||
}
|
||||
}
|
||||
|
||||
UNIT_CLASS_TEST(Runner, FillEmptyTrackNames)
|
||||
{
|
||||
BookmarkManager bmManager(BM_CALLBACKS);
|
||||
bmManager.EnableTestMode(true);
|
||||
|
||||
string const kmlFile1 = GetPlatform().TestsDataPathForFile("gpx_test_data/empty_names1.gpx");
|
||||
auto fileData1 = LoadKmlFile(kmlFile1, KmlFileType::Gpx);
|
||||
TEST_EQUAL(fileData1->m_categoryData.m_name[kml::kDefaultLangCode], "empty_names1", ());
|
||||
TEST_EQUAL(fileData1->m_tracksData[0].m_name[kml::kDefaultLangCode], "empty_names1 1", ());
|
||||
TEST_EQUAL(fileData1->m_tracksData[1].m_name[kml::kDefaultLangCode], "empty_names1 2", ());
|
||||
|
||||
string const kmlFile2 = GetPlatform().TestsDataPathForFile("gpx_test_data/empty_names2.gpx");
|
||||
auto fileData2 = LoadKmlFile(kmlFile2, KmlFileType::Gpx);
|
||||
TEST_EQUAL(fileData2->m_categoryData.m_name[kml::kDefaultLangCode], "empty_names2", ());
|
||||
TEST_EQUAL(fileData2->m_tracksData[0].m_name[kml::kDefaultLangCode], "empty_names2", ());
|
||||
}
|
||||
|
||||
UNIT_CLASS_TEST(Runner, TrackParsingTest_2)
|
||||
{
|
||||
string const kmlFile = GetPlatform().TestsDataPathForFile("kml_test_data/track-from-google-earth.kml");
|
||||
|
|
Reference in a new issue