航线图中以浮点数形式存储了每个机场的地理坐标(经度和纬度)。一些图系统如杰森图有内置的地理坐标和形态(时空)函数,但是廷克图没有。甚至GraphML没有提供任何的地理时空支持。为了让事情保持简洁和灵活。航线图的数据集没有假设有指定的后端能力。已经提供的基本的浮点数坐标可以允许我们写一些有趣的地理空间的查询。
The air-routes graph stores the geographic coordinates (latitude and longitude) of each airport as
floating point numbers. Some graph systems such as JanusGraph have geographic coordinate and
shape (geospatial) functions built in, but TinkerGraph does not. Moreover, GraphML does not offer
any specific geospatial support. To keep things simple and flexible, the air routes data set does not
assume any specific back end capabilities. Having coordinates provided as basic floating point
numbers still allows us to write some interesting geospatial queries.
目前为止, 我们还没有在查询中利用这些经度和纬度的坐标。在这一节中笔者将只使用小精灵的标准的步骤进行一些有趣的地理空间计算的查询。
So far, we have not taken advantage of these latitude and longitude coordinates in queries. In this
section I have included some queries that perform interesting geospatial calculations using just the
standard Gremlin steps.
这是一个简单的查询,找出位置在北纬77度的机场。注意where步骤可以用来代替filter,因为在这个例子中它们是相似的。
Here is a simple query that finds any airports located North of 77 degrees latitude. Note that a
where step could be used instead of filter as they are synonymous in this case.
在本书前边提到过,您可以只用 has步骤而不使用where或filter步骤。我们可以把前边一个查询写成下面这样。
As discussed earlier in the book, you can often just use has steps instead of where or filter steps. We
could have written the previous query as follows.
有两个机场在最北边,如下所示,运行两个查询的任一个都可以得到这一结果。
It turns out that just two airports in the graph are located that far North as shown below in the
results from running either form of the query.
我们可以修改查询用一条查询语句在指定纬度(-50到77)之外的,找到最北边和最南边的机场。这是这样的一个查询。
We could modify our query to look for airports with a latitude value outside of a provided upper
and lower bound thus finding the most Northerly and Southerly airports with a single query. Here
is a simple example of such a query.
运行查询找到的机场在这。
Here are the airports found by running the query.
注意在写前边的查询时看起来可以用一个without步骤,来指定一个范围例如(-55,77)但是它是不奏效的,因为withou会查找范围指定的确切的数字,这个范围生成的形式的-50,-49,-48等等。只有当机场所在的纬度刚好出范围生成的确切的数字一模一样时才可以排除掉。这就是为什么在类似这样的情况下outside步骤这么有用了。下面一个查询将返回LHR的坐标。
Note that while writing the above query it might have seemed appropriate to use a without step
with a range such as -50..77 but that will not work as without looks for exact matches against the
values in the range and the range generated is of the form -50,-49,-48 and so on, so the only airports
that would get filtered out would be the ones having a latitude value that exactly matches one of
the values generated by the range. This is why the outside step is so useful in cases like this.
This next query just returns the coordinates for London Heathrow.
下面这个查询将返回英国伦敦的机场的代码纬度和经度。注意因为在世界上也有其它的城市也叫伦敦比如加拿大的London, Ontario,所以我们得利用地区代码这样才能只返回在英国伦敦的机场。
This next query returns the code, latitude and longitude for all airports in London, England. Note
that because there are other cities in the world also called London, such as London, Ontario in
Canada, we have to take advantage of the region code GB-ENG to only return airports in London,
England.
现在我们知道了如何时查询地理坐标。我们可以写一个查询找出来图中接近格林威治子午线的机场。在这个例子上我们找到的机场的纬度在东经0.1度和西经0.1度之间。
Now that we know how to query the geographic coordinates, we can write a query to find out
which airports in the graph are very close to the Greenwich Meridian. In this case we will look for
any airports that have a longitude between -0.1 and 0.1
下一个查询是找出来哪个机场接近赤道。
This next query can be used to find out which airports are closest to the equator.
下面的查询是找到所有指定地理区域的机场。这个区域是LHR附近一度的一个范围内。这类事情可以使用杰森图提供的Geo shape类来完成。但是我们没有学到书的那部分。这也是一种好的处理方式。
The code below will find all the airports in the geographic area defined by a one degree box around
London Heathrow. This type of thing can be done using the Geo shape classes provided by
JanusGraph but given we are not at that part of the book yet this is the next best way!
如我们在本书前边提到的,常可以把has步骤串联起来,从而避免了and步骤。下面的查询和前边的代码等价,便是它避免了where和and的使用。
As we have discussed earlier in this book, it is often possible to avoid use of and step by chaining
has steps together. The code below is equivalent to the code above but avoids the use of where and
and.
这是在小精灵控制台运行上面的任一段代码产生的输出。
Here is the output produced by running either of the snippets of code above inside the Gremlin
Console.
在“测试值和值的范围”一节中我们有这样一个查询(如下所示):找出AUS和LAX之间中转一次的路径。只是美国或加拿大的机场,排除在PHX,LAX中转。
In the "Testing values and ranges of values" section we came up with a query (shown below) to find
routes with one stop between Austin and Las Vegas, using only airports in the United States or
Canada and avoiding PHX and LAX for plane changes.
现在我们知道了怎么使用航线图中保存的坐标信息。出于好玩们可以用一种不同的方式来写这个查询。如果您看了下面的查询,您会发现我们增加了一个where步骤。我们只找在美国或加拿大的机场,然后在where步骤中,我们进一步地限制我们想进一步关注的机场是经度比AUS小的机场。换句话说, 我们只想在AUS的西部换乘。这实际上是对前边的查询的一种提高,它将返回在纽约和那什维尔地区换乘的那些路线。在我们的新查询中,没有在AUS东部的机场换乘的路线。
Now that we know how to use the longitude and latitude coordinates stored in the air routes graph,
we could for fun, write this query a different way. If you take a look at the query below you will see
we have added a where step. We still check that we are only looking at airports in the United States
or Canada but then, in the where step, we further limit the airports we want to consider further by
saying we are only interested if their longitude value is less than that of Austin. In other words, we
only want to change planes at an airport that is to the West of Austin. This is actually an
improvement on the previous query that would have returned routes that included plane changes
in New York and Nashville among other places. With our new query, no airport that is East of
Austin will be considered as a place to change planes.
![]()
下面您会看到运行查询的输出。如果您知道您的航班代码您可以看到所有这些机场都在AUS 西部。我们可能想进一步的提升这个查询,分析相当接近的机场,但是不在AUS西部。例如,DFW,它不在结果中,因为它位于AUS以北,但有一点点靠东。我们把它留做一个练习进一步地优化查询,这样DAL就可以包含在结果中了。
Below you will find the output from running the query. If you know your airport codes you will see
that all of these airports are indeed to the West of Austin. We might want to improve the query even
more however, to factor in sensible nearby airports that are not to the West of Austin. For example,
Dallas Fort Worth (DFW) is not included in the results as it is situated North of Austin but also a
little to the East. We will leave it as an exercise to come up with a refinement to the query so that
Dallas is also included!.
又一个和前一个例子相似的例子。首先我们把DFW的经度存在变量dfw中,然后我们用变量去找到从LAS到AUS只中转一次的路线,便是不在PHX和LAX中转,中转只能或在DFW或在DFW西部。注意在测试经度值时我们用了lte而不是lt。如果我们使用lt它将会把DFW排除了。这个查询和前边的一样,也排除了比DFW靠东的那些中转的路线。
Here is one more example that is similar to the previous one. First of all we store the longitude of
the Dallas (DFW) airport in the variable dfw. Then we use that variable to find routes to Las Vegas
(LAS) from Austin (AUS) that have one stop but avoid Phoenix (PHX) and Los Angeles (LAX) and
only have a plane change either in Dallas or to the West of Dallas. Note that we use lte and not lt
when testing the longitude value. It we used lt instead that would also rule out Dallas as an option.
This query, as in the prior one, has the effect of ruling out plane changes at airports further to the
East of Austin than Dallas.
运行查询我们得到返回在这。如果我们避开了LAX和PHX也任有很多种方案。
Here is what we get back when we run the query. Still lots of choices even if we avoid LAX and PHX
it seems.
现在我们看最后一个查贸易用到了region这个属性,图中所有机场顶点都有的。
Let’s look at one last query that uses the region property, present on all airport vertices in the
graph.
下面的查询从DFW机场开始,然后找所有可以到达的美国的机场。然后这些机场按它们的地区代码和机场代码分组。最后选择一些州的。查询的最后用一个unfold步骤来展开,这样结果会更可读一些。
The query below starts at the DFW airport then looks for all routes to airports also within the
United States. Next those airports are grouped by their region code and airport code. Finally a few
states are selected. The query ends with an unfold step to make the results a bit more readable.
下面您看到了查询的结果。每个选定的周列出了一系列可以从DFW到达的机场。
Below you can see the query results. Each of the selected states is listed along with a list of airports
reachable from DFW.
在下一节我们会见到一人更复杂的查询,它以前边一个查询为基础,使用机场顶点的经纬坐标进行了一些距离的计算。
In the next section we will look at a more complicated query that builds upon the examples above
and performs distance calculations using the latitude and longitude coordinates present on the
airport vertices.