Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support curvilinear grids #64

Closed
zachsa opened this issue Sep 14, 2022 · 11 comments
Closed

Support curvilinear grids #64

zachsa opened this issue Sep 14, 2022 · 11 comments

Comments

@zachsa
Copy link

zachsa commented Sep 14, 2022

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
Copy link
Member

Fil commented Sep 14, 2022

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

@zachsa
Copy link
Author

zachsa commented Sep 14, 2022

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
Copy link
Member

Fil commented Sep 14, 2022

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
Copy link
Author

zachsa commented Sep 14, 2022

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
Copy link
Member

Fil commented Sep 14, 2022

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
Copy link
Author

zachsa commented Sep 14, 2022

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
Copy link
Author

zachsa commented Sep 14, 2022

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 Fil closed this as completed Sep 14, 2022
@Fil
Copy link
Member

Fil commented Sep 14, 2022

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
Copy link
Author

zachsa commented Sep 14, 2022

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
Copy link
Member

Fil commented Sep 14, 2022 via email

@zachsa
Copy link
Author

zachsa commented Sep 14, 2022

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants