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:- module(georss,
   36          [ georss_candidate/2,
   37            georss_candidate/3,
   38            georss_simple_candidate/2,
   39            georss_gml_candidate/2,
   40            georss_uri_shape_triple/5
   41          ]).   42
   43:- use_module(library(semweb/rdf_db)).   44:- use_module(library(dcg/basics)).   45:- use_module(gml).   46
   47:- rdf_meta(georss_simple_candidate(r,?,?)).   48
   49:- rdf_register_ns(georss,'http://www.georss.org/georss/').   50:- rdf_register_ns(foaf,'http://xmlns.com/foaf/0.1/').
 georss_candidate(?URI, ?Shape) is nondet
Finds URI-Shape pairs by searching for RDF triples that link URI to a Shape with GeoRSS RDF properties (e.g. georss:where, georss:line, georss:polygon). Both GeoRSS Simple and GML are supported.
   59georss_candidate(URI, Shape) :-
   60    georss_simple_candidate(URI, Shape).
   61georss_candidate(URI, Shape) :-
   62    georss_gml_candidate(URI, Shape).
 georss_candidate(?URI, ?Shape, +Source) is nondet
Finds URI-Shape pairs using georss_candidate/2 in RDF that was loaded from a certain Source.
   69georss_candidate(URI, Shape, Source) :-
   70    var(Source),
   71    georss_simple_candidate(URI, Shape, Source).
   72georss_candidate(URI, Shape, Source) :-
   73    var(Source),
   74    georss_gml_candidate(URI, Shape, Source).
   75
   76georss_candidate(URI, Shape, Source) :-
   77    nonvar(Source),
   78    georss_simple_candidate(URI, Shape, Source).
   79georss_candidate(URI, Shape, Source) :-
   80    nonvar(Source),
   81    georss_gml_candidate(URI, Shape, Source).
   82
   83%
   84% GeoRSS Simple
   85%
 georss_simple_candidate(?URI, ?Shape) is nondet
Finds URI-Shape pairs by searching for GeoRSS Simple properties (e.g. georss:point, georss:line, georss:polygon) in the RDF database.
   92georss_simple_candidate(URI, Shape) :-
   93    georss_simple_candidate(URI, Shape, _).
   94
   95georss_simple_candidate(URI, Point, Source) :-
   96    rdf(URI, georss:point, PointStringLit, Source),
   97    parse_poslist_literal(PointStringLit,[Point]).
   98georss_simple_candidate(URI, linestring(Line), Source) :-
   99    rdf(URI, georss:line, LineStringLit, Source),
  100    parse_poslist_literal(LineStringLit,Line).
  101georss_simple_candidate(URI, polygon([Line]), Source) :-
  102    rdf(URI, georss:polygon, LineStringLit, Source),
  103    parse_poslist_literal(LineStringLit,Line).
  104georss_simple_candidate(URI, box(Line), Source) :-
  105    rdf(URI, georss:box, LineStringLit, Source),
  106    parse_poslist_literal(LineStringLit,Line).
  107georss_simple_candidate(URI, Circle, Source) :-
  108    rdf(URI, georss:circle, CenterRadiusLit, Source),
  109    parse_circle_literal(CenterRadiusLit,Circle).
  110
  111parse_poslist_literal(literal(Lit), Shape) :-
  112    atom_codes(Lit,LSC),
  113    phrase(poslist(Shape),LSC).
  114
  115parse_circle_literal(literal(Lit), Shape) :-
  116    atom_codes(Lit,CRC),
  117    phrase(circle(Shape),CRC).
  118
  119circle(circle(Center,Radius)) -->
  120    blanks, pos(Center), blank, blanks, float(Radius), blanks.
 georss_uri_shape_triple(+URI, +Shape, -Subject, -Predicate, -Object) is det
georss_uri_shape_triple(-URI, -Shape, +Subject, +Predicate, +Object) is det
Converts between a URI-Shape pair and its GeoRSS simple RDF triple form.
  127georss_uri_shape_triple(URI, Shape, URI, P, O) :-
  128    (   (   var(URI)
  129        ;   var(Shape)
  130        )
  131    ->  georss_simple_candidate(URI,Shape)
  132    ;   true
  133    ),
  134    functor(Shape,GeomType,_),
  135    georss_simple_predicate(GeomType, P),
  136    georss_simple_literal(Shape, O).
  137
  138georss_simple_predicate(point,P) :- rdf_equal(georss:point,P).
  139georss_simple_predicate(linestring,P) :- rdf_equal(georss:line,P).
  140georss_simple_predicate(linearring,P) :- rdf_equal(georss:line,P).
  141georss_simple_predicate(polygon,P) :- rdf_equal(georss:polygon,P).
  142
  143georss_simple_literal(X, literal(L)) :-
  144    phrase(coordinates(X), Atomics),
  145    atomic_list_concat(Atomics, ' ', L).
  146
  147coordinates(polygon([Coords|_])) --> !, point_list(Coords).
  148coordinates(linestring(Coords)) --> !, point_list(Coords).
  149coordinates(linearing(Coords)) --> !, point_list(Coords).
  150coordinates(Point) --> point(Point).
  151
  152point_list([]) --> [].
  153point_list([H|T]) -->
  154    point(H), point_list(T).
  155
  156point(Point) -->
  157    { Point =.. [ point | List ] },
  158    List.
  159
  160%
  161% GeoRSS GML
  162%
 georss_gml_candidate(?URI, ?Shape) is nondet
Finds URI-Shape pairs by searching for GeoRSS GML properties (i.e. georss:where) in the RDF database. Uses gml_shape/2 to parse the XMLLiteral representing the GML shape.
  170georss_gml_candidate(URI, Shape) :-
  171    georss_gml_candidate(URI, Shape, _).
  172
  173georss_gml_candidate(URI, Shape, Source) :-
  174    (   rdf_equal(georss:where, P)
  175    ;   rdf_equal(foaf:based_near, P)
  176    ),
  177    georss_gml_triple(URI, P, GML, Source),
  178    gml_shape(GML, Shape).
  179
  180georss_gml_triple(URI, Property, GML, Source) :-
  181    rdf(URI, Property, Lit, Source),
  182    gml_literal(Lit, GML).
  183
  184gml_literal(literal(type(_,GML)),GML) :- !.
  185gml_literal(literal(GML),GML).
  186
  187poslist(T) --> blank_star, poslist_plus(T), blank_star, !.
  188poslist_plus([H|T]) --> pos(H), poslist_star(T).
  189poslist_star(T) --> blank_plus, poslist(T).
  190poslist_star([]) --> [], !.
  191
  192pos(point(X,Y)) --> c(X), blank_plus, c(Y).
  193pos(point(X,Y,Z)) --> c(X), blank_plus, c(Y), blank_plus, c(Z).
  194pos(point(X,Y,Z,M)) --> c(X), blank_plus, c(Y), blank_plus, c(Z), blank_plus, c(M).
  195c(X) --> float(X).
  196
  197blank_plus --> blank, blank_star, !.
  198blank_plus --> " ", !.
  199blank_star --> blanks, !.
  200blank_star --> [], !