最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - union of googlemaps polygons with clipper.js - Stack Overflow

programmeradmin2浏览0评论

I'm trying to merge 2 polygons (green) on Google maps (API v3 Javascript) with clipper.js.

before : /

My goal is to have only 1 polygon (red). The problem is that the final polygon is not exactly follow the path and sometime it's even worst.

after : /

to merge the 2 green polygons i used clipper.js and this function ClipperLib.ClipType.ctUnion

var clipType = ClipperLib.ClipType.ctUnion;
function mergePolygon()
  {
      for(j=0;j<array_polygon.length;j++){
          array_polygon_clipper = createarray_clipper_polygon(array_polygon[j]);
          subj_polygons.push(array_polygon_clipper);
      }
      cpr.AddPolygons(subj_polygons, ClipperLib.PolyType.ptSubject);
      var succeeded = cpr.Execute(clipType, solution_polygons);
      return solution_polygons;
  }

How can i solve this problem? Clipper.js is a good answer or there are other libraries to work with googlemaps polygons?

I'm trying to merge 2 polygons (green) on Google maps (API v3 Javascript) with clipper.js.

before : http://jsfiddle/kevdiho/tc53Y/

My goal is to have only 1 polygon (red). The problem is that the final polygon is not exactly follow the path and sometime it's even worst.

after : http://jsfiddle/kevdiho/uF6ec/

to merge the 2 green polygons i used clipper.js and this function ClipperLib.ClipType.ctUnion

var clipType = ClipperLib.ClipType.ctUnion;
function mergePolygon()
  {
      for(j=0;j<array_polygon.length;j++){
          array_polygon_clipper = createarray_clipper_polygon(array_polygon[j]);
          subj_polygons.push(array_polygon_clipper);
      }
      cpr.AddPolygons(subj_polygons, ClipperLib.PolyType.ptSubject);
      var succeeded = cpr.Execute(clipType, solution_polygons);
      return solution_polygons;
  }

How can i solve this problem? Clipper.js is a good answer or there are other libraries to work with googlemaps polygons?

Share Improve this question edited Dec 23, 2015 at 16:23 mkierc 1,1862 gold badges15 silver badges29 bronze badges asked Nov 22, 2013 at 15:03 madiknightmadiknight 853 silver badges5 bronze badges 1
  • This SO question might help: stackoverflow./questions/15052430/… – geocodezip Commented Nov 22, 2013 at 16:05
Add a ment  | 

2 Answers 2

Reset to default 4

Your example could work if you upscale the coordinates before AddPolygons call and downscale them after union operation, eg. by 1000000000000. The scaling is needed to maintain precision, when floats are used, because Clipper uses integers innerly. This has a downside that the operation is slow, because Clipper uses big integer library, if coordinates are upscaled heavily.

To overe the slowness (and precision) issue, there is available a new (though still experimental) "floats" Clipper: http://jsclipper.sourceforge/6.1.3.1_fpoint/clipper.js http://jsclipper.sourceforge/6.1.3.1_fpoint/clipper_unminified.js

Clipper 6 has some things changed, which you have to take into account when migrating from Clipper 5 to 6: https://sourceforge/p/jsclipper/wiki/migration5to6/

I made a change for you: http://jsfiddle/uF6ec/2/

function dummy(){}

Here is a way to make a non-hole union that NAJ asked in the ment to the answer. The image below shows two polygons unioned so that the hole is missing:

FIDDLE: http://jsfiddle/uF6ec/121/

The reason for a hole is different orientations of subject polygons. We have to detect if the area is negative and reverse the orientation:

if (ClipperLib.JS.AreaOfPolygon(array_polygon_clipper) < 0)
{
    array_polygon_clipper.reverse();
}

(You could also make the exact opposite to achieve the same solution, by reversing all polygons whose area is positive.)

And then use pftNonZero filling rule in Execute():

var succeeded = cpr.Execute(ClipperLib.ClipType.ctUnion,
solution_polygons, ClipperLib.PolyFillType.pftNonZero, 
ClipperLib.PolyFillType.pftNonZero);

You can read more about fill rules and ReversePath.

发布评论

评论列表(0)

  1. 暂无评论