Code coverage report for WGS84IntersectUtil/index.js

Statements: 83.87% (52 / 62)      Branches: 58.33% (7 / 12)      Functions: 75% (3 / 4)      Lines: 83.87% (52 / 62)      Ignored: none     

All files » WGS84IntersectUtil/ » index.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101          1 1 1 1 1 1 1 1 1 1 1 1     1   1                     1 3 3 3   3 18 18 18 1             3     1 15 15 15   15   15   15   15   15 60   60 9       15     1 3 3 3 3 3   3 15 15 6 6 6   9 9               3    
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 
var cloneDeep = require('lodash.clonedeep');
var geojsonUtils = require('geojson-utils');
var featurecollection = require('turf-featurecollection');
var pointOnSurface = require('turf-point-on-surface');
var bboxPolygon = require('turf-bbox-polygon');
var linestring = require('turf-linestring');
var intersect = require('turf-intersect');
var explode = require('turf-explode');
var within = require('turf-within');
var extent = require('turf-extent');
var inside = require('turf-inside');
var reportedErrorsMap = {};
 
/** @module wgs84-intersect-util */
var WGS84IntersectUtil = exports;
 
var logError = function(e, typeID) {
   if (!typeID) {
      typeID = "N/A";
   }
   if (!reportedErrorsMap[e.name + typeID]) {
      var errorString = e.name + ": " + "ID: " + typeID + " " + e.message + "\n";
      console.error(errorString);
      reportedErrorsMap[e.name + typeID] = true;
   }
};
 
WGS84IntersectUtil.intersectPolygons = function(searchWithin, polygons) {
   var intersectedPolygons = [];
   var overlap;
   var overlapExtentPoints;
 
   for (var polyIndex = 0; polyIndex < polygons.length; polyIndex++) {
      try {
         overlap = intersect(searchWithin, polygons[polyIndex]);
         if (overlap) {
            intersectedPolygons.push(cloneDeep(polygons[polyIndex]));
         }
      } catch (e) {
         logError(e, polygons[polyIndex].id);
      }
   }
 
   return intersectedPolygons;
};
 
WGS84IntersectUtil.intersectLineBBox = function(line, bbox) {
   var segments = [];
   var intersection;
   var intersectionPoints = [];
   // top
   segments[0] = linestring([[bbox[0], bbox[3]], [bbox[2], bbox[3]]]);
   // bottom
   segments[1] = linestring([[bbox[0], bbox[1]], [bbox[2], bbox[1]]]);
   // left
   segments[2] = linestring([[bbox[0], bbox[1]], [bbox[0], bbox[3]]]);
   // right
   segments[3] = linestring([[bbox[2], bbox[3]], [bbox[2], bbox[1]]]);
 
   for (var c = 0; c < segments.length; c++) {
      intersection = geojsonUtils.lineStringsIntersect(segments[c].geometry, line.geometry);
 
      if (intersection) {
         Array.prototype.push.apply(intersectionPoints, intersection);
      }
   }
 
   return intersectionPoints;
};
 
WGS84IntersectUtil.intersectLines = function(searchWithin, lines) {
   var intersectedLines = [];
   var bbox = extent(searchWithin);
   var intersectionPoints;
   var pointOnLine;
   var line;
 
   for (var lineIndex = 0; lineIndex < lines.length; lineIndex++) {
      intersectionPoints = WGS84IntersectUtil.intersectLineBBox(lines[lineIndex], bbox);
      if (intersectionPoints.length) {
         line = cloneDeep(lines[lineIndex]);
         line.properties.intersectionPoints = intersectionPoints;
         intersectedLines.push(line);
      } else {
         pointOnLine = pointOnSurface(lines[lineIndex]);
         Iif (inside(pointOnLine, bboxPolygon(bbox))) {
            line = cloneDeep(lines[lineIndex]);
            line.properties.intersectionPoints = [pointOnLine.geometry];
            intersectedLines.push(line);
         }
      }
   }
 
   return intersectedLines;
};