arcgis jsapi接口入门系列(5):几何(点线面)基本操作

0
分享 2018-10-22

        point: function () {
//通过wkt生成点
//wkt,代表点的坐标
let wkt = "POINT(113.566806 22.22445)";

//wkt转点几何对象
// * @param apiInstance api
// * @param wkt wkt
// * @param sr 空间参考
let point = mapUtil.geometry.wktToPoint(this.apiInstance, wkt, this.mapView.spatialReference);

//通过坐标生成点,坐标格式是arcgis jsapi的标准,详情可看官方文档
let coord = [113.566806, 22.22445];
// * @param apiInstance api
// * @param wkt wkt
// * @param sr 空间参考
point = mapUtil.geometry.coordToPoint(this.apiInstance, coord, this.mapView.spatialReference);

//点转wkt
wkt = mapUtil.geometry.pointToWkt(point);

//获取点坐标
let x = point.x;
let y = point.y;


},
线
        polyline: function () {
//通过wkt生成线
//wkt,代表线的坐标
let wkt = "LINESTRING(113.545949 22.24015749,113.56989 22.24916,113.55324 22.220588)";
// * @param apiInstance api
// * @param wkt wkt
// * @param sr 空间参考
let polyline = mapUtil.geometry.wktToPolyline(this.apiInstance, wkt, this.mapView.spatialReference);

//通过坐标生成线,坐标格式是arcgis jsapi的标准,详情可看官方文档
let coord = [[[113.545949, 22.24015749], [113.56989, 22.24916], [113.55324, 22.220588]]];
// * @param apiInstance api
// * @param coord 坐标对象,格式如:[[[113.545949, 22.24015749], [113.56989, 22.24916], [113.55324, 22.220588]]]
// * @param sr 空间参考
polyline = mapUtil.geometry.coordToPolyline(this.apiInstance, coord, this.mapView.spatialReference);

//线转wkt
wkt = mapUtil.geometry.polylineToWkt(polyline);

//几何对象是否几何正确的检查与修复
//线和面(点不会)存在几何错误的情况,当有几何错误,几何对象的某些操作会出错,jsapi提供检查是否有几何错误的isSimple方法以及解决几何错误的simplify方法
if (this.apiInstance.geometryEngine.isSimple(polyline) === false) {
polyline = this.apiInstance.geometryEngine.simplify(polyline);
}

//遍历线的所有点的坐标
//paths指线的“路径”,一条线可以有多条路径,但我们使用大多数情况都只有一条路径
let path = polyline.paths[0];
//遍历一条路径的所有点坐标
for (let i = 0; i < path.length; i++) {
//获取点坐标
let pointCoord = path[i];
let x = pointCoord[0];
let y = pointCoord[1];

//获取点对象
let pointLine = polyline.getPoint(0, i);
}

//获取线的起点和终点(假设只有一个path)
let pointStart = polyline.getPoint(0, 0);
let pointEnd = polyline.getPoint(0, polyline.paths[0].length - 1);

//获取线长度
//jsapi提供两个获取长度的方法,大意是如果几何对象的坐标系是地理坐标系,例如wgs84,那就用geodesicLength,如果是平面坐标系,就用planarLength
//参数2是计算的长度的单位,可以有这些值:meters | feet | kilometers | miles | nautical-miles | yards
let length1 = this.apiInstance.geometryEngine.geodesicLength(polyline, "meters");
//PS:因为测试数据是wgs84,所以把planarLength屏蔽了,其实不是无效代码
// let length2 = this.apiInstance.geometryEngine.planarLength(polyline, "meters");

//几何对象的范围
let extent = polyline.extent;

},

        polygon: function () {
//通过wkt生成面
let wkt = "POLYGON((113.527839 22.27028,113.527238 22.2557786,113.5437178 22.2597268,113.54423 22.2730306,113.527839 22.27028))";
// * @param apiInstance api
// * @param wkt wkt
// * @param sr 空间参考
let polygon = mapUtil.geometry.wktToPolygon(this.apiInstance, wkt, this.mapView.spatialReference);

//通过坐标生成面,坐标格式是arcgis jsapi的标准,详情可看官方文档
let coord = [[[113.527839, 22.27028], [113.527238, 22.2557786], [113.5437178, 22.2597268], [113.54423, 22.2730306], [113.527839, 22.27028]]];
// * @param apiInstance api
// * @param coord 坐标对象 格式是arcgis jsapi标准,例如:[[[113.527839, 22.27028], [113.527238, 22.2557786], [113.5437178, 22.2597268], [113.54423, 22.2730306], [113.527839, 22.27028]]]
// * @param sr 空间参考
polygon = mapUtil.geometry.coordToPolygon(this.apiInstance, coord, this.mapView.spatialReference);

//面转wkt
wkt = mapUtil.geometry.polygonToWkt(polygon);

//几何对象是否几何正确的检查与修复
//线和面(点不会)存在几何错误的情况,当有几何错误,几何对象的某些操作会出错,jsapi提供检查是否有几何错误的isSimple方法以及解决几何错误的simplify方法
if (this.apiInstance.geometryEngine.isSimple(polygon) === false) {
polygon = this.apiInstance.geometryEngine.simplify(polygon);
}

//遍历面的所有点的坐标
//rings指面的“路径”,一条面对象可以有多个面,但我们使用大多数情况都只有一个面
let ring = polygon.rings[0];
//遍历一个面的所有点坐标
for (let i = 0; i < ring.length; i++) {
//获取点坐标
let pointCoord = ring[i];
let x = pointCoord[0];
let y = pointCoord[1];

//获取点对象
let pointLine = polygon.getPoint(0, i);
}

//获取面的周长
//jsapi提供两个获取长度的方法,大意是如果几何对象的坐标系是地理坐标系,例如wgs84,那就用geodesicLength,如果是平面坐标系,就用planarLength
//参数2是计算的长度的单位,可以有这些值:meters | feet | kilometers | miles | nautical-miles | yards
let length1 = this.apiInstance.geometryEngine.geodesicLength(polygon, "meters");
//PS:因为测试数据是wgs84,所以把planarLength屏蔽了,其实不是无效代码
// let length2 = this.apiInstance.geometryEngine.planarLength(polygon, "meters");

//获取面积也是两个方法,也是geodesicArea和planarArea,情况和获取长度的相同
//参数2是计算的面积的单位,可以有这些值:acres | ares | hectares | square-feet | square-meters | square-yards | square-kilometers | square-miles
//PS:计算面积前要先检查是否有几何错误,目前已知有几何错误会导致面积计算不准确
let area1 = this.apiInstance.geometryEngine.geodesicArea(polygon, "square-meters");
// let area2 = this.apiInstance.geometryEngine.planarArea(polygon, "square-meters");

//获取面是否自相交
let isSelfIntersecting = polygon.isSelfIntersecting;

//获取面的几何中心点
let pointCenter = polygon.centroid;

//几何对象的范围
let extent = polygon.extent;

//把extent转面
//PS:extent是矩形,也属于面(多边形),因此逻辑上可以单向转换
let polygon2 = this.apiInstance.Polygon.fromExtent(extent);
},
extent(范围,矩形,envelope)
        extent: function () {
//先生成任意矩形,作为测试数据
let wkt = "LINESTRING(113.545949 22.24015749,113.56989 22.24916,113.55324 22.220588)";
let polyline = mapUtil.geometry.wktToPolyline(this.apiInstance, wkt, this.mapView.spatialReference);
let extent = polyline.extent;

//矩形4个角的坐标
let xmax = extent.xmax;
let xmin = extent.xmin;
let ymax = extent.ymax;
let ymin = extent.ymin;
//矩形的宽高
let width = extent.width;
let height = extent.height;

//矩形中心点
let centerPoint = extent.center;

let point1 = mapUtil.geometry.xyToPoint(this.apiInstance, 113.545949, 22.24015749, this.mapView.spatialReference);
//把矩形移动到以某个点为中心点的位置
let extent3 = extent.centerAt(point1);

//把矩形按比例缩放,例如值为1.2即放到到原来的1.2倍
extent3 = extent.expand(1.2);

//生成矩形
let extent2 = new this.apiInstance.Extent({
xmin: -9177882,
ymin: 4246761,
xmax: -9176720,
ymax: 4247967,
spatialReference: this.mapView.spatialReference
});


},
几何计算
        geometryCalculate: function () {
//测试的线
let wkt = "LINESTRING(113.545949 22.24015749,113.56989 22.24916,113.55324 22.220588)";
let polyline = mapUtil.geometry.wktToPolyline(this.apiInstance, wkt, this.mapView.spatialReference);

//测试的面
wkt = "POLYGON((113.527839 22.27028,113.527238 22.2557786,113.5437178 22.2597268,113.54423 22.2730306,113.527839 22.27028))";
let polygon = mapUtil.geometry.wktToPolygon(this.apiInstance, wkt, this.mapView.spatialReference);

//以下只包含常用的几何操作,其他操作请看官方文档的geometryEngine类的方法

//生成缓冲区
//参数1:生成缓冲区的几何对象
//参数2:缓冲半径
//参数3:缓冲半径单位
//PS:关于单位。当几何对象坐标系为地理坐标系时,如果单位用平面的坐标系,例如米,会报错,但是单位的选项没有经纬度
//这情况可以传null,官方文档说法是默认值就是几何对象坐标系的单位
let bufferPolygon = this.apiInstance.geometryEngine.buffer(polyline, 100, null);

//判断两个几何对象是否重叠
//intersects关系是只要有任意部分重叠就为true
let isIntersect = this.apiInstance.geometryEngine.intersects(polyline, bufferPolygon);

//获取两个几何对象的重叠部分并输出新的几何对象
let intersectGeometry = this.apiInstance.geometryEngine.intersect(polygon, bufferPolygon);

//计算两个几何对象的距离
//参数3:距离的单位
let distance = this.apiInstance.geometryEngine.distance(polyline, bufferPolygon, null);
},

0 个评论

要回复文章请先登录注册