Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #43571 from mhugent/server_annotation_items
Fix placement of annotation items in WMS GetMap
  • Loading branch information
mhugent committed Jun 15, 2021
2 parents a0e2ee4 + 6f7873e commit 6b10a21
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 3 deletions.
36 changes: 34 additions & 2 deletions src/server/services/wms/qgswmsrenderer.cpp
Expand Up @@ -877,7 +877,7 @@ namespace QgsWms
painter.reset( layersRendering( mapSettings, *image ) );

// rendering step for annotations
annotationsRendering( painter.get() );
annotationsRendering( painter.get(), mapSettings );

// painting is terminated
painter->end();
Expand Down Expand Up @@ -3281,7 +3281,7 @@ namespace QgsWms
}
}

void QgsRenderer::annotationsRendering( QPainter *painter ) const
void QgsRenderer::annotationsRendering( QPainter *painter, const QgsMapSettings &mapSettings ) const
{
const QgsAnnotationManager *annotationManager = mProject->annotationManager();
const QList< QgsAnnotation * > annotations = annotationManager->annotations();
Expand All @@ -3293,7 +3293,39 @@ namespace QgsWms
if ( !annotation || !annotation->isVisible() )
continue;

//consider item position
double offsetX = 0;
double offsetY = 0;
if ( annotation->hasFixedMapPosition() )
{
QgsPointXY mapPos = annotation->mapPosition();
if ( mapSettings.destinationCrs() != annotation->mapPositionCrs() )
{
QgsCoordinateTransform coordTransform( annotation->mapPositionCrs(), mapSettings.destinationCrs(), mapSettings.transformContext() );
try
{
mapPos = coordTransform.transform( mapPos );
}
catch ( const QgsCsException &e )
{
QgsMessageLog::logMessage( QStringLiteral( "Error transforming coordinates of annotation item: %1" ).arg( e.what() ) );
}
}
const QgsPointXY devicePos = mapSettings.mapToPixel().transform( mapPos );
offsetX = devicePos.x();
offsetY = devicePos.y();
}
else
{
const QPointF relativePos = annotation->relativePosition();
offsetX = mapSettings.outputSize().width() * relativePos.x();
offsetY = mapSettings.outputSize().height() * relativePos.y();
}

painter->save();
painter->translate( offsetX, offsetY );
annotation->render( renderContext );
painter->restore();
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/server/services/wms/qgswmsrenderer.h
Expand Up @@ -156,7 +156,7 @@ namespace QgsWms
QPainter *layersRendering( const QgsMapSettings &mapSettings, QImage &image ) const;

// Rendering step for annotations
void annotationsRendering( QPainter *painter ) const;
void annotationsRendering( QPainter *painter, const QgsMapSettings &mapSettings ) const;

// Set layer opacity
void setLayerOpacity( QgsMapLayer *layer, int opacity ) const;
Expand Down
22 changes: 22 additions & 0 deletions tests/src/python/test_qgsserver_wms_getmap.py
Expand Up @@ -1853,6 +1853,28 @@ def test_wms_getmap_plus_sign(self):
r, h = self._result(self._execute_request_project(qs, p))
self.assertTrue(b"The layer 'test plus' does not exist" in r)

def test_wms_annotation_item(self):
qs = "?" + "&".join(["%s=%s" % i for i in list({
"MAP": urllib.parse.quote(os.path.join(self.testdata_path,
'test_project_annotation_item.qgz')),
"SERVICE": "WMS",
"VERSION": "1.3.0",
"REQUEST": "GetMap",
"BBOX": "44.9014,8.20346,44.9015,8.20364",
"CRS": "EPSG:4326",
"WIDTH": "800",
"HEIGHT": "400",
"LAYERS": "points",
"STYLES": ",",
"FORMAT": "image/png",
"DPI": "96",
"MAP_RESOLUTION": "96",
"FORMAT_OPTIONS": "dpi:96"
}.items())])

r, h = self._result(self._execute_request(qs))
self._img_diff_error(r, h, "WMS_GetMap_AnnotationItem")


if __name__ == '__main__':
unittest.main()
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.

0 comments on commit 6b10a21

Please sign in to comment.