Skip to content

Instantly share code, notes, and snippets.

@mathias-goebel
Last active October 24, 2015 15:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mathias-goebel/7e1b68724feaf9ab1ba4 to your computer and use it in GitHub Desktop.
Save mathias-goebel/7e1b68724feaf9ab1ba4 to your computer and use it in GitHub Desktop.
xquery version "3.0";
import module namespace console="http://exist-db.org/xquery/console" at "java:org.exist.console.xquery.ConsoleModule";
declare namespace rdfs="http://www.w3.org/2000/01/rdf-schema#";
declare namespace ps="http://www.wikidata.org/prop/statement/";
declare namespace wdt="http://www.wikidata.org/prop/direct/";
declare namespace rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#";
declare option exist:serialize "method=xhtml media-type=application/vnd.google-earth.kml+xml highlight-matches=none";
declare function local:get($cat as xs:string, $continue as xs:string){
(: max 500 items returned on cmlist query :)
(: max 50 items to be passed as pageIds :)
let $interval := 50
let $data := doc('https://de.wikipedia.org/w/api.php?action=query&list=categorymembers&cmtitle=Category:'||encode-for-uri($cat)||'&format=xml&cmlimit='|| string($interval) ||'&cmcontinue='||encode-for-uri($continue))
let $ids := $data//cm/string(@pageid)
let $pages:= doc('https://de.wikipedia.org/w/api.php?action=query&prop=pageprops&format=xml&pageids='||encode-for-uri( string-join($ids, '|') ))
return
(
$pages//page
,if($data//continue) then local:get($cat, string($data//continue/@cmcontinue)) else ()
)
};
declare function local:wikidatardf($Q as xs:string){
doc('https://www.wikidata.org/wiki/Special:EntityData/' || $Q ||'.rdf')
};
let $start := console:log(current-time())
let $cat := request:get-parameter('cat', 'DDR-Bürger')
let$kml :=
<kml>
{
for $item at $pos in local:get($cat,'')
let $log:= console:log('kml: '||$pos)
let $wikidataPerson := if(string($item/pageprops/@wikibase_item) = '') then () else local:wikidatardf( string($item/pageprops/@wikibase_item) )
let $wikidataPlace := for $item in $wikidataPerson//wdt:P20/substring-after(@rdf:resource, '/entity/') where $item != '' return ( local:wikidatardf($item))
let $coordinates := $wikidataPlace//ps:P625/substring-after(substring-before(., ')'), 'Point(')
let $coordinates := tokenize($coordinates, ' ')[2]||', '||tokenize($coordinates, ' ')[1]
where $coordinates != ', '
order by $item/pageprops/@defaultsort
return
<Placemark>
<address>{($wikidataPlace//rdfs:label[@xml:lang="de"]/string())[1]}</address>
<description>
{codepoints-to-string( string-to-codepoints('<') )||string('![CDATA[')}
<a href="https://de.wikipedia.org/wiki/{encode-for-uri(replace(string($item/@title), ' ', '_')) }" target='_blank'>
{string($item/@title)}
</a>
{string(']]')||codepoints-to-string( string-to-codepoints('>') )}
</description>
<name>{string($item/@title)}</name>
<Point>
<coordinates>{$coordinates}</coordinates>
</Point>
<TimeStamp>
<when>{$wikidataPerson//wdt:P570/string()}</when>
</TimeStamp>
</Placemark>
}
</kml>
return $kml
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment