Commit a80599e0 authored by Volker Krause's avatar Volker Krause
Browse files

Remove the now no longer needed old timezone lookup code in the generator

parent 8110cfa9
Pipeline #21328 passed with stage
in 2 minutes and 43 seconds
......@@ -18,20 +18,6 @@ target_include_directories(generate-knowledgedb PRIVATE
)
target_link_libraries(generate-knowledgedb PRIVATE Qt5::Network Qt5::Gui KOSM)
find_program(XSLTPROC_EXECUTABLE xsltproc)
add_custom_command(
OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/timezones.colormap
COMMAND
${XSLTPROC_EXECUTABLE} --novalid
${CMAKE_CURRENT_SOURCE_DIR}/timezones.xsl
${CMAKE_CURRENT_SOURCE_DIR}/timezones.qgs
> ${CMAKE_CURRENT_SOURCE_DIR}/timezones.colormap
DEPENDS
${CMAKE_CURRENT_SOURCE_DIR}/timezones.xsl
${CMAKE_CURRENT_SOURCE_DIR}/timezones.qgs
)
# extract all elements we are interested in (airports, terminals, stations at airports)
osm_filter(OUTPUT airports.o5m FILTER --keep=\"iata=* or aeroway=terminal or public_transport=station or railway=station or railway=halt or railway=tram_stop\")
......@@ -52,8 +38,6 @@ function(generate_db dbtype outfile)
COMMAND generate-knowledgedb -o ${CMAKE_CURRENT_SOURCE_DIR}/../knowledgedb/${outfile} -d ${dbtype} ${extra_args}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
DEPENDS
${CMAKE_CURRENT_SOURCE_DIR}/timezones.colormap
${CMAKE_CURRENT_SOURCE_DIR}/timezones.png
${ARGN}
generate-knowledgedb
)
......@@ -66,4 +50,4 @@ generate_db(timezoneheader timezonedb_data.h)
generate_db(airport airportdb_data.cpp ${OSM_PLANET_DIR}/airports-bbox.osm)
generate_db(trainstation trainstationdb_data.cpp)
add_custom_target(rebuild-knowledgedb DEPENDS ${outfiles} ${CMAKE_CURRENT_SOURCE_DIR}/timezones.png)
add_custom_target(rebuild-knowledgedb DEPENDS ${outfiles})
......@@ -29,56 +29,6 @@ using namespace KItinerary::Generator;
Timezones::Timezones()
{
// load the color to timezone mapping file
QFile colorMap(QStringLiteral("timezones.colormap"));
if (!colorMap.open(QFile::ReadOnly)) {
qCritical() << "Unable to open timezone colormap file: " << colorMap.errorString();
exit(1);
}
while (!colorMap.atEnd()) {
const auto line = colorMap.readLine();
const auto split = line.split(',');
if (split.size() < 5) {
continue;
}
const auto tzName = split.at(4).trimmed();
if (tzName.isEmpty()) {
continue;
}
QColor c;
c.setRed(split.at(0).toInt());
c.setGreen(split.at(1).toInt());
c.setBlue(split.at(2).toInt());
if (m_colorMap.contains(c.rgb())) {
qWarning() << "Color collision on timezones:" << m_colorMap.value(c.rgb()) << split.at(4).trimmed();
}
m_colorMap.insert(c.rgb(), split.at(4).trimmed());
}
// load the wold file for correcting the pixel to coordinate mapping in the timezone image
// see https://en.wikipedia.org/wiki/World_file for format and math behind this
QFile worldFile(QStringLiteral("timezones.pgw"));
if (!worldFile.open(QFile::ReadOnly|QFile::Text)) {
qCritical() << "Unable to open world file: " << worldFile.errorString();
exit(1);
}
const auto worldFileContent = worldFile.readAll().split('\n');
if (worldFileContent.size() < 6) {
qCritical() << "Invalid world map file format.";
exit(1);
}
if (worldFileContent[1].toDouble() != 0.0 || worldFileContent[2].toDouble() != 0.0) {
qCritical() << "Timezone map is rotated, that is not supported!";
exit(1);
}
m_xMapUnitsPerPixel = worldFileContent[0].toDouble();
m_yMapUnitsPerPixel = worldFileContent[3].toDouble();
m_topLeftMapUnitX = worldFileContent[4].toDouble();
m_topLeftMapUnitY = worldFileContent[5].toDouble();
// load zone.tab for country mapping
QFile zoneTab(QStringLiteral("/usr/share/zoneinfo/zone.tab"));
if (!zoneTab.open(QFile::ReadOnly)) {
......@@ -137,68 +87,6 @@ Timezones::Timezones()
Timezones::~Timezones() = default;
QByteArray Timezones::timezoneForLocation(const QString &isoCode, KnowledgeDb::Coordinate coord) const
{
// look up by country
QSet<QByteArray> countryTzs;
const auto it = m_countryZones.find(isoCode);
if (it != m_countryZones.end()) {
for (const auto &tz : (*it).second) {
countryTzs.insert(tz);
}
}
// look up by coordinate
QByteArray coordTz; // search radius 0
QSet<QByteArray> coordTzs; // search radius 1
if (coord.isValid()) {
if (m_map.isNull() && !m_map.load(QStringLiteral("timezones.png"))) {
qCritical() << "Unable to open timezone map.";
exit(1);
}
const auto p = coordinateToPixel(coord);
//qDebug() << p.x() << p.y() << m_map.width() << m_map.height() << coord.longitude << coord.latitude << QColor(m_map.pixel(p)) << m_colorMap.value(m_map.pixel(p));
coordTz = timezoneForPixel(p.x(), p.y());
// search the vicinity, helps with islands/costal regions or border regions
const QPoint offsets[] = { {-1, -1}, {-1, 0}, {-1, 1}, {0, -1}, {0, 1}, {1, -1}, {1, 0}, {1, 1} };
for (auto offset : offsets) {
const auto tz = timezoneForPixel(p.x() + offset.x(), p.y() + offset.y());
if (!tz.isEmpty()) {
coordTzs.insert(tz);
}
}
}
// determine the result: either just one method yieled a unique result, or both have a unique intersection
if (coordTz.isEmpty() && countryTzs.size() == 1) {
return *countryTzs.constBegin();
}
if (!coordTz.isEmpty() && (countryTzs.isEmpty() || countryTzs.contains(coordTz))) {
return coordTz;
}
// if the above wasn't the case, look for a unique intersection in the vicinity of the coordinate
// this covers cases of locations within the 1.5km resolution of the timezone image
coordTzs.insert(coordTz);
const auto tzs = coordTzs.intersect(countryTzs);
if (tzs.size() == 1) {
return *tzs.constBegin();
}
// if the above still doesn't help, we take the coodinate-based result, can't be entirely wrong
//qDebug() << "Timezone candidates:" << isoCode << coordTz << coordTzs << countryTzs << coord.latitude << coord.longitude;
return coordTz;
}
QByteArray Timezones::timezoneForPixel(int x, int y) const
{
x %= m_map.width();
y %= m_map.height();
return m_colorMap.value(m_map.pixel(x, y));
}
uint16_t Timezones::offset(const QByteArray& tz) const
{
const auto it = std::lower_bound(m_zones.begin(), m_zones.end(), tz);
......@@ -208,14 +96,6 @@ uint16_t Timezones::offset(const QByteArray& tz) const
return m_zoneOffsets[std::distance(m_zones.begin(), it)];
}
QPoint Timezones::coordinateToPixel(KnowledgeDb::Coordinate coord) const
{
QPoint p;
p.setX(qRound((coord.longitude - m_topLeftMapUnitX) / m_xMapUnitsPerPixel));
p.setY(qRound((coord.latitude - m_topLeftMapUnitY) / m_yMapUnitsPerPixel));
return p;
}
void Timezones::setCountryForZone(const QByteArray &tz, const QString &country)
{
auto it = m_countryForZone.find(tz);
......
......@@ -22,11 +22,12 @@
#include <knowledgedb.h>
#include <QImage>
#include <QHash>
#include <QString>
#include <map>
#include <vector>
class QByteArray;
class QColor;
namespace KItinerary {
namespace Generator {
......@@ -44,18 +45,9 @@ public:
private:
friend class TimezoneDbGenerator;
QPoint coordinateToPixel(KnowledgeDb::Coordinate coord) const;
QByteArray timezoneForPixel(int x, int y) const;
void setCountryForZone(const QByteArray &tz, const QString &country);
mutable QImage m_map;
QHash<QRgb, QByteArray> m_colorMap;
double m_xMapUnitsPerPixel;
double m_yMapUnitsPerPixel;
double m_topLeftMapUnitX;
double m_topLeftMapUnitY;
std::vector<QByteArray> m_zones;
std::vector<uint16_t> m_zoneOffsets;
......
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="text" encoding="utf8"/>
<xsl:template match="/qgis">
<xsl:apply-templates select="//symbols/symbol">
<xsl:sort select="@name" data-type="number"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="symbol">
<xsl:variable name="name" select="@name" />
<xsl:value-of select="concat(layer/prop[@k = 'color']/@v, ',', ../../categories/category[@symbol = $name]/@value, '&#10;')" />
</xsl:template>
</xsl:stylesheet>
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment