QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
qgsgeocoderlocatorfilter.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsgeocoderlocatorfilter.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 "qgsmapcanvas.h"
18 #include "qgsmessagelog.h"
19 #include "qgsgeocoderresult.h"
20 
21 QgsGeocoderLocatorFilter::QgsGeocoderLocatorFilter( const QString &name, const QString &displayName, const QString &prefix, QgsGeocoderInterface *geocoder, QgsMapCanvas *canvas, const QgsRectangle &boundingBox )
22  : QgsAbstractGeocoderLocatorFilter( name, displayName, prefix, geocoder, boundingBox )
23  , mCanvas( canvas )
24 {
25 
26 }
27 
29 {
30  QgsLocatorFilter *filter = new QgsGeocoderLocatorFilter( name(), displayName(), prefix(), geocoder(), mCanvas );
31  filter->setFetchResultsDelay( fetchResultsDelay() );
32  return filter;
33 }
34 
35 void QgsGeocoderLocatorFilter::handleGeocodeResult( const QgsGeocoderResult &result )
36 {
37  const QgsCoordinateTransform ct( result.crs(), mCanvas->mapSettings().destinationCrs(), mCanvas->mapSettings().transformContext() );
38  QgsGeometry g = result.geometry();
39  const QgsRectangle viewport = result.viewport();
40  try
41  {
42  QgsRectangle bounds;
43  g.transform( ct );
44  if ( viewport.isNull() )
45  {
46  bounds = g.boundingBox();
47  }
48  else
49  {
50  QgsCoordinateTransform extentTransform = ct;
51  extentTransform.setBallparkTransformsAreAppropriate( true );
52  bounds = extentTransform.transformBoundingBox( viewport );
53  }
54  mCanvas->zoomToFeatureExtent( bounds );
55 
56  mCanvas->flashGeometries( QList< QgsGeometry >() << g );
57  }
58  catch ( QgsCsException & )
59  {
60  QgsMessageLog::logMessage( tr( "Could not transform result to canvas CRS" ) );
61  }
62 }
63 
QgsLocatorFilter
Abstract base class for filters which collect locator results.
Definition: qgslocatorfilter.h:152
QgsAbstractGeocoderLocatorFilter::displayName
QString displayName() const override
Definition: qgsabstractgeocoderlocatorfilter.cpp:35
qgsmapcanvas.h
QgsGeometry::transform
Qgis::GeometryOperationResult transform(const QgsCoordinateTransform &ct, Qgis::TransformDirection direction=Qgis::TransformDirection::Forward, bool transformZ=false) SIP_THROW(QgsCsException)
Transforms this geometry as described by the coordinate transform ct.
Definition: qgsgeometry.cpp:3128
QgsMapCanvas::mapSettings
const QgsMapSettings & mapSettings() const
Gets access to properties used for map rendering.
Definition: qgsmapcanvas.cpp:437
QgsLocatorFilter::setFetchResultsDelay
void setFetchResultsDelay(int delay)
Sets a delay (in milliseconds) for the filter to wait prior to fetching results.
Definition: qgslocatorfilter.h:376
QgsMapCanvas::flashGeometries
void flashGeometries(const QList< QgsGeometry > &geometries, const QgsCoordinateReferenceSystem &crs=QgsCoordinateReferenceSystem(), const QColor &startColor=QColor(255, 0, 0, 255), const QColor &endColor=QColor(255, 0, 0, 0), int flashes=3, int duration=500)
Causes a set of geometries to flash within the canvas.
Definition: qgsmapcanvas.cpp:1876
QgsCoordinateTransform::transformBoundingBox
QgsRectangle transformBoundingBox(const QgsRectangle &rectangle, Qgis::TransformDirection direction=Qgis::TransformDirection::Forward, bool handle180Crossover=false) const SIP_THROW(QgsCsException)
Transforms a rectangle from the source CRS to the destination CRS.
Definition: qgscoordinatetransform.cpp:560
QgsMapCanvas
Map canvas is a class for displaying all GIS data types on a canvas.
Definition: qgsmapcanvas.h:89
QgsAbstractGeocoderLocatorFilter::name
QString name() const override
Definition: qgsabstractgeocoderlocatorfilter.cpp:30
QgsRectangle
A rectangle specified with double values.
Definition: qgsrectangle.h:41
QgsGeocoderResult::crs
QgsCoordinateReferenceSystem crs() const
Returns the coordinate reference system for the calculated geometry().
Definition: qgsgeocoderresult.h:113
qgsgeocoderresult.h
QgsCsException
Custom exception class for Coordinate Reference System related exceptions.
Definition: qgsexception.h:65
QgsMapSettings::transformContext
QgsCoordinateTransformContext transformContext() const
Returns the coordinate transform context, which stores various information regarding which datum tran...
Definition: qgsmapsettings.cpp:463
QgsCoordinateTransform::setBallparkTransformsAreAppropriate
void setBallparkTransformsAreAppropriate(bool appropriate)
Sets whether approximate "ballpark" results are appropriate for this coordinate transform.
Definition: qgscoordinatetransform.cpp:939
QgsAbstractGeocoderLocatorFilter
An abstract base class which implements a locator filter populated from a QgsGeocoderInterface.
Definition: qgsabstractgeocoderlocatorfilter.h:39
QgsGeocoderResult::geometry
QgsGeometry geometry() const
Returns the resultant geometry resulting from the geocoding operation.
Definition: qgsgeocoderresult.h:94
QgsMessageLog::logMessage
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::MessageLevel::Warning, bool notifyUser=true)
Adds a message to the log instance (and creates it if necessary).
Definition: qgsmessagelog.cpp:27
qgsgeocoderlocatorfilter.h
QgsMapCanvas::zoomToFeatureExtent
void zoomToFeatureExtent(QgsRectangle &rect)
Zooms to feature extent.
Definition: qgsmapcanvas.cpp:1687
QgsMapSettings::destinationCrs
QgsCoordinateReferenceSystem destinationCrs() const
Returns the destination coordinate reference system for the map render.
Definition: qgsmapsettings.cpp:358
QgsGeocoderResult::viewport
QgsRectangle viewport() const
Returns the suggested viewport for the result, which reflects a recommended map extent for displaying...
Definition: qgsgeocoderresult.h:134
QgsAbstractGeocoderLocatorFilter::prefix
QString prefix() const override
Definition: qgsabstractgeocoderlocatorfilter.cpp:40
QgsGeocoderResult
Represents a matching result from a geocoder search.
Definition: qgsgeocoderresult.h:40
QgsGeometry
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:124
QgsGeometry::boundingBox
QgsRectangle boundingBox() const
Returns the bounding box of the geometry.
Definition: qgsgeometry.cpp:1080
QgsGeocoderLocatorFilter::QgsGeocoderLocatorFilter
QgsGeocoderLocatorFilter(const QString &name, const QString &displayName, const QString &prefix, QgsGeocoderInterface *geocoder, QgsMapCanvas *canvas, const QgsRectangle &boundingBox=QgsRectangle())
Constructor for QgsGeocoderLocatorFilter.
Definition: qgsgeocoderlocatorfilter.cpp:21
QgsCoordinateTransform
Class for doing transforms between two map coordinate systems.
Definition: qgscoordinatetransform.h:57
QgsRectangle::isNull
bool isNull() const
Test if the rectangle is null (all coordinates zero or after call to setMinimal()).
Definition: qgsrectangle.h:479
QgsGeocoderInterface
Interface for geocoders.
Definition: qgsgeocoder.h:36
QgsAbstractGeocoderLocatorFilter::geocoder
QgsGeocoderInterface * geocoder() const
Returns the geocoder attached to the filter.
Definition: qgsabstractgeocoderlocatorfilter.cpp:68
qgsmessagelog.h
QgsGeocoderLocatorFilter::clone
QgsLocatorFilter * clone() const override SIP_FACTORY
Definition: qgsgeocoderlocatorfilter.cpp:28