第二篇記錄下Geometry函數,相對于其它語言,Arcade對Geometry的支持是一大亮點,這使得它的上限被大大提高了。
三、Geometry函數
1、Angle【角度】
單位為度(0-360),正北為90度,只考慮x-y平面。忽略任何z坐標。
// 返回點和要素之間的角度【兩點之間的角度】
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)
連接的字符串間可以添加符號:
Concatenate([$feature.DLBM,$feature.DLMC,$feature.QSDWMC],"/")
return "0307/其他林地/小村村"
2、Area【面積】
第二個參數是面積單位,可以不輸入。
Area($feature, 'square-meters')
3、AreaGeodetic【大地測量面積】
盲猜應該就是橢球面積,待驗證。
AreaGeodetic($feature, 'square-meters')
4、Buffer【緩沖區】
和緩沖工具生成的結果差不多。
Buffer($feature, 0.5, 'miles')
5、BufferGeodetic【Geodetic緩沖區】
也是緩沖區,不過是大地測量的,不知道用在什么地方...
BufferGeodetic($feature, 0.5, 'miles')
6、Centroid【質心點】
返回輸入幾何體的質心。
// 可以返回要素的質心
Centroid($feature)
// 也可以返回環(Geometry)的質心
var ringPoints = Geometry($feature).rings[0];
Centroid(ringPoints);
7、Clip【裁剪】
跟裁剪工具差不多,參數1是輸入要素,參數2是裁剪要素。
var envelope = Extent({ ... });
Clip($feature, envelope)
8、Contains【圖形包含】
判斷一個幾何圖形是否包含另一個幾何圖形。Contains返回值有2個情況,一種是布爾值,一種是返回包含在內部的要素集?FeatureSet。
// 參數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【凸包】
計算幾何體的凸包。凸包是包圍幾何體的最小凸多邊形。
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【相交】
判斷一個幾何圖形是否與另一個幾何圖元相交。
var geom2 = Polygon({ ... });
Crosses($feature, geom2);
11、Cut【剪切】
包括線線剪切,線面剪切等。
var cutter = Polyline({ ... });
Cut($feature, cutter));
12、DefaultValue【獲取值】
如果幾何體中不存在關鍵點,或者指定關鍵點處的值為null或為空文本值,則返回指定的默認值。
找一個要素的Z值的情況:
// 如果有Z值的話返回Z值,沒有的話就返回1000
DefaultValue(Geometry($feature), "z", 1000)
如果是找多層的值就麻煩一點,這里找第一個環的第一個點的“z”值,值為100,就返回100。
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。
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。
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【加密】
通過插入頂點以創建不超過指定間隔的線段來加密幾何體。
// 按最大間隔10米來加密線段
var maxLength = 10;
Densify($feature, maxLength, 'meters');
14、DensifyGeodetic【Geodetic加密】
DensifyGeodetic($feature, 10000, 'meters');
15、Difference【擦除】
類似擦除工具。
var subtractor = Polygon({ ... });
Difference($feature, subtractor);
16、Disjoint【不相交判斷】
判斷2個要素是否不相交,返回布爾值。
var geom2 = Polygon({ ... });
Disjoint($feature, geom2);
17、Distance【距離】
以給定單位返回兩個幾何圖形之間的平面距離。
var geom2 = Point({ ... });
Distance($feature, geom2, 'meters')
18、DistanceToCoordinate【沿線移動得到的坐標】
返回基于給定距離沿輸入線的坐標。返回值為Dictionary,里面含有一個coordinate屬性,即坐標點。
var result = DistanceToCoordinate($feature, 1038);
return result.coordinate;
19、EnvelopeIntersects【包絡線相交】
判斷一個幾何圖形的包絡線(或范圍)是否與另一個幾何圖元的包絡線相交。。
var geom2 = Polygon({ ... });
EnvelopeIntersects($feature, geom2);
20、Equals【相等】
在給定數據的空間參考和容差的情況下,判斷兩個幾何圖形是否相等。
var geom2 = Point({ ... });
Equals($feature, geom2);
21、Extent【范圍】
從字典中構造一個【Extent】對象。
Extent({xMax: -95.34,xMin: -97.06138,yMax: 32.837,yMin: 12.003,hasM: false,hasZ: false,spatialReference: { wkid: 3857 }
});
?或者從Geometry構造一個【Extent】對象。
Extent($feature);
或者從序列化的JSON文本構造Extent對象。?
var extentJSON = '{"xmin": -109.55, "ymin": 25.76, "xmax": -86.39, "ymax": 49.94, "spatialReference": { "wkid": 3857 }}';
Extent(extentJSON);
22、Generalize【概化】
和編輯里的概化是一樣的作用。
例如:刪除頂點,使線段距離原始幾何體不超過100米
Generalize($feature, 100, true, 'meters')
23、Geometry【構造Geometry】
從要素、序列化的JSON文本或字典構造Geometry對象。
// 從要素
Geometry($feature)
// 從Json文本
var pointJSON = {"x": -118.15, "y": 33.80, "spatialReference": { "wkid": 3857 } };
Geometry(pointJSON);
24、HasValue【判斷是否有值】
判斷幾何圖形在給定鍵或索引處是否具有值。
if( TypeOf(Geometry($feature)) == "Point"){return HasValue(Geometry($feature), "z")// returns true
}if( TypeOf(Geometry($feature)) == "Polygon"){return HasValue(Geometry($feature), "verticalCoordinateSystem")// returns false
}
多層查找:
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【相交】
和相交工具一樣。
var geom2 = Polygon({ ... });
Area(Intersection($feature, geom2), 'square-miles');
26、Intersects【相交判斷】
判斷2個要素是否相交,返回布爾值。
var geom2 = Polygon({ ... });
Intersects($feature, geom2);
27、IsSelfIntersecting【自相交判斷】
判斷Geometry是否存在自相交。
var polyline = Polyline({ ... });
IsSelfIntersecting(polyline);
28、IsSimple【單部件判斷】
判斷要素是否是單部件,返回布爾值。
IsSimple($feature);
29、MultiPartToSinglePart【多部件轉單部件】
和多部件轉單部件作用相同。
var allParts = MultiPartToSinglePart($feature)
30、Multipoint【多點】
從點坐標、Json文本構造多點對象。
// 從屬性定義構造
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【最近點】
返回輸入幾何體與搜索點最近的坐標(而非頂點)。返回的字典還包括從搜索點到最近坐標的最短平面距離。如果搜索點與輸入幾何體相交,則搜索點將作為距離為零的結果返回。
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【偏移】
和編輯里的偏移作用相同。
Offset($feature, 10, 'meters', 'square');
33、Overlaps【長度】
判斷一個幾何圖形是否與另一個幾何圖元重疊。
var geom2 = Polygon({ ... });
Overlaps($feature, geom2);
34、Point【構造點】
// 從字典構造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的距離】
例如:根據單擊的位置返回到輸入多段線上最近坐標的距離。
var result;if (TypeOf($userInput) == "Point"){result = PointToCoordinate(Geometry($feature), $userInput);
}return result.distanceAlong;
36、Polygon【構建面】
// 從字典構造,有內環的情況
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【構造線】
// 從字典構造,多部件的線
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【順時針】
判斷多邊形環中的點是否按順時針方向排列。
var polygonRings = Geometry($feature).rings;
IIf(RingIsClockwise(polygonRings[0]), 'correct polygon', 'incorrect direction')
39、Rotate【旋轉】
按幾何體的質心、指定的度數將幾何體逆時針旋轉。
Rotate($feature, 90)
40、SetGeometry【設置或替換Geometry】
var pointFeature = Feature(Point( ... ), 'name', 'buffer centroid');
var mileBuffer = BufferGeodetic(Geometry(pointFeature), 1, 'mile');
SetGeometry(pointFeature, mileBuffer);
41、Simplify【簡化】
對幾何圖形執行簡化操作。
Simplify($feature);
42、SymmetricDifference【交集取反】
和交集取反工具效果相同。
var geom2 = Polygon({ ... });
SymmetricDifference($feature, geom2);
43、Union【聯合】
和聯合工具效果相同。
var geom2 = Polygon({ ... });
Union([ $feature, geom2 ]);
44、Within【包含】
判斷一個幾何體是否在另一個幾何體內。
var outerGeom = Polygon({ ... });
Within($feature, outerGeom);