Members of the KDE Community are recommended to subscribe to the kde-community mailing list at https://mail.kde.org/mailman/listinfo/kde-community to allow them to participate in important discussions and receive other important announcements

Commit dfcebc61 authored by Volker Krause's avatar Volker Krause

Add helper function to extract coordinates from Google Maps URLs

parent 57097ac4
......@@ -20,9 +20,13 @@
#include <../src/jsapi/jsonld.h>
#include <QDebug>
#include <QJSEngine>
#include <QJSValue>
#include <QObject>
#include <QTest>
#include <cmath>
using namespace KItinerary;
#define s(x) QStringLiteral(x)
......@@ -66,8 +70,42 @@ private Q_SLOTS:
jsonLd.setContextDate({{2018, 4, 1}, {}});
QCOMPARE(jsonLd.toDateTime(input, format, locale), result);
}
void testToGeoCoordinates_data()
{
QTest::addColumn<QString>("url");
QTest::addColumn<double>("latitude");
QTest::addColumn<double>("longitude");
QTest::newRow("empty") << s("") << (double)NAN << (double)NAN;
QTest::newRow("non-map") << s("http://www.kde.org") << (double)NAN << (double)NAN;
QTest::newRow("google maps 1") << s("https://www.google.com/maps/place/48.182849,16.378636") << 48.182849 << 16.378636;
QTest::newRow("google maps 1 no ssl") << s("http://www.google.com/maps/place/48.182849,16.378636") << 48.182849 << 16.378636;
QTest::newRow("google maps 2") << s("http://maps.google.fr/?ll=-48.8471603393555,-2.37761735916138&z=19") << -48.8471603393555 << -2.37761735916138;
QTest::newRow("google maps 3") << s("https://maps.google.com/maps?f=q&hl=en&q=52.434788,-13.544329") << 52.434788 << -13.544329;
QTest::newRow("google maps api 1") << s("http://maps.googleapis.com/maps/api/staticmap?language=en-gb&size=280x120&center=69.54363918781344,31.028996706008911&sensor=false&markers=color:0x0896FF%7C69.54363918781344,31.028996706008911&zoom=14&client=gme-booking&signature=xxxxxxxxxxxxxxxxxxxxxx") << 69.54363918781344 << 31.028996706008911;
}
void testToGeoCoordinates()
{
QFETCH(QString, url);
QFETCH(double, latitude);
QFETCH(double, longitude);
QCOMPARE(std::isnan(latitude), std::isnan(longitude));
QJSEngine engine;
JsApi::JsonLd api(&engine);
const auto geo = api.toGeoCoordinates(url);
QVERIFY((geo.isUndefined() && std::isnan(latitude)) || (!geo.isUndefined() && !std::isnan(latitude)));
if (geo.isUndefined()) {
return;
}
QCOMPARE(geo.property(s("latitude")).toNumber(), latitude);
QCOMPARE(geo.property(s("longitude")).toNumber(), longitude);
}
};
QTEST_APPLESS_MAIN(JsApiTest)
QTEST_GUILESS_MAIN(JsApiTest)
#include "jsapitest.moc"
......@@ -39,12 +39,8 @@ function main(text) {
idx += departureDate.index + departureDate[0].length;
res.reservationFor = JsonLd.newObject("LodgingBusiness");
res.reservationFor.geo = JsonLd.newObject("GeoCoordinates");
var geo = text.substr(idx).match(/google.com\/maps\/place\/([0-9\.]+),([0-9\.]+)>/);
if (geo) {
res.reservationFor.geo.latitude = geo[1];
res.reservationFor.geo.longitude = geo[2];
}
var geo = text.substr(idx).match(/<(http[^>]*google.com\/maps[^>]*)>/);
res.reservationFor.geo = JsonLd.toGeoCoordinates(geo[1]);
res.underName = JsonLd.newObject("Person");
var name = text.substr(idx).match(/\*First name\*\s+([^\s{2}]+)\s{2,}/);
......
......@@ -24,6 +24,8 @@
#include <QJSEngine>
#include <QJsonArray>
#include <QLocale>
#include <QRegularExpression>
#include <QUrl>
using namespace KItinerary;
......@@ -108,6 +110,28 @@ QJSValue JsApi::JsonLd::clone(const QJSValue& v) const
return m_engine->toScriptValue(v.toVariant());
}
QJSValue JsApi::JsonLd::toGeoCoordinates(const QString &mapUrl)
{
QUrl url(mapUrl);
if (url.host().contains(QLatin1String("google"))) {
QRegularExpression regExp(QStringLiteral("[/=](-?\\d+\\.\\d+),(-?\\d+\\.\\d+)"));
auto match = regExp.match(url.path());
if (!match.hasMatch()) {
match = regExp.match(url.query());
}
if (match.hasMatch()) {
auto geo = m_engine->newObject();
geo.setProperty(QStringLiteral("@type"), QStringLiteral("GeoCoordinates"));
geo.setProperty(QStringLiteral("latitude"), match.capturedRef(1).toDouble());
geo.setProperty(QStringLiteral("longitude"), match.capturedRef(2).toDouble());
return geo;
}
}
return {};
}
void JsApi::JsonLd::setContextDate(const QDateTime& dt)
{
m_contextDate = dt;
......
......@@ -60,6 +60,11 @@ public:
* That is, create a deep copy of @p v.
*/
Q_INVOKABLE QJSValue clone(const QJSValue &v) const;
/** Parses geo coordinates from a given mapping service URLs.
* This consumes for example Google Maps links and returns a JSON-LD
* GeoCoordinates object.
*/
Q_INVOKABLE QJSValue toGeoCoordinates(const QString &mapUrl);
///@cond internal
void setContextDate(const QDateTime &dt);
......
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