Google Maps Polygons自相交检测

时间:2014-07-29 14:10:04

标签: javascript google-maps-api-3 polygon shapes self-intersection

我正在尝试从Google Maps API V3多边形中实现多边形自相交算法
目标只是检测是或否,用户绘制的简单多边形是自交叉。

我找到了this very interesting link,但它假定多边形顶点的坐标是以geoJSON格式给出的。但是,这不是我的情况;我只能使用polygon.getPath()将多边形坐标检索到polygoncomplete事件中。

这就是我检索坐标的方法:

google.maps.event.addDomListener(drawingManager, 'polygoncomplete', function(polygon)
{
    var polygonBounds = polygon.getPath();
    var coordinates = [];

    for(var i = 0 ; i < polygonBounds.length ; i++)
    {            
        vertice = {
                      "Latitude" : polygonBounds.getAt(i).lat(),
                      "Longitude" : polygonBounds.getAt(i).lng()
                  }

        coordinates.push(vertice );
    }
}

如何将polygon.getpath()给出的坐标转换为 geoJSON 格式?
有没有更好的方法来检测Google地图多边形是否自相交?如果是这样,请你分享一些代码样本,而不仅仅是数学解释?

PS:我看过this链接,但没有任何代码示例,我有点迷失。

1 个答案:

答案 0 :(得分:5)

您不需要将它们转换为GeoJSON以使用jsts library,您需要将它们从google.maps.LatLng对象转换为jsts.geom.Coordinates。而不是使用它:

var geoJSON2JTS = function(boundaries) {
  var coordinates = [];
  for (var i = 0; i < boundaries.length; i++) {
    coordinates.push(new jsts.geom.Coordinate(
        boundaries[i][1], boundaries[i][0]));
  }
  return coordinates;
};

使用此选项可将google.maps.Polygon路径中的坐标转换为JTS格式:

var googleMaps2JTS = function(boundaries) {
  var coordinates = [];
  for (var i = 0; i < boundaries.getLength(); i++) {
    coordinates.push(new jsts.geom.Coordinate(
        boundaries.getAt(i).lat(), boundaries.getAt(i).lng()));
  }
  return coordinates;
};

然后改变&#34; findSelfIntersects&#34;像这样:

/**
 * findSelfIntersects
 *
 * Detect self-intersections in a polygon.
 *
 * @param {object} google.maps.Polygon path co-ordinates.
 * @return {array} array of points of intersections.
 */
var findSelfIntersects = function(googlePolygonPath) {
  var coordinates = googleMaps2JTS(googlePolygonPath);
  var geometryFactory = new jsts.geom.GeometryFactory();
  var shell = geometryFactory.createLinearRing(coordinates);
  var jstsPolygon = geometryFactory.createPolygon(shell);

  // if the geometry is aleady a simple linear ring, do not
  // try to find self intersection points.
  var validator = new jsts.operation.IsSimpleOp(jstsPolygon);
  if (validator.isSimpleLinearGeometry(jstsPolygon)) {
    return;
  }

  var res = [];
  var graph = new jsts.geomgraph.GeometryGraph(0, jstsPolygon);
  var cat = new jsts.operation.valid.ConsistentAreaTester(graph);
  var r = cat.isNodeConsistentArea();
  if (!r) {
    var pt = cat.getInvalidPoint();
    res.push([pt.x, pt.y]);
  }
  return res;
};

proof of concept fiddle (credit to HoffZ)

代码段

&#13;
&#13;
var mapOptions = {
  zoom: 16,
  center: new google.maps.LatLng(62.1482, 6.0696)
};

var drawingManager = new google.maps.drawing.DrawingManager({
  drawingControl: false,
  polygonOptions: {
    editable: true
  }
});

var googleMaps2JTS = function(boundaries) {
  var coordinates = [];
  for (var i = 0; i < boundaries.getLength(); i++) {
    coordinates.push(new jsts.geom.Coordinate(
      boundaries.getAt(i).lat(), boundaries.getAt(i).lng()));
  }
  coordinates.push(coordinates[0]);
  console.log(coordinates);
  return coordinates;
};

/**
 * findSelfIntersects
 *
 * Detect self-intersections in a polygon.
 *
 * @param {object} google.maps.Polygon path co-ordinates.
 * @return {array} array of points of intersections.
 */
var findSelfIntersects = function(googlePolygonPath) {
  var coordinates = googleMaps2JTS(googlePolygonPath);
  var geometryFactory = new jsts.geom.GeometryFactory();
  var shell = geometryFactory.createLinearRing(coordinates);
  var jstsPolygon = geometryFactory.createPolygon(shell);

  // if the geometry is aleady a simple linear ring, do not
  // try to find self intersection points.
  var validator = new jsts.operation.IsSimpleOp(jstsPolygon);
  if (validator.isSimpleLinearGeometry(jstsPolygon)) {
    return;
  }

  var res = [];
  var graph = new jsts.geomgraph.GeometryGraph(0, jstsPolygon);
  var cat = new jsts.operation.valid.ConsistentAreaTester(graph);
  var r = cat.isNodeConsistentArea();
  if (!r) {
    var pt = cat.getInvalidPoint();
    res.push([pt.x, pt.y]);
  }
  return res;
};


var map = new google.maps.Map(document.getElementById("map"), mapOptions);
drawingManager.setDrawingMode(google.maps.drawing.OverlayType.POLYGON);
drawingManager.setMap(map);
google.maps.event.addListener(drawingManager, 'polygoncomplete', function(polygon) {
  //var polyPath = event.overlay.getPath();
  var intersects = findSelfIntersects(polygon.getPath());
  console.log(intersects);
  if (intersects && intersects.length) {
    alert('Polygon intersects itself');
  } else {
    alert('Polygon does not intersect itself');
  }
});
&#13;
#map {
  width: 500px;
  height: 400px;
}
&#13;
<script src="https://maps.google.com/maps/api/js?libraries=drawing"></script>
<script src="https://cdn.rawgit.com/bjornharrtell/jsts/gh-pages/1.4.0/jsts.min.js"></script>
<p>
  Draw a polygon on the map
</p>

<div id="map">

</div>
&#13;
&#13;
&#13;