[OpenLayers-Dev] pixels not floats

Christopher Schmidt crschmidt at metacarta.com
Sun Nov 2 06:55:28 EST 2008


On Sun, Nov 02, 2008 at 06:06:52AM -0500, John R. Frank wrote:
> Is there a reason that getPixelFromLonLat is where pixels get turned into 
> integers instead of floats?

Yes.

> in Map.js
>      getPixelFromLonLat: function (lonlat) {
>          var px = this.getViewPortPxFromLonLat(lonlat);
>          px.x = Math.round(px.x);
>          px.y = Math.round(px.y);
>          return px;
>      },
> 
>      getViewPortPxFromLonLat: function (lonlat) {
>          var px = null;
>          if (this.baseLayer != null) {
>              px = this.baseLayer.getViewPortPxFromLonLat(lonlat);
>          }
>          return px;
>      },
> 
> 
> Shouldn't the rounding be happening here in Layer.js?

No.

> in Layers.js
>      getViewPortPxFromLonLat: function (lonlat) {
>          var px = null;
>          if (lonlat != null) {
>              var resolution = this.map.getResolution();
>              var extent = this.map.getExtent();
>              px = new OpenLayers.Pixel(
>                  (1/resolution * (lonlat.lon - extent.left)),
>                  (1/resolution * (extent.top - lonlat.lat))
>              );
>          }
>          return px;
>      },
> 
> 
> 
> 
> and corresponding changes to getPixelFromLonLat?  Or is there some reason 
> to have pixel values that are secretly floats...?

Yes.

We use getPixelfromLonLat extensively in our building of a grid based on
bounding boxes. Rounding these values to ints early in the process would
cost us precision that we need in order to determine accurate alignments
later in the process. Specifically, this change was added in r6831 to
"fix panning with odd-sized viewports" -- because calculations
internally use a more accurate (non-rounded) pixel value, the dragging
no longer 'loses' distance by rounding the wrong direction as it did in
the past with non-even sized viewports.

The function is explicitly documented to return fractional units for
this reason. 

Regards,
-- 
Christopher Schmidt
MetaCarta



More information about the Dev mailing list