1/*  Part of SWI-Prolog
    2
    3    Author:        Willem Robert van Hage
    4    E-mail:        W.R.van.Hage@vu.nl
    5    WWW:           http://www.few.vu.nl/~wrvhage
    6    Copyright (c)  2009-2012, Vrije Universiteit Amsterdam
    7    All rights reserved.
    8
    9    Redistribution and use in source and binary forms, with or without
   10    modification, are permitted provided that the following conditions
   11    are met:
   12
   13    1. Redistributions of source code must retain the above copyright
   14       notice, this list of conditions and the following disclaimer.
   15
   16    2. Redistributions in binary form must reproduce the above copyright
   17       notice, this list of conditions and the following disclaimer in
   18       the documentation and/or other materials provided with the
   19       distribution.
   20
   21    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   22    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   23    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   24    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
   25    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   26    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
   27    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   28    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
   29    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   30    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
   31    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   32    POSSIBILITY OF SUCH DAMAGE.
   33*/
   34
   35% load the Prolog module that uses the space shared object file
   36:- use_module(library(space/space)).   37
   38% load the semantic web package
   39:- use_module(library('semweb/rdf_db.pl')).   40
   41% eye candy, declare shorthand for namespaces
   42:- rdf_register_ns(geo, 'http://www.geonames.org/ontology#').   43:- rdf_register_ns(wgs84, 'http://www.w3.org/2003/01/geo/wgs84_pos#').   44
   45:- writef("\n----\n\nLoading demo RDF file of Geonames features in the Rotterdam harbor.\n").   46% load the demo RDF file containing Geonames features around the Rotterdam harbor
   47:- rdf_load('demo_geonames.rdf').   48
   49% this adds all URIs with associated coordinates to the space indexing queue
   50:- writef("Selecting features with coordinates to put in the spatial index.\n").   51:- space_bulkload(uri_shape,'demo_index').   52:- writef("done loading demo\n\n----\n\n").   53
   54% find Features in order of proximity to the point Lat Long
   55nearest_features(point(Lat,Long), Name) :-
   56    space_nearest(point(Lat,Long), Nearest,'demo_index'),
   57    rdf(Nearest, rdf:type, geo:'Feature'), % atoms starting with capitals have to be quoted.
   58    rdf(Nearest, geo:name, literal(Name)).
   59
   60% find Features contained in the box defined by the two points
   61contained_features(box(point(NWLat,NWLong),point(SELat,SELong)), Name) :-
   62    space_contains(box(point(NWLat,NWLong),point(SELat,SELong)), Contained, 'demo_index'),
   63    rdf(Contained, rdf:type, geo:'Feature'),
   64    rdf(Contained, geo:name, literal(Name)).
   65
   66% find Features in order of proximity, but restrict them to those with featureCode harbor
   67% also, fetch and show their coordinates
   68nearest_harbors(point(Lat,Long), Name, point(HarborLat,HarborLong)) :-
   69    space_nearest(point(Lat,Long), Nearest,'demo_index'),
   70    rdf(Nearest, rdf:type, geo:'Feature'),
   71    rdf(Nearest, geo:featureCode, geo:'H.HBR'),
   72    rdf(Nearest, geo:name, literal(Name)),
   73    rdf(Nearest, wgs84:lat, literal(HarborLat)),
   74    rdf(Nearest, wgs84:long, literal(HarborLong)).
   75
   76
   77
   78:- writef("Welcome to the SWI-Prolog \"space\" package demo.\n\n").   79:- writef("Try finding features in the Rotterdam harbor.\n").   80:- writef("To find features near lat 51.96 long 4.13, try the following:\n").   81:- writef("?- nearest_features(point(51.96,4.13), Name).\n\n").   82:- writef("To find the nearest harbor to that point, try the following:\n").   83:- writef("?- nearest_harbors(point(51.96,4.13), Name, point(HarborLat,HarborLong)).\n\n").   84:- writef("To find features in the rectangular area between ").   85:- writef("lat 51.93 long 4.10 and lat 51.96 long 4.19, try the following:\n").   86:- writef("?- contained_features(box(point(51.93,4.10),point(51.96,4.19)), Name).\n\n").   87:- writef("Have fun experimenting with the \"space\" package!\n").   88:- writef("If you have questions, please e-mail me.\n\n").   89:- writef("Willem Robert van Hage <W.R.van.Hage@vu.nl>\n\n").