我们正在从 MongoDb 迁移到 RavenDb,但遇到了一个问题。
我们有一个简单的模型(C# DotNet)
using System.Globalization;
public class Location
{
#region Properties
public double Latitude { get; set; }
public double Longitude { get; set; }
#endregion Properties
#region Methods
public override string ToString()
{
var numberFormatInfo = new NumberFormatInfo { NumberDecimalSeparator = "." };
return $"{Longitude.ToString(numberFormatInfo)} {Latitude.ToString(numberFormatInfo)}";
}
#endregion Methods
}
public class Place
{
public Location[] Area {get;set;} // Please note the array
}
Run Code Online (Sandbox Code Playgroud)
客户可以选择一个地点作为一对,或者他们可以在交互式地图上绘制一个多边形,代表该地点,该多边形将存储在上面的 Area 属性中。
在 MongoDb 中,这再简单不过了
var locationQuery = new FilterDefinitionBuilder<Place>()
.GeoWithinBox(
field: x => x.Area,
lowerLeftX: query.Criteria.LongitudeBottomLeft,
lowerLeftY: query.Criteria.LatitudeBottomLeft,
upperRightX: query.Criteria.LongitudeTopRight,
upperRightY: query.Criteria.LatitudeTopRight);
Run Code Online (Sandbox Code Playgroud)
上面的查询类,代表了地图上当前可见的区域,客户正在查看。
尽管我尽了最大的努力,但我无法破译有关如何实现相同目标的文档。我希望有人能够对此事有所了解,了解如何使用 RavenDb Dotnet 客户端 (RavenDB.Client) 实现这一目标
谢谢
更新和样品!
在下面 Ayende Rahien 给出了很好的回答之后,我想我会在这里添加一些细节,以简化其他人在这里登陆的旅程。
这就是索引的样子。
using System.Linq;
using Raven.Client.Documents.Indexes;
public class PlaceAreaIndex : AbstractIndexCreationTask<Place>
{
public PlaceAreaIndex()
{
Map = places => from place in places
select new
{
Area = place.Area.Select(location => CreateSpatialField(location.Latitude, location.Longitude))
};
}
}
Run Code Online (Sandbox Code Playgroud)
在使用查询之前,您需要在服务器上注册您的索引。
new PlaceAreaIndex().Execute(store);
Run Code Online (Sandbox Code Playgroud)
我添加了一些辅助类来帮助我测试这一点,我在此处添加它们,以便示例让您尽可能接近正在运行的代码
public class Box
{
#region Constructor
public Box(Location topRight, Location bottomLeft)
{
TopRight = topRight;
BottomLeft = bottomLeft;
TopLeft = new Location { Latitude = topRight.Latitude, Longitude = bottomLeft.Longitude };
BottomRight = new Location { Latitude = bottomLeft.Latitude, Longitude = topRight.Longitude };
}
#endregion Constructor
#region Properties
public Location TopRight { get;}
public Location TopLeft { get; }
public Location BottomLeft { get; }
public Location BottomRight { get; }
#endregion Properties
#region Methods
public override string ToString()
{
return $"POLYGON (({TopRight}, {TopLeft}, {BottomLeft}, {BottomRight}, {TopRight}))";
}
#endregion Methods
}
Run Code Online (Sandbox Code Playgroud)
然后使用助手
var topRight = Location.New(latitude: -22.674847351188916, longitude: 31.25061035156253);
var bottomLeft = Location.New(latitude: -28.9600886880069, longitude: 25.1422119140623);
var box = new Box(topRight: topRight, bottomLeft: bottomLeft);
Run Code Online (Sandbox Code Playgroud)
之后你可以这样查询
var places = await session
.Query<Place, PlaceAreaIndex>()
.Spatial(
factory => factory.Area,
criteria => criteria.RelatesToShape(
shapeWkt: box.ToString(),
relation: SpatialRelation.Within))
.ToArrayAsync();
Run Code Online (Sandbox Code Playgroud)
希望对你有帮助。
IIUC,问题是你可能有一个特定的位置或多边形,对吗?在 RavenDB 中表示多边形时,您需要使用 WKT。
例如,如下所示:
POLYGON((14.316406249999982 37.541615344157876,32.68554687499998 37.541615344157876,32.68554687499998 25.439901234431595,14.316406249999982 25.439901234431595,14.316406249999982 37.541615344157876))
Run Code Online (Sandbox Code Playgroud)
然后,RavenDB 内部的空间引擎可以使用它,并允许您使用spatial.Contains.
但是,这可能不是您想要的,具体取决于使用情况。
如果您想要的只是找到矩形内Places具有位置的所有位置,那么将其威胁为位置数组会更容易。另一方面,如果这确实是一个多边形,那就不同了。
可能更容易展示东西。RavenDB 中的示例数据具有空间数据,如下所示:
$wkt = "POLYGON((-1.0800308814195025 51.942980464942416,0.5789046654554975 51.942980464942416,0.5789046654554975 51.01935531918276,-1.0800308814195025 51.01935531918276,-1.0800308814195025 51.942980464942416))"
from Employees
where spatial.within( spatial.point(Address.Location.Latitude , Address .Location.Longitude),
spatial.wkt($wkt))
Run Code Online (Sandbox Code Playgroud)
然后您可以看到新的空间选项卡,它将显示结果:
这显示了矩形 (wkt) 和一个点。
但是,如果您有数组,也可以使用:
"Locations": [
{
"Latitude": 51.52384070000001,
"Longitude": -0.0944233
},
{
"Latitude": 51.48145355123356,
"Longitude": -0.17475717569860105
}
],
Run Code Online (Sandbox Code Playgroud)
然而,需要一个显式索引来处理,你可以这样写:
from e in docs.Employees
select new
{
Locations = e.Locations.Select(l => CreateSpatialField(l.Latitude, l.Longitude))
}
Run Code Online (Sandbox Code Playgroud)
这将为员工的所有个人位置建立索引。
然后您可以使用以下方式查询它:
$wkt = "POLYGON((-1.0800308814195025 51.942980464942416,0.5789046654554975 51.942980464942416,0.5789046654554975 51.01935531918276,-1.0800308814195025 51.01935531918276,-1.0800308814195025 51.942980464942416))"
from index 'Employees/ByLocations'
where spatial.within( Locations, spatial.wkt($wkt))
Run Code Online (Sandbox Code Playgroud)
然而,另一种选择是当您需要将位置列表转换为多边形时。例如,考虑这个 WKT:
POLYGON((-0.09345874990495329 51.7543103056377,-0.6823256691840518 51.56348306507395,-0.2252946874049533 51.30046869394702,0.09990102742639895 51.42120466591834,-0.09345874990495329 51.7543103056377))
这代表这个多边形:
然后,您可以使用与以前相同的查询样式,但请注意:
contains查询现在需要包含整个多边形。您也可以使用intersect,但是使用单点选项可能会更好。| 归档时间: |
|
| 查看次数: |
62 次 |
| 最近记录: |