@@ -593,36 +593,47 @@ namespace pal
593593 polygon->createGeosGeom ();
594594
595595 GEOSContextHandle_t geosctxt = geosContext ();
596-
597596 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-
602597 try
603598 {
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 )
607600 {
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 )
610608 {
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
613613 {
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 ;
616618 }
617619 }
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 ;
619627 }
620628 }
621629 catch ( GEOSException &e )
622630 {
623631 QgsMessageLog::logMessage ( QObject::tr ( " Exception: %1" ).arg ( e.what () ), QObject::tr ( " GEOS" ) );
624632 }
625633
634+ // maintain scaling from 0 -> 12
635+ cost = 12.0 * cost / 13.0 ;
636+
626637 if ( nextPart )
627638 {
628639 cost += nextPart->polygonIntersectionCostForParts ( polygon );
0 commit comments