Skip to content

Commit 5bde13b

Browse files
author
wonder
committed
Penalize small lines/polygons with additional cost for all candidates of the feature part.
This has the effect that larger objects get higher chance of being labeled. git-svn-id: http://svn.osgeo.org/qgis/branches/symbology-ng-branch@11317 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent fba9efc commit 5bde13b

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

src/core/pal/costcalculator.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,9 @@ namespace pal
188188
setPolygonCandidatesCost( stop, (LabelPosition**) feat->lPos, max_p, obstacles, bbx, bby );
189189
}
190190

191+
// add size penalty (small lines/polygons get higher cost)
192+
feat->feature->addSizePenalty(max_p, feat->lPos, bbx, bby);
193+
191194
return max_p;
192195
}
193196

src/core/pal/feature.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1279,6 +1279,44 @@ void FeaturePart::removeDuplicatePoints()
12791279
return rnbp;
12801280
}
12811281

1282+
void FeaturePart::addSizePenalty( int nbp, LabelPosition** lPos, double bbx[4], double bby[4])
1283+
{
1284+
int geomType = GEOSGeomTypeId(the_geom);
1285+
1286+
double sizeCost = 0;
1287+
if (geomType == GEOS_LINESTRING)
1288+
{
1289+
double length;
1290+
if (GEOSLength(the_geom, &length) != 1)
1291+
return; // failed to calculate length
1292+
double bbox_length = max(bbx[2]-bbx[0], bby[2]-bby[0]);
1293+
if (length >= bbox_length/4)
1294+
return; // the line is longer than quarter of height or width - don't penalize it
1295+
1296+
sizeCost = 1 - (length / (bbox_length/4)); // < 0,1 >
1297+
}
1298+
else if (geomType == GEOS_POLYGON)
1299+
{
1300+
double area;
1301+
if (GEOSArea(the_geom, &area) != 1)
1302+
return;
1303+
double bbox_area = (bbx[2]-bbx[0])*(bby[2]-bby[0]);
1304+
if (area >= bbox_area/16)
1305+
return; // covers more than 1/16 of our view - don't penalize it
1306+
1307+
sizeCost = 1 - (area / (bbox_area/16)); // < 0, 1 >
1308+
}
1309+
else
1310+
return; // no size penalty for points
1311+
1312+
std::cout << "size cost " << sizeCost << std::endl;
1313+
1314+
// apply the penalty
1315+
for (int i = 0; i < nbp; i++)
1316+
{
1317+
lPos[i]->setCost( lPos[i]->getCost() + sizeCost / 100 );
1318+
}
1319+
}
12821320

12831321
bool FeaturePart::isConnected(FeaturePart* p2)
12841322
{

src/core/pal/feature.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,9 @@ namespace pal
266266
/** merge other (connected) part with this one and save the result in this part (other is unchanged).
267267
* Return true on success, false if the feature wasn't modified */
268268
bool mergeWithFeaturePart(FeaturePart* other);
269+
270+
void addSizePenalty( int nbp, LabelPosition** lPos, double bbx[4], double bby[4]);
271+
269272
};
270273

271274
} // end namespace pal

0 commit comments

Comments
 (0)