26#include <QNetworkRequest>
27#include <QJsonDocument>
30QReadWriteLock QgsGoogleMapsGeocoder::sMutex;
39 , mRegion( regionBias )
40 , mEndpoint( QStringLiteral( "https:
53 fields.
append(
QgsField( QStringLiteral(
"location_type" ), QMetaType::Type::QString ) );
54 fields.
append(
QgsField( QStringLiteral(
"formatted_address" ), QMetaType::Type::QString ) );
55 fields.
append(
QgsField( QStringLiteral(
"place_id" ), QMetaType::Type::QString ) );
58 fields.
append(
QgsField( QStringLiteral(
"street_number" ), QMetaType::Type::QString ) );
59 fields.
append(
QgsField( QStringLiteral(
"route" ), QMetaType::Type::QString ) );
60 fields.
append(
QgsField( QStringLiteral(
"locality" ), QMetaType::Type::QString ) );
61 fields.
append(
QgsField( QStringLiteral(
"administrative_area_level_2" ), QMetaType::Type::QString ) );
62 fields.
append(
QgsField( QStringLiteral(
"administrative_area_level_1" ), QMetaType::Type::QString ) );
63 fields.
append(
QgsField( QStringLiteral(
"country" ), QMetaType::Type::QString ) );
64 fields.
append(
QgsField( QStringLiteral(
"postal_code" ), QMetaType::Type::QString ) );
87 QgsDebugError(
"Could not transform geocode bounds to WGS84" );
94 const auto it = sCachedResults()->constFind( url );
95 if ( it != sCachedResults()->constEnd() )
101 QNetworkRequest request( url );
113 const QJsonDocument doc = QJsonDocument::fromJson( newReq.
reply().
content(), &err );
118 const QVariantMap res = doc.object().toVariantMap();
119 const QString status = res.value( QStringLiteral(
"status" ) ).toString();
120 if ( status.isEmpty() || !res.contains( QStringLiteral(
"results" ) ) )
122 return QList<QgsGeocoderResult>();
125 if ( res.contains( QLatin1String(
"error_message" ) ) )
130 if ( status == QLatin1String(
"REQUEST_DENIED" ) || status == QLatin1String(
"OVER_QUERY_LIMIT" ) )
134 if ( status != QLatin1String(
"OK" ) && status != QLatin1String(
"ZERO_RESULTS" ) )
142 const QVariantList results = res.value( QStringLiteral(
"results" ) ).toList();
143 if ( results.empty() )
145 sCachedResults()->insert( url, QList<QgsGeocoderResult>() );
146 return QList<QgsGeocoderResult>();
149 QList< QgsGeocoderResult > matches;
150 matches.reserve( results.size( ) );
151 for (
const QVariant &result : results )
155 sCachedResults()->insert( url, matches );
162 QUrl res( mEndpoint );
166 query.addQueryItem( QStringLiteral(
"bounds" ), QStringLiteral(
"%1,%2|%3,%4" ).arg( bounds.
yMinimum() )
171 if ( !mRegion.isEmpty() )
173 query.addQueryItem( QStringLiteral(
"region" ), mRegion.toLower() );
175 query.addQueryItem( QStringLiteral(
"sensor" ), QStringLiteral(
"false" ) );
176 query.addQueryItem( QStringLiteral(
"address" ), address );
177 query.addQueryItem( QStringLiteral(
"key" ), mApiKey );
178 res.setQuery( query );
181 if ( res.toString().contains( QLatin1String(
"fake_qgis_http_endpoint" ) ) )
184 QString modifiedUrlString = res.toString();
186 modifiedUrlString = QUrl::fromPercentEncoding( modifiedUrlString.toUtf8() );
187 modifiedUrlString.replace( QLatin1String(
"fake_qgis_http_endpoint/" ), QLatin1String(
"fake_qgis_http_endpoint_" ) );
189 modifiedUrlString = modifiedUrlString.mid( QStringLiteral(
"http://" ).size() );
190 QString args = modifiedUrlString.mid( modifiedUrlString.indexOf(
'?' ) );
191 if ( modifiedUrlString.size() > 150 )
193 args = QCryptographicHash::hash( args.toUtf8(), QCryptographicHash::Md5 ).toHex();
197 args.replace( QLatin1String(
"?" ), QLatin1String(
"_" ) );
198 args.replace( QLatin1String(
"&" ), QLatin1String(
"_" ) );
199 args.replace( QLatin1String(
"<" ), QLatin1String(
"_" ) );
200 args.replace( QLatin1String(
">" ), QLatin1String(
"_" ) );
201 args.replace( QLatin1String(
"'" ), QLatin1String(
"_" ) );
202 args.replace( QLatin1String(
"\"" ), QLatin1String(
"_" ) );
203 args.replace( QLatin1String(
" " ), QLatin1String(
"_" ) );
204 args.replace( QLatin1String(
":" ), QLatin1String(
"_" ) );
205 args.replace( QLatin1String(
"/" ), QLatin1String(
"_" ) );
206 args.replace( QLatin1String(
"\n" ), QLatin1String(
"_" ) );
211 if ( modifiedUrlString[1] ==
'/' )
213 modifiedUrlString = modifiedUrlString[0] +
":/" + modifiedUrlString.mid( 2 );
216 modifiedUrlString = modifiedUrlString.mid( 0, modifiedUrlString.indexOf(
'?' ) ) + args;
217 QgsDebugMsgLevel( QStringLiteral(
"Get %1 (after laundering)" ).arg( modifiedUrlString ), 2 );
218 res = QUrl::fromLocalFile( modifiedUrlString );
226 const QVariantMap geometry = json.value( QStringLiteral(
"geometry" ) ).toMap();
227 const QVariantMap location = geometry.value( QStringLiteral(
"location" ) ).toMap();
228 const double latitude = location.value( QStringLiteral(
"lat" ) ).toDouble();
229 const double longitude = location.value( QStringLiteral(
"lng" ) ).toDouble();
233 QgsGeocoderResult res( json.value( QStringLiteral(
"formatted_address" ) ).toString(),
237 QVariantMap attributes;
239 if ( json.contains( QStringLiteral(
"formatted_address" ) ) )
240 attributes.insert( QStringLiteral(
"formatted_address" ), json.value( QStringLiteral(
"formatted_address" ) ).toString() );
241 if ( json.contains( QStringLiteral(
"place_id" ) ) )
242 attributes.insert( QStringLiteral(
"place_id" ), json.value( QStringLiteral(
"place_id" ) ).toString() );
243 if ( geometry.contains( QStringLiteral(
"location_type" ) ) )
244 attributes.insert( QStringLiteral(
"location_type" ), geometry.value( QStringLiteral(
"location_type" ) ).toString() );
246 const QVariantList components = json.value( QStringLiteral(
"address_components" ) ).toList();
247 for (
const QVariant &component : components )
249 const QVariantMap componentMap = component.toMap();
250 const QStringList types = componentMap.value( QStringLiteral(
"types" ) ).toStringList();
252 for (
const QString &t :
254 QStringLiteral(
"street_number" ),
255 QStringLiteral(
"route" ),
256 QStringLiteral(
"locality" ),
257 QStringLiteral(
"administrative_area_level_2" ),
258 QStringLiteral(
"administrative_area_level_1" ),
259 QStringLiteral(
"country" ),
260 QStringLiteral(
"postal_code" )
263 if ( types.contains( t ) )
265 attributes.insert( t, componentMap.value( QStringLiteral(
"long_name" ) ).toString() );
266 if ( t == QLatin1String(
"administrative_area_level_1" ) )
267 res.
setGroup( componentMap.value( QStringLiteral(
"long_name" ) ).toString() );
272 if ( geometry.contains( QStringLiteral(
"viewport" ) ) )
274 const QVariantMap viewport = geometry.value( QStringLiteral(
"viewport" ) ).toMap();
275 const QVariantMap northEast = viewport.value( QStringLiteral(
"northeast" ) ).toMap();
276 const QVariantMap southWest = viewport.value( QStringLiteral(
"southwest" ) ).toMap();
278 southWest.value( QStringLiteral(
"lat" ) ).toDouble(),
279 northEast.value( QStringLiteral(
"lng" ) ).toDouble(),
280 northEast.value( QStringLiteral(
"lat" ) ).toDouble()
290 mEndpoint = endpoint;
WkbType
The WKB type describes the number of dimensions a geometry has.
A thread safe class for performing blocking (sync) network requests, with full support for QGIS proxy...
ErrorCode get(QNetworkRequest &request, bool forceRefresh=false, QgsFeedback *feedback=nullptr)
Performs a "get" operation on the specified request.
QString errorMessage() const
Returns the error message string, after a get(), post(), head() or put() request has been made.
@ NoError
No error was encountered.
QgsNetworkReplyContent reply() const
Returns the content of the network reply, after a get(), post(), head() or put() request has been mad...
This class represents a coordinate reference system (CRS).
Custom exception class for Coordinate Reference System related exceptions.
Base class for feedback objects to be used for cancellation of something running in a worker thread.
Encapsulate a field in an attribute table or data source.
Container of fields for a vector layer.
bool append(const QgsField &field, Qgis::FieldOrigin origin=Qgis::FieldOrigin::Provider, int originIndex=-1)
Appends a field.
Encapsulates the context of a geocoding operation.
QgsCoordinateTransformContext transformContext() const
Returns the coordinate transform context, which should be used whenever the geocoder constructs a coo...
QgsCoordinateReferenceSystem areaOfInterestCrs() const
Returns the coordinate reference system for the area of interest, which can be used to indicate the d...
QgsGeometry areaOfInterest() const
Returns the optional area of interest, which can be used to indicate the desired geographic area wher...
@ GeocodesStrings
Can geocode string input values.
Represents a matching result from a geocoder search.
void setAdditionalAttributes(const QVariantMap &attributes)
Setss additional attributes generated during the geocode, which may be added to features being geocod...
void setGroup(const QString &group)
Sets the optional group value for the result.
void setViewport(const QgsRectangle &viewport)
Sets the suggested viewport for the result, which reflects a recommended map extent for displaying th...
static QgsGeocoderResult errorResult(const QString &errorMessage)
Creates an invalid error result, with the specified errorMessage string.
A geometry is the spatial representation of a feature.
Qgis::GeometryOperationResult transform(const QgsCoordinateTransform &ct, Qgis::TransformDirection direction=Qgis::TransformDirection::Forward, bool transformZ=false)
Transforms this geometry as described by the coordinate transform ct.
static QgsGeometry fromPointXY(const QgsPointXY &point)
Creates a new geometry from a QgsPointXY object.
bool isEmpty() const
Returns true if the geometry is empty (eg a linestring with no vertices, or a collection with no geom...
QgsRectangle boundingBox() const
Returns the bounding box of the geometry.
A geocoder which uses the Google Map geocoding API to retrieve results.
QList< QgsGeocoderResult > geocodeString(const QString &string, const QgsGeocoderContext &context, QgsFeedback *feedback=nullptr) const override
Geocodes a string.
void setEndpoint(const QString &endpoint)
Sets a specific API endpoint to use for requests.
QgsGeocoderResult jsonToResult(const QVariantMap &json) const
Converts a JSON result returned from the Google Maps service to a geocoder result object.
QgsFields appendedFields() const override
Returns a set of newly created fields which will be appended to existing features during the geocode ...
QString apiKey() const
Returns the API key which will be used when accessing the Google Maps API.
Qgis::WkbType wkbType() const override
Returns the WKB type of geometries returned by the geocoder.
QString region() const
Returns the optional region bias which will be used to prioritize results in a certain region.
QUrl requestUrl(const QString &address, const QgsRectangle &bounds=QgsRectangle()) const
Returns the URL generated for geocoding the specified address.
void setRegion(const QString ®ion)
Sets the optional region bias which will be used to prioritize results in a certain region.
void setApiKey(const QString &key)
Sets the API key to use when accessing the Google Maps API.
Flags flags() const override
Returns the geocoder's capability flags.
QByteArray content() const
Returns the reply content.
A class to represent a 2D point.
The QgsReadWriteLocker class is a convenience class that simplifies locking and unlocking QReadWriteL...
void unlock()
Unlocks the lock.
void changeMode(Mode mode)
Change the mode of the lock to mode.
A rectangle specified with double values.
double xMinimum() const
Returns the x minimum value (left side of rectangle).
double yMinimum() const
Returns the y minimum value (bottom side of rectangle).
bool isNull() const
Test if the rectangle is null (holding no spatial information).
double yMaximum() const
Returns the y maximum value (top side of rectangle).
Q_GLOBAL_STATIC(QReadWriteLock, sDefinitionCacheLock)
QMap< QUrl, QList< QgsGeocoderResult > > CachedGeocodeResult
#define QgsDebugMsgLevel(str, level)
#define QgsDebugError(str)
#define QgsSetRequestInitiatorClass(request, _class)