title: Can turf.js be used to find and repair bad geojson geometry?
body: <p><strong>Methods:</strong> I'm currently using turf.js for geoprocessing as part of a <a href="https://github.com/wboykinm/tribes" rel="noreferrer">broader mapping project</a>; my workflow looks like this:</p>
  
  <ol>
  <li>Pull geojson of census tracts for an entire county</li>
  <li>Pull geojson tiles of water polygons from OSM for the county area</li>
  <li><a href="https://github.com/wboykinm/tribes/blob/master/build_communities.sh#L66-L69" rel="noreferrer">Recursively pull</a> the tracts and each water tile into <a href="https://github.com/wboykinm/tribes/blob/master/processing/water/piranha.js" rel="noreferrer">a node script</a> </li>
  <li>In the script, <a href="https://github.com/wboykinm/tribes/blob/master/processing/water/piranha.js#L21" rel="noreferrer"><code>turf.merge</code> the water tile</a> into a single multipolygon</li>
  <li>In the script, <a href="https://github.com/wboykinm/tribes/blob/master/processing/water/piranha.js#L26" rel="noreferrer">recursively <code>turf.intersect</code> each tract polygon</a> against the water multipolygon</li>
  <li>In the script, <a href="https://github.com/wboykinm/tribes/blob/master/processing/water/piranha.js#L28" rel="noreferrer"><code>turf.erase</code> the water area from the tract polygon</a> if it intersects</li>
  <li>Dump the water-erased tracts to file</li>
  </ol>
  
  <p><strong>Problem:</strong> As my process iterates through each water tile in step 3, it hits one particular tile and begins throwing this error:</p>
  
  <pre><code>/Users/wboykinm/github/tribes/processing/water/node_modules/turf/node_modules/turf-intersect/index.js:45
    if(poly2.type === 'Feature') geom2 = poly2.geometry;
            ^
  TypeError: Cannot read property 'type' of null
      at Object.module.exports [as intersect] (/Users/wboykinm/github/tribes/processing/water/node_modules/turf/node_modules/turf-intersect/index.js:45:11)
  at Object.&lt;anonymous&gt; (/Users/wboykinm/github/tribes/processing/water/piranha.js:26:14)
  at Module._compile (module.js:460:26)
  at Object.Module._extensions..js (module.js:478:10)
  at Module.load (module.js:355:32)
  at Function.Module._load (module.js:310:12)
  at Function.Module.runMain (module.js:501:10)
  at startup (node.js:129:16)
  at node.js:814:3
  </code></pre>
  
  <p>. . . which continues for all subsequent iterations. As soon as the error occurs, the tracts geojson file is malformed and won't load in any viewer.</p>
  
  <ul>
  <li><a href="http://geojson.io/#id=gist:wboykinm/221c2004e12ea2f37fbb28ec84d892e3&amp;map=11/47.5920/-122.2957" rel="noreferrer">This is the input tracts file</a></li>
  <li><a href="http://geojson.io/#id=gist:wboykinm/69dd581e910f42756197b0ed3c763d04&amp;map=13/47.3071/-122.3077" rel="noreferrer">This is the OSM water tile that continually throws the error</a></li>
  </ul>
  
  <p>What's most perplexing is the fact that this error only occurs when I run all the tiles in sequence through the turf script. <em>The script succeeds when I run just the offending tile, on its own.</em> The script also results in malformed geojson if I merge all the water tiles into a single geojson file and <code>turf.erase</code> it directly against the tracts file. An additional complication is that the entire procedure works fine if I combine the water tiles into one file, then take it over to QGIS and manually run a "difference" geoprocess; no errors, valid output geometry.</p>
  
  <p><strong>All the signs are pointing to a problem of invalid geometry, possibly related to the tile boundaries or the way I'm merging the water polygons. How can I find the bad geometry and repair it using the tools at hand (turf.js)?</strong></p>