如何使用JSTS Library计算Google Maps API中的交叉区域? [英] How to calculate intersection area in Google Maps API with JSTS Library?

查看:495
本文介绍了如何使用JSTS Library计算Google Maps API中的交叉区域?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图计算两个三角形之间的交点的面积。
我发现 JSTS Topology Suite 有一个几何类其中一个方法 intersection()
我在 JSFiddle 和我的本地计算机上尝试过它,但是我得到了一个 Uncaught TypeError:undefined不是函数
JSTS还有一个示例。在这里你可以看到代码。
我的代码在JSFiddle中看起来相同:

  var union = triangleCoords.union(secondTriangleCoords); 
var intersection = triangleCoords.intersection(secondTriangleCoords);
console.log('Intersection'+ intersection);
google.maps.geometry.spherical.computeArea(intersection);

是否有转换我也应该做?

解决方案

一般的答案是,您需要将google.maps.Polygon对象转换为jsts.geom.Geometry对象,然后运行联合。如果您想在Google Maps JavaScript API v3地图上显示结果,则需要将返回的jsts.geom.Geometry对象转换回google.maps.Polygon。



此代码(来自此问题)将转换路径转换为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);

jsfiddle



然而,要在结果上使用computeArea方法,您需要将其转换回数组或MVCArray。 maps.LatLng对象( computeArea(path:Array。| MVCArray。,radius ?:数字)



或者您可以使用[jsts.geom.Polygon.getArea]( http://bjornharrtell.github.io/jsts/doc/api/symbols/jsts.geom.Polygon .html#getArea )方法。



工作小提琴与三角形,联合和交叉点的区域使用jsts方法。



将结果转换回google.maps.LatLng和google的代码。 maps.Polyg对象:

  var jsts2googleMaps =函数(几何){
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) );
}
返回GMcoords;
}

var intersectionGMArray = jsts2googleMaps(intersection);

然后获得区域:

  var intersectionGMarea = google.maps.geometry.spherical.computeArea(intersectionGMArray); 

工作小提琴


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?

解决方案

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.

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);

Something like this:

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);

jsfiddle

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))

Or you could use the [jsts.geom.Polygon.getArea] (http://bjornharrtell.github.io/jsts/doc/api/symbols/jsts.geom.Polygon.html#getArea) method.

working fiddle with area of triangles, union and intersection using the jsts method.

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);

Then to get the area:

var intersectionGMarea = google.maps.geometry.spherical.computeArea(intersectionGMArray);

working fiddle

这篇关于如何使用JSTS Library计算Google Maps API中的交叉区域?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