26#include <QCryptographicHash>
27#include <QJsonDocument>
29#include <QNetworkRequest>
33QReadWriteLock QgsGoogleMapsGeocoder::sMutex;
42 , mRegion( regionBias )
43 , mEndpoint( QStringLiteral(
"https://maps.googleapis.com/maps/api/geocode/json" ) )
56 fields.
append(
QgsField( QStringLiteral(
"location_type" ), QMetaType::Type::QString ) );
57 fields.
append(
QgsField( QStringLiteral(
"formatted_address" ), QMetaType::Type::QString ) );
58 fields.
append(
QgsField( QStringLiteral(
"place_id" ), QMetaType::Type::QString ) );
61 fields.
append(
QgsField( QStringLiteral(
"street_number" ), QMetaType::Type::QString ) );
62 fields.
append(
QgsField( QStringLiteral(
"route" ), QMetaType::Type::QString ) );
63 fields.
append(
QgsField( QStringLiteral(
"locality" ), QMetaType::Type::QString ) );
64 fields.
append(
QgsField( QStringLiteral(
"administrative_area_level_2" ), QMetaType::Type::QString ) );
65 fields.
append(
QgsField( QStringLiteral(
"administrative_area_level_1" ), QMetaType::Type::QString ) );
66 fields.
append(
QgsField( QStringLiteral(
"country" ), QMetaType::Type::QString ) );
67 fields.
append(
QgsField( QStringLiteral(
"postal_code" ), QMetaType::Type::QString ) );
90 QgsDebugError(
"Could not transform geocode bounds to WGS84" );
97 const auto it = sCachedResultsGM()->constFind( url );
98 if ( it != sCachedResultsGM()->constEnd() )
104 QNetworkRequest request( url );
116 const QJsonDocument doc = QJsonDocument::fromJson( newReq.
reply().
content(), &err );
121 const QVariantMap res = doc.object().toVariantMap();
122 const QString status = res.value( QStringLiteral(
"status" ) ).toString();
123 if ( status.isEmpty() || !res.contains( QStringLiteral(
"results" ) ) )
125 return QList<QgsGeocoderResult>();
128 if ( res.contains( QLatin1String(
"error_message" ) ) )
133 if ( status == QLatin1String(
"REQUEST_DENIED" ) || status == QLatin1String(
"OVER_QUERY_LIMIT" ) )
137 if ( status != QLatin1String(
"OK" ) && status != QLatin1String(
"ZERO_RESULTS" ) )
145 const QVariantList results = res.value( QStringLiteral(
"results" ) ).toList();
146 if ( results.empty() )
148 sCachedResultsGM()->insert( url, QList<QgsGeocoderResult>() );
149 return QList<QgsGeocoderResult>();
152 QList< QgsGeocoderResult > matches;
153 matches.reserve( results.size( ) );
154 for (
const QVariant &result : results )
158 sCachedResultsGM()->insert( url, matches );
165 QUrl res( mEndpoint );
169 query.addQueryItem( QStringLiteral(
"bounds" ), QStringLiteral(
"%1,%2|%3,%4" ).arg( bounds.
yMinimum() )
174 if ( !mRegion.isEmpty() )
176 query.addQueryItem( QStringLiteral(
"region" ), mRegion.toLower() );
178 query.addQueryItem( QStringLiteral(
"sensor" ), QStringLiteral(
"false" ) );
179 query.addQueryItem( QStringLiteral(
"address" ), address );
180 query.addQueryItem( QStringLiteral(
"key" ), mApiKey );
181 res.setQuery( query );
184 if ( res.toString().contains( QLatin1String(
"fake_qgis_http_endpoint" ) ) )
187 QString modifiedUrlString = res.toString();
189 modifiedUrlString = QUrl::fromPercentEncoding( modifiedUrlString.toUtf8() );
190 modifiedUrlString.replace( QLatin1String(
"fake_qgis_http_endpoint/" ), QLatin1String(
"fake_qgis_http_endpoint_" ) );
192 modifiedUrlString = modifiedUrlString.mid( QStringLiteral(
"http://" ).size() );
193 QString args = modifiedUrlString.mid( modifiedUrlString.indexOf(
'?' ) );
194 if ( modifiedUrlString.size() > 150 )
196 args = QCryptographicHash::hash( args.toUtf8(), QCryptographicHash::Md5 ).toHex();
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(
" " ), QLatin1String(
"_" ) );
207 args.replace( QLatin1String(
":" ), QLatin1String(
"_" ) );
208 args.replace( QLatin1String(
"/" ), QLatin1String(
"_" ) );
209 args.replace( QLatin1String(
"\n" ), QLatin1String(
"_" ) );
214 if ( modifiedUrlString[1] ==
'/' )
216 modifiedUrlString = modifiedUrlString[0] +
":/" + modifiedUrlString.mid( 2 );
219 modifiedUrlString = modifiedUrlString.mid( 0, modifiedUrlString.indexOf(
'?' ) ) + args;
220 QgsDebugMsgLevel( QStringLiteral(
"Get %1 (after laundering)" ).arg( modifiedUrlString ), 2 );
221 res = QUrl::fromLocalFile( modifiedUrlString );
229 const QVariantMap geometry = json.value( QStringLiteral(
"geometry" ) ).toMap();
230 const QVariantMap location = geometry.value( QStringLiteral(
"location" ) ).toMap();
231 const double latitude = location.value( QStringLiteral(
"lat" ) ).toDouble();
232 const double longitude = location.value( QStringLiteral(
"lng" ) ).toDouble();
236 QgsGeocoderResult res( json.value( QStringLiteral(
"formatted_address" ) ).toString(),
240 QVariantMap attributes;
242 if ( json.contains( QStringLiteral(
"formatted_address" ) ) )
243 attributes.insert( QStringLiteral(
"formatted_address" ), json.value( QStringLiteral(
"formatted_address" ) ).toString() );
244 if ( json.contains( QStringLiteral(
"place_id" ) ) )
245 attributes.insert( QStringLiteral(
"place_id" ), json.value( QStringLiteral(
"place_id" ) ).toString() );
246 if ( geometry.contains( QStringLiteral(
"location_type" ) ) )
247 attributes.insert( QStringLiteral(
"location_type" ), geometry.value( QStringLiteral(
"location_type" ) ).toString() );
249 const QVariantList components = json.value( QStringLiteral(
"address_components" ) ).toList();
250 for (
const QVariant &component : components )
252 const QVariantMap componentMap = component.toMap();
253 const QStringList types = componentMap.value( QStringLiteral(
"types" ) ).toStringList();
255 for (
const QString &t :
257 QStringLiteral(
"street_number" ),
258 QStringLiteral(
"route" ),
259 QStringLiteral(
"locality" ),
260 QStringLiteral(
"administrative_area_level_2" ),
261 QStringLiteral(
"administrative_area_level_1" ),
262 QStringLiteral(
"country" ),
263 QStringLiteral(
"postal_code" )
266 if ( types.contains( t ) )
268 attributes.insert( t, componentMap.value( QStringLiteral(
"long_name" ) ).toString() );
269 if ( t == QLatin1String(
"administrative_area_level_1" ) )
270 res.
setGroup( componentMap.value( QStringLiteral(
"long_name" ) ).toString() );
275 if ( geometry.contains( QStringLiteral(
"viewport" ) ) )
277 const QVariantMap viewport = geometry.value( QStringLiteral(
"viewport" ) ).toMap();
278 const QVariantMap northEast = viewport.value( QStringLiteral(
"northeast" ) ).toMap();
279 const QVariantMap southWest = viewport.value( QStringLiteral(
"southwest" ) ).toMap();
281 southWest.value( QStringLiteral(
"lat" ) ).toDouble(),
282 northEast.value( QStringLiteral(
"lng" ) ).toDouble(),
283 northEast.value( QStringLiteral(
"lat" ) ).toDouble()
293 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...
QString errorMessage() const
Returns the error message string, after a get(), post(), head() or put() request has been made.
ErrorCode get(QNetworkRequest &request, bool forceRefresh=false, QgsFeedback *feedback=nullptr, RequestFlags requestFlags=QgsBlockingNetworkRequest::RequestFlags())
Performs a "get" operation on the specified request.
@ 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...
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.
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.
QgsGoogleMapsGeocoder(const QString &apiKey, const QString ®ionBias=QString())
Constructor for QgsGoogleMapsGeocoder.
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 convenience class that simplifies locking and unlocking QReadWriteLocks.
void unlock()
Unlocks the lock.
void changeMode(Mode mode)
Change the mode of the lock to mode.
A rectangle specified with double values.
Q_GLOBAL_STATIC(QReadWriteLock, sDefinitionCacheLock)
QMap< QUrl, QList< QgsGeocoderResult > > CachedGeocodeResult
#define QgsDebugMsgLevel(str, level)
#define QgsDebugError(str)
#define QgsSetRequestInitiatorClass(request, _class)