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

Suggestion to include a function for cell area #4

Open
ppKrauss opened this issue Jun 25, 2019 · 1 comment
Open

Suggestion to include a function for cell area #4

ppKrauss opened this issue Jun 25, 2019 · 1 comment

Comments

@ppKrauss
Copy link

See also sidewalklabs/s2sphere#38

@ppKrauss
Copy link
Author

ppKrauss commented Dec 27, 2019

Solved (!), the functions developed at osm-codes/s2geom-PoC can be used here:

CREATE or replace FUNCTION s2_token_exact_area(token text, ret_meters boolean DEFAULT false) RETURNS float8 AS $f$
  import s2sphere
  kEarthRadiusMeters =  6371010
  steradians = s2sphere.Cell( s2sphere.CellId.from_token(token) ).exact_area()
  return (steradians*kEarthRadiusMeters*kEarthRadiusMeters) if ret_meters else steradians
$f$ LANGUAGE plpython3u IMMUTABLE;
COMMENT ON function s2_cellid_exact_area 
  IS 'Exact area in steradians of a cell-Token, and approximated (better for small cells) in meters';

CREATE or replace FUNCTION s2_cellid_exact_area(id0 bigint, ret_meters boolean DEFAULT false) RETURNS float8
AS $f$
  import s2sphere
  id = s2sphere.CellId(
       int.from_bytes( id0.to_bytes(8,'big',signed=True), 'big', signed=False )
  )
  kEarthRadiusMeters =  6371010
  steradians = s2sphere.Cell( id ).exact_area()
  return (steradians*kEarthRadiusMeters*kEarthRadiusMeters) if ret_meters else steradians
$f$ LANGUAGE plpython3u IMMUTABLE;
COMMENT ON function s2_cellid_exact_area 
  IS 'Exact area in steradians of a cell-ID, and approximated (better for small cells) in meters';

Another important function, to work with any "geo library" (e.g. Turf) or PostGIS, is s2_cellid_geojson() that returns the geometry of the cell in GeoJSON format.

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

No branches or pull requests

1 participant