File indexing completed on 2024-07-14 07:23:05

0001 // SPDX-License-Identifier: LGPL-2.1-or-later
0002 //
0003 // SPDX-FileCopyrightText: 2011 Dennis Nienhüser <nienhueser@kde.org>
0004 // SPDX-FileCopyrightText: 2013 Bernhard Beschow <bbeschow@cs.tu-berlin.de>
0005 //
0006 
0007 #include "DatabaseQuery.h"
0008 
0009 #include "GeoDataLatLonBox.h"
0010 #include "MarbleModel.h"
0011 #include "PositionTracking.h"
0012 
0013 #include <QMap>
0014 
0015 namespace Marble
0016 {
0017 
0018 DatabaseQuery::DatabaseQuery( const MarbleModel* model, const QString &searchTerm, const GeoDataLatLonBox &preferred ) :
0019     m_queryType( BroadSearch ), m_resultFormat( AddressFormat ), m_searchTerm( searchTerm.trimmed() ),
0020     m_category( OsmPlacemark::UnknownCategory )
0021 {
0022     if ( model && model->positionTracking()->status() == PositionProviderStatusAvailable ) {
0023         m_position = model->positionTracking()->currentLocation();
0024         m_resultFormat = DistanceFormat;
0025     } else if ( !preferred.isEmpty() ) {
0026         m_position = preferred.center();
0027         m_resultFormat = AddressFormat;
0028     } else {
0029         m_resultFormat = AddressFormat;
0030     }
0031 
0032     QStringList terms = m_searchTerm.split(QLatin1Char(','), QString::SkipEmptyParts );
0033 
0034     QRegExp streetAndHouse( "^(.*)\\s+(\\d+\\D?)$" );
0035     if ( streetAndHouse.indexIn( terms.first() ) != -1 ) {
0036         if ( streetAndHouse.capturedTexts().size() == 3 ) {
0037             terms.removeFirst();
0038             terms.push_front( streetAndHouse.capturedTexts().at( 1 ) );
0039             terms.push_front( streetAndHouse.capturedTexts().at( 2 ) );
0040         }
0041     }
0042 
0043     Q_ASSERT( terms.size() > 0 );
0044     if ( terms.size() == 1 ) {
0045         m_queryType = isPointOfInterest( m_searchTerm ) ? CategorySearch : BroadSearch;
0046     } else if ( terms.size() == 2 ) {
0047         m_street = terms.first().trimmed();
0048         m_region = terms.last().trimmed();
0049         m_queryType = isPointOfInterest( m_street ) ? CategorySearch : AddressSearch;
0050     } else {
0051         m_houseNumber = terms.first().trimmed();
0052         m_street = terms.at( 1 ).trimmed();
0053         m_region = terms.last().trimmed(); // skips 2, 3, ..., if any
0054         m_queryType = AddressSearch;
0055     }
0056 }
0057 
0058 bool DatabaseQuery::isPointOfInterest( const QString &category )
0059 {
0060     static QMap<QString, OsmPlacemark::OsmCategory> pois;
0061     if ( pois.isEmpty() ) {
0062         pois[QObject::tr( "pois").toLower()] = OsmPlacemark::UnknownCategory;
0063         pois["pois"] = OsmPlacemark::UnknownCategory;
0064         pois[QObject::tr( "camping" ).toLower()] = OsmPlacemark::AccomodationCamping;
0065         pois["camping"] = OsmPlacemark::AccomodationCamping;
0066         pois[QObject::tr( "hostel" ).toLower()] = OsmPlacemark::AccomodationHostel;
0067         pois["hostel"] = OsmPlacemark::AccomodationHostel;
0068         pois[QObject::tr( "hotel" ).toLower()] = OsmPlacemark::AccomodationHotel;
0069         pois["hotel"] = OsmPlacemark::AccomodationHotel;
0070         pois[QObject::tr( "motel" ).toLower()] = OsmPlacemark::AccomodationMotel;
0071         pois["motel"] = OsmPlacemark::AccomodationMotel;
0072         pois[QObject::tr( "youth hostel" ).toLower()] = OsmPlacemark::AccomodationYouthHostel;
0073         pois["youth hostel"] = OsmPlacemark::AccomodationYouthHostel;
0074         pois[QObject::tr( "library" ).toLower()] = OsmPlacemark::AmenityLibrary;
0075         pois["library"] = OsmPlacemark::AmenityLibrary;
0076         pois[QObject::tr( "college" ).toLower()] = OsmPlacemark::EducationCollege;
0077         pois["college"] = OsmPlacemark::EducationCollege;
0078         pois[QObject::tr( "school" ).toLower()] = OsmPlacemark::EducationSchool;
0079         pois["school"] = OsmPlacemark::EducationSchool;
0080         pois[QObject::tr( "university" ).toLower()] = OsmPlacemark::EducationUniversity;
0081         pois["university"] = OsmPlacemark::EducationUniversity;
0082         pois[QObject::tr( "bar" ).toLower()] = OsmPlacemark::FoodBar;
0083         pois["bar"] = OsmPlacemark::FoodBar;
0084         pois[QObject::tr( "biergarten" ).toLower()] = OsmPlacemark::FoodBiergarten;
0085         pois["biergarten"] = OsmPlacemark::FoodBiergarten;
0086         pois[QObject::tr( "cafe" ).toLower()] = OsmPlacemark::FoodCafe;
0087         pois["cafe"] = OsmPlacemark::FoodCafe;
0088         pois[QObject::tr( "fast food" ).toLower()] = OsmPlacemark::FoodFastFood;
0089         pois["fast food"] = OsmPlacemark::FoodFastFood;
0090         pois[QObject::tr( "pub" ).toLower()] = OsmPlacemark::FoodPub;
0091         pois["pub"] = OsmPlacemark::FoodPub;
0092         pois[QObject::tr( "restaurant" ).toLower()] = OsmPlacemark::FoodRestaurant;
0093         pois["restaurant"] = OsmPlacemark::FoodRestaurant;
0094         pois[QObject::tr( "doctor" ).toLower()] = OsmPlacemark::HealthDoctors;
0095         pois["doctor"] = OsmPlacemark::HealthDoctors;
0096         pois[QObject::tr( "hospital" ).toLower()] = OsmPlacemark::HealthHospital;
0097         pois["hospital"] = OsmPlacemark::HealthHospital;
0098         pois[QObject::tr( "pharmacy" ).toLower()] = OsmPlacemark::HealthPharmacy;
0099         pois["pharmacy"] = OsmPlacemark::HealthPharmacy;
0100         pois[QObject::tr( "bank" ).toLower()] = OsmPlacemark::MoneyBank;
0101         pois["bank"] = OsmPlacemark::MoneyBank;
0102         pois[QObject::tr( "beverages" ).toLower()] = OsmPlacemark::ShoppingBeverages;
0103         pois["beverages"] = OsmPlacemark::ShoppingBeverages;
0104         pois[QObject::tr( "hifi" ).toLower()] = OsmPlacemark::ShoppingHifi;
0105         pois["hifi"] = OsmPlacemark::ShoppingHifi;
0106         pois[QObject::tr( "supermarket" ).toLower()] = OsmPlacemark::ShoppingSupermarket;
0107         pois["supermarket"] = OsmPlacemark::ShoppingSupermarket;
0108         pois[QObject::tr( "attraction" ).toLower()] = OsmPlacemark::TouristAttraction;
0109         pois["attraction"] = OsmPlacemark::TouristAttraction;
0110         pois[QObject::tr( "castle" ).toLower()] = OsmPlacemark::TouristCastle;
0111         pois["castle"] = OsmPlacemark::TouristCastle;
0112         pois[QObject::tr( "cinema" ).toLower()] = OsmPlacemark::TouristCinema;
0113         pois["cinema"] = OsmPlacemark::TouristCinema;
0114         pois[QObject::tr( "monument" ).toLower()] = OsmPlacemark::TouristMonument;
0115         pois["monument"] = OsmPlacemark::TouristMonument;
0116         pois[QObject::tr( "museum" ).toLower()] = OsmPlacemark::TouristMuseum;
0117         pois["museum"] = OsmPlacemark::TouristMuseum;
0118         pois[QObject::tr( "ruin" ).toLower()] = OsmPlacemark::TouristRuin;
0119         pois["ruin"] = OsmPlacemark::TouristRuin;
0120         pois[QObject::tr( "theatre" ).toLower()] = OsmPlacemark::TouristTheatre;
0121         pois["theatre"] = OsmPlacemark::TouristTheatre;
0122         pois[QObject::tr( "theme park" ).toLower()] = OsmPlacemark::TouristThemePark;
0123         pois["theme park"] = OsmPlacemark::TouristThemePark;
0124         pois[QObject::tr( "view point" ).toLower()] = OsmPlacemark::TouristViewPoint;
0125         pois["view point"] = OsmPlacemark::TouristViewPoint;
0126         pois[QObject::tr( "zoo" ).toLower()] = OsmPlacemark::TouristZoo;
0127         pois["zoo"] = OsmPlacemark::TouristZoo;
0128         pois[QObject::tr( "airport" ).toLower()] = OsmPlacemark::TransportAirportTerminal;
0129         pois["airport"] = OsmPlacemark::TransportAirportTerminal;
0130         pois[QObject::tr( "airport runway" ).toLower()] = OsmPlacemark::TransportAirportRunway;
0131         pois["airport runway"] = OsmPlacemark::TransportAirportRunway;
0132         pois[QObject::tr( "airport apron" ).toLower()] = OsmPlacemark::TransportAirportApron;
0133         pois["airport apron"] = OsmPlacemark::TransportAirportApron;
0134         pois[QObject::tr( "airport taxiway" ).toLower()] = OsmPlacemark::TransportAirportTaxiway;
0135         pois["airport taxiway"] = OsmPlacemark::TransportAirportTaxiway;
0136         pois[QObject::tr( "bus station" ).toLower()] = OsmPlacemark::TransportBusStation;
0137         pois["bus station"] = OsmPlacemark::TransportBusStation;
0138         pois[QObject::tr( "bus stop" ).toLower()] = OsmPlacemark::TransportBusStop;
0139         pois["bus stop"] = OsmPlacemark::TransportBusStop;
0140         pois[QObject::tr( "car share" ).toLower()] = OsmPlacemark::TransportCarShare;
0141         pois["car share"] = OsmPlacemark::TransportCarShare;
0142         pois[QObject::tr( "fuel" ).toLower()] = OsmPlacemark::TransportFuel;
0143         pois["fuel"] = OsmPlacemark::TransportFuel;
0144         pois[QObject::tr( "parking" ).toLower()] = OsmPlacemark::TransportParking;
0145         pois["parking"] = OsmPlacemark::TransportParking;
0146         pois[QObject::tr( "train station" ).toLower()] = OsmPlacemark::TransportTrainStation;
0147         pois["train station"] = OsmPlacemark::TransportTrainStation;
0148         pois[QObject::tr( "atm" ).toLower()] = OsmPlacemark::MoneyAtm;
0149         pois["atm"] = OsmPlacemark::MoneyAtm;
0150         pois[QObject::tr( "tram stop" ).toLower()] = OsmPlacemark::TransportTramStop;
0151         pois["tram stop"] = OsmPlacemark::TransportTramStop;
0152         pois[QObject::tr( "bicycle rental" ).toLower()] = OsmPlacemark::TransportRentalBicycle;
0153         pois["bicycle rental"] = OsmPlacemark::TransportRentalBicycle;
0154         pois[QObject::tr( "car rental" ).toLower()] = OsmPlacemark::TransportRentalCar;
0155         pois["car rental"] = OsmPlacemark::TransportRentalCar;
0156         pois[QObject::tr( "speed camera" ).toLower()] = OsmPlacemark::TransportSpeedCamera;
0157         pois["speed camera"] = OsmPlacemark::TransportSpeedCamera;
0158         pois[QObject::tr( "taxi" ).toLower()] = OsmPlacemark::TransportTaxiRank;
0159         pois["taxi"] = OsmPlacemark::TransportTaxiRank;
0160         pois[QObject::tr( "memorial" ).toLower()] = OsmPlacemark::HistoricMemorial;
0161         pois["memorial"] = OsmPlacemark::HistoricMemorial;
0162     }
0163 
0164     Q_ASSERT( !pois.isEmpty() );
0165     QString const searchTerm = category.toLower();
0166     if ( pois.contains( searchTerm ) ) {
0167         m_category = pois[searchTerm];
0168         return true;
0169     }
0170 
0171     m_category = OsmPlacemark::UnknownCategory;
0172     return false;
0173 }
0174 
0175 OsmPlacemark::OsmCategory DatabaseQuery::category() const
0176 {
0177     return m_category;
0178 }
0179 
0180 DatabaseQuery::QueryType DatabaseQuery::queryType() const
0181 {
0182     return m_queryType;
0183 }
0184 
0185 DatabaseQuery::ResultFormat DatabaseQuery::resultFormat() const
0186 {
0187     return m_resultFormat;
0188 }
0189 
0190 QString DatabaseQuery::street() const
0191 {
0192     return m_street;
0193 }
0194 
0195 QString DatabaseQuery::houseNumber() const
0196 {
0197     return m_houseNumber;
0198 }
0199 
0200 QString DatabaseQuery::region() const
0201 {
0202     return m_region;
0203 }
0204 
0205 QString DatabaseQuery::searchTerm() const
0206 {
0207     return m_searchTerm;
0208 }
0209 
0210 GeoDataCoordinates DatabaseQuery::position() const
0211 {
0212     return m_position;
0213 }
0214 
0215 }