Turf: does turf.intersect work for spatially querying more than 2 polygons OR what library OR workaround is recommended?

Created on 8 Feb 2017  路  8Comments  路  Source: Turfjs/turf

I have a GeoJSON of meshed/connected/neighboring polygons and want to onclick of a single polygon, select those polygons that interesect (a.k.a. border) the clicked polygon.

I was thinking of using turf.intersect to do this. However, the docs only describe 2 polygons as being the arguments.

OR I _think_ (if needed as a workaround using TurfJS) I could use turf.buffer buffering the polygon centroid to select the neighboring polygons? turf.buffer

Hoping to get a nudge in the right direction here. I haven't had luck finding my use case in open issues, GIS SE or SOflow. Any recommendations would be super appreciated.

All 8 comments

@shawnmgoulet just for ease, change your filename your Gist with the (.geojson) extension that way GitHub can preview the map.

Well unfortunately TurfJS doesn't have the ability to do large scale intersects like you are describing, however... you can pair Turf intersect with RBush.

  1. Build index with Rbush
  2. Find all polygons and loop using intersect.

This would be very nice to have @turf/intersect do this in Turf.

@DenisCarriere I added the .geojson extension (unfortunately GH returned an error trying to visualize), then removed hyphens in the name, still returning an error.

This is a valid geojson, however. You can visualize it and/or download it directly from NOAA Tool dd - click on the "APIs" dropdown to get the geojson. Showing fine in my web app shown here.

noaa-screen-shot

Yes, it would be, for my needs at least. Thanks @DenisCarriere. I'll work on your described workaround. I don't _need_ to use TurfJS for this, however I haven't seen this functionality baked into any other JS spatial query libs. Closing for now, I would think it would be a useful enhancement if & when.

@DenisCarriere being that this would be my 1st time pairing @turf/intersect + RBush, are there any examples that I might be able to reference/build upon to do what I need to do that you know of?

@shawnmgoulet Well I have a pretty complex example from one of my public repos.

https://github.com/osmottawa/data-research/blob/master/Ottawa-Buildings/align-address.js

The data is not provided in the repo, however you can preview the data here.

What the script does:

  • Selects an address
  • Finds the property parcel inside that address
  • Finds all the buildings inside that parcel
  • Align address with largest building inside parcel

Might be useful to look at the syntax

@DenisCarriere so far as the OSM Ottawa code, please correct me if I'm wrong, but it looks like you're using your geojson-writer geojson-writer package to accomplish the bulk loading of bboxes into retree instead of using rbush.load?

Is this also accomplishing the insertion of bbox + parcel polygon indexes into an array of bboxes or is that unnecessary for your application here?

Apologies for any misunderstandings here, I'm trying to replicate your general workflow against @morganherlocker 's suggested approach described here.

Also, I am pulling my geojson file from a URL, through the execution of the jQuery getJSON method. Can the package work upon the method execution in my case or does it have to be an actual file somewhere within the web application architecture (on the same machine)?

GeoJSON writer is a tool I built to save compact GeoJSON to the file system, if your application is entirely web based, I wouldn't be using that library.

Use that source code as a general guideline, try to extract some of the rbush components of it.

I built that OSM Ottawa code for a very specific one of project, so there's definitely some improvements to do.

Was this page helpful?
0 / 5 - 0 ratings