133 lines
4.4 KiB
Markdown
133 lines
4.4 KiB
Markdown
---
|
||
title: Geometry
|
||
date: 2020-10-10
|
||
author: ac
|
||
tags:
|
||
- OpenLayers
|
||
- Geometry
|
||
categories:
|
||
- GIS
|
||
---
|
||
|
||
## 1 几何对象
|
||
|
||
矢量数据以Geometry抽象类作为基类,派生出点、线、面和多点、多线、多面以及集合类还有线性环等类。`GeometryCollection`则为Geometry对象的集合,可以包含多种几何对象的实例。
|
||
|
||

|
||
|
||
`Geometry`作为基类有一些几何对象常用的方法,如:
|
||
|
||
- `getExtent`获取几何对象的范围
|
||
- `getClosestPoint`获取几何对象点集中最后一个点
|
||
|
||
### 1.1 Coordinate
|
||
|
||
几何对象记录空间数据在空间坐标系下的位置坐标信息来表示矢量数据。
|
||
|
||
`ol`中的坐标信息用一个单独的类`ol.coordinate`来表示,可以直接用数值数组来创建一个`xy`坐标的实例,如:`[113.958334, 22.535640]`。
|
||
|
||
> 如果在`view`中没有指定空间坐标系,默认为`EPSG:3857`Web墨卡托投影坐标系。
|
||
|
||
|
||
|
||
### 1.2 子类实例
|
||
|
||
```javascript
|
||
/**
|
||
* new Point(coordinates, opt_layout)
|
||
* opt_layout是`GeometryLayout`是坐标格式`XY`、`XYZ`、`XYM`、`XYZM`等
|
||
* 常用方法getExtent()
|
||
*/
|
||
var point = new ol.geom.Point([12647823.642380577,2583474.64090162471]);
|
||
|
||
/**
|
||
* new LineString(coordinates, opt_layout)
|
||
* 常用方法:
|
||
* appendCoordinate(coordinate) 添加点
|
||
* getCoordinates() 获取坐标点集
|
||
* getExtent(opt_extent)
|
||
* getLength() 返回在投影坐标下的长度
|
||
*/
|
||
var lineString = new ol.geom.LineString([
|
||
[12679009.949920928,2584697.6333541875],
|
||
[12690322.630107135,2579041.2932610847],
|
||
[12700565.191897348,2585691.314721895]
|
||
]);
|
||
|
||
/**
|
||
* new Polygon(coordinates, opt_layout, opt_ends)
|
||
* Linear ring geometry. Only used as part of polygon; cannot be rendered on its own.
|
||
* LinearRing是一段封闭的分段的线状路径,(coordinates 标签中)至少4个坐标点,三个坐标可以确定 LinearRing,
|
||
* 第四个坐标用于闭合,与第一个坐标相同。
|
||
*
|
||
* Polygon 必须有一个且最多只有一个 Exterior boundary 和 零个或多个 Interior boundary。
|
||
* Interior boundary 之间不能相交或包含。
|
||
*
|
||
* Polygon 构造器中的coordinates是LinearRing数组,第一个LinearRing作为面的外边界,其余的都是面内部的“洞”
|
||
*/
|
||
var polygon = new ol.geom.Polygon([
|
||
[
|
||
[12710578.442602707,2588366.610711876],
|
||
[12719980.197081784,2581487.2781662103],
|
||
[12731369.314296274,2588519.4847684465],
|
||
[12710578.442602707,2588366.610711876]
|
||
],[
|
||
[12716693.40486552,2584926.9444390433],
|
||
[12722808.367128335,2584468.322269332],
|
||
[12719521.574912071,2587755.1144855944],
|
||
[12716693.40486552,2584926.9444390433]
|
||
]
|
||
]);
|
||
|
||
var multiPoint = new ol.geom.MultiPoint([
|
||
[12640332.81360863,2567499.301990023],
|
||
[12640256.376580345,2560925.717557498],
|
||
[12648664.449691713,2564212.50977376]
|
||
]);
|
||
|
||
var multiLineString = new ol.geom.MultiLineString([
|
||
[
|
||
[12676487.527987517,2568875.168499156],
|
||
[12685965.71949488,2565664.8133111787],
|
||
[12696590.46642652,2568263.6722728745]
|
||
],[
|
||
[12670601.87680956,2560619.969444357],
|
||
[12681761.682939194,2565129.7541131824],
|
||
[12692539.303927405,2563983.1986889048]
|
||
]
|
||
]);
|
||
|
||
var multiPolygon = new ol.geom.MultiPolygon([
|
||
[
|
||
[
|
||
[12707903.146612726,2570403.9090648596],
|
||
[12703469.798972186,2561384.339727209],
|
||
[12716616.967837235,2563601.0135474787],
|
||
[12720133.071138354,2570633.220149715],
|
||
[12707903.146612726,2570403.9090648596]
|
||
]
|
||
],[
|
||
[
|
||
[12726859.52962745,2571626.9015174224],
|
||
[12723649.174439471,2566123.43548089],
|
||
[12731827.936465986,2565817.687367749],
|
||
[12738095.77278537,2571091.842319426],
|
||
[12726859.52962745,2571626.9015174224]
|
||
]
|
||
]
|
||
]);
|
||
```
|
||
|
||
<img src="./images/image-20201116134048860.png" alt="image-20201116134048860" style="zoom: 80%;" />
|
||
|
||
|
||
|
||
`ol.geom.LinearRing`是一段封闭的分段的线状路径,(coordinates 标签中)至少4个坐标点,三个坐标可以确定` LinearRing`,第四个坐标用于闭合,与第一个坐标相同。只能作为构建`polygon`要素的一部分,不能单独渲染。
|
||
|
||
注意:
|
||
|
||
构建`Polygon` 必须有一个且最多只有一个 Exterior boundary 和 零个或多个 Interior boundary。Interior boundary 之间不能相交或包含。
|
||
|
||
|
||
|