neo4j空间包含搜索 [英] neo4j spatial contain search

查看:164
本文介绍了neo4j空间包含搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试开发一个Web服务,该服务可以将包含给定gps职位的管理区域的名称还给我.

i'm trying to develop a web service able to give me back the name of the administrative area that contains a given gps position.

我已经开发了一个Java应用程序,能够使用空间插件和Java API在neo4j中插入一些多边形(我的国家的行政区域).然后,给一个gps位置,我就能得到包含它的多边形的名称.

I have already developed a java application able to insert some polygons (administrative areas of my country) in neo4j using spatial plugin and Java API. Then, giving a gps position, i'm able to get the name of the polygon that contains it.

现在,我正在尝试使用Neo4j的REST API(而不是Java api)执行相同的操作,但是我找不到任何示例.

Now i'm trying to do the same using REST API of Neo4j (instead of java api) but i'm not able to find any example.

所以我的问题是:

1)是否可以使用REST API在Neo4j中插入多边形(如果我很理解,可以使用WKT格式)?

1) Is possible to insert polygons in Neo4j using REST API (if i well understood is possible using WKT format) ?

2)可以执行空间查询,以查找包含给定gps位置?的所有多边形.

2) is possible to execute a spatial query that finds all polygons that contain a given gps position ?

谢谢,恩里科

推荐答案

两个问题的答案都是肯定的.这是使用REST和Cypher的示例步骤.

The answer to both of your questions is yes. Here are example steps that use REST and Cypher.

1)创建空间层和索引(REST).在此示例中,我的索引名为"test"(将创建一个具有相同名称的图层,并将创建基本空间节点),并且包含wkt几何信息的节点上的属性名称为"wkt".

1) Create your spatial layer and index (REST). In this example, my index is named 'test' (a layer of the same name and base spatial nodes will be created), and the name of the property on my nodes that will contain the wkt geometry information is 'wkt'.

POST http://localhost:7474/db/data/index/node {"name":"test", "config":{"provider":"spatial", "wkt":"wkt"}}

2)创建一个节点(密码).您可以具有标签和各种属性. Neo4j Spatial唯一关心的部分是"wkt"属性. (您可以使用REST执行此步骤.)

2) Create a node (Cypher). You can have labels and various properties. The only part that Neo4j Spatial cares about is the 'wkt' property. (You could do this step with REST.)

CREATE (n { name : "Fooville", wkt : "POLYGON((11.0 11.0, 11.0 12.0, 12.0 12.0, 12.0 11.0, 11.0 11.0))" })

3)将节点添加到图层.您可以通过将节点添加到索引或图层来完成此操作,但是有一个重要的区别.如果将其添加到索引,将创建仅包含几何数据的复制节点,并将其添加到图层.通过Cypher查询将返回您的原始节点,但是通过REST或Java查询将返回复制节点.如果将节点直接添加到图层,则如果想以后再使用Cypher进行查询,则必须采取额外的步骤.在这两种情况下,您都需要节点的URI,其最后一个元素是Neo4j节点号.在下面的示例中,我假设节点号为4(如果您在一个新的空数据库中执行此示例,则该节点号为该节点号.)

3) Add the node to the layer. You can do this by adding the node to the index or to the layer, but there is an important difference. If you add it to the index, a copy node containing only the geometry data will be created, and that will be added to the layer. Querying via Cypher will return your original node, but querying via REST or Java will return the copy node. If you add the node directly to the layer, then you must take an extra step if you want to be able to query with Cypher later. In both cases you will need the URI of the node, the last element of which is the Neo4j node number. In the example below, I assume the node number is 4 (which it will be if you do this example on a fresh, empty database).

方法1:

POST http://localhost:7474/db/data/ext/SpatialPlugin/graphdb/addNodeToLayer { "layer":"test", "node":"http://localhost:7474/db/data/node/4" }

要使该节点可通过Cypher进行搜索,请将节点号作为用户"id"属性添加到该节点. (您可以使用REST来做到这一点.)

To make this node searchable via Cypher, add the node number to the node as a user 'id' property. (You could do this with REST.)

START n = node(4) SET n.id = id(n)

方法2:使用此方法将使您的节点数增加一倍,WKT存储量增加一倍,并在通过REST vs Cypher查询时产生不同的结果.

Method 2: Using this method will double your node count, double your WKT storage, and produce differing results when querying via REST vs Cypher.

POST http://localhost:7474/db/data/index/node/test {"value":"dummy","key":"dummy","uri":"http://localhost:7474/db/data/node/4"}

3)运行查询.您可以在REST或Cypher中进行查询(假设您如上所述调节了节点).可用的Cypher查询为:"withinDistance","withinWKTGeometry"和"bbox".可用的REST查询为:"findGeometriesWithinDistance","findClosestGeometries"和"findGeometriesInBBox".有趣的是,只有Cypher允许您查询WKT几何图形内的节点.即使参数相同,我仍然不了解的findClosestGeometries和findGeometriesWithinDistance之间的REST也存在差异.要查看如何进行REST调用,可以发出以下命令:

3) Run your query. You can do a query in REST or Cypher (assuming you conditioned the nodes as described above). The Cypher queries available are: 'withinDistance', 'withinWKTGeometry', and 'bbox'. The REST queries available are: 'findGeometriesWithinDistance', 'findClosestGeometries', and 'findGeometriesInBBox'. It's interesting to note that only Cypher allows you to query for nodes within a WKT geometry. There's also a difference in REST between the findClosestGeometries and findGeometriesWithinDistance that I don't yet understand, even though the arguments are the same. To see how to make the REST calls, you can issue these commands:

POST http://localhost:7474/db/data/ext/SpatialPlugin/graphdb/findGeometriesWithinDistance
POST http://localhost:7474/db/data/ext/SpatialPlugin/graphdb/findClosestGeometries
POST http://localhost:7474/db/data/ext/SpatialPlugin/graphdb/findGeometriesInBBox

Cypher查询为:(用实际值替换'<>'之间的文本,包括'<>')

The Cypher queries are: (replace text between '<>', including the '<>', with actual values)

START n = node:<layer>("withinDistance:[<y>, <x>, <max distance in km>]")
START n = node:<layer>("withinWKTGeometry:POLYGON((<x1> <y1>, ..., <xN> <yN>, <x1> <y1>))")
START n = node:<layer>("bbox:[<min x>, <max x>, <min y>, <max y>]")

在所有这些中,我都假定您正在使用经度/纬度坐标参考系统(CRS),因此x是经度,y是纬度. (这将保留z向上的右手坐标系.)

I have assumed in all of this that you are using a longitude/latitude coordinate reference system (CRS), so x is longitude and y is latitude. (This preserves a right-handed coordinate system in which z is up.)

这篇关于neo4j空间包含搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