d3 / d3-contour

Compute contour polygons using marching squares.
https://d3js.org/d3-contour
ISC License
494 stars 63 forks source link

Support curvilinear grids #64

Closed zachsa closed 2 years ago

zachsa commented 2 years ago

I have an XY grid, with each cell address mapped to a specific GPS coordinate. After generating contours I see I get polygons with coordinates based on cell positions.

I guess for earth data where pixels are regular and bound by some projection system, I would be able to re-project back to map coordinates, but I can't easily do this with my irregular grid - I think I would need to interpolate every coordinate of the resulting polygon based on closest known real coordinates.

Would it be possible to update the API to allow for specifying coordinates along with a values array that the library then uses to calculate the resulting polygons? (I'm not sure if this makes sense as I don't know how the marching squares algorithm works)

If so, I would be happy to try contribute to this, although I would probably need some guidance!

Fil commented 2 years ago

Have you tried https://github.com/Fil/d3-tricontour instead?

zachsa commented 2 years ago

I have tried it and it works quite well - I just read that marching squares is more efficient and the contouring is slightly better. Also, I'm porting some logic from matplotlib, which uses the marching squares contouring algorithm and does support curvilinear grids.

Comparing d3-tricontour with d3-contour I can see that creating contours with 100 steps takes 450 ms using tricontours vs 110ms with this library. That's a pretty substantial difference on the performance side.

Fil commented 2 years ago

There's no built-in support for curvilinear grids, so I guess it's just a matter of finding an efficient way to compute the projection back to the original coordinates system.

zachsa commented 2 years ago

Okay thanks. Generally speaking, is it possible to calculate polygons using x/y values that correspond to value positions instead of numeric grid positions? (or in the case of matplotlib is the library just interpolating the result)

Fil commented 2 years ago

I've made this notebook with a projection that uses bilinear interpolation on the grid: https://observablehq.com/@recifs/contours-on-a-curvilinear-grid

If you want to share a dataset I'd be happy to try it.

zachsa commented 2 years ago

My grid looks like this:

image

You can download data from this endpoint:

curl 'https://somisana.ac.za/graphql' -H 'Accept-Encoding: gzip, deflate, br' -H 'Content-Type: application/json' -H 'Accept: application/json' -H 'Connection: keep-alive' -H 'DNT: 1' -H 'Origin: https://somisana.ac.za' --data-binary '{"query":"{\n  data(timeStep:120 runId:8 depth:0) {\n    id,\n    json\n  }\n}"}' --compressed > data-issue-64.json

The endpoint may disappear/change/etc but if it's helpful you are welcome to use the image and data in an example. The format of the data is:

{
  data: {
    {
      id: "...",
      json: [
        [lng, lat, temperature],
        [lng, lat, temperature],
        [lng, lat, temperature],
        etc
      ]
    }
  }
}

(It's sea-surface temperature as output from a run of the Coastal and Regional Ocean Community model)

zachsa commented 2 years ago

Thank you very much for the notebook example - I will have a chance to try it out either tomorrow or the week after next.

Fil commented 2 years ago

See https://observablehq.com/@recifs/contours-on-a-curvilinear-grid-geo for a geo version using your dataset. Remains to link this to a mapbox view, I guess. geo contours

zachsa commented 2 years ago

Wow - that's really nice!

Currently I'm plotting the geojon directly onto the maplibre view (https://somisana.ac.za/explore/1). I hadn't considered using d3 plots on a mapview before. This plot currently uses the tricontour library. It's actually pretty close in terms of contouring:

image

The dataset is actually a cumulative effort of a number of different people including research funded by SAEON. Would you mind changing the description to reflect that? I'd like to get feedback from others involved in this before confirming, but would something along these lines be suitable?

============

Sea-surface temperature as output from a run of the Coastal and Regional Ocean Community model, configured by SAEON, for the Algoa Bay region.

Data made available by Zach Smith on behalf of the South African Sustainable Ocean Modelling Initiative; see this notebook for generated data.

============

Fil commented 2 years ago

Sure! Thanks

Message ID: @.***>

zachsa commented 2 years ago

I've confirmed with others that the blurb mentioned above is fine. Please can you update the notebook when you get a chance.

Thanks very much for your help.