利用Turf.js實現點線面幾何體的拓撲關係判斷
阿新 • • 發佈:2018-12-21
第一步:先引用js檔案
<script src='https://npmcdn.com/@turf/turf/turf.min.js'></script>
第二步:呼叫函式,判斷關係
booleanClockwise(line) //線是否閉合
var clockwiseRing = turf.lineString([[0,0],[1,1],[1,0],[0,0]]);
var counterClockwiseRing = turf.lineString([[0,0],[1,0],[1,1],[0,0]]);
turf.booleanClockwise(clockwiseRing)
//=true
turf. booleanClockwise(counterClockwiseRing)
//=false
booleanContains(feature1,feature2) //feature2是否完全包含在feature1中
var line = turf.lineString([[1, 1], [1, 2], [1, 3], [1, 4]]);
var point = turf.point([1, 2]);
turf.booleanContains(line, point);
//=true
booleanCrosses(feature1,feature2) //feature1,feature2是否相交
var line1 = turf.lineString([[-2, 2], [4, 2]]);
var line2 = turf.lineString([[1, 1], [1, 2], [1, 3], [1, 4]]);
var cross = turf.booleanCrosses(line1, line2);
//=true
booleanDisjoint(feature1,feature2) //feature1,feature2是否相離
var point = turf.point([2, 2]);
var line = turf.lineString([[1, 1], [1, 2], [ 1, 3], [1, 4]]);
turf.booleanDisjoint(line, point);
//=true
booleanEqual(feature1,feature2) //feature1,feature2是否重合
var pt1 = turf.point([0, 0]);
var pt2 = turf.point([0, 0]);
var pt3 = turf.point([1, 1]);
turf.booleanEqual(pt1, pt2);
//= true
turf.booleanEqual(pt2, pt3);
//= false
booleanOverlap(feature1,feature2) //feature1,feature2是否重疊 ,feature1,feature2型別為Polygon、LineString、 Multipoint、MultiLineString、MultiPolygon,且相同
var poly1 = turf.polygon([[[0,0],[0,5],[5,5],[5,0],[0,0]]]);
var poly2 = turf.polygon([[[1,1],[1,6],[6,6],[6,1],[1,1]]]);
var poly3 = turf.polygon([[[10,10],[10,15],[15,15],[15,10],[10,10]]]);
turf.booleanOverlap(poly1, poly2)
//=true
turf.booleanOverlap(poly2, poly3)
//=false
booleanParallel(line,line) //兩條線是否平行
var line1 = turf.lineString([[0, 0], [0, 1]]);
var line2 = turf.lineString([[1, 0], [1, 1]]);
turf.booleanParallel(line1, line2);
//=true
booleanPointInPolygon(point,polygon) //點是否在面中
var pt = turf.point([-77, 44]);
var poly = turf.polygon([[
[-81, 41],
[-81, 47],
[-72, 47],
[-72, 41],
[-81, 41]
]]);
turf.booleanPointInPolygon(pt, poly);
//= true
booleanPointOnLine(point,line) //點是否在線上
var pt = turf.point([0, 0]);
var line = turf.lineString([[-1, -1],[1, 1],[1.5, 2.2]]);
var isPointOnLine = turf.booleanPointOnLine(pt, line);
//=true
booleanWithin(feature1,feature2) //feature1是否完全在feature2中
var line = turf.lineString([[1, 1], [1, 2], [1, 3], [1, 4]]);
var point = turf.point([1, 2]);
turf.booleanWithin(point, line);
//=true