cleanup: use taxonomy_set_category() function

Instead of manipulating the taxonomy structures directly, use the
taxonomy_set_category() function. This improves encapsulation and
gives us the possibility to improve the taxonomy data structures.

This concerns three places:
1) git parser
2) XML parser
3) reverse geo-lookup

This improves the XML parser code slightly: The parser assumes that
the value-attribute comes last (after origin and category). While
it still does that, it now at least generates a warning if it encounters
a value-attribute without origin- or category-attribute.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2020-09-06 12:39:51 +02:00 committed by Dirk Hohndel
parent 55e4237306
commit 7f1def8602
5 changed files with 32 additions and 38 deletions

View file

@ -85,18 +85,14 @@ taxonomy_data reverseGeoLookup(degrees_t latitude, degrees_t longitude)
QString url;
QJsonObject obj;
taxonomy_data taxonomy = { 0, alloc_taxonomy() };
taxonomy_data taxonomy = { 0, 0 };
// check the oceans API to figure out the body of water
url = geonamesOceanURL.arg(getUiLanguage().section(QRegExp("[-_ ]"), 0, 0)).arg(latitude.udeg / 1000000.0).arg(longitude.udeg / 1000000.0);
obj = doAsyncRESTGetRequest(url, 5000); // 5 secs. timeout
QVariantMap oceanName = obj.value("ocean").toVariant().toMap();
if (oceanName["name"].isValid()) {
taxonomy.category[0].category = TC_OCEAN;
taxonomy.category[0].origin = taxonomy_origin::GEOCODED;
taxonomy.category[0].value = copy_qstring(oceanName["name"].toString());
taxonomy.nr = 1;
}
if (oceanName["name"].isValid())
taxonomy_set_category(&taxonomy, TC_OCEAN, qPrintable(oceanName["name"].toString()), taxonomy_origin::GEOCODED);
// check the findNearbyPlaces API from geonames - that should give us country, state, city
url = geonamesNearbyPlaceNameURL.arg(getUiLanguage().section(QRegExp("[-_ ]"), 0, 0)).arg(latitude.udeg / 1000000.0).arg(longitude.udeg / 1000000.0);
@ -114,10 +110,8 @@ taxonomy_data reverseGeoLookup(degrees_t latitude, degrees_t longitude)
// fill out all the data - start at COUNTRY since we already got OCEAN above
for (int idx = TC_COUNTRY; idx < TC_NR_CATEGORIES; idx++) {
if (firstData[taxonomy_api_names[idx]].isValid()) {
taxonomy.category[taxonomy.nr].category = idx;
taxonomy.category[taxonomy.nr].origin = taxonomy_origin::GEOCODED;
taxonomy.category[taxonomy.nr].value = copy_qstring(firstData[taxonomy_api_names[idx]].toString());
taxonomy.nr++;
QString value = firstData[taxonomy_api_names[idx]].toString();
taxonomy_set_category(&taxonomy, (taxonomy_category)idx, qPrintable(value), taxonomy_origin::GEOCODED);
}
}
int l3 = taxonomy_index_for_category(&taxonomy, TC_ADMIN_L3);
@ -126,10 +120,7 @@ taxonomy_data reverseGeoLookup(degrees_t latitude, degrees_t longitude)
// basically this means we did get a local name (what we call town), but just like most places
// we didn't get an adminName_3 - which in some regions is the actual city that town belongs to,
// then we copy the town into the city
taxonomy.category[taxonomy.nr].category = TC_ADMIN_L3;
taxonomy.category[taxonomy.nr].origin = taxonomy_origin::GEOCOPIED;
taxonomy.category[taxonomy.nr].value = copy_string(taxonomy.category[lt].value);
taxonomy.nr++;
taxonomy_set_category(&taxonomy, TC_ADMIN_L3, taxonomy.category[lt].value, taxonomy_origin::GEOCOPIED);
}
} else {
report_error("geonames.org did not provide reverse lookup information");