Class: Geometry

Geometry

new Geometry(options)

base/geometry/Geometry.js, line 9

几何图形对象基类,用于统一几何对象的空间参考系、类型标识与序列化能力。

Name Type Default Description
options Object {} 可选

构造参数

Name Type Default Description
spatialReference SpatialReference new zondy.SpatialReference('EPSG:4326') 可选

几何对象的空间参考系,默认 4326。

extensionOptions Object {} 可选

初始化几何对象的扩展参数,可用于传入底层引擎原生构造参数。

Example

通过 JSON 创建几何对象

// ES5引入方式
const { Geometry } = zondy
const { GeometryType } = zondy.enum

// ES6引入方式
import { Geometry, GeometryType } from "@mapgis/webclient-common"

const geometry = Geometry.fromJSON({
  type: GeometryType.point,
  coordinates: [110, 30]
})

Extends

Members

extensionOptionsObject

初始化几何的额外参数,可以通过该参数传入引擎原生的构造参数

Default Value:
{}

extentNumber

几何的范围

hasZBoolean

是否含有z坐标

spatialReferenceSpatialReference

几何点的空间参考系

几何类型

Methods

clone(){Geometry}

base/geometry/Geometry.js, line 80

克隆几何对象

Returns:
Type Description
Geometry 克隆后的几何对象

inherited fire(type, data, propagate){Object}

base/Evented.js, line 523

激发指定类型的事件

Name Type Description
type string 可选

事件类型

data Object 可选

待传递的数据

propagate Boolean 可选

是否传播到父级

Returns:
Type Description
Object 当前实例

fromGeoJSON(GeoJSON)

base/geometry/Geometry.js, line 144

导入GeoJSON

Name Type Description
GeoJSON Object

Object

getGeometryType(){String}

base/geometry/Geometry.js, line 181

获取GeometryModule型

Returns:
Type Description
String GeometryModule型

getIGSType(){String}

base/geometry/Geometry.js, line 173

返回IGS所对应的GeometryModule型

Returns:
Type Description
String GeometryModule型

getType(){String}

base/geometry/Geometry.js, line 165

返回所对应的GeometryModule型

Returns:
Type Description
String GeometryModule型

inherited off(types, fn, context){Object}

base/Evented.js, line 337
Name Type Description
types string 可选

移除指定事件类型上绑定的回调函数
当类型为字符串时,可以移除单个或多个事件类型绑定的回调函数,单个事件:"click",多个事件:以空格分割:"click double-click";
当types为对象时,使用如下方式移除事件:{'click': onClickFun, 'mouse-move': onMouseMoveFun}

fn function 可选

事件回调函数,当types为字符串,且不指定要删除的回调函数时,删除该事件上的所有回调函数

context Object 可选

事件回调函数的this关键字将指向的对象

Returns:
Type Description
Object 当前实例
Examples

移除一个事件的指定回调函数

// 一个事件的回调函数
const clickFunction = function (event) {
  console.log("点击事件:", event)
}
// 调用MapView或SceneView的off方法移除一个事件的回调函数
view.off('click', clickFunction)

移除一个事件的所有回调函数

// 一个事件的回调函数1
const clickFunction1 = function (event) {
  console.log("点击事件1:", event)
}

// 一个事件的回调函数2
const clickFunction2 = function (event) {
  console.log("点击事件2:", event)
}

// 调用MapView或SceneView的off方法移除一个事件的所有回调函数
// 不指定回调函数,则移除该事件上的所有绑定的回调函数
view.off('click')

移除多个事件的同一个指定的回调函数

// 多个事件的同一个回调函数
const eventFunction = function (event) {
  console.log("事件:", event)
}
// 调用MapView或SceneView的off方法移除多个事件的同一个指定的回调函数
view.off('click double-click', eventFunction)

移除多个指定事件的回调函数

// 一个事件的回调函数
const clickFunction = function (event) {
  console.log("click事件:", event)
}
// 调用MapView或SceneView的off方法移除多个指定事件的回调函数
view.off({
   // 移除click事件上一个指定的函数
  "click": clickFunction,
  // 移除double-click上所有指定的函数
  "double-click": undefined
})

删除时指定上下文 - types类型为字符串

