detectOverlaps static method

Set<List<LatLng>> detectOverlaps(
  1. Polygon mainPolygon,
  2. List<Polygon> sampleSet
)

This method accepts 1 mainPolygon which will be taken as reference to calculate overlaps with the sampleSet

Implementation

/// as reference to calculate overlaps with the [sampleSet]
 static Set<List<LatLng>> detectOverlaps(Polygon mainPolygon, List<Polygon> sampleSet) {
   Set<List<LatLng>> overlaps = {};

   try {

     var mainCoords = <jts.Coordinate>[];
     for(LatLng latLng in mainPolygon.points){
       mainCoords.add(jts.Coordinate(latLng.latitude,latLng.longitude));
     }



     for(Polygon neighbour in sampleSet){

       var neighbourCoords = <jts.Coordinate>[];
       for(LatLng latLng in neighbour.points){
         neighbourCoords.add(jts.Coordinate(latLng.latitude,latLng.longitude));
       }

       RegionPolygon mainPolyRegion = RegionPolygon(regions: [
         mainCoords,
       ]);

       RegionPolygon neighbourPolyRegion = RegionPolygon(regions: [
         neighbourCoords
       ]);

       var mainSeg = PolyBool().segments(mainPolyRegion);
       var neighbourSeg = PolyBool().segments(neighbourPolyRegion);
       var comb = PolyBool().combine(mainSeg, neighbourSeg);
       Map<String,RegionPolygon> result = {
         'difference': PolyBool().polygon(PolyBool().selectDifference(comb)),
         'intersect': PolyBool().polygon(PolyBool().selectIntersect(comb))
       };

       if(result['intersect'] != null && result['intersect']!.regions != null) {
         var latlngs = <LatLng>[];

         if((result['intersect']?.regions?.length ?? -1)>0) {
           for (jts.Coordinate coords in (result['intersect']?.regions?[0] ?? [])) {
             LatLng latLng = LatLng(coords.x, coords.y);
             latlngs.add(latLng);
           }

           overlaps.add(latlngs);

         }
       }
     }
   } catch(e) {
     log("ERROR!!! in detectOverlaps - $e");
   }
   return overlaps;
 }