Skip to content

Commit

Permalink
Add unit tests for multipolygon random marker fill
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Oct 28, 2019
1 parent f373203 commit 52dba5d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 14 deletions.
8 changes: 6 additions & 2 deletions src/core/symbology/qgsfillsymbollayer.cpp
Expand Up @@ -3988,10 +3988,13 @@ void QgsRandomMarkerFillSymbolLayer::renderPolygon( const QPolygonF &points, QLi

if ( mRenderingFeature )
{
// in the middle of rendering a possibly multi-part feature, so we collect all the parts and defer the actual rendering
// until after we've received the final part
mCurrentParts << part;
}
else
{
// not rendering a feature, so we can just render the polygon immediately
render( context.renderContext(), QVector< Part>() << part, context.feature() ? *context.feature() : QgsFeature(), context.selected() );
}
}
Expand Down Expand Up @@ -4036,8 +4039,7 @@ void QgsRandomMarkerFillSymbolLayer::render( QgsRenderContext &context, const QV
}
}

QgsGeometry geom = QgsGeometry::unaryUnion( geometryParts );

const QgsGeometry geom = geometryParts.count() != 1 ? QgsGeometry::unaryUnion( geometryParts ) : geometryParts.at( 0 );

if ( clipPoints )
{
Expand All @@ -4060,6 +4062,8 @@ void QgsRandomMarkerFillSymbolLayer::render( QgsRenderContext &context, const QV

QVector< QgsPointXY > randomPoints = geom.randomPointsInPolygon( count, seed );
#if 0
// in some cases rendering from top to bottom is nice (e.g. randomised tree markers), but in other cases it's not wanted..
// TODO consider exposing this as an option
std::sort( randomPoints.begin(), randomPoints.end(), []( const QgsPointXY & a, const QgsPointXY & b )->bool
{
return a.y() < b.y();
Expand Down
37 changes: 25 additions & 12 deletions tests/src/python/test_qgsrandommarkersymbollayer.py
Expand Up @@ -38,19 +38,8 @@
QgsReadWriteContext,
QgsSymbolLayerUtils,
QgsSimpleMarkerSymbolLayer,
QgsLineSymbolLayer,
QgsTemplatedLineSymbolLayerBase,
QgsMarkerLineSymbolLayer,
QgsSimpleFillSymbolLayer,
QgsMarkerSymbol,
QgsGeometryGeneratorSymbolLayer,
QgsSymbol,
QgsFontMarkerSymbolLayer,
QgsFontUtils,
QgsLineSymbol,
QgsSymbolLayer,
QgsProperty,
QgsRectangle,
QgsUnitTypes,
QgsRandomMarkerFillSymbolLayer
)

Expand Down Expand Up @@ -142,6 +131,30 @@ def testCreate(self):
random_fill = QgsRandomMarkerFillSymbolLayer.create({})
self.assertNotEqual(random_fill.seed(), 0)

def testMultipart(self):
"""
Test rendering a multi-part random marker fill symbol
"""
s = QgsFillSymbol()
s.deleteSymbolLayer(0)

simple_fill = QgsSimpleFillSymbolLayer(color=QColor(255,255,255))
s.appendSymbolLayer(simple_fill.clone())

random_fill = QgsRandomMarkerFillSymbolLayer(12, seed=481523)
marker = QgsSimpleMarkerSymbolLayer(QgsSimpleMarkerSymbolLayer.Triangle, 4)
marker.setColor(QColor(255, 0, 0))
marker.setStrokeStyle(Qt.NoPen)
marker_symbol = QgsMarkerSymbol()
marker_symbol.changeSymbolLayer(0, marker)
random_fill.setSubSymbol(marker_symbol)

s.appendSymbolLayer(random_fill.clone())

g = QgsGeometry.fromWkt('MultiPolygon(((0 0, 5 0, 5 10, 0 10, 0 0),(1 1, 1 9, 4 9, 4 1, 1 1)), ((6 0, 10 0, 10 5, 6 5, 6 0)), ((8 6, 10 6, 10 10, 8 10, 8 6)))')
rendered_image = self.renderGeometry(s, g)
self.assertTrue(self.imageCheck('randommarkerfill_multipoly', 'randommarkerfill_multipoly', rendered_image))

def renderGeometry(self, symbol, geom, buffer=20):
f = QgsFeature()
f.setGeometry(geom)
Expand Down
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 52dba5d

Please sign in to comment.