Navigation Menu

Skip to content

Commit

Permalink
Add unit tests for outside polygon labels
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed May 3, 2020
1 parent e9f024b commit 9364196
Show file tree
Hide file tree
Showing 12 changed files with 126 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/core/labeling/qgspallabeling.cpp
Expand Up @@ -2515,7 +2515,7 @@ void QgsPalLayerSettings::registerFeature( const QgsFeature &f, QgsRenderContext
|| ( ( placement == QgsPalLayerSettings::Horizontal
|| placement == QgsPalLayerSettings::AroundPoint
|| placement == QgsPalLayerSettings::OverPoint ||
placement == QgsPalLayerSettings::Free ) && polygonPlacementFlags() & QgsLabeling::PolygonPlacementFlag::AllowPlacementOutsideOfPolygon ) )
placement == QgsPalLayerSettings::Free ) && polygonPlacement & QgsLabeling::PolygonPlacementFlag::AllowPlacementOutsideOfPolygon ) )
{
distance = std::max( distance, 2.0 );
}
Expand Down
126 changes: 125 additions & 1 deletion tests/src/python/test_qgspallabeling_placement.py
Expand Up @@ -27,7 +27,8 @@
QgsMarkerSymbol,
QgsProperty,
QgsVectorLayerSimpleLabeling,
QgsLabelObstacleSettings)
QgsLabelObstacleSettings,
QgsLabeling)
from utilities import getTempfilePath, renderMapToImage, mapSettingsString

from test_qgspallabeling_base import TestQgsPalLabeling, runSuite
Expand Down Expand Up @@ -777,6 +778,129 @@ def test_small_polygon_placement_perimeter(self):
self.removeMapLayer(self.layer)
self.layer = None

def test_small_polygon_large_label(self):
# Default polygon placement for small polygon with a large label
self.layer = TestQgsPalLabeling.loadFeatureLayer('polygon_small')
self._TestMapSettings = self.cloneMapSettings(self._MapSettings)
self.lyr.placement = QgsPalLayerSettings.OverPoint
self.lyr.format().setSize(30)
self.checkTest()
self.removeMapLayer(self.layer)
self.layer = None

def test_small_polygon_large_label_force_inside(self):
# Default polygon placement for small polygon with a large label, with only placement of inside labels
self.layer = TestQgsPalLabeling.loadFeatureLayer('polygon_small')
self._TestMapSettings = self.cloneMapSettings(self._MapSettings)
self.lyr.placement = QgsPalLayerSettings.OverPoint
self.lyr.fitInPolygonOnly = True
self.checkTest()
self.removeMapLayer(self.layer)
self.layer = None

def test_small_polygon_large_label_allow_outside(self):
# Default polygon placement for small polygon with a large label, allowing outside placement
# we expect this to sit outside, because it CAN'T fit
self.layer = TestQgsPalLabeling.loadFeatureLayer('polygon_small')
self._TestMapSettings = self.cloneMapSettings(self._MapSettings)
self.lyr.placement = QgsPalLayerSettings.OverPoint
self.lyr.setPolygonPlacementFlags(QgsLabeling.PolygonPlacementFlags(QgsLabeling.PolygonPlacementFlag.AllowPlacementOutsideOfPolygon | QgsLabeling.PolygonPlacementFlag.AllowPlacementInsideOfPolygon))
self.checkTest()
self.removeMapLayer(self.layer)
self.layer = None

def test_small_polygon_small_label_inside_and_outside(self):
# Default polygon placement for small polygon with a small label, allowing outside placement
# we expect this to sit inside, because it CAN fit
self.layer = TestQgsPalLabeling.loadFeatureLayer('polygon_small')
self._TestMapSettings = self.cloneMapSettings(self._MapSettings)
self.lyr.placement = QgsPalLayerSettings.OverPoint
self.lyr.setPolygonPlacementFlags(QgsLabeling.PolygonPlacementFlags(QgsLabeling.PolygonPlacementFlag.AllowPlacementOutsideOfPolygon | QgsLabeling.PolygonPlacementFlag.AllowPlacementInsideOfPolygon))
f = self.lyr.format()
f.setSize(8)
self.lyr.setFormat(f)
self.checkTest()
self.removeMapLayer(self.layer)
self.layer = None

