第二篇记录下Geometry函数,相对于其它语言,Arcade对Geometry的支持是一大亮点,这使得它的上限被大大提高了。
**三、**Geometry函数
1、Angle【角度】
单位为度(0-360),正北为90度,只考虑x-y平面。忽略任何z坐标。
cs
// 返回点和要素之间的角度【两点之间的角度】
var pointA = Point({ "x":976259, "y":8066511, "spatialReference": { "wkid": 3857 } });
Angle(pointA, $feature)
// 三点之间的角度
var pointA = Point({ "x":976259, "y":8066511, "spatialReference": { "wkid": 3857 } });
var pointC = Point({ "x":308654, "y":9005421, "spatialReference": { "wkid": 3857 } });
Angle(pointA, $feature, pointC)
连接的字符串间可以添加符号:
bash
Concatenate([$feature.DLBM,$feature.DLMC,$feature.QSDWMC],"/")
return "0307/其他林地/小村村"
2、Area【面积】
第二个参数是面积单位,可以不输入。
bash
Area($feature, 'square-meters')
**3、**AreaGeodetic【大地测量面积】
盲猜应该就是椭球面积,待验证。
bash
AreaGeodetic($feature, 'square-meters')
4、 Buffer【缓冲区】
和缓冲工具生成的结果差不多。
bash
Buffer($feature, 0.5, 'miles')
5、 BufferGeodetic【Geodetic缓冲区**】**
也是缓冲区,不过是大地测量的,不知道用在什么地方...
bash
BufferGeodetic($feature, 0.5, 'miles')
6、 Centroid【质心点**】**
返回输入几何体的质心。
cs
// 可以返回要素的质心
Centroid($feature)
// 也可以返回环(Geometry)的质心
var ringPoints = Geometry($feature).rings[0];
Centroid(ringPoints);
7、 Clip【裁剪**】**
跟裁剪工具差不多,参数1是输入要素,参数2是裁剪要素。
cs
var envelope = Extent({ ... });
Clip($feature, envelope)
8、 Contains【图形包含**】**
判断一个几何图形是否包含另一个几何图形。Contains返回值有2个情况,一种是布尔值,一种是返回包含在内部的要素集 FeatureSet。
cs
// 参数2是feature的话,返回布尔值
var container = Polygon({ ... });
Contains(containerGeometry, $feature);
// 参数2是features的话,返回features
var parcels = FeatureSetByName($map, 'parcels')
var projectArea = $feature;
Count(Contains(projectArea, parcels));
9、 ConvexHull【凸包**】**
计算几何体的凸包。凸包是包围几何体的最小凸多边形。
cs
var pacman_like_shape = Polygon({
"rings": [[[1, 2], [2, 0], [1, -2], [-1, -2], [-2, -1], [-1, -1.5], [0, -1.5], [-2, 1], [-1, 2]]],
"spatialReference": { "wkid": 3857 }
});
return ConvexHull(pacman_like_shape).rings[0];
// Returns the geometry [[1,2],[2,0],[1,-2],[-1,-2],[-2,-1],[-2,1],[-1,2],[1,2]]
10、 Crosses【相交**】**
判断一个几何图形是否与另一个几何图元相交。
bash
var geom2 = Polygon({ ... });
Crosses($feature, geom2);
11、 Cut【剪切**】**
包括线线剪切,线面剪切等。
cs
var cutter = Polyline({ ... });
Cut($feature, cutter));
12、 DefaultValue【获取值**】**
如果几何体中不存在关键点,或者指定关键点处的值为null或为空文本值,则返回指定的默认值。
找一个要素的Z值的情况:
cs
// 如果有Z值的话返回Z值,没有的话就返回1000
DefaultValue(Geometry($feature), "z", 1000)
如果是找多层的值就麻烦一点,这里找第一个环的第一个点的"z"值,值为100,就返回100。
cs
var shape = Polygon({
rings: [[
Point({ x: -97.06138, y: 32.837, z: 100, hasZ: true, spatialReference: { wkid: 102100 } }),
Point({ x: -97.06133, y: 32.836, z: 50, hasZ: true, spatialReference: { wkid: 102100 } }),
Point({ x: -97.06124, y: 32.834, z: 20, hasZ: true, spatialReference: { wkid: 102100 } }),
Point({ x: -97.06127, y: 32.832, z: 0, hasZ: true, spatialReference: { wkid: 102100 } })
]],
hasZ: true,
spatialReference: { wkid: 102100 }
});
return DefaultValue(shape, ["rings",0,0,"z"], 1000)
如果是去找第3个环,然而不存在这个对象,则返回默认值1000。
cs
var shape = Polygon({
rings: [[
Point({ x: -97.06138, y: 32.837, z: 100, hasZ: true, spatialReference: { wkid: 102100 } }),
Point({ x: -97.06133, y: 32.836, z: 50, hasZ: true, spatialReference: { wkid: 102100 } }),
Point({ x: -97.06124, y: 32.834, z: 20, hasZ: true, spatialReference: { wkid: 102100 } }),
Point({ x: -97.06127, y: 32.832, z: 0, hasZ: true, spatialReference: { wkid: 102100 } })
]],
hasZ: true,
spatialReference: { wkid: 102100 }
});
return DefaultValue(shape, ["rings",2,0,"z"], 1000)
环找到,但是对应的M值没有的情况,一样返回默认值1000。
cs
var shape = Polygon({
rings: [[
Point({ x: -97.06138, y: 32.837, z: 100, hasZ: true, spatialReference: { wkid: 102100 } }),
Point({ x: -97.06133, y: 32.836, z: 50, hasZ: true, spatialReference: { wkid: 102100 } }),
Point({ x: -97.06124, y: 32.834, z: 20, hasZ: true, spatialReference: { wkid: 102100 } }),
Point({ x: -97.06127, y: 32.832, z: 0, hasZ: true, spatialReference: { wkid: 102100 } })
]],
hasZ: true,
spatialReference: { wkid: 102100 }
});
return DefaultValue(shape, ["rings",0,0,"m"], 100)
13、 Densify【加密**】**
通过插入顶点以创建不超过指定间隔的线段来加密几何体。
cs
// 按最大间隔10米来加密线段
var maxLength = 10;
Densify($feature, maxLength, 'meters');
14、 DensifyGeodetic【Geodetic加密**】**
cs
DensifyGeodetic($feature, 10000, 'meters');
15、 Difference【擦除**】**
类似擦除工具。
cs
var subtractor = Polygon({ ... });
Difference($feature, subtractor);
16、 Disjoint【不相交判断**】**
判断2个要素是否不相交,返回布尔值。
bash
var geom2 = Polygon({ ... });
Disjoint($feature, geom2);
17、 Distance【距离**】**
以给定单位返回两个几何图形之间的平面距离。
bash
var geom2 = Point({ ... });
Distance($feature, geom2, 'meters')
18、 DistanceToCoordinate【沿线移动得到的坐标**】**
返回基于给定距离沿输入线的坐标。返回值为Dictionary,里面含有一个coordinate属性,即坐标点。
bash
var result = DistanceToCoordinate($feature, 1038);
return result.coordinate;
19、 EnvelopeIntersects【包络线相交**】**
判断一个几何图形的包络线(或范围)是否与另一个几何图元的包络线相交。。
bash
var geom2 = Polygon({ ... });
EnvelopeIntersects($feature, geom2);
20、 Equals【相等**】**
在给定数据的空间参考和容差的情况下,判断两个几何图形是否相等。
bash
var geom2 = Point({ ... });
Equals($feature, geom2);
21、 Extent【范围**】**
从字典中构造一个【Extent】对象。
bash
Extent({
xMax: -95.34,
xMin: -97.06138,
yMax: 32.837,
yMin: 12.003,
hasM: false,
hasZ: false,
spatialReference: { wkid: 3857 }
});
或者从Geometry构造一个【Extent】对象。
bash
Extent($feature);
或者从序列化的JSON文本构造Extent对象。
bash
var extentJSON = '{"xmin": -109.55, "ymin": 25.76, "xmax": -86.39, "ymax": 49.94, "spatialReference": { "wkid": 3857 }}';
Extent(extentJSON);
22、 Generalize【概化**】**
和编辑里的概化是一样的作用。
例如:删除顶点,使线段距离原始几何体不超过100米
bash
Generalize($feature, 100, true, 'meters')
23、 Geometry【构造Geometry**】**
从要素、序列化的JSON文本或字典构造Geometry对象。
cs
// 从要素
Geometry($feature)
// 从Json文本
var pointJSON = {"x": -118.15, "y": 33.80, "spatialReference": { "wkid": 3857 } };
Geometry(pointJSON);
24、 HasValue【判断是否有值**】**
判断几何图形在给定键或索引处是否具有值。
cs
if( TypeOf(Geometry($feature)) == "Point"){
return HasValue(Geometry($feature), "z")
// returns true
}
if( TypeOf(Geometry($feature)) == "Polygon"){
return HasValue(Geometry($feature), "verticalCoordinateSystem")
// returns false
}
多层查找:
cs
var shape = Polygon({
rings: [[
Point({ x: -97.06138, y: 32.837, z: 100, hasZ: true, spatialReference: { wkid: 102100 } }),
Point({ x: -97.06133, y: 32.836, z: 50, hasZ: true, spatialReference: { wkid: 102100 } }),
Point({ x: -97.06124, y: 32.834, z: 20, hasZ: true, spatialReference: { wkid: 102100 } }),
Point({ x: -97.06127, y: 32.832, z: 0, hasZ: true, spatialReference: { wkid: 102100 } })
]],
hasZ: true,
spatialReference: { wkid: 102100 }
});
if(HasValue(shape, ["rings",0,0,"x"])){
return shape.rings[0][0].x;
// returns -97.06138
}
25、 Intersection【相交**】**
和相交工具一样。
bash
var geom2 = Polygon({ ... });
Area(Intersection($feature, geom2), 'square-miles');
26、 Intersects【相交判断**】**
判断2个要素是否相交,返回布尔值。
bash
var geom2 = Polygon({ ... });
Intersects($feature, geom2);
27、 IsSelfIntersecting【自相交判断**】**
判断Geometry是否存在自相交。
bash
var polyline = Polyline({ ... });
IsSelfIntersecting(polyline);
28、 IsSimple【单部件判断**】**
判断要素是否是单部件,返回布尔值。
bash
IsSimple($feature);
29、 MultiPartToSinglePart【多部件转单部件**】**
和多部件转单部件作用相同。
bash
var allParts = MultiPartToSinglePart($feature)
30、 Multipoint【多点**】**
从点坐标、Json文本构造多点对象。
cs
// 从属性定义构造
Multipoint({
hasM: true,
hasZ: true,
points: [
[-97.06138,32.837,1000,0],
[-97.06133,32.836,1500,15],
[-97.06124,32.834,1000,30],
[-97.06127,32.832,500,50],
[-97.06138,32.837,1000,0]
],
spatialReference: { wkid: 3857 }
});
// 从Json文本构造
var multipointJSON = '{"points": [[-97.06138,32.837],[-97.06133,32.836],[-97.06124,32.834],[-97.06127,32.832]],"spatialReference" : { "wkid": 3857 }}';
Multipoint(multipointJSON);
31、 NearestCoordinate【最近点**】**
返回输入几何体与搜索点最近的坐标(而非顶点)。返回的字典还包括从搜索点到最近坐标的最短平面距离。如果搜索点与输入几何体相交,则搜索点将作为距离为零的结果返回。
cs
var buildings = FeatureSetByPortalItem(
Portal('https://www.arcgis.com'),
'7b1fb95ab77f40bf8aa09c8b59045449',
0,
['*'],
true
);
var nearestBuilding = First(Intersects(buildings, BufferGeodetic($feature, 100, "feet")));
var result = NearestCoordinate(nearestBuilding, $feature);
return result.distance;
// or
return result.coordinate;
32、 Offset【偏移**】**
和编辑里的偏移作用相同。
bash
Offset($feature, 10, 'meters', 'square');
33、 Overlaps【长度**】**
判断一个几何图形是否与另一个几何图元重叠。
bash
var geom2 = Polygon({ ... });
Overlaps($feature, geom2);
34、 Point【构造点**】**
cs
// 从字典构造Point对象
Point({
hasM: true,
hasZ: true,
x: -97.06138,
y: 32.837,
z: 1500,
m: 15,
spatialReference: { wkid: 3857 }
});
// 从JSON文本构造Point对象
var pointJSON = '{ "x": -118.15, "y": 33.80, "spatialReference": { "wkid": 3857 }}';
Point(pointJSON)
35、 PointToCoordinate【点到Geometry的距离**】**
例如:根据单击的位置返回到输入多段线上最近坐标的距离。
bash
var result;
if (TypeOf($userInput) == "Point"){
result = PointToCoordinate(Geometry($feature), $userInput);
}
return result.distanceAlong;
36、 Polygon【构建面**】**
cs
// 从字典构造,有内环的情况
Polygon({
rings: [
[
[-97.06138,32.837],
[-97.06133,32.836],
[-97.06124,32.834],
[-97.06127,32.832],
[-97.06138,32.837]
],
[
[-97.06326,32.759],
[-97.06298,32.755],
[-97.06326,32.759]
]
],
spatialReference: { wkid: 3857 }
});
// 从json构造
var polygonJSON = '{"rings": [[[-97.06138,32.837],[-97.06133,32.836],[-97.06124,32.834],[-97.06127,32.832], [-97.06138,32.837]],[[-97.06326,32.759],[-97.06298,32.755],[-97.06153,32.749], [-97.06326,32.759]]],"spatialReference": { "wkid": 3857 }}';
Polygon(polygonJSON);
37、 Polyline【构造线**】**
cs
// 从字典构造,多部件的线
Polyline({
hasM: true,
hasZ: true,
paths: [
[
[-97.06138,32.837],
[-97.06133,32.836],
[-97.06124,32.834],
[-97.06127,32.832]
],
[
[-97.06326,32.759],
[-97.06298,32.755]
]
],
spatialReference: { wkid: 3857 }
});
// 从json构造
var polylineJSON = '{"paths": [[[-97.06138,32.837],[-97.06133,32.836],[-97.06124,32.834],[-97.06127,32.832]], [[-97.06326,32.759],[-97.06298,32.755]]], "spatialReference": { "wkid": 3857 } }'
Polyline(polylineJSON);
38、 RingIsClockwise【顺时针**】**
判断多边形环中的点是否按顺时针方向排列。
bash
var polygonRings = Geometry($feature).rings;
IIf(RingIsClockwise(polygonRings[0]), 'correct polygon', 'incorrect direction')
39、 Rotate【旋转**】**
按几何体的质心、指定的度数将几何体逆时针旋转。
bash
Rotate($feature, 90)
40、 SetGeometry【设置或替换Geometry**】**
bash
var pointFeature = Feature(Point( ... ), 'name', 'buffer centroid');
var mileBuffer = BufferGeodetic(Geometry(pointFeature), 1, 'mile');
SetGeometry(pointFeature, mileBuffer);
41、 Simplify【简化**】**
对几何图形执行简化操作。
bash
Simplify($feature);
42、 SymmetricDifference【交集取反**】**
和交集取反工具效果相同。
bash
var geom2 = Polygon({ ... });
SymmetricDifference($feature, geom2);
43、 Union【联合**】**
和联合工具效果相同。
bash
var geom2 = Polygon({ ... });
Union([ $feature, geom2 ]);
44、 Within【包含**】**
判断一个几何体是否在另一个几何体内。
bash
var outerGeom = Polygon({ ... });
Within($feature, outerGeom);