开放层以英里为单位计算范围的长度和宽度并反向

问题描述

OpenLayer - 6.4.3

需要计算当前 EXTENT 的 LENGTH 和 WIDTH(以英里为单位)。

然后将 x 英里添加到计算的 LENGTH 和 WIDTH。

然后从新的 LENGTH 和 WIDTH 中获取新的 extent,其中心与初始范围相同。

const extent_initial = this.map.getView().calculateExtent(this.map.getSize());

// get the Length and width in miles from 'extent_inital' 

// add x miles to Length and Width 
// so Length_new = Length + x,Width_new = Width + x
// Calculate new Extent with Length_new and Width_new and extent_inital center


提前致谢

解决方法

您想要的精度取决于您使用的投影以及该投影中范围的大小。

enter image description here 虽然投影时这是一个矩形,但由于地球的形状,顶部(北纬 60 度)的宽度(以英里为单位)仅为底部(赤道)宽度的一半。

因此,这在比例接近恒定的小范围内最有效。计算范围中心以英里为单位的比例尺,然后可以得到以英里为单位的中心线的宽度和高度。

const center = ol.extent.getCenter(extent_initial);

const pointResolutionInMiles = ol.proj.getPointResolution(this.map.getView().getProjection(),1,center,'ft')  / 5280;

const widthInMiles = ol.extent.getWidth(extent_initial) * pointResolutionInMiles;

const heightInMiles = ol.extent.getHeight(extent_initial) * pointResolutionInMiles;

要创建新的范围,您可以缓冲每个方向(顶部、底部、左侧、右侧)的初始范围,以便将宽度和高度增加 x,您需要 x / 2 英里的缓冲区每条边,必须转换回投影单位:

const extent_new = ol.extent.buffer(extent_initial,(x / 2) / pointResolutionInMiles);