[OpenLayers-Users] Vector Feature Label and Geometry.getCentroid()

Pierre Giraud pierre.giraud at camptocamp.com
Fri Jun 24 09:21:33 EDT 2011


You seem to be right. getCentroid isn't correct for the example you're
giving and it has apparently nothing to do with the fact that it's a
"multiple" geometry.
Good catch.

My first investigation showed me that the decimals are the problem. If
I remove them I've something better.
The following code samples can be tested in the spherical-mercator.html example.

Wrong:
var format = new OpenLayers.Format.WKT();
var feature = format.read('MULTIPOLYGON(((-10842755.069337
3645931.096789,-10842790.775979 3645953.496622,-10842740.232706
3645955.23634,-10842755.069337 3645931.096789)))');
var layer = map.getLayersByName('Editable Vectors')[0];
layer.addFeatures([feature]);
map.zoomToExtent(layer.getDataExtent());
layer.addFeatures(new
OpenLayers.Feature.Vector(feature.geometry.getCentroid()));

Correct:
var format = new OpenLayers.Format.WKT();
var feature = format.read('MULTIPOLYGON(((-10842755 3645931,-10842790
3645953,-10842740 3645955,-10842755 3645931)))');
var layer = map.getLayersByName('Editable Vectors')[0];
layer.addFeatures([feature]);
map.zoomToExtent(layer.getDataExtent());
layer.addFeatures(new
OpenLayers.Feature.Vector(feature.geometry.getCentroid()));

Pierre

On Fri, Jun 24, 2011 at 12:16 AM, B. Heath Robinson
<heath at midnighthour.org> wrote:
> I am using a vector layer with text features on Polygon geometries.
>  Relatively often the text label is drawn pretty far off the center of the
> polygon.  My polygon is basically a rectangle with a rounded corner.  Below
> is the WKT of it.  When I do a getCentroid in OpenLayers it is different
> than if I do it in PostGIS.  I think if the label was drawn at the PostGIS
> centroid it would be right.  My site allows measurement, so I also tried
> getting the centroid of a geometry I traced with the rubberband tool.  It
> also returned a Centroid more like I expected.
> Many of the labels also appear to be correct.  Am I missing something?  Does
> it have to do with the geometry being Multipolygon?  Any insight would be
> appreciated.
> Geometry
> MULTIPOLYGON(((-10842755.069337 3645931.096789,-10842790.775979
> 3645953.496622,-10842772.182218 3645984.113569,-10842743.492303
> 3645965.972283,-10842742.28656 3645964.787145,-10842741.293463
> 3645963.414273,-10842740.538336 3645961.898144,-10842740.044376
> 3645960.275967,-10842739.827709 3645958.592826,-10842739.889943
> 3645956.896071,-10842740.232706 3645955.23634,-10842740.843507
> 3645953.655201,-10842755.069337 3645931.096789)))
> OpenLayers Centroid:
>
> id: "OpenLayers.Geometry.Point_492"
> x: -10842792.853788493
> y: 3645967.2444699085
>
> PostGIS Centroid
> "POINT(-10842763.8558177 3645957.49366021)"
>
> Rubberband Tool Geometry:
> POLYGON((-10842772.247212 3645983.9949693,-10842790.759305
> 3645953.5395909,-10842754.929448 3645931.1459303,-10842740.597505
> 3645955.0325016,-10842739.701759 3645957.7197409,-10842739.701759
> 3645961.3027266,-10842740.298923 3645962.7956373,-10842743.284744
> 3645965.7814587,-10842745.374819 3645967.2743694,-10842772.247212
> 3645983.9949693))
> Rubberband Tool Centroid:
>
> id: "OpenLayers.Geometry.Point_689"
> x: -10842779.12432854
> y: 3645962.747894722
>
> _______________________________________________
> Users mailing list
> Users at lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/openlayers-users
>
>



-- 
Pierre GIRAUD
Géomaticien, Analyste

Camptocamp France SAS
Savoie Technolac, BP 352
73377 Le Bourget du Lac, Cedex

Tel : 00 33 4 79 44 44 93
Mail : pierre.giraud at camptocamp.com
http://www.camptocamp.com


More information about the Users mailing list