Skip to content

Commit

Permalink
Allow geobox to be constructed from 4 coordinates (#3341)
Browse files Browse the repository at this point in the history
  • Loading branch information
nirinchev authored Jun 6, 2023
1 parent 995f64b commit c60ec99
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 21 deletions.
49 changes: 39 additions & 10 deletions Realm/Realm/DatabaseTypes/Geospatial/GeoBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,28 @@ namespace Realms
public class GeoBox : GeoShapeBase
{
/// <summary>
/// Gets the bottom left corner of the rectangle.
/// Gets the longitude of the left edge of the rectangle.
/// </summary>
/// <value>The box's bottom left corner.</value>
public GeoPoint BottomLeftCorner { get; }
/// <value>The box's left edge.</value>
public double Left { get; }

/// <summary>
/// Gets the top right corner of the rectangle.
/// Gets the latitude of the top edge of the rectangle.
/// </summary>
/// <value>The box's top right corner.</value>
public GeoPoint TopRightCorner { get; }
/// <value>The box's top edge.</value>
public double Top { get; }

/// <summary>
/// Gets the longitude of the right edge of the rectangle.
/// </summary>
/// <value>The box's right edge.</value>
public double Right { get; }

/// <summary>
/// Gets the latitude of the bottom edge of the rectangle.
/// </summary>
/// <value>The box's bottom edge.</value>
public double Bottom { get; }

/// <summary>
/// Initializes a new instance of the <see cref="GeoBox"/> class from the provided coordinates.
Expand All @@ -44,13 +56,30 @@ public class GeoBox : GeoShapeBase
/// <param name="topRightCorner">The top right corner of the rectangle.</param>
public GeoBox(GeoPoint bottomLeftCorner, GeoPoint topRightCorner)
{
BottomLeftCorner = bottomLeftCorner;
TopRightCorner = topRightCorner;
Left = bottomLeftCorner.Longitude;
Bottom = bottomLeftCorner.Latitude;
Right = topRightCorner.Longitude;
Top = topRightCorner.Latitude;
}

/// <summary>
/// Initializes a new instance of the <see cref="GeoBox"/> class from the provided coordinates.
/// </summary>
/// <param name="left">The longitude of the left edge of the rectangle.</param>
/// <param name="top">The latitude of the top edge of the rectangle.</param>
/// <param name="right">The longitude of the right edge of the rectangle.</param>
/// <param name="bottom">The latitude of the bottom edge of the rectangle.</param>
public GeoBox(double left, double top, double right, double bottom)
{
Left = left;
Top = top;
Right = right;
Bottom = bottom;
}

internal NativeGeoBox ToNative() => new(BottomLeftCorner.ToNative(), TopRightCorner.ToNative());
internal NativeGeoBox ToNative() => new(Left, Top, Right, Bottom);

/// <inheritdoc />
public override string ToString() => $"Box: {{ {BottomLeftCorner}, {TopRightCorner} }}";
public override string ToString() => $"Box: {{ left: {Left}, top: {Top}, right: {Right}, bottom: {Bottom} }}";
}
}
16 changes: 10 additions & 6 deletions Realm/Realm/Native/Queries/NativeGeoBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,19 @@ namespace Realms.Native
[StructLayout(LayoutKind.Sequential)]
internal readonly struct NativeGeoBox
{
private readonly NativeGeoPoint _bottom_left_corner;
private readonly NativeGeoPoint _top_right_corner;
private readonly double _left;
private readonly double _top;
private readonly double _right;
private readonly double _bottom;

public NativeGeoBox(NativeGeoPoint bottom_left, NativeGeoPoint top_right)
public NativeGeoBox(double left, double top, double right, double bottom)
{
_bottom_left_corner = bottom_left;
_top_right_corner = top_right;
_left = left;
_top = top;
_right = right;
_bottom = bottom;
}

public override string ToString() => $"Box {{bl: {_bottom_left_corner}, tr: {_top_right_corner}}}";
public override string ToString() => $"Box {{ {_left}, {_top}, {_right}, {_bottom} }}";
}
}
4 changes: 2 additions & 2 deletions Tests/Realm.Tests/Database/GeospatialTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -327,10 +327,10 @@ public void NativeGeoPoint_ToString()
public void NativeGeoBox_ToString()
{
var box = new GeoBox((1, 2), (3, 4));
Assert.That(box.ToNative().ToString(), Does.Contain("Box").And.Contains("[1, 2]").And.Contains("[3, 4]"));
Assert.That(box.ToNative().ToString(), Does.Contain("Box").And.Contains("{ 2, 3, 4, 1 }"));

QueryArgument arg = box;
Assert.That(arg.ToNative().ToString(), Does.Contain("QueryArgument").And.Contains("[1, 2]").And.Contains("[3, 4]"));
Assert.That(arg.ToNative().ToString(), Does.Contain("QueryArgument").And.Contains("{ 2, 3, 4, 1 }"));
}

[Test]
Expand Down
8 changes: 5 additions & 3 deletions wrappers/src/marshalling.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,10 @@ struct geo_point {
};

struct geo_box {
geo_point bottom_left;
geo_point top_right;
double left;
double top;
double right;
double bottom;
};

struct geo_circle {
Expand Down Expand Up @@ -256,7 +258,7 @@ static inline GeoPoint from_capi(geo_point point)

static inline GeoBox from_capi(geo_box box)
{
return GeoBox{ from_capi(box.bottom_left), from_capi(box.top_right) };
return GeoBox{ GeoPoint(box.left, box.bottom), GeoPoint(box.right, box.top) };
}

static inline GeoCircle from_capi(geo_circle circle)
Expand Down

0 comments on commit c60ec99

Please sign in to comment.