Skip to content

Commit 9643906

Browse files
committed
Rationalize function add test case
1 parent 12cc995 commit 9643906

5 files changed

Lines changed: 38 additions & 56 deletions

File tree

app/inpututils.cpp

Lines changed: 19 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -303,60 +303,40 @@ void InputUtils::setExtentToFeature( const FeatureLayerPair &pair, InputMapSetti
303303
mapSettings->setExtent( currentExtent );
304304
}
305305

306-
QPointF InputUtils::geometryCenterToScreenCoordinates( const QgsGeometry &geom, InputMapSettings *mapSettings )
306+
QPointF InputUtils::onScreenGeometryCenterToScreenCoordinates( const QgsGeometry &geom, InputMapSettings *mapSettings )
307307
{
308308
QPointF screenPoint;
309-
309+
QgsPoint target;
310310
if ( !mapSettings || geom.isNull() || !geom.constGet() )
311311
return screenPoint;
312312

313-
QgsRectangle bbox = geom.boundingBox();
314-
screenPoint = mapSettings->coordinateToScreen( QgsPoint( bbox.center() ) );
315-
316-
return screenPoint;
317-
}
318-
/**
319-
* Returns true if the geometry \a geom is fully contain within the current map extent
320-
*
321-
* Nota Bene: Assume geometry and map canvas CRS are the same
322-
*/
323-
bool InputUtils::extentContainGeometry( const QgsGeometry &geom, InputMapSettings *mapSettings )
324-
{
325-
if ( !mapSettings || geom.isNull() || !geom.constGet() )
326-
return false;
327-
328313
QgsRectangle currentExtent = mapSettings->mapSettings().visibleExtent();
329314
QgsRectangle geomBbox = geom.boundingBox();
330315

331-
return currentExtent.contains( geomBbox );
332-
}
316+
qDebug() << "currentExtent:" << currentExtent.xMinimum()<<","
317+
<< currentExtent.yMinimum()<<","
318+
<< currentExtent.xMaximum()<<","
319+
<< currentExtent.yMaximum();
320+
qDebug() << "geom:" << geom.asWkt();
333321

334322

335-
/**
336-
* Returns the center point of the \a geom currently display on screen
337-
*
338-
* Nota Bene: Assume geometry and map canvas CRS are the same
339-
*/
340-
QPointF InputUtils::onScreenGeometryCenterToScreenCoordinates( const QgsGeometry &geom, InputMapSettings *mapSettings )
341-
{
342-
QPointF screenPoint;
343-
if ( !mapSettings || geom.isNull() || !geom.constGet() )
344-
return screenPoint;
345-
346-
QgsRectangle currentExtent = mapSettings->mapSettings().visibleExtent();
347-
QgsGeometry currentExtentAsGeom = QgsGeometry::fromRect( currentExtent );
348-
349-
QgsGeometry intersectedGeom = currentExtentAsGeom.intersection( geom );
350-
351-
QgsRectangle bbox = intersectedGeom.boundingBox();
352-
QgsPoint target = QgsPoint( bbox.center().x(), bbox.center().y() );
323+
if (currentExtent.contains( geomBbox )){
324+
// Keep the geometry as is
325+
target = QgsPoint( geomBbox.center() );
326+
}
327+
else{
328+
// Cut the geometry to current extent
329+
QgsGeometry currentExtentAsGeom = QgsGeometry::fromRect( currentExtent );
330+
QgsGeometry intersectedGeom = geom.intersection( currentExtentAsGeom );
331+
QgsRectangle bbox = intersectedGeom.boundingBox();
332+
target = QgsPoint( bbox.center() );
333+
}
353334

354335
screenPoint = mapSettings->coordinateToScreen( target );
355-
336+
qDebug() << "screenPoint:" << screenPoint;
356337
return screenPoint;
357338
}
358339

