@@ -593,36 +593,47 @@ namespace pal
593
593
polygon->createGeosGeom ();
594
594
595
595
GEOSContextHandle_t geosctxt = geosContext ();
596
-
597
596
double cost = 0 ;
598
- // check the label center. if covered by polygon, initial cost of 4
599
- if ( polygon->containsPoint (( x[0 ] + x[2 ] ) / 2.0 , ( y[0 ] + y[2 ] ) / 2.0 ) )
600
- cost += 4 ;
601
-
602
597
try
603
598
{
604
- // calculate proportion of label candidate which is covered by polygon
605
- GEOSGeometry* intersectionGeom = GEOSIntersection_r ( geosctxt, mGeos , polygon->mGeos );
606
- if ( intersectionGeom )
599
+ if ( GEOSPreparedIntersects_r ( geosctxt, polygon->preparedGeom (), mGeos ) == 1 )
607
600
{
608
- double positionArea = 0 ;
609
- if ( GEOSArea_r ( geosctxt, mGeos , &positionArea ) == 1 )
601
+ // at least a partial intersection
602
+ cost += 1 ;
603
+
604
+ double px, py;
605
+
606
+ // check each corner
607
+ for ( int i = 0 ; i < 4 ; ++i )
610
608
{
611
- double intersectionArea = 0 ;
612
- if ( GEOSArea_r ( geosctxt, intersectionGeom, &intersectionArea ) == 1 )
609
+ px = x[i];
610
+ py = y[i];
611
+
612
+ for ( int a = 0 ; a < 2 ; ++a ) // and each middle of segment
613
613
{
614
- double portionCovered = intersectionArea / positionArea;
615
- cost += portionCovered * 8.0 ; // cost of 8 if totally covered
614
+ if ( polygon->containsPoint ( px, py ) )
615
+ cost++;
616
+ px = ( x[i] + x[( i+1 ) %4 ] ) / 2.0 ;
617
+ py = ( y[i] + y[( i+1 ) %4 ] ) / 2.0 ;
616
618
}
617
619
}
618
- GEOSGeom_destroy_r ( geosctxt, intersectionGeom );
620
+
621
+ px = ( x[0 ] + x[2 ] ) / 2.0 ;
622
+ py = ( y[0 ] + y[2 ] ) / 2.0 ;
623
+
624
+ // check the label center. if covered by polygon, cost of 4
625
+ if ( polygon->containsPoint ( px, py ) )
626
+ cost += 4 ;
619
627
}
620
628
}
621
629
catch ( GEOSException &e )
622
630
{
623
631
QgsMessageLog::logMessage ( QObject::tr ( " Exception: %1" ).arg ( e.what () ), QObject::tr ( " GEOS" ) );
624
632
}
625
633
634
+ // maintain scaling from 0 -> 12
635
+ cost = 12.0 * cost / 13.0 ;
636
+
626
637
if ( nextPart )
627
638
{
628
639
cost += nextPart->polygonIntersectionCostForParts ( polygon );
0 commit comments