如何找到线串到给定位置的最近点-rgeo

时间:2019-05-23 06:09:33

标签: ruby postgis geos rgeo

我的点坐标位于地理坐标系中。他们在记忆中。我需要使用Ruby的RGeo库,找到线串上最接近给定点的点。

我的场景是在googlemap中将点绘制为折线作为道路表示,而位置是我的车辆的位置。我需要一条最短的途径上路。

PostGIS中有解决方案,但我的数据在内存中,我不想使用Postgresql来仅为此目的。

我正在检查RGeo宝石,但找不到与此相关的任何线索。

[已编辑]

enter image description here

enter image description here

参考:https://postgis.net/docs/ST_ClosestPoint.html

1 个答案:

答案 0 :(得分:1)

此处有详细描述:https://groups.google.com/forum/#!topic/rgeo-users/e1FgzpPISs8

# Create a Geos factory that uses the ffi interface
factory = RGeo::Geos.factory(:native_interface => :ffi)

# Create your polyline and point A using that ffi-backed factory.
# You can create the objects directly using the factory, or cast objects to the
# factory, whatever is the easiest way for you to get objects that are attached
# to the ffi factory.
polyline = factory.line_string( ... )
point = factory.point( ... )

# Objects that are attached to an ffi-geos factory provide access, via the
# fg_geom method, to low-level objects that understand the ffi-geos api.
# This is not really documented well, but it's a stable api that you can use.
low_level_polyline = polyline.fg_geom
low_level_point = point.fg_geom

# Now invoke the low-level libgeos calls.
# This first method, "project", gives you the distance "along" the linestring
# where it comes closest to the given point.
dist = low_level_polyline.project(low_level_point)
# This second method, "interpolate", takes a distance "along" the linestring,
# and returns the actual point on the linestring.
low_level_closest_point = low_level_polyline.interpolate(dist)

# Finally, wrap the low-level result in an RGeo point object
closest_point = factory.wrap_fg_geom(low_level_closest_point)