Skip to content
Permalink
Browse files

Add group and description to QgsGeocoderResult, link in to locator,

and populate group in Google Maps geocoding results
  • Loading branch information
nyalldawson committed Nov 3, 2020
1 parent cbaeb99 commit 1acfe1fbe6dd15f8ac467391ce2e0897cec0719e
@@ -141,6 +141,38 @@ Setss additional attributes generated during the geocode,
which may be added to features being geocoded.

.. seealso:: :py:func:`additionalAttributes`
%End

QString group() const;
%Docstring
Returns the optional group value for the result.

The group can be used to categorize results into different groups, e.g. by appropriate administrative region.

.. seealso:: :py:func:`setGroup`
%End

void setGroup( const QString &group );
%Docstring
Sets the optional ``group`` value for the result.

The ``group`` can be used to categorize results into different groups, e.g. by appropriate administrative region.

.. seealso:: :py:func:`group`
%End

QString description() const;
%Docstring
Returns the optional description for the result.

.. seealso:: :py:func:`setDescription`
%End

void setDescription( const QString &description );
%Docstring
Sets an optional ``description`` for the result.

.. seealso:: :py:func:`description`
%End

};
@@ -77,6 +77,8 @@ QgsGeocoderResult QgsAbstractGeocoderLocatorFilter::locatorResultToGeocoderResul
attrs.value( QStringLiteral( "crs" ) ).value< QgsCoordinateReferenceSystem >() );
geocodeResult.setAdditionalAttributes( attrs.value( QStringLiteral( "attributes" ) ).toMap() );
geocodeResult.setViewport( attrs.value( QStringLiteral( "viewport" ) ).value< QgsRectangle >() );
geocodeResult.setDescription( result.description );
geocodeResult.setGroup( result.group );
return geocodeResult;
}

@@ -88,6 +90,9 @@ QgsLocatorResult QgsAbstractGeocoderLocatorFilter::geocoderResultToLocatorResult
attrs.insert( QStringLiteral( "viewport" ), result.viewport() );
attrs.insert( QStringLiteral( "crs" ), result.crs() );
attrs.insert( QStringLiteral( "attributes" ), result.additionalAttributes() );
return QgsLocatorResult( this, result.identifier(), attrs );
QgsLocatorResult res( this, result.identifier(), attrs );
res.description = result.description();
res.group = result.group();
return res;
}

@@ -29,3 +29,23 @@ QgsGeocoderResult::QgsGeocoderResult( const QString &identifier, const QgsGeomet
, mGeometry( geometry )
, mCrs( crs )
{}

QString QgsGeocoderResult::group() const
{
return mGroup;
}

void QgsGeocoderResult::setGroup( const QString &group )
{
mGroup = group;
}

QString QgsGeocoderResult::description() const
{
return mDescription;
}

void QgsGeocoderResult::setDescription( const QString &description )
{
mDescription = description;
}
@@ -149,6 +149,38 @@ class CORE_EXPORT QgsGeocoderResult
*/
void setAdditionalAttributes( const QVariantMap &attributes ) { mAdditionalAttributes = attributes; }

/**
* Returns the optional group value for the result.
*
* The group can be used to categorize results into different groups, e.g. by appropriate administrative region.
*
* \see setGroup()
*/
QString group() const;

/**
* Sets the optional \a group value for the result.
*
* The \a group can be used to categorize results into different groups, e.g. by appropriate administrative region.
*
* \see group()
*/
void setGroup( const QString &group );

/**
* Returns the optional description for the result.
*
* \see setDescription()
*/
QString description() const;

/**
* Sets an optional \a description for the result.
*
* \see description()
*/
void setDescription( const QString &description );

private:

QgsGeocoderResult() = default;
@@ -157,6 +189,8 @@ class CORE_EXPORT QgsGeocoderResult
QString mErrorString;

QString mIdentifier;
QString mDescription;
QString mGroup;
QgsGeometry mGeometry;
QgsCoordinateReferenceSystem mCrs;
QgsRectangle mViewport;
@@ -239,7 +239,11 @@ QgsGeocoderResult QgsGoogleMapsGeocoder::jsonToResult( const QVariantMap &json )
} )
{
if ( types.contains( t ) )
{
attributes.insert( t, componentMap.value( QStringLiteral( "long_name" ) ).toString() );
if ( t == QLatin1String( "administrative_area_level_1" ) )
res.setGroup( componentMap.value( QStringLiteral( "long_name" ) ).toString() );
}
}
}

@@ -91,7 +91,7 @@ class CORE_EXPORT QgsLocatorResult
* \note This should be translated.
* \since QGIS 3.2
*/
QString group = QString();
QString group;

/**
* The ResultAction stores basic information for additional
@@ -53,6 +53,8 @@ def geocodeString(self, string, context, feedback):
result1 = QgsGeocoderResult('res 1', QgsGeometry.fromPointXY(QgsPointXY(11, 12)),
QgsCoordinateReferenceSystem('EPSG:4326'))
result1.setAdditionalAttributes({'b': 123, 'c': 'xyz'})
result1.setDescription('desc')
result1.setGroup('group')
result2 = QgsGeocoderResult('res 2', QgsGeometry.fromPointXY(QgsPointXY(13, 14)),
QgsCoordinateReferenceSystem('EPSG:3857'))
result2.setAdditionalAttributes({'d': 456})
@@ -96,6 +98,8 @@ def test_geocode(self):
self.assertEqual(geocode_result.crs().authid(), 'EPSG:4326')
self.assertEqual(geocode_result.additionalAttributes(), {'b': 123, 'c': 'xyz'})
self.assertTrue(geocode_result.viewport().isNull())
self.assertFalse(geocode_result.description())
self.assertFalse(geocode_result.group())

# two possible results
filter.fetchResults('b', context, feedback)
@@ -109,13 +113,17 @@ def test_geocode(self):
self.assertEqual(geocode_result.crs().authid(), 'EPSG:4326')
self.assertEqual(geocode_result.additionalAttributes(), {'b': 123, 'c': 'xyz'})
self.assertTrue(geocode_result.viewport().isNull())
self.assertEqual(geocode_result.description(), 'desc')
self.assertEqual(geocode_result.group(), 'group')
self.assertEqual(res2.displayString, 'res 2')
geocode_result = filter.locatorResultToGeocoderResult(res2)
self.assertEqual(geocode_result.identifier(), 'res 2')
self.assertEqual(geocode_result.geometry().asWkt(), 'Point (13 14)')
self.assertEqual(geocode_result.crs().authid(), 'EPSG:3857')
self.assertEqual(geocode_result.additionalAttributes(), {'d': 456})
self.assertEqual(geocode_result.viewport(), QgsRectangle(1, 2, 3, 4))
self.assertFalse(geocode_result.description())
self.assertFalse(geocode_result.group())


if __name__ == '__main__':
@@ -170,6 +170,7 @@ def test_json_to_result(self):
'place_id': 'ChIJeU4e_C2HO4gRRcM6RZ_IPHw', 'postal_code': '94043',
'route': 'Amphitheatre Pkwy', 'street_number': '1600'})
self.assertEqual(res.viewport(), QgsRectangle(-87.7, 42, -87.7, 42.1))
self.assertEqual(res.group(), 'California')

def test_geocode(self):
geocoder = QgsGoogleMapsGeocoder('my key')

0 comments on commit 1acfe1f

Please sign in to comment.
You can’t perform that action at this time.