// 一个事件的回调函数
const clickFunction = function (event) {
  console.log("点击事件:", event)
}
// 调用MapView或SceneView的off方法移除一个事件的回调函数
view.off('click', clickFunction, view)
// 调用MapView或SceneView的off方法移除一个事件的所有回调函数
view.off('click', undefined, view)

删除时指定上下文 - types类型为对象

// 一个事件的回调函数
const clickFunction = function (event) {
  console.log("click事件:", event)
}
// 调用MapView或SceneView的off方法移除多个指定事件的回调函数
view.off({
   // 移除click事件上一个指定的函数
  "click": clickFunction,
  // 移除double-click上所有指定的函数
  "double-click": undefined
}, view)

inherited on(types, fn, context){Object}

base/Evented.js, line 240
Name Type Default Description
types String | Object null 可选

事件类型
当types为字符串时,可以定义单个或多个事件,单个事件:"click",多个事件:以空格分割:"click double-click";
当types为对象时,使用如下方式指定事件:{'click': onClickFun, 'mouse-move': onMouseMoveFun}

fn function null 可选

事件回调函数

context Object null 可选

事件回调函数的this关键字将指向的对象

Returns:
Type Description
Object 当前实例
Examples

注册一个事件

// 初始化一个点击事件回调函数
const clickFunction = function (event) {
  console.log("点击事件:", event)
}
// 调用MapView或SceneView的on方法注册一个点击事件
view.on('click', clickFunction)

一次注册多个事件 - 同一个回调函数

// 初始化一个事件回调函数
const eventFunction = function (event) {
  console.log("事件:", event)
}

// 调用MapView或SceneView的on方法注册多个事件
// 多个事件类型使用同一个回调函数
view.on('click right-click-down', eventFunction)

一次注册多个事件 - 分别指回调应函数

// 初始化一个左键点击事件回调函数
const clickFunction = function (event) {
  console.log("click事件:", event)
}

// 初始化一个右键按下事件回调函数
const rightClickFunction = function (event) {
  console.log("right-click-down事件:", event)
}

// 调用MapView或SceneView的on方法注册多个事件
// 每一个事件类型,使用单独的回调函数
// 注意使用此种方式,一种类型的事件仅能指定一个回调函数
view.on({
  "click": clickFunction,
  "right-click-down": rightClickFunction
})

指定上下文 - types类型为字符串

// 初始化一个点击事件回调函数
const clickFunction = function (event) {
  console.log("点击事件:", event)
  console.log("上下文对象:", this)
}
// 调用MapView或SceneView的on方法注册一个点击事件
// 指定view为回调函数的上下文对象
view.on('click', clickFunction, view)

指定上下文 - types类型为对象

// 初始化一个点击事件回调函数
const clickFunction = function (event) {
  console.log("点击事件:", event)
  console.log("上下文对象:", this)
}
// 调用MapView或SceneView的on方法注册一个点击事件
// 指定view为回调函数的上下文对象
view.on({
  "click": clickFunction,
  "right-click-down": clickFunction
}, view)

inherited once(types, fn, context){Object}

base/Evented.js, line 592

事件监听,仅触发一次

Name Type Description
types string 可选

将侦听器函数(fn)添加到对象的特定事件类型中。您还可以传递几个空格分隔的类型(例如“click dblclick”),也可以传入例如 {click: onClick, mousemove: onMouseMove}。

fn function 可选

侦听器函数

context Object 可选

指定侦听器的上下文(this关键字将指向的对象)

Returns:
Type Description
Object 当前实例

toGeoJSON(){Object}

base/geometry/Geometry.js, line 133

导出为GeoJSON

Returns:
Type Description
Object GeoJSON对象

toJSON(){Object}

base/geometry/Geometry.js, line 120

导出为 JSON 对象

Returns:
Type Description
Object JSON对象

toString(){String}

base/geometry/Geometry.js, line 189

导出为字符串,子类实现,这里必须是igs要求的字符串

Returns:
Type Description
String 字符串

toXML(){String}

base/geometry/Geometry.js, line 157

导出为OGC服务要求的xml字符串,子类实现

Returns:
Type Description
String 字符串

Geometry.fromJSON(json){Geometry}

base/geometry/Geometry.js, line 111

通过一个JSON对象创建一个几何对象

Name Type Description
json Object

JSON对象

Returns:
Type Description
Geometry 几何对象
Example

通过一个JSON对象创建一个几何对象

// ES5引入方式
const { Geometry } = zondy
const { GeometryType } = zondy.enum