359-
360340
double InputUtils::convertCoordinateString( const QString &rationalValue )
361341
{
362342
QStringList values = rationalValue.split( "," );

app/inpututils.h

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -84,17 +84,10 @@ class InputUtils: public QObject
8484
Q_INVOKABLE void setExtentToFeature( const FeatureLayerPair &pair, InputMapSettings *mapSettings );
8585

8686
/**
87-
* Returns the screen coordinates for a geometry's bounding box centroid
88-
* Geometry must be in canvas CRS
89-
*/
90-
Q_INVOKABLE QPointF geometryCenterToScreenCoordinates( const QgsGeometry &geom, InputMapSettings *mapSettings );
91-
92-
93-
/**
94-
* Returns the true if the geometry could fully be contained in the current screen otherwise false
95-
* Geometry must be in canvas CRS
87+
* Returns the center point of the \a geom currently displayed on screen
88+
*
89+
* Nota Bene: Assume geometry and map canvas CRS are the same
9690
*/
97-
Q_INVOKABLE bool extentContainGeometry( const QgsGeometry &geom, InputMapSettings *mapSettings );
9891
Q_INVOKABLE QPointF onScreenGeometryCenterToScreenCoordinates( const QgsGeometry &geom, InputMapSettings *mapSettings );
9992

10093
// utility functions to extract information from map settings

app/qml/map/MMMapController.qml

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1210,13 +1210,7 @@ Item {
12101210
function jumpToHighlighted( mapOffset ) {
12111211
if ( identifyHighlight.geometry === null )
12121212
return
1213-
let screenPt
1214-
if ( __inputUtils.extentContainGeometry( identifyHighlight.geometry, mapCanvas.mapSettings ) ){
1215-
screenPt = __inputUtils.geometryCenterToScreenCoordinates( identifyHighlight.geometry, mapCanvas.mapSettings )
1216-
}
1217-
else{
1218-
screenPt = __inputUtils.onScreenGeometryCenterToScreenCoordinates( identifyHighlight.geometry, mapCanvas.mapSettings )
1219-
}
1213+
let screenPt = __inputUtils.onScreenGeometryCenterToScreenCoordinates( identifyHighlight.geometry, mapCanvas.mapSettings )
12201214

12211215
screenPt.y += mapOffset / 2
12221216
mapCanvas.jumpTo( screenPt )

app/test/testutilsfunctions.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -946,3 +946,17 @@ void TestUtilsFunctions::testFormatAreaInProjectUnit()
946946
area2str = mUtils->formatAreaInProjectUnit( 7000, 1, project );
947947
QVERIFY( area2str == "1.7 ac" );
948948
}
949+
950+
void TestUtilsFunctions::testOnScreenGeometryCenterToScreenCoordinates()
951+
{
952+
InputMapSettings ms;
953+
ms.setDestinationCrs( QgsCoordinateReferenceSystem::fromEpsgId( 3857 ) );
954+
ms.setExtent( QgsRectangle( 603472 , 5.39034e+06 , 654803 , 5.4641e+06 ) );
955+
ms.setOutputSize( QSize( 1000, 500 ) );
956+
957+
QgsGeometry geom = QgsGeometry::fromWkt( "LineString (605540.02427726075984538 5422974.88796170614659786, 618450.11232534842565656 5430064.85434877127408981, 631042.73919192561879754 5418953.71299590915441513, 652418.458746955730021 5431228.87868097703903913)");
958+
959+
QVERIFY( mUtils->onScreenGeometryCenterToScreenCoordinates(geom, &ms) == QPointF(161.5,247) );
960+
961+
962+
}

app/test/testutilsfunctions.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ class TestUtilsFunctions: public QObject
5050
void testParsePositionUpdates();
5151
void testFormatDistanceInProjectUnit();
5252
void testFormatAreaInProjectUnit();
53+
void testOnScreenGeometryCenterToScreenCoordinates();
5354

5455
private:
5556
void testFormatDuration( const QDateTime &t0, qint64 diffSecs, const QString &expectedResult );

0 commit comments

Comments
 (0)