Opened 17 years ago

Closed 17 years ago

#1979 closed enhancement (fixed)

[PATCH] Exporting mapserver's labelPoint() function to mapscript

Reported by: marcello@… Owned by: dmorissette
Priority: high Milestone:
Component: MapScript-PHP Version: 5.0
Severity: minor Keywords:
Cc:

Description

Hello, while developing a mapscript application in ruby, i needed the
labelPoint() function into my application code: I am generating SVG polygons on
top of a mapserver-generated bitmap, and i need to place labels on these
polygons, and mapserver already implement the scanlines stuff in fast C code,
and i didn't want to reinvent the wheel :).

So, here it goes a small patch that adds a labelPoint() call to the Shape
object, defined in mapscript/swiginc/shape.i:

Maybe this could be useful to some other folk seeking for similar functionality.

Thank you for your software!

Marcello



--- shape.i.orig        2006-12-06 16:31:57.543102371 +0100
+++ shape.i     2006-12-06 16:32:33.498636299 +0100
@@ -172,6 +172,23 @@
         return -1;
     }

+    pointObj *labelPoint()
+    {
+        pointObj *point = (pointObj *)calloc(1, sizeof(pointObj));
+        if (point == NULL) {
+            msSetError(MS_MEMERR, "Failed to allocate memory for point",
"labelPoint()");
+            return NULL;
+        }
+
+        if(self->type == MS_SHAPE_POLYGON &&
+                msPolygonLabelPoint(self, point, -1) == MS_SUCCESS) {
+            return point;
+        }
+
+        free(point);
+        return NULL;
+    }
+
     double distanceToPoint(pointObj *point)
     {
         return msDistancePointToShape(point, self); /* should there be a GEOS
version of this? */

Change History (6)

comment:1 by sdlime, 17 years ago

Status: newassigned
Marcello: That function was intended to be run on clipped and transformed 
shapes. I suppose it could work with raw shapes too. Is that how you're using 
it?

I think one thing needs to be added to the patch. The line before the method 
signature should be:

%newoject labelPoint;

This tells the scripting language that it owns the returned object and it 
should destroy it when it goes out of scope.

Steve

comment:2 by marcello@…, 17 years ago

Hello Steve, thanks for pointing the %newobject stuff out. I'll recompile my
mapserver in order to remove the memory leak :).

Yes, I'm using ShapeObj#labelPoint() to dynamically-allocated raw shapes, whose
points are stored in a PostGIS database.

Code snippet:

def label_point(geometry)
  shape = ShapeObj.new MS_SHAPE_POLYGON
  ring = LineObj.new
  geometry.elements.each do |point|
    ring.add PointObj.new(point.x, point.y)
  end
  shape.add ring
  point = shape.labelPoint

  { :x => point.x, :y => point.y }
end

and it works really well :).

comment:3 by sdlime, 17 years ago

Component: MapScript-SWIGMapScript-PHP
Owner: changed from sdlime to mapserverbugs
Status: assignednew
Version: 4.105.0
I've commited this as getLabelPoint (for consistency since there's also a
getCentroid) in cvs head. The policy has been not to add features like this to
maintenance releases. Thanks for the help!

Changing component to MapScript-PHP...

Steve

comment:4 by dmorissette, 17 years ago

Cc: mapserver-bugs@… added
Owner: changed from mapserverbugs to dmorissette
Taking this bug. Adding shapeObj.getLabelPoint() to PHP MapScript now.

comment:5 by dmorissette, 17 years ago

Status: newassigned

comment:6 by dmorissette, 17 years ago

Resolution: fixed
Status: assignedclosed
Fixed. shapeObj.getLabelPoint() added to PHP MapScript. Example of use in PHP:

<?php

dl("php_mapscript.so");

$shapefile = ms_newShapefileObj("province.shp", -1);

$shape = $shapefile->getShape(1);
print_r($shape);

$labelpoint = $shape->getLabelPoint();
print_r($labelpoint);

?>
Note: See TracTickets for help on using tickets.