title: Calculating the percent overlap of two polygons in JavaScript
body: <p>Here's the problem: I have geoJSON and topoJSON files that give me the polygons for Census block groups and voting precincts. I'm trying to see by how much a given Census block group overlaps with a given precinct.</p>
  
  <p>I've seen a couple examples of what I'm after in other languages—i.e. <a href="https://stackoverflow.com/questions/9977074/how-to-calculate-the-area-of-polygon-overlap-in-r">R</a> and in some GIS tools—but I'm trying to write this as a Node.js script. A few questions:</p>
  
  <ol>
  <li>Is there an NPM module (I've done a fair amount of Googling, but I haven't found one) that can spit out the percent overlap?</li>
  <li>Is there an algorithm, or an exmaple written in another language, that I should know about (I've looked, but I haven't the foggiest where to begin) and that I could port to JavaScript?</li>
  <li>Failing these, can someone explain to me how I <em>would</em> go about thinking about creating an algorithm for this?</li>
  </ol>
  
  <p>In the end, the final product would look something like this—imagining that I have arrays of precincts and blockgroups, and each one is an object with a geometry property that contains the polygon data for the precinct or block group, and also imagining that I have a function called <code>overlap</code> that, when passed two polygons spits out the percent overlap:</p>
  
  <pre><code>// Iterate over each precinct.
  _.each( precincts, function ( precinct ) {
  
      // Iterate over each blockgroup.
      _.each( blockgroups, function ( blockgroup ) {
  
          // Get the overlap for the current precinct and blockgroup.
          var o = overlap( precinct.geometry, blockgroup.geometry );
  
          // If they overlap at all...
          if ( o &gt; 0 ) {
  
              // ...Add information about the overlap to the precinct.
              precinct.overlaps.push({
                  blockgroup: blockgroup.id,
                  overlap: o
              });
  
          }
  
      }
  
  }
  </code></pre>
  
  <p>(I've seen <a href="https://www.npmjs.com/package/turf-overlaps" rel="nofollow noreferrer">this module</a>, but that only gives <em>if</em> the polygons overlap, not by how much they do.)</p>