Global Positioning System Reference
In-Depth Information
The fi rst step for the creation of a query is to construct a “try” block with
the “fi nally” option containing the database.shutdown() command (line 39).
The command ends the connection with the database as soon as the code
inserted in the “try” block is processed. The “database” variable, used to
maintain the connection with the Neo4j Spatial database, was instantiated at
some previous moment and is indispensable for the correct execution of the
presented code. This can be observed in the third and fourth lines of Fig. 16,
where we instantiate a service for accessing the spatial database. Using this
service, two variables are instantiated to store the spatial data layers: the fi rst
variable represents the layer containing the data of the MUNICIPALITIES
entity, while the second one represents a layer containing the data of the POIS
entity (the sixth and nineteenth lines). The indexes of the instantiated spatial
layers are stored in objects of the type SpatialIndexReader (the eighth and
twenty lines). This object type has the executeSearch() method, responsible
for processing the queries on a layer and storing the results in an object of the
type Search. In addition to the results, the object of the Search type stores the
constraint conditions for the execution of a conventional query on a spatial
data layer. An example of defi ning constraining conditions is illustrated in
line 10 of Fig. 16. The Search object stores the constraint of returning only the
geometry of the New York municipality from layer one, which represents
the MUNICIPALITIES entity. The results of the conventional queries on the
layers consist of lists of objects of the type SpatialDatabaseRecord.
The lists of records contain the results of the conventional queries on
the layers. The fi rst list (line 13) contains the geometry of the New York
municipality and the second list (line 28) contains only the PoIs with type
“restaurant” in a bounding box of the New York City geometry. We use the
object of the type SearchIntersect, a specialisation of the generic object type
Search, to perform the spatial intersection of these two geometry lists (lines
31 to 36). The result of this procedure is a list containing only the PoIs of the
type “restaurant” that intersect the geometry of New York City.
An excerpt of the algorithm implemented to answer query Q2 (list
all drugstores located within a radius of one km from hospitals) in Neo4j
Spatial is presented in Fig. 17. Similarly to query Q1, the fi rst steps needed
to perform Q2 focus on the instantiation of the object that accesses the
requested data layer (in this case, the “POIS” entity) and on the instantiation
of the objects that prepare a conventional query on this layer. In the third
line of Fig. 17, we can observe the insertion of the constraint in “POIS” such
that the variable “searchQuery” returns only the PoIs of the “hospital” type,
which will then be made available in the “hospitals” variable (sixth line).
Using the startNearestNeighborLatLonSearch (Layer to search,
pointCentroid, maxDistanceInKm) method, as shown in line 12 of Fig. 17,
we can obtain a list of the PoIs within the radius of one km from a hospital.
Because this method demands a Layer object instead of a Search object, an
Search WWH ::




Custom Search