欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 房产 > 家装 > 源码分析之Openlayers中MultiPolygon类

源码分析之Openlayers中MultiPolygon类

2025/6/6 23:48:23 来源:https://blog.csdn.net/m0_46281382/article/details/144796687  浏览:    关键词:源码分析之Openlayers中MultiPolygon类

概述

在Openlayers中,MultiPolygon类顾名思义就是表示由多个多边形组成的几何对象,关于Polygon类可以参考这篇文章源码分析之Openlayers中Polygon类;同Polygon类一样,MultiPolygon类继承于SimpleGeometry类。

本文主要介绍MultiPolygon类的源码实现和原理。

源码分析

MultiPolygon类的源码实现

MultiPolygon类的源码实现如下:

class MultiPolygon extends SimpleGeometry {constructor(coordinates, layout, endss) {super();this.endss_ = [];this.flatInteriorPointRevision_ = -1;this.flatInteriorPoints = null;this.maxDelta_ = -1;this.maxDeltaRevision_ = -1;this.orientedRevision_ = -1;this.orientedFlatCoordinates_ = null;if (!endss && !Array.isArray(coordinates[0])) {const polygons = coordinates;const flatCoordinates = [];const thisEndss = [];for (let i = 0, ii = polygons.length; i < ii; ++i) {const polygon = polygons[i];const offset = flatCoordinates.length;const ends = polygon.getEnds();for (let j = 0, jj = ends.length; j < jj; ++j) {ends[j] += offset;}extend(flatCoordinates, polygon.getFlatCoordinates());thisEndss.push(ends);}layout =polygons.length === 0 ? this.getLayout() : polygons[0].getLayout();coordinates = flatCoordinates;endss = thisEndss;}if (layout !== undefined && endss) {this.setFlatCoordinates(layout, coordinates);this.endss_ = endss;} else {this.setCoordinates(coordinates, layout);}}appendPolygon(polygon) {let ends;if (!this.flatCoordinates) {this.flatCoordinates = polygon.getFlatCoordinates().slice();ends = polygon.getEnds().slice();this.endss_.push();} else {const offset = this.flatCoordinates.length;extend(this.flatCoordinates, polygon.getFlatCoordinates());ends = polygon.getEnds().slice();for (let i = 0, ii = ends.length; i < ii; ++i) {ends[i] += offset;}}this.endss_.push(ends);this.changed();}clone() {const len = this.endss_.length;const newEndss = new Array(len);for (let i = 0; i < len; ++i) {newEndss[i] = this.endss_[i].slice();}const multiPolygon = new MultiPolygon(this.flatCoordinates.slice(),this.layout,newEndss);multiPolygon.applyProperties(this);return multiPolygon;}closestPointXY(x, y, closestPoint, minSquaredDistance) {if (minSquaredDistance < closestSquaredDistanceXY(this.getExtent(), x, y)) {return minSquaredDistance;}if (this.maxDeltaRevision_ != this.getRevision()) {this.maxDelta_ = Math.sqrt(multiArrayMaxSquaredDelta(this.flatCoordinates,0,this.endss_,this.stride,0));this.maxDeltaRevision_ = this.getRevision();}return assignClosestMultiArrayPoint(this.getOrientedFlatCoordinates(),0,this.endss_,this.stride,this.maxDelta_,true,x,y,closestPoint,minSquaredDistance);}containsXY(x, y) {return linearRingssContainsXY(this.getOrientedFlatCoordinates(),0,this.endss_,this.stride,x,y);}getArea() {return linearRingssArea(this.getOrientedFlatCoordinates(),0,this.endss_,this.stride);}getCoordinates(right) {let flatCoordinates;if (right !== undefined) {flatCoordinates = this.getOrientedFlatCoordinates().slice();orientLinearRingsArray(flatCoordinates,0,this.endss_,this.stride,right);} else {flatCoordinates = this.flatCoordinates;}return inflateMultiCoordinatesArray(flatCoordinates,0,this.endss_,this.stride);}getEnds() {return this.endss_;}getFlatInteriorPoint() {if (this.flatInteriorPointsRevision_ != this.getRevision()) {const flatCenters = linearRingssCenter(this.flatCoordinates,0,this.endss_,this.stride);this.flatInteriorPoints_ = getInteriorPointsOfMultiArray(this.getOrientedFlatCoordinates(),0,this.endss_,this.stride,flatCenters);this.flatInteriorPointsRevision_ = this.getRevision();}return this.flatInteriorPoints_;}getInteriorPoints() {return new MultiPoint(this.getFlatInteriorPoints().slice(), "XYM");}getOrientedFlatCoordiantes() {if (this.orientedRevision_ != this.getRevision()) {const flatCoordinates = this.flatCoordinates;if (linearRingssAreOriented(flatCoordinates, 0, this.endss_, this.stride)) {this.orientedFlatCoordinates_ = flatCoordinates;} else {this.orientedFlatCoordinates_ = flatCoordinates.slice();this.orientedFlatCoordinates_.length = orientLinearRingsArray(this.orientedFlatCoordinates_,0,this.endss_,this.stride);}this.orientedRevision_ = this.getRevision();}return this.orientedFlatCoordinates_;}getSimplifiedGeometryInternal(squaredTolerance) {const simplifiedFlatCoordinates = [];const simplifiedEndss = [];simplifiedFlatCoordinates.length = quantizeMultiArray(this.flatCoordinates,0,this.endss_,this.stride,Math.sqrt(squaredTolerance),simplifiedFlatCoordinates,0,simplifiedEndss);return new MultiPolygon(simplifiedFlatCoordinates, "XY", simplifiedEndss);}getPolygon(index) {if (index < 0 || this.endss_.length <= index) {return null;}let offset;if (index === 0) {offset = 0;} else {const prevEnds = this.endss_[index - 1];offset = prevEnds[prevEnds.length - 1];}const ends = this.endss_[index].slice();const end = ends[ends.length - 1];if (offset !== 0) {for (let i = 0, ii = ends.length; i < ii; ++i) {ends[i] -= offset;}}return new Polygon(this.flatCoordinates.slice(offset, end),this.layout,ends);}getPolygons() {const layout = this.layout;const flatCoordinates = this.flatCoordinates;const endss = this.endss_;const polygons = [];let offset = 0;for (let i = 0, ii = endss.length; i < ii; ++i) {const ends = endss[i].slice();const end = ends[ends.length - 1];if (offset !== 0) {for (let j = 0, jj = ends.length; j < jj; ++j) {ends[j] -= offset;}}const polygon = new Polygon(flatCoordinates.slice(offset, end),layout,ends);polygons.push(polygon);offset = end;}return polygons;}getType() {return "MultiPolygon";}intersectsExtent(extent) {return intersectsLinearRingMultiArray(this.getOrientedFlatCoordinates(),0,this.endss_,this.stride,extent);}setCoordinates(coordinates, layout) {this.setLayout(layout, coordinates, 3);if (!this.flatCoordinates) {this.flatCoordinates = [];}const endss = deflateMultiCoordinatesArray(this.flatCoordinates,0,coordinates,this.stride,this.endss_);if (endss.length === 0) {this.flatCoordinates.length = 0;} else {const lastEnds = endss[endss.length - 1];this.flatCoordinates.length =lastEnds.length === 0 ? 0 : lastEnds[lastEnds.length - 1];}this.changed();}
}

