QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
qgsabstractgeocoderlocatorfilter.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsabstractgeocoderlocatorfilter.cpp
3  ---------------
4  Date : August 2020
5  Copyright : (C) 2020 by Nyall Dawson
6  Email : nyall dot dawson at gmail dot com
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
17 #include "qgsgeocoder.h"
18 #include "qgsgeocodercontext.h"
19 
20 QgsAbstractGeocoderLocatorFilter::QgsAbstractGeocoderLocatorFilter( const QString &name, const QString &displayName, const QString &prefix, QgsGeocoderInterface *geocoder, const QgsRectangle &boundingBox )
21  : mName( name )
22  , mDisplayName( displayName )
23  , mPrefix( prefix )
24  , mGeocoder( geocoder )
25  , mBoundingBox( boundingBox )
26 {
27 
28 }
29 
31 {
32  return mName;
33 }
34 
36 {
37  return mDisplayName;
38 }
39 
41 {
42  return mPrefix;
43 }
44 
45 void QgsAbstractGeocoderLocatorFilter::fetchResults( const QString &string, const QgsLocatorContext &context, QgsFeedback *feedback )
46 {
47  QgsGeocoderContext geocodeContext( context.transformContext );
48  geocodeContext.setAreaOfInterest( QgsGeometry::fromRect( context.targetExtent ) );
49  geocodeContext.setAreaOfInterestCrs( context.targetExtentCrs );
50 
51  const QList< QgsGeocoderResult > results = mGeocoder->geocodeString( string, geocodeContext, feedback );
52  for ( const QgsGeocoderResult &result : results )
53  {
54  if ( result.isValid() )
55  {
56  const QgsLocatorResult locatorRes = geocoderResultToLocatorResult( result );
57  emit resultFetched( locatorRes );
58  }
59  }
60 }
61 
63 {
64  const QgsGeocoderResult geocodeResult = locatorResultToGeocoderResult( result );
65  handleGeocodeResult( geocodeResult );
66 }
67 
69 {
70  return mGeocoder;
71 }
72 
74 {
75  const QVariantMap attrs = result.userData.toMap();
76  QgsGeocoderResult geocodeResult( attrs.value( QStringLiteral( "identifier" ) ).toString(),
77  attrs.value( QStringLiteral( "geom" ) ).value< QgsGeometry >(),
78  attrs.value( QStringLiteral( "crs" ) ).value< QgsCoordinateReferenceSystem >() );
79  geocodeResult.setAdditionalAttributes( attrs.value( QStringLiteral( "attributes" ) ).toMap() );
80  geocodeResult.setViewport( attrs.value( QStringLiteral( "viewport" ) ).value< QgsRectangle >() );
81  geocodeResult.setDescription( result.description );
82  geocodeResult.setGroup( result.group );
83  return geocodeResult;
84 }
85 
87 {
88  QVariantMap attrs;
89  attrs.insert( QStringLiteral( "identifier" ), result.identifier() );
90  attrs.insert( QStringLiteral( "geom" ), result.geometry() );
91  attrs.insert( QStringLiteral( "viewport" ), result.viewport() );
92  attrs.insert( QStringLiteral( "crs" ), result.crs() );
93  attrs.insert( QStringLiteral( "attributes" ), result.additionalAttributes() );
94  QgsLocatorResult res( this, result.identifier(), attrs );
95  res.description = result.description();
96  res.group = result.group();
97  return res;
98 }
99 
QgsLocatorContext::transformContext
QgsCoordinateTransformContext transformContext
Coordinate transform context, to use whenever performing coordinate transformations inside a locator.
Definition: qgslocatorcontext.h:61
QgsAbstractGeocoderLocatorFilter::displayName
QString displayName() const override
Definition: qgsabstractgeocoderlocatorfilter.cpp:35
QgsGeocoderResult::additionalAttributes
QVariantMap additionalAttributes() const
Contains additional attributes generated during the geocode, which may be added to features being geo...
Definition: qgsgeocoderresult.h:155
qgsgeocoder.h
QgsAbstractGeocoderLocatorFilter::name
QString name() const override
Definition: qgsabstractgeocoderlocatorfilter.cpp:30
QgsGeocoderResult::setAdditionalAttributes
void setAdditionalAttributes(const QVariantMap &attributes)
Setss additional attributes generated during the geocode, which may be added to features being geocod...
Definition: qgsgeocoderresult.h:163
QgsRectangle
A rectangle specified with double values.
Definition: qgsrectangle.h:41
QgsLocatorResult
Encapsulates properties of an individual matching result found by a QgsLocatorFilter.
Definition: qgslocatorfilter.h:38
QgsLocatorContext::targetExtent
QgsRectangle targetExtent
Map extent to target in results.
Definition: qgslocatorcontext.h:47
QgsGeocoderResult::setGroup
void setGroup(const QString &group)
Sets the optional group value for the result.
Definition: qgsgeocoderresult.cpp:38
QgsGeocoderResult::crs
QgsCoordinateReferenceSystem crs() const
Returns the coordinate reference system for the calculated geometry().
Definition: qgsgeocoderresult.h:113
QgsLocatorContext
Encapsulates the properties relating to the context of a locator search.
Definition: qgslocatorcontext.h:32
QgsGeocoderContext::setAreaOfInterestCrs
void setAreaOfInterestCrs(const QgsCoordinateReferenceSystem &crs)
Sets the crs for the area of interest, which can be used to indicate the desired geographic area wher...
Definition: qgsgeocodercontext.h:118
QgsLocatorContext::targetExtentCrs
QgsCoordinateReferenceSystem targetExtentCrs
Coordinate reference system for the map extent variable.
Definition: qgslocatorcontext.h:53
QgsFeedback
Base class for feedback objects to be used for cancellation of something running in a worker thread.
Definition: qgsfeedback.h:44
QgsGeocoderContext::setAreaOfInterest
void setAreaOfInterest(const QgsGeometry &area)
Sets the optional area of interest, which can be used to indicate the desired geographic area where g...
Definition: qgsgeocodercontext.h:100
QgsAbstractGeocoderLocatorFilter::triggerResult
void triggerResult(const QgsLocatorResult &result) override
Definition: qgsabstractgeocoderlocatorfilter.cpp:62
QgsGeocoderResult::geometry
QgsGeometry geometry() const
Returns the resultant geometry resulting from the geocoding operation.
Definition: qgsgeocoderresult.h:94
QgsGeocoderResult::setDescription
void setDescription(const QString &description)
Sets an optional description for the result.
Definition: qgsgeocoderresult.cpp:48
QgsGeocoderResult::setViewport
void setViewport(const QgsRectangle &viewport)
Sets the suggested viewport for the result, which reflects a recommended map extent for displaying th...
Definition: qgsgeocoderresult.h:147
QgsGeocoderInterface::geocodeString
virtual QList< QgsGeocoderResult > geocodeString(const QString &string, const QgsGeocoderContext &context, QgsFeedback *feedback=nullptr) const
Geocodes a string.
Definition: qgsgeocoder.cpp:29
QgsAbstractGeocoderLocatorFilter::fetchResults
void fetchResults(const QString &string, const QgsLocatorContext &context, QgsFeedback *feedback) override
Definition: qgsabstractgeocoderlocatorfilter.cpp:45
QgsGeocoderResult::group
QString group() const
Returns the optional group value for the result.
Definition: qgsgeocoderresult.cpp:33
QgsGeocoderResult::viewport
QgsRectangle viewport() const
Returns the suggested viewport for the result, which reflects a recommended map extent for displaying...
Definition: qgsgeocoderresult.h:134
QgsGeocoderContext
Encapsulates the context of a geocoding operation.
Definition: qgsgeocodercontext.h:31
QgsAbstractGeocoderLocatorFilter::prefix
QString prefix() const override
Definition: qgsabstractgeocoderlocatorfilter.cpp:40
QgsGeocoderResult
Represents a matching result from a geocoder search.
Definition: qgsgeocoderresult.h:40
QgsLocatorResult::group
QString group
Group the results by categories If left as empty string, this means that results are all shown withou...
Definition: qgslocatorfilter.h:101
QgsGeometry::fromRect
static QgsGeometry fromRect(const QgsRectangle &rect) SIP_HOLDGIL
Creates a new geometry from a QgsRectangle.
Definition: qgsgeometry.cpp:241
QgsAbstractGeocoderLocatorFilter::QgsAbstractGeocoderLocatorFilter
QgsAbstractGeocoderLocatorFilter(const QString &name, const QString &displayName, const QString &prefix, QgsGeocoderInterface *geocoder, const QgsRectangle &boundingBox=QgsRectangle())
Constructor for QgsAbstractGeocoderLocatorFilter.
Definition: qgsabstractgeocoderlocatorfilter.cpp:20
QgsAbstractGeocoderLocatorFilter::locatorResultToGeocoderResult
QgsGeocoderResult locatorResultToGeocoderResult(const QgsLocatorResult &result) const
Converts a locator result to a geocoder result.
Definition: qgsabstractgeocoderlocatorfilter.cpp:73
QgsGeocoderResult::description
QString description() const
Returns the optional description for the result.
Definition: qgsgeocoderresult.cpp:43
QgsGeocoderResult::identifier
QString identifier() const
Returns the identifier string for the result.
Definition: qgsgeocoderresult.h:83
QgsAbstractGeocoderLocatorFilter::geocoderResultToLocatorResult
QgsLocatorResult geocoderResultToLocatorResult(const QgsGeocoderResult &result)
Converts a geocoder result to a locator result.
Definition: qgsabstractgeocoderlocatorfilter.cpp:86
QgsLocatorResult::description
QString description
Descriptive text for result.
Definition: qgslocatorfilter.h:77
qgsabstractgeocoderlocatorfilter.h
QgsGeocoderInterface
Interface for geocoders.
Definition: qgsgeocoder.h:36
QgsAbstractGeocoderLocatorFilter::geocoder
QgsGeocoderInterface * geocoder() const
Returns the geocoder attached to the filter.
Definition: qgsabstractgeocoderlocatorfilter.cpp:68
QgsLocatorResult::userData
QVariant userData
Custom reference or other data set by the filter.
Definition: qgslocatorfilter.h:82
qgsgeocodercontext.h