2017-04-27 18:24:53 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2015-02-23 17:09:48 +00:00
|
|
|
//
|
|
|
|
// infrastructure to deal with dive sites
|
|
|
|
//
|
2015-05-10 15:44:35 +00:00
|
|
|
|
|
|
|
#include "divesitehelpers.h"
|
|
|
|
|
2015-02-23 17:09:48 +00:00
|
|
|
#include "divesite.h"
|
2018-05-11 15:25:41 +00:00
|
|
|
#include "subsurface-string.h"
|
2018-06-03 20:15:19 +00:00
|
|
|
#include "qthelper.h"
|
2015-02-23 17:09:48 +00:00
|
|
|
#include "membuffer.h"
|
2017-07-04 13:48:43 +00:00
|
|
|
#include <QDebug>
|
2015-02-23 17:09:48 +00:00
|
|
|
#include <QJsonDocument>
|
|
|
|
#include <QJsonArray>
|
|
|
|
#include <QJsonObject>
|
|
|
|
#include <QNetworkReply>
|
|
|
|
#include <QNetworkRequest>
|
|
|
|
#include <QNetworkAccessManager>
|
|
|
|
#include <QUrlQuery>
|
|
|
|
#include <QEventLoop>
|
2015-06-01 18:47:25 +00:00
|
|
|
#include <QTimer>
|
2018-10-11 18:46:44 +00:00
|
|
|
#include <memory>
|
2015-02-23 17:09:48 +00:00
|
|
|
|
2018-10-11 19:02:55 +00:00
|
|
|
void reverseGeoLookup(degrees_t latitude, degrees_t longitude, taxonomy_data *taxonomy)
|
2017-10-04 14:49:56 +00:00
|
|
|
{
|
2018-10-11 18:07:20 +00:00
|
|
|
// By making the QNetworkAccessManager static and local to this function,
|
|
|
|
// only one manager exists for all geo-lookups and it is only initialized
|
|
|
|
// on first call to this function.
|
|
|
|
static QNetworkAccessManager rgl;
|
2015-02-23 17:09:48 +00:00
|
|
|
QNetworkRequest request;
|
2015-07-01 19:36:21 +00:00
|
|
|
QEventLoop loop;
|
2017-10-16 09:59:17 +00:00
|
|
|
QString geonamesURL("http://api.geonames.org/findNearbyPlaceNameJSON?lang=%1&lat=%2&lng=%3&radius=50&username=dirkhh");
|
|
|
|
QString geonamesOceanURL("http://api.geonames.org/oceanJSON?lang=%1&lat=%2&lng=%3&radius=50&username=dirkhh");
|
2015-07-01 19:36:21 +00:00
|
|
|
QTimer timer;
|
|
|
|
|
2015-02-23 17:09:48 +00:00
|
|
|
request.setRawHeader("Accept", "text/json");
|
|
|
|
request.setRawHeader("User-Agent", getUserAgent().toUtf8());
|
2018-10-11 16:44:05 +00:00
|
|
|
QObject::connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit()));
|
2015-07-01 19:36:21 +00:00
|
|
|
|
2017-10-03 06:20:08 +00:00
|
|
|
// first check the findNearbyPlaces API from geonames - that should give us country, state, city
|
2018-10-11 19:02:55 +00:00
|
|
|
request.setUrl(geonamesURL.arg(uiLanguage(NULL).section(QRegExp("[-_ ]"), 0, 0)).arg(latitude.udeg / 1000000.0).arg(longitude.udeg / 1000000.0));
|
2015-06-01 18:47:25 +00:00
|
|
|
|
2018-10-11 18:46:44 +00:00
|
|
|
// By using a std::unique_ptr<>, we can exit from the function at any point
|
|
|
|
// and the reply will be freed. Likewise, when overwriting the pointer with
|
|
|
|
// a new value. According to Qt's documentation it is fine the delete the
|
|
|
|
// reply as long as we're not in a slot connected to error() or finish().
|
|
|
|
std::unique_ptr<QNetworkReply> reply(rgl.get(request));
|
2017-10-03 06:20:08 +00:00
|
|
|
timer.setSingleShot(true);
|
2018-10-11 18:46:44 +00:00
|
|
|
QObject::connect(&*reply, SIGNAL(finished()), &loop, SLOT(quit()));
|
2017-10-03 06:20:08 +00:00
|
|
|
timer.start(5000); // 5 secs. timeout
|
|
|
|
loop.exec();
|
2015-06-01 18:47:25 +00:00
|
|
|
|
2017-10-04 14:49:56 +00:00
|
|
|
if (timer.isActive()) {
|
2017-10-03 06:20:08 +00:00
|
|
|
timer.stop();
|
2017-10-04 14:49:56 +00:00
|
|
|
if (reply->error() > 0) {
|
2017-10-03 06:20:08 +00:00
|
|
|
report_error("got error accessing geonames.org: %s", qPrintable(reply->errorString()));
|
2018-10-11 18:46:44 +00:00
|
|
|
return;
|
2017-10-03 06:20:08 +00:00
|
|
|
}
|
|
|
|
int v = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
|
|
|
if (v < 200 || v >= 300)
|
2018-10-11 18:46:44 +00:00
|
|
|
return;
|
2017-10-03 06:20:08 +00:00
|
|
|
QByteArray fullReply = reply->readAll();
|
|
|
|
QJsonParseError errorObject;
|
|
|
|
QJsonDocument jsonDoc = QJsonDocument::fromJson(fullReply, &errorObject);
|
|
|
|
if (errorObject.error != QJsonParseError::NoError) {
|
|
|
|
report_error("error parsing geonames.org response: %s", qPrintable(errorObject.errorString()));
|
2018-10-11 18:46:44 +00:00
|
|
|
return;
|
2017-10-03 06:20:08 +00:00
|
|
|
}
|
|
|
|
QJsonObject obj = jsonDoc.object();
|
|
|
|
QVariant geoNamesObject = obj.value("geonames").toVariant();
|
|
|
|
QVariantList geoNames = geoNamesObject.toList();
|
|
|
|
if (geoNames.count() > 0) {
|
|
|
|
QVariantMap firstData = geoNames.at(0).toMap();
|
|
|
|
int ri = 0, l3 = -1, lt = -1;
|
2018-10-11 19:02:55 +00:00
|
|
|
if (taxonomy->category == NULL) {
|
|
|
|
taxonomy->category = alloc_taxonomy();
|
2017-10-03 06:20:08 +00:00
|
|
|
} else {
|
|
|
|
// clear out the data (except for the ocean data)
|
|
|
|
int ocean;
|
2018-10-11 19:02:55 +00:00
|
|
|
if ((ocean = taxonomy_index_for_category(taxonomy, TC_OCEAN)) > 0) {
|
|
|
|
taxonomy->category[0] = taxonomy->category[ocean];
|
|
|
|
taxonomy->nr = 1;
|
2015-07-13 22:18:52 +00:00
|
|
|
} else {
|
2017-10-03 06:20:08 +00:00
|
|
|
// ocean is -1 if there is no such entry, and we didn't copy above
|
|
|
|
// if ocean is 0, so the following gets us the correct count
|
2018-10-11 19:02:55 +00:00
|
|
|
taxonomy->nr = ocean + 1;
|
2015-07-13 22:18:52 +00:00
|
|
|
}
|
2017-10-03 06:20:08 +00:00
|
|
|
}
|
|
|
|
// get all the data - OCEAN is special, so start at COUNTRY
|
|
|
|
for (int j = TC_COUNTRY; j < TC_NR_CATEGORIES; j++) {
|
|
|
|
if (firstData[taxonomy_api_names[j]].isValid()) {
|
2018-10-11 19:02:55 +00:00
|
|
|
taxonomy->category[ri].category = j;
|
|
|
|
taxonomy->category[ri].origin = taxonomy_origin::GEOCODED;
|
|
|
|
free((void *)taxonomy->category[ri].value);
|
|
|
|
taxonomy->category[ri].value = copy_qstring(firstData[taxonomy_api_names[j]].toString());
|
2017-10-03 06:20:08 +00:00
|
|
|
ri++;
|
2015-07-10 16:51:50 +00:00
|
|
|
}
|
2015-07-01 19:36:21 +00:00
|
|
|
}
|
2018-10-11 19:02:55 +00:00
|
|
|
taxonomy->nr = ri;
|
|
|
|
l3 = taxonomy_index_for_category(taxonomy, TC_ADMIN_L3);
|
|
|
|
lt = taxonomy_index_for_category(taxonomy, TC_LOCALNAME);
|
2017-10-03 06:20:08 +00:00
|
|
|
if (l3 == -1 && lt != -1) {
|
|
|
|
// 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
|
2018-10-11 19:02:55 +00:00
|
|
|
taxonomy->category[ri].value = copy_string(taxonomy->category[lt].value);
|
|
|
|
taxonomy->category[ri].origin = taxonomy_origin::GEOCOPIED;
|
|
|
|
taxonomy->category[ri].category = TC_ADMIN_L3;
|
|
|
|
taxonomy->nr++;
|
2017-10-03 06:20:08 +00:00
|
|
|
}
|
2015-06-01 18:47:25 +00:00
|
|
|
} else {
|
2017-10-03 06:20:08 +00:00
|
|
|
report_error("geonames.org did not provide reverse lookup information");
|
|
|
|
qDebug() << "no reverse geo lookup; geonames returned\n" << fullReply;
|
2015-07-01 19:36:21 +00:00
|
|
|
}
|
2017-10-03 06:20:08 +00:00
|
|
|
} else {
|
|
|
|
report_error("timeout accessing geonames.org");
|
2018-10-11 18:46:44 +00:00
|
|
|
QObject::disconnect(&*reply, SIGNAL(finished()), &loop, SLOT(quit()));
|
2017-10-03 06:20:08 +00:00
|
|
|
reply->abort();
|
|
|
|
}
|
|
|
|
// next check the oceans API to figure out the body of water
|
2018-10-11 19:02:55 +00:00
|
|
|
request.setUrl(geonamesOceanURL.arg(uiLanguage(NULL).section(QRegExp("[-_ ]"), 0, 0)).arg(latitude.udeg / 1000000.0).arg(longitude.udeg / 1000000.0));
|
2018-10-11 18:46:44 +00:00
|
|
|
reply.reset(rgl.get(request)); // Note: frees old reply.
|
|
|
|
QObject::connect(&*reply, SIGNAL(finished()), &loop, SLOT(quit()));
|
2017-10-03 06:20:08 +00:00
|
|
|
timer.start(5000); // 5 secs. timeout
|
|
|
|
loop.exec();
|
2017-10-04 14:49:56 +00:00
|
|
|
if (timer.isActive()) {
|
2017-10-03 06:20:08 +00:00
|
|
|
timer.stop();
|
2017-10-04 14:49:56 +00:00
|
|
|
if (reply->error() > 0) {
|
2017-10-03 06:20:08 +00:00
|
|
|
report_error("got error accessing oceans API of geonames.org: %s", qPrintable(reply->errorString()));
|
2018-10-11 18:46:44 +00:00
|
|
|
return;
|
2017-10-03 06:20:08 +00:00
|
|
|
}
|
|
|
|
int v = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
|
|
|
if (v < 200 || v >= 300)
|
2018-10-11 18:46:44 +00:00
|
|
|
return;
|
2017-10-03 06:20:08 +00:00
|
|
|
QByteArray fullReply = reply->readAll();
|
|
|
|
QJsonParseError errorObject;
|
|
|
|
QJsonDocument jsonDoc = QJsonDocument::fromJson(fullReply, &errorObject);
|
|
|
|
if (errorObject.error != QJsonParseError::NoError) {
|
|
|
|
report_error("error parsing geonames.org response: %s", qPrintable(errorObject.errorString()));
|
2018-10-11 18:46:44 +00:00
|
|
|
return;
|
2017-10-03 06:20:08 +00:00
|
|
|
}
|
|
|
|
QJsonObject obj = jsonDoc.object();
|
|
|
|
QVariant oceanObject = obj.value("ocean").toVariant();
|
|
|
|
QVariantMap oceanName = oceanObject.toMap();
|
|
|
|
if (oceanName["name"].isValid()) {
|
|
|
|
int idx;
|
2018-10-11 19:02:55 +00:00
|
|
|
if (taxonomy->category == NULL)
|
|
|
|
taxonomy->category = alloc_taxonomy();
|
|
|
|
idx = taxonomy_index_for_category(taxonomy, TC_OCEAN);
|
2017-10-03 06:20:08 +00:00
|
|
|
if (idx == -1)
|
2018-10-11 19:02:55 +00:00
|
|
|
idx = taxonomy->nr;
|
2017-10-03 06:20:08 +00:00
|
|
|
if (idx < TC_NR_CATEGORIES) {
|
2018-10-11 19:02:55 +00:00
|
|
|
taxonomy->category[idx].category = TC_OCEAN;
|
|
|
|
taxonomy->category[idx].origin = taxonomy_origin::GEOCODED;
|
|
|
|
taxonomy->category[idx].value = copy_qstring(oceanName["name"].toString());
|
|
|
|
if (idx == taxonomy->nr)
|
|
|
|
taxonomy->nr++;
|
2015-07-01 19:36:21 +00:00
|
|
|
}
|
2015-05-10 15:44:35 +00:00
|
|
|
}
|
2017-10-03 06:20:08 +00:00
|
|
|
} else {
|
|
|
|
report_error("timeout accessing geonames.org");
|
2018-10-11 18:46:44 +00:00
|
|
|
QObject::disconnect(&*reply, SIGNAL(finished()), &loop, SLOT(quit()));
|
2017-10-03 06:20:08 +00:00
|
|
|
reply->abort();
|
|
|
|
}
|
2015-05-10 15:44:35 +00:00
|
|
|
}
|