MultiPolygon类的构造函数

MultiPolygon类构造函数接受三个参数:坐标数据coordinates、坐标布局layoutendss每个多边形结束点数组;在Polygon类的构造函数中用this.ends_存储每个线性环的结束坐标的索引,而在MultiPolygon类中用this.endss_存储每个多边形的结束点新鲜,每个多边形的结束点是一个坐标数组;其余变量如this.flatInteriorPointRevision_等等同Polygon类中一样,都是用于优化几何对象的处理和渲染、比如计算多边形的内部点、顶点排序变化等;MultiPolygon类的构造函数还会判断,若参数endss不存在并且coordinates的第一个值不是数组,即coordinates是一个包含多个多边形对象的数组,则遍历这些多边形,获取其结束点ends并将它们根据当前的偏移调整,然后将多个多边形的坐标扁平化最后赋值给coordinates,将每个多边形的结束点数组存储到this.Endss最后赋值给endss;然后根据坐标布局风格layoutendss来决定是调用this.setFlatCoordiantes还是this.setCoordiantes设置this.endss_this.layoutthis.stridethis.flatCoordinates

MultiPolygon类的主要方法

MultiPolygon类的主要方法如下

  • appendPolygon方法:该方法是向当前几何对象添加一个多边形,接受一个参数polygon多边形;首先会判断,若this.flatCoordinates不存在,则调用polygon.getFlatCoordiantes方法获取参数多边形的坐标赋值给this.flatCoordiantes;并且获取多边形的结束点;若存在,则获取多边形的坐标添加到this.faltCoordiantes中,并且获取多边形坐标的长度,以此来设置该多边形的结束点的偏移值,然后将ends添加到this.endss_的末端,最后调用this.changed方法

  • clone方法:复制当前几何对象,通过this.endss_获取每个多边形的结束点信息,然后实例化MultiPolygon类,调用实例对象的applyProperties方法应用属性,最后返回实例对象。

  • closestPointXY方法:计算给定点(x,y)到当前几何对象的最近距离的平方,以及可能会修改最近点坐标closestPoint和最近距离的平方minSquaredDistance;方法内部同Polygon类中同名函数类似,会基于几何对象发生变化时重新计算this.maxDelta_

  • containsXY方法:判断给定点(x,y)是否在当前几何对象内部或者边界上,内部会逐一判断每个多边形是否包含该点,若包含则返回true;否则判断下一个多边形,若都不包含,则返回false.

  • getArea方法:获取当前几何对象的面积,内部调用的方法是linearRingsArea方法

  • getCoordinates方法:获取几何对象的坐标,内部就是调用inflateMultiCoordinatesArray方法

  • getEnds方法:获取this.endss_的值

  • getFlatInteriorPoints方法:实现原理和Polygon类中的同名函数类似,不过是需要通过this.endss_变量获取每个多边形的坐标,再计算对应多边形的内部点,也就说this.flatInteriorPoints_中保存的是每个多边形的内部点

  • getInteriorPoints方法:获取当前几何对象每个多边形的内部点

  • getOrientedFlatCoordiantes方法:实现原理和Polygon类中的同名函数一样

  • getSimplifiedGeometryInternal方法:获取简化后的几何对象,接受一个参数squaredTolerance容差平方,该值越大,表示要去除的点更多;内部是调用quantizeMultiArray方法进行简化当前几何对象,简化后对象的坐标保存在simplifiedFlatCoordiantes中,最后调用MultiPolygon实例化并返回实例对象

  • getPolygon方法:返回几何对象中索引值对应的多边形,首先会计算参数index是否合法,然后通过indexthis.endss_计算该索引值对应的坐标,然后调用Polygon类实例化一个多边形,最后返回该多边形的实例。

  • getPolygons方法:获取几何对象的多边形,以数组形式返回;通过this.endss_变量计算其中某个多边形的坐标(起止位置),然后调用Polygon进行实例化,将其实例对象保存到数组polygons中最后返回。

  • getType方法:返回当前几何对象的类型,MultiPolygon

  • intersectExtent方法:判断extent是否与当前几何对象相交,内部是调用intersectsLinearRingMultiArray方法

  • setCoordinates方法:内部是调用delatMultiCoordinatesArray方法,设置this.flatCoordinatesthis.layoutthis.stride,最后调用this.changed方法

总结

本文主要介绍了MultiPolygon类的实现原理,MultiPolygon类和Polygon类的实现原理几乎大同小异。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词