Skip to content

Commit

Permalink
Add explicit from_lat_lon and from_lon_lat to GeoPoints in Python
Browse files Browse the repository at this point in the history
  • Loading branch information
abey79 committed Nov 4, 2024
1 parent cd97ad7 commit af9756c
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 7 deletions.
2 changes: 1 addition & 1 deletion docs/snippets/all/archetypes/geo_line_string_simple.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
int main() {
const auto rec = rerun::RecordingStream("rerun_example_geo_line_strings");
rec.spawn().exit_on_failure();

auto line_string = rerun::components::GeoLineString::from_lat_lon(
{{41.0000, -109.0452},
{41.0000, -102.0415},
Expand Down
2 changes: 1 addition & 1 deletion docs/snippets/all/archetypes/geo_point_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

rr.log(
"rerun_hq",
rr.GeoPoints(
rr.GeoPoints.from_lat_lon(
[59.319221, 18.075631],
radii=rr.Radius.ui_points(10.0),
colors=[255, 0, 0],
Expand Down
2 changes: 1 addition & 1 deletion rerun_cpp/src/rerun/archetypes/geo_points_ext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ namespace rerun {
// </CODEGEN_COPY_TO_HEADER>
#endif
} // namespace archetypes
} // namespace rerun
} // namespace rerun
4 changes: 2 additions & 2 deletions rerun_cpp/src/rerun/components/geo_line_string_ext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ namespace rerun {

// </CODEGEN_COPY_TO_HEADER>
#endif
} // namespace archetypes
} // namespace rerun
} // namespace components
} // namespace rerun
5 changes: 3 additions & 2 deletions rerun_py/rerun_sdk/rerun/archetypes/geo_points.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

86 changes: 86 additions & 0 deletions rerun_py/rerun_sdk/rerun/archetypes/geo_points_ext.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
from __future__ import annotations

from typing import TYPE_CHECKING, Sequence

import numpy as np

from .. import datatypes
from .._converters import to_np_float64

if TYPE_CHECKING:
from .. import GeoPoints

NUMPY_VERSION = tuple(map(int, np.version.version.split(".")[:2]))


class GeoPointsExt:
"""Extension for [GeoPoints][rerun.archetypes.GeoPoints]."""

@staticmethod
def from_lat_lon(
positions: datatypes.DVec2DArrayLike,
*,
radii: datatypes.Float32ArrayLike | None = None,
colors: datatypes.Rgba32ArrayLike | None = None,
) -> GeoPoints:
"""
Create a new instance of the GeoPoints archetype using latitudes and longitudes, in that order.
*Note*: this is how Rerun natively stores geospatial data.
Parameters
----------
positions:
The [EPSG:4326](https://epsg.io/4326) latitudes and longitudes (in that order) coordinates for the points (North/East-positive degrees).
radii:
Optional radii for the points, effectively turning them into circles.
colors:
Optional colors for the points.
The colors are interpreted as RGB or RGBA in sRGB gamma-space,
As either 0-1 floats or 0-255 integers, with separate alpha.
"""

from .. import GeoPoints

return GeoPoints(positions, radii=radii, colors=colors)

@staticmethod
def from_lon_lat(
positions: datatypes.DVec2DArrayLike,
*,
radii: datatypes.Float32ArrayLike | None = None,
colors: datatypes.Rgba32ArrayLike | None = None,
) -> GeoPoints:
"""
Create a new instance of the GeoPoints archetype using longitude and latitudes, in that order.
*Note*: Rerun stores latitude first, so this method converts the input to a Numpy array and swaps the
coordinates first.
Parameters
----------
positions:
The [EPSG:4326](https://epsg.io/4326) latitudes and longitudes (in that order) coordinates for the points (North/East-positive degrees).
radii:
Optional radii for the points, effectively turning them into circles.
colors:
Optional colors for the points.
The colors are interpreted as RGB or RGBA in sRGB gamma-space,
As either 0-1 floats or 0-255 integers, with separate alpha.
"""

from .. import GeoPoints
from ..datatypes import DVec2D

if isinstance(positions, Sequence):
flipped_pos = np.array([np.array(p.xy) if isinstance(p, DVec2D) else p for p in positions])
elif isinstance(positions, DVec2D):
flipped_pos = np.array(positions.xy)
else:
flipped_pos = to_np_float64(positions)

return GeoPoints(np.fliplr(flipped_pos), radii=radii, colors=colors)
12 changes: 12 additions & 0 deletions rerun_py/tests/unit/test_geopoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,18 @@ def test_geopoints() -> None:
assert arch.colors == colors_expected(colors)


def test_geopoints_lat_lon_constructors() -> None:
positions_lat_lon = np.array([[59.319221, 18.075631], [50.319221, 12.075631]])
positions_lon_lat = np.fliplr(positions_lat_lon)

arch1 = rr.GeoPoints.from_lat_lon(positions_lat_lon)
arch2 = rr.GeoPoints.from_lon_lat(positions_lon_lat)
arch3 = rr.GeoPoints(positions_lat_lon)

assert arch1.positions == arch2.positions
assert arch2.positions == arch3.positions


@pytest.mark.parametrize(
"data",
[
Expand Down

0 comments on commit af9756c

Please sign in to comment.