geospatialgeoh3# What does the r parameter stand for in the _geoToHex2d function in Uber's H3 geospatial indexing system?

I'm trying to understand uber's H3 geo-indexing system. I understand the _geoToVec3d() function but I am stuck with the _geoToHex2d() function.

Does anyone understand where the equation
`cos(r) = 1 - 2 * sin^2(r/2) = 1 - 2 * (sqd / 4) = 1 - sqd/2`

in the function _geoToHex2d() in uber's H3 source code comes from? What does it describe?
Or maybe someone understands what the parameter *r* in the equation stands for?
I understand that *sqd* is the squared distance from the icosahedron face center to the point to encode. On the next few lines in the code, the 2D hex coordinates of the cell containing the point are set to (0,0) if *r* is smaller than *epsilon*. I'm speculating that r must somehow be the distance from the face center but maybe already in the gnomonoic projected plane?

Solution

I have to admit that this is the area of the H3 library I am least familiar with, so take the following explanation with a grain of salt:

I believe that in the beginning of this function, `r`

is the distance in radians from the point being indexed to the center of the nearest icosahedron face. The line

```
// cos(r) = 1 - 2 * sin^2(r/2) = 1 - 2 * (sqd / 4) = 1 - sqd/2
double r = acos(1 - sqd / 2);
```

converts the squared 3d distance between the two points on a unit sphere to the distance in radians. The comment uses the double angle identity `cos(2𝛼) = 1 − 2sin^2(𝛼)`

to formulate `cos(r)`

in terms of `sqd`

.

**Explanation**

In the above diagram, a cross section of the unit sphere, the distance from `F->C`

is `1`

, and the squared distance from `C->E`

is `sqd`

. We're trying to find the angle `CFE`

.

- The value of
`sin^2(r/2)`

is the same as`sin^2(CFd)`

. Since`sin(CFd) = C->d / 1 = C->d`

, the`sin^2(CFd)`

is the squared distance`C->d`

. - The squared distance from
`C->d`

is`(sqrt(sqd) / 2)^2 = sqd / 4`

- Using the double angle identity, we can calculate
`cos(r) = 1 - 2 * sin^2(r/2)`

. Substituting the squared distance`C->d`

gives us`cos(r) = 1 - 2 * (sqd / 4)`

, which simplifies to`1 - sqd / 2`

. - We can use the inverse trigonometric function
*arccosine*to solve for`r`

as`acos(1 - sqd / 2)`

Apologies for the slightly tortured explanation, as I'm not a mathematician. As you can see, `r`

is now the angle `CFE`

, which is the distance in radians between `C`

and `E`

. Later in the function, this is projected using the gnomonic projection into the planar space of the icosahedron face, and then located in the planar hex grid on that face using 2-dimensional hex coordinates.

- Why does geopandas' `.to_crs()` give (inf, inf) the first time and correct result the second time for the same inputs?
- Converting ggplot to georeferenced pdf results in mirror image
- Converting the Coordinate Reference System (CRS) to Create a Spatial Dataframe using SpatialPoints() in the SP Package
- terra::extract exact=TRUE error TopologyException
- Mismatches resulting from spatial join between two shapefiles
- Given the lat/long coordinates, how can we find out the city/country?
- Determine if Lat/Lng in Bounds
- How to get maps to geopandas after datasets are removed?
- Mismatch SpatialPolygons when left_join with a dataframe in R
- st_intersects in an if statement runs when the if statement is not met
- Using ThinkGeo I need select polygon on mouse click and style it - highlight the border - in Windows Forms C#
- Map vector group in specific order
- Joining Maps Together
- Simulating Geographical Points Around the Eiffel Tower
- How to view the different values in a geopoint to a Data table in Kibana Elasticsearch Maps
- What is the behaviour of xarray when multiplying data arrays?
- Converting Data Frames into Shapefiles
- Is R displaying points twice?
- Point not a valid property - MongoDB & Grails 3.3+
- geopandas map centering with countries
- Issue with setting CRS with make_track() in amt
- Optimize ST_Distance_Sphere query
- Error with nesting GPS data and doing amt analysis
- Show data point names in grouped geodata plotly in R
- Is the Haversine Formula or the Vincenty's Formula better for calculating distance?
- Merging one kml with multiple polygons with another kml with multiple polygons that sometimes overlap
- Total area under multiple overlapping polygons
- Is there python code to parse a geoPDF file to get the projection and image data? a geoPDF2KML tool?
- Converting Cartesian (ECEF) to WGS84 (Lat/Long Decimal) using JavaScript
- How to transform a linestring geometry type to a SpatialPolygon