// ES6引入方式
import { Geometry, GeometryType } from "@mapgis/webclient-common"
// 通过json构造一个几何对象
const polygon = Geometry.fromJSON({
  // 注意必须传入一个几何类型
  type: GeometryType.polygon,
  coordinates: [
      [
        [113, 29],
        [114, 29],
        [114, 30],
        [113, 30],
        [113, 29]
      ]
  ]
})

Geometry.fromRings(pointArr, spatialReference){Array}

base/geometry/Geometry.js, line 348

对入参进行判断处理,构建polygon;
支持的参数类型为闭合的三维点数组(多个ring构成的数组),不区分顺逆时针;
示例如下:
[1、根据入参构建polygon]
[1、根据入参构建polygon,指定rings坐标系]

Name Type Description
pointArr Array

点数组

spatialReference SpatialReference

几何点的空间参考系,默认4326,非必填,当不是4326时请指定坐标系,方便进行投影转换

Returns:
Type Description
Array 构建的polygon组成的对象数组
Examples

点数组构建polygon

// ES5引入方式
const { Geometry} = zondy.geometry

// ES6引入方式
import { Geometry } from "@mapgis/webclient-common"
// 输入三维点数组
const pointArr = [
     [
       [108, 30],
       [108, 33],
       [115, 33],
       [115, 30],
       [108, 30],
     ],
     [
       [110, 31],
       [113, 31],
       [113, 32],
       [110, 32],
       [110, 31],
     ],
     [
       [110, 20],
       [110, 25],
       [115, 25],
       [115, 20],
       [110, 20],
     ],
   ];
// 点数组构建polygon
const fromRings = Geometry.fromRings(pointArr)
console.log("构建的多边形对象:", fromRings)

指定坐标系的点数组构建polygon

// ES5引入方式
const { Geometry,SpatialReference} = zondy.geometry

// ES6引入方式
import { Geometry,SpatialReference } from "@mapgis/webclient-common"
// 输入指定坐标系的点数组
 const pointArr_4547 = [
   [
     [-79450.434008356, 3335318.50110312],
     [-61138.8507993389, 3668785.80194059],
     [593455.16027313, 3653192.25094895],
     [596488.74806744, 3320534.43645214],
     [-79450.434008356, 3335318.50110312],
   ],
   [
     [22192.1178133016, 3441729.02787479],
     [213425.289259696, 3434840.60529103],
     [216463.35881388, 3545788.2250497],
     [27269.5496056064, 3552800.06686639],
     [22192.1178133016, 3441729.02787479],
   ],
   [
     [307007.697900227, 3441729.02787479],
     [500000.000000819, 3441729.02787479],
     [500000.00000081, 3545788.2250497],
     [308973.125268913, 3545788.2250497],
     [307007.697900227, 3441729.02787479],
   ],
   [
     [81149.2212748193, 2217372.5132106],
     [81149.2212748193, 2766426.51455527],
     [600953.408059909, 2766426.51455527],
     [600953.408059909, 2217372.5132106],
     [81149.2212748193, 2217372.5132106],
   ],
 ];

// 创建坐标系
const spatialReference = new SpatialReference({
   wkid: 4547,
 });
// 点数组构建polygon
const fromRings = Geometry.fromRings(pointArr_4547, spatialReference)
console.log("构建的指定坐标系的多边形对象:", fromRings)

Geometry.isClockwise(pointArr){Boolean}

base/geometry/Geometry.js, line 217

对点数组进行是否为顺时针判断;
支持的参数类型为闭合的二维点数组;
示例见下方 @example

Name Type Description
pointArr Array

点数组

Returns:
Type Description
Boolean 是否为顺时针
Example

对点数组进行顺时针判断

// ES5引入方式
const { Geometry } = zondy.geometry

// ES6引入方式
import { Geometry } from "@mapgis/webclient-common"
// 构造线几何对象
const pointArr = [
     [112, 30],
     [114, 30],
     [114, 32],
     [112, 30],
   ];
// 对点数组进行顺时针判断
const isClockwise = Geometry.isClockwise(pointArr)
console.log("是否为顺时针:", isClockwise)

Geometry.reduceDimension(geometry){Geometry}

base/geometry/Geometry.js, line 603

降低几何对象维度(三维降为二维)

Name Type Description
geometry Geometry
Returns:
Type Description
Geometry