问题描述
我正在尝试计算两个三角形之间相交的面积.我发现 JSTS Topology Suite 有一个 几何类 其中一个方法intersection().我在 JSFiddle 和我的本地计算机上尝试过,但我收到了 Uncaught TypeError: undefined不是函数
.还有一个来自 JSTS 的 example.在这里你可以看到代码.我的代码在 JSFiddle 中看起来是一样的:
I am trying to calculate the area of the intersection between two triangles. I found out the JSTS Topology Suite has a Geometry class which a method intersection().I tried it on JSFiddle and on my local computer but i am getting an Uncaught TypeError: undefined is not a function
.There is also an example from JSTS. Here you can see the code. My code looks the same in JSFiddle:
var union = triangleCoords.union(secondTriangleCoords);
var intersection = triangleCoords.intersection(secondTriangleCoords);
console.log('Intersection ' + intersection);
google.maps.geometry.spherical.computeArea(intersection);
有没有我也应该做的转换?
Is there a conversion that i should do also?
推荐答案
一般的答案是您需要将 google.maps.Polygon 对象转换为 jsts.geom.Geometry 对象,然后运行并集.如果要在 Google Maps Javascript API v3 地图上显示结果,则需要将返回的 jsts.geom.Geometry 对象转换回 google.maps.Polygon.
The general answer is you need to convert the google.maps.Polygon objects into jsts.geom.Geometry objects, then run the union. If you want to display the result on a Google Maps Javascript API v3 map, then you need to convert the returned jsts.geom.Geometry object back into a google.maps.Polygon.
此代码(来自此问题)将一个进入jstsPolygon的路径:
This code (from this question) converts a path into a jstsPolygon:
var coordinates = googleMaps2JTS(googlePolygonPath);
var geometryFactory = new jsts.geom.GeometryFactory();
var shell = geometryFactory.createLinearRing(coordinates);
var jstsPolygon = geometryFactory.createPolygon(shell);
像这样:
var geometryFactory = new jsts.geom.GeometryFactory();
var trito = bermudaTriangle.getPath();
var tritoCoor = googleMaps2JTS(trito);
var shell = geometryFactory.createLinearRing(tritoCoor);
var trito2 = secondBermuda.getPath();
var tritoCoor2 = googleMaps2JTS(trito2);
var shell2 = geometryFactory.createLinearRing(tritoCoor2);
var jstsPolygon = geometryFactory.createPolygon(shell);
var jstsPolygon2 = geometryFactory.createPolygon(shell2);
var intersection = jstsPolygon.intersection(jstsPolygon2);
要对结果使用 computeArea 方法,您需要将其转换回数组或 google.maps.LatLng 对象的 MVCArray (computeArea(path:Array.|MVCArray., radius?:number))
To use the computeArea method on the result, though, you need to convert it back into an array or an MVCArray of google.maps.LatLng objects (computeArea(path:Array.|MVCArray., radius?:number))
或者你可以使用 [jsts.geom.Polygon.getArea] (http://bjornharrtell.github.io/jsts/doc/api/symbols/jsts.geom.Polygon.html#getArea) 方法.
Or you could use the [jsts.geom.Polygon.getArea] (http://bjornharrtell.github.io/jsts/doc/api/symbols/jsts.geom.Polygon.html#getArea) method.
工作小提琴 使用 jsts 方法处理三角形的面积、并集和交集.
working fiddle with area of triangles, union and intersection using the jsts method.
将结果转换回 google.maps.LatLng 和 google.maps.Polygon 对象的代码:
Code to convert the results back to google.maps.LatLng and google.maps.Polygon objects:
var jsts2googleMaps = function (geometry) {
var coordArray = geometry.getCoordinates();
GMcoords = [];
for (var i = 0; i < coordArray.length; i++) {
GMcoords.push(new google.maps.LatLng(coordArray[i].x, coordArray[i].y));
}
return GMcoords;
}
var intersectionGMArray = jsts2googleMaps(intersection);
然后得到面积:
var intersectionGMarea = google.maps.geometry.spherical.computeArea(intersectionGMArray);
这篇关于如何使用 JSTS 库计算 Google Maps API 中的交叉区域?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!