Methods: I'm currently using turf.js for geoprocessing as part of a broader mapping project; my workflow looks like this:
turf.merge
the water tile into a single multipolygonturf.intersect
each tract polygon against the water multipolygonturf.erase
the water area from the tract polygon if it intersectsProblem: As my process iterates through each water tile in step 3, it hits one particular tile and begins throwing this error:
/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.<anonymous> (/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
. . . 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.
What's most perplexing is the fact that this error only occurs when I run all the tiles in sequence through the turf script. The script succeeds when I run just the offending tile, on its own. The script also results in malformed geojson if I merge all the water tiles into a single geojson file and turf.erase
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.
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)?
Based on the error, it would appear that the issue is that poly2 is not a feature (since it doesn't have a type property). Ignoring that, one way to sometimes fix bad geometries with turf is to do a buffer with 0 for the amount. You can try turf.buffer(badgeometry, 0)
. That has fixed bad geometries for me in the past.
Another thing that you might try is logging the tile that is causing issues before it breaks in the process to see if you can spot anything obviously wrong. You could also try seeing if for some reason the data that you expect to be a feature is either A. a feature collection, or B. an encoded geojson feature.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With