def test_small_polygon_small_label_outside_only(self):
# Default polygon placement for small polygon with a small label, allowing outside placement only
# we expect this to sit outside, cos we are blocking inside placement
self.layer = TestQgsPalLabeling.loadFeatureLayer('polygon_small')
self._TestMapSettings = self.cloneMapSettings(self._MapSettings)
self.lyr.placement = QgsPalLayerSettings.OverPoint
self.lyr.setPolygonPlacementFlags(QgsLabeling.PolygonPlacementFlags(QgsLabeling.PolygonPlacementFlag.AllowPlacementOutsideOfPolygon))
f = self.lyr.format()
f.setSize(8)
self.lyr.setFormat(f)
self.checkTest()
self.removeMapLayer(self.layer)
self.layer = None

def test_small_polygon_small_data_defined_allow_outside(self):
# Default data defined allow outside mode
self.layer = TestQgsPalLabeling.loadFeatureLayer('polygon_small')
self._TestMapSettings = self.cloneMapSettings(self._MapSettings)
self.lyr.placement = QgsPalLayerSettings.Horizontal
self.lyr.dataDefinedProperties().setProperty(QgsPalLayerSettings.PolygonLabelOutside, QgsProperty.fromValue(1))
f = self.lyr.format()
f.setSize(8)
self.lyr.setFormat(f)
self.checkTest()
self.removeMapLayer(self.layer)
self.layer = None

def test_small_polygon_small_data_defined_force_outside(self):
# Default data defined allow outside mode
self.layer = TestQgsPalLabeling.loadFeatureLayer('polygon_small')
self._TestMapSettings = self.cloneMapSettings(self._MapSettings)
self.lyr.placement = QgsPalLayerSettings.Horizontal
self.lyr.dataDefinedProperties().setProperty(QgsPalLayerSettings.PolygonLabelOutside, QgsProperty.fromValue('force'))
f = self.lyr.format()
f.setSize(8)
self.lyr.setFormat(f)
self.checkTest()
self.removeMapLayer(self.layer)
self.layer = None

def test_small_polygon_small_data_defined_allow_outside_large(self):
# Default data defined allow outside mode
self.layer = TestQgsPalLabeling.loadFeatureLayer('polygon_small')
self._TestMapSettings = self.cloneMapSettings(self._MapSettings)
self.lyr.placement = QgsPalLayerSettings.Horizontal
self.lyr.dataDefinedProperties().setProperty(QgsPalLayerSettings.PolygonLabelOutside, QgsProperty.fromValue(1))
f = self.lyr.format()
f.setSize(20)
self.lyr.setFormat(f)
self.checkTest()
self.removeMapLayer(self.layer)
self.layer = None

def test_small_polygon_small_label_outside_mode(self):
# Forced outside placement for polygon
self.layer = TestQgsPalLabeling.loadFeatureLayer('polygon_small')
self._TestMapSettings = self.cloneMapSettings(self._MapSettings)
self.lyr.placement = QgsPalLayerSettings.OutsidePolygons
f = self.lyr.format()
f.setSize(8)
self.lyr.setFormat(f)
self.checkTest()
self.removeMapLayer(self.layer)
self.layer = None

def test_small_polygon_small_label_outside_mode_distance(self):
# Forced outside placement for polygon with distance
self.layer = TestQgsPalLabeling.loadFeatureLayer('polygon_small')
self._TestMapSettings = self.cloneMapSettings(self._MapSettings)
self.lyr.placement = QgsPalLayerSettings.OutsidePolygons
self.lyr.dist = 10
f = self.lyr.format()
f.setSize(8)
self.lyr.setFormat(f)
self.checkTest()
self.removeMapLayer(self.layer)
self.layer = None

def test_small_polygon_perimeter_only_fit(self):
# Polygon perimeter placement for small polygon when set to only show labels which fit in polygon
self.layer = TestQgsPalLabeling.loadFeatureLayer('polygon_small')
Expand Down
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.
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.
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.

0 comments on commit 9364196

Please sign in to comment.