前边在“在查询中使用经度、纬度,地理空间区域”这一节中,笔者提供了一些例子:如何写查询来利用航线图中的机场属性所包含的纬度和经度。在处理杰森图时,有一些内置的额外的能力, 我们可以利用。
Earlier, in the "Using latitude, longitude and geographical region in queries" section, I provided a
few examples of how we could write some queries that took advantage of the fact that the airports
in the air-routes graph include their latitude and longitude among their properties. When working
with JanusGraph there are some additional built in capabilities that we can take advantage of.
杰森图的官方API文档是一个好地方,可以读到关于GeoShape类和相关类的内容。文档总是可以从这找到http://docs.janusgraph.org/latest/javadoc.html。
The official JanusGraph API documentation is a good place to read up on the
GeoShape class and related classes. That documentation can always be found by
starting here: http://docs.janusgraph.org/latest/javadoc.html
下面的例子说明了一种方式:我们可以使用GeoSpatial API来找到以LHR为中心,100千米半径的圆中的机场。 要注意的一个关键的类就是GeoShape类. 它可以用来创建区域,在测试其它的对象是否也在那个区域时,我们可以使用它。
The example below shows one way that we could use the GeoSpatial API to find airports within a
circle having a 100 kilometer radius with London Heathrow (LHR) at the center of that circle. A key
class to be aware of is the Geoshape class. It can be used to create areas that we can use when
testing for other coordinates falling within that area.
注意下面的代码对于图中的每个机场都有一个点基于机场的经度和纬度被创建。接下来进行的测试来看这个点是否在我们的100千米圈内。只有符合条件的机场可以传到valueMap步骤中。注意map步骤是怎么使用的,这样我们就可以在创建点的同时在闭包中进行一些计算。
Notice in the code below that for each airport in the graph a point is created based on the latitude
and longitude of that airport. A test is then performed to see if that point lies within our 100km
circle. Only airports that do are passed on to the valueMap step. Notice also how a map step is used
so that we can do some calculations inside of a closure while creating the point.
您可能期望通过运行以上查询得到的输出在这。如果小精灵控制台连到了一个包含了航线图的杰森图实例,查询就可以运行。
Below is the output that you might bet back from running the above query. The query can be run
as-is from the Gremlin Console connected to a JanusGraph instance containing the air-routes graph.
有多种方式可以优化我们的查询从而避免为图中的每一个机场顶点创建一个点。在这个特别的例子中, 我们可以决定。 比如,我们只对于在英国的机场感兴趣。为此,我们可以加一个验证到我们的查询语句中,来保证只有地区代码是GB- ENG的机场才需验证。这是增加了验证的已修改过的查询。
There are many ways we could optimize our query to avoid creating a point for every single airport
in the graph. In this particular case, we might decide, for example, that we are only interested in
airports in England. To do this we could add a check to our query to make sure that only airports
with a region code of GB-ENG are tested. Here is the query modified with that check added.
这是再次运行查询的输出。我们得到了同样的结果,除了返回的结果的顺序是不同的之外。然而这个查询会更高效一些,因为它可以利用我们前边为地区创建的索引来过滤掉不在英国地区的那些机场。
Here is the output from running the query again. Other than the order in which results were
returned being different we got the same results. However this query is more efficient as it is able
to take advantage of the index that we created earlier for the region property to filter out all
airports not in the region GB-ENG.
使用 GeoSpatial API ,我们可以创建代表不同地理区域的形状并比较它们。下面这个例子说明了如何去创建一个以LHR为中心的100千里的圆和第二个以MAN为中心的圆。然后使用intersect 方法来看是否有点在两个圆中都出现。
Using the GeoSpatial API we can create shapes representing different geographic regions and
compare them. The example below shows how to create a 100km circle with Longdon heathrow
(LHR) at the center and a second circle with Manchester (MAN) at the center. The intersect method
is then used to see if any points appear in both circles.
如您所见,测试返回结果是假,说明没有点在两个圆中都出现。为了证明当点确实存在时,测试的正确性,我们以LPL为中心创建另一个100千里的圆,把它与另一个MAN圆进行比较。
As you can see the test returns false indicating that there are no shared points. To prove that the
tests work when points do overlap, let’s create another 100km circle with Liverpool (LPL) in the
middle and compare that one with the Manchester circle.
Geoshape类提供了一些有用的方法。如果我们想要验证从LHR顶点得到的经度和纬度是正确的,意味着它们确实代表了地球上的某个位置, 我们可以像下面这么做。
The Geoshape class provides a number of useful methods. If we wanted to verify that the latitude
and longitude values we got back from the LHR vertex were valid, meaning they do indeed
represent a point somewhere on Earth, we could do so as follows.
前边在“在查询中使用纬度,经度和地理区域”这一节中,笔者演示过下面的查询。这个查询找到邻近LHR的概念上的矩形的所有的机场。这个矩形是通过以LHR为对角线中心,增加或在相反方法减少经度和纬度来定义的。
Earlier, in the "Using latitude, longitude and geographical region in queries" section I demonstrated
the query below. The query finds all airports within a conceptual rectangle around the London
Heathrow (LHR) airport. The rectangle is defined by adding or subtracting one degree of latitude
and longitude to the opposite diagonals with LHR at the center.
这是查询产生的输出。
Here is the output that query produced.
我们可以使用杰森图的Geoshape类来重写查询,如下所示。这次没有使用圆,我们创造了一个代表邻近LHR的地理区域的box
We can use the JanusGraph Geoshape class to rewrite the query as shown below. Instead of using a
circle this time we will create a box representing a geographical region around London Heathrow
(LHR).
运行我们的新查询的结果如下所示。如您所见同样的机场被找到了。
The results from running our new query are shown below. As you can see the same airports were
found.
笔者已经演示了一些使用杰林图Geoshape API的各种东西的例子。如果这个领域是您所感兴趣的,笔者推荐您阅读Geoshape和相关类的API文档。
I have just shown a few examples of the many things that you can do using the JanusGraph
GeoSpatial API. If this is an area that interests you I recommend reading the API documentation for
the Geoshape and related classes.