In the GIS system often use some cutting method, first recommend a very useful space analysis JavaScript library--turf.js, not only powerful, easy to use, but also fast processing speed.
The clipping method for a polygon in turf.js is to use a polygon to crop the polygon, but it is not sufficient to use the line to cut the polygon in the actual work.
Http://turfjs.org/docs#bboxClip
This article uses the basic method of turf.js, and constructs the method of line clipping polygon on this basis.
Click to view online demo
Demo Preview
Algorithm principle
(i) Cutting of individual polygon
Intersection requirements: lines and polygons have only two intersections, and polygons can be divided into two parts
1. Calculate two intersections of polygons and lines and divide polygons into two lines according to intersections
2, divides the two lines according to the cutting point and the cutting line splicing, respectively composes two polygons,(need to note is the line directionality question)
(b) Clipping of the ring polygon
Intersection requirements: lines and polygons have only two intersection points, and polygons can be divided into two parts, while the cut line must not intersect with the inner ring
Note: The order of the outer polygons in the Geojson data is clockwise and the inner polygon order of the Rings is counterclockwise
1. Split the ring polygon into inner and outer rings
2, the outer ring polygon cut through the cutting line method with (a)
3. The outer ring polygon and the inner ring polygon after the combined cutting:
Determine how to combine restores by judging the inner ring polygon inside the cut polygon
(iii) clipping of Multipolygon polygons
Intersect requirement: The cut line can only have two intersections with one polygon in Multipolygon
1, split Multipolygon split into multiple polygon
2, according to the intersection of cutting line and polygon, the polygon with two intersections is cut
3, the segmented polygon and not participate in the cut polygon merged into a feature set to return
Project Address
Github:https://github.com/fwc1994/clip-polygon
Webgis clipping algorithm-line clipping polygon