MCPcopy Create free account
hub / github.com/MapServer/MapServer / msPolygonLabelPoint

Function msPolygonLabelPoint

mapprimitive.c:1277–1506  ·  view source on GitHub ↗

** Find a label point in a polygon. */

Source from the content-addressed store, hash-verified

1275** Find a label point in a polygon.
1276*/
1277int msPolygonLabelPoint(shapeObj *p, pointObj *lp, double min_dimension)
1278{
1279 double slope;
1280 pointObj *point1=NULL, *point2=NULL, cp;
1281 int i, j, nfound;
1282 double x, y, *intersect, temp;
1283 double min, max;
1284 int wrong_order, n;
1285 double len, max_len=0;
1286 double minx, maxx, maxy, miny;
1287
1288 int method = 2;
1289
1290 msComputeBounds(p);
1291 minx = p->bounds.minx;
1292 miny = p->bounds.miny;
1293 maxx = p->bounds.maxx;
1294 maxy = p->bounds.maxy;
1295
1296 if(min_dimension != -1)
1297 if(MS_MIN(maxx-minx,maxy-miny) < min_dimension) return(MS_FAILURE);
1298
1299 cp.x = (maxx+minx)/2.0;
1300 cp.y = (maxy+miny)/2.0;
1301
1302 switch (method) {
1303 case 0: /* MBR */
1304 lp->x = cp.x;
1305 lp->y = cp.y;
1306 break;
1307 case 1: /* centroid */
1308 if(msGetPolygonCentroid(p, lp, &miny, &maxy) != MS_SUCCESS) return(MS_FAILURE);
1309 break;
1310 case 2: /* center of gravity */
1311 if(getPolygonCenterOfGravity(p, lp) != MS_SUCCESS) return(MS_FAILURE);
1312 break;
1313 }
1314
1315 if(msIntersectPointPolygon(lp, p) == MS_TRUE) {
1316 double dist, min_dist=-1;
1317
1318 /* compute a distance to the polygon */
1319 for(j=0;j<p->numlines;j++) {
1320 for(i=1; i<p->line[j].numpoints; i++) {
1321 dist = msSquareDistancePointToSegment(lp, &(p->line[j].point[i-1]), &(p->line[j].point[i]));
1322 if((dist < min_dist) || (min_dist < 0)) min_dist = dist;
1323 }
1324 }
1325 min_dist = sqrt(min_dist);
1326
1327 if(min_dist > .1*MS_MAX(maxx-minx, maxy-miny))
1328 return(MS_SUCCESS); /* point is not too close to the edge */
1329 }
1330
1331 // printf("label: %s\n", p->text);
1332 // printf(" bbox: %g %g %g %g\n",minx, miny, maxx, maxy);
1333 // printf(" center: %g %g\n", cp.x, cp.y);
1334 // printf(" center of gravity: %g %g\n", lp->x, lp->y);

Callers 4

msDrawShapeFunction · 0.85
processShplabelTagFunction · 0.85
findChartPointFunction · 0.85
shapeObj_getLabelPointFunction · 0.85

Calls 5

msComputeBoundsFunction · 0.85
msGetPolygonCentroidFunction · 0.85
msIntersectPointPolygonFunction · 0.85

Tested by

no test coverage detected