[OpenLayers-Users] Re: getCentroid method influenced by floating-point math

mca auer at uni-heidelberg.de
Thu Aug 25 07:52:22 EDT 2011


Hi,

I experienced the same problem with similar conditions.
Small Polygons, EPSG:900913, far away from 0,0 in China.
Resulting problem:
Vector Labels are placed by retrieving the Centroid from
OpenLayers.LinearRing.getCentroid(), but they where off about 1km outside
the viewport.

My quick and dirty workaround for labelling was to compute the
bounds.centerLonLat  for the location computation by changing one Line in
Renderer.drawFeature():

drawFeature: function(feature, style) {
        if(style == null) {
            style = feature.style;
        }
        if (feature.geometry) {
            var bounds = feature.geometry.getBounds();
            if(bounds) {
                if (!bounds.intersectsBounds(this.extent)) {
                    style = {display: "none"};
                }
                var rendered = this.drawGeometry(feature.geometry, style,
feature.id);
                if(style.display != "none" && style.label && rendered !==
false) {
//                    var location = feature.geometry.getCentroid();
		      var location = new
OpenLayers.Geometry.Point(feature.geometry.getBounds().getCenterLonLat().lon,feature.geometry.getBounds().getCenterLonLat().lat); 
                    if(style.labelXOffset || style.labelYOffset) {
                        xOffset = isNaN(style.labelXOffset) ? 0 :
style.labelXOffset;
                        yOffset = isNaN(style.labelYOffset) ? 0 :
style.labelYOffset;
                        var res = this.getResolution();
                        location.move(xOffset*res, yOffset*res);
                    }
                    this.drawText(feature.id, style, location);
                } else {
                    this.removeText(feature.id);
                }
                return rendered;
            }
        }
    }

May be a BigDecimal implementation can help in the future?

Best 
mca

--
View this message in context: http://osgeo-org.1803224.n2.nabble.com/getCentroid-method-influenced-by-floating-point-math-tp6346365p6724167.html
Sent from the OpenLayers Users mailing list archive at Nabble.com.


More information about the Users mailing list