RでSPARQLが使えないかと検索してみたら、あった。
パッケージ名はそのままのSPARQL。
さっそく、dbpedia.orgのEndpointを使ってテストしてみた。
クエリは以前に使った日本の都道府県の人口を検索するもの。
せっかくなので、検索結果はKMLで出力する。
Google Earthで表示してみると、こんな感じ。
ちょっとデータのウェブを使っている感じがある。
昨今のオープンデータをSPARQLで探してこれるようにすると、面白いかもしれない。(read.csv等で読みこめばいいのだけど、ファイル自体を探してこないといけない)
むかしExcelをRDFに変換して、SPARQLで検索、グラフで表示ということをやっていたけど、あの苦労が嘘のよう。
以下はRのソース。
library(SPARQL)
library("sp")
library("rgdal")
url<-"http://dbpedia.org/sparql"
query<-"PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX wgs84_pos: <http://www.w3.org/2003/01/geo/wgs84_pos#>
PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>
PREFIX yago: <http://dbpedia.org/class/yago/>
SELECT DISTINCT ?title ?lat ?lon ?populationTotal where{
?s rdf:type yago:PrefecturesOfJapan .
?s rdfs:label ?title .
? S dbpedia-owl: populationTotal ?populationTotal .
?s wgs84_pos:lat ?lat .
?s wgs84_pos:long ?lon .
filter langMatches(lang(?title),\"en\") .
}"
res<-SPARQL(url,query)
d<-res$result
coordinates(d)<-c('lon','lat')
proj4string(d)<-CRS("+init=epsg:2451")
writeOGR(d, dsn="hoge.kml", layer="sample", driver="KML")
2013年11月10日日曜日
2012年12月3日月曜日
dbpedia.orgの内容をopenstreetmapに表示する
dbpedia.orgの内容をopenstreetmapに表示する実験.
本当はリアルタイムでdbpedia.org endpointに問い合わせられるといいのだけど,
何となく負荷が気になるので
今回は問い合わせ結果をSPARQL Result XML Formatに保存し,
それをopenstreetmapに表示する.
まずはdbpedia.orgへの問い合わせ.
SPARQLクエリは以下のとおり.
日本の都道府県を検索しその概要と緯度経度情報を取得するクエリである.
この結果をOpenLayers.Layer.Text()を使って表示する.
OPTIONALはかなり強引・・・
先のクエリをdbpedia.orgのendpointに投げるとこんな感じのSPARQL Result XML Formatが帰ってくる.
これを先のポストで作成したXSLでTSVに変換し,
OpenLayers.Layer.Text()で読み込めば完成.
XSLTはphpで実行している.
うん.いい感じ.
北海道がないのはクエリにある
?s rdf:type yago:PrefecturesOfJapan .
の所為.
なんでかな.
本当はリアルタイムでdbpedia.org endpointに問い合わせられるといいのだけど,
何となく負荷が気になるので
今回は問い合わせ結果をSPARQL Result XML Formatに保存し,
それをopenstreetmapに表示する.
まずはdbpedia.orgへの問い合わせ.
SPARQLクエリは以下のとおり.
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX wgs84_pos: <http://www.w3.org/2003/01/geo/wgs84_pos#>
PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>
PREFIX yago: <http://dbpedia.org/class/yago/>
select ?lat ?lon ?title ?description ?icon ?iconSize ?iconOffset where{
?s rdf:type yago:PrefecturesOfJapan .
?s rdfs:label ?title .
?s dbpedia-owl:abstract ?description .
?s wgs84_pos:lat ?lat .
?s wgs84_pos:long ?lon .
filter (lang(?title)="en") .
filter (lang(?description)="en").
optional {
?s <urn:hoge> ?icon .
?s <urn:hoge2> ?iconSize .
?s <urn:hoge3> ?iconOffset .
}
}
日本の都道府県を検索しその概要と緯度経度情報を取得するクエリである.
この結果をOpenLayers.Layer.Text()を使って表示する.
OPTIONALはかなり強引・・・
先のクエリをdbpedia.orgのendpointに投げるとこんな感じのSPARQL Result XML Formatが帰ってくる.
これを先のポストで作成したXSLでTSVに変換し,
OpenLayers.Layer.Text()で読み込めば完成.
XSLTはphpで実行している.
うん.いい感じ.
北海道がないのはクエリにある
?s rdf:type yago:PrefecturesOfJapan .
の所為.
なんでかな.
2012年12月2日日曜日
XSLTを使ってSPARQL Query Results XML FormatをCSV/TSV形式にする
SPARQL Query Results XML FormatにあるXSLTサンプルresult-to-html.xslを少し書き換えてCSV/TSVを出力するようにしてみた.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet exclude-result-prefixes="res xsl" version="1.0" xmlns:res="http://www.w3.org/2005/sparql-results#" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml">
<xsl:output encoding="UTF-8" method="text">
<xsl:template name="boolean-result">
<xsl:value-of select="res:boolean">
</xsl:value-of></xsl:template>
<xsl:template name="vb-result">
<xsl:for-each select="res:head/res:variable">
<xsl:value-of select="@name">
<xsl:text>,</xsl:text>
</xsl:value-of></xsl:for-each>
<xsl:for-each select="res:results/res:result">
<xsl:text>
</xsl:text>
<xsl:apply-templates select=".">
</xsl:apply-templates></xsl:for-each>
</xsl:template>
<xsl:template match="res:result">
<xsl:variable name="current" select=".">
<xsl:for-each select="//res:head/res:variable">
<xsl:variable name="name" select="@name">
<xsl:choose>
<xsl:when test="$current/res:binding[@name=$name]">
<xsl:apply-templates select="$current/res:binding[@name=$name]">
</xsl:apply-templates></xsl:when>
<xsl:otherwise>
</xsl:otherwise>
</xsl:choose>
<xsl:text>,</xsl:text>
</xsl:variable></xsl:for-each>
</xsl:variable></xsl:template>
<xsl:template match="res:bnode">
<xsl:value-of select="text()">
</xsl:value-of></xsl:template>
<xsl:template match="res:uri">
<xsl:variable name="uri" select="text()">
<xsl:value-of select="$uri">
</xsl:value-of></xsl:variable></xsl:template>
<xsl:template match="res:literal">
<xsl:choose>
<!--
<xsl:when test="@datatype">
<xsl:value-of select="text()"/> (datatype <xsl:value-of select="@datatype"/> )
</xsl:when>
<xsl:when test="@xml:lang">
<xsl:value-of select="text()"/> @ <xsl:value-of select="@xml:lang"/>
</xsl:when>
<xsl:when test="string-length(text()) != 0">
<xsl:value-of select="text()"/>
</xsl:when>
<xsl:when test="string-length(text()) = 0">
<xsl:text></xsl:text>
</xsl:when>
-->
<xsl:when test="string-length(text()) != 0">
<xsl:value-of select="text()">
</xsl:value-of></xsl:when>
</xsl:choose>
</xsl:template>
<xsl:template match="res:sparql">
<xsl:choose>
<xsl:when test="res:boolean">
<xsl:call-template name="boolean-result">
</xsl:call-template></xsl:when>
<xsl:when test="res:results">
<xsl:call-template name="vb-result">
</xsl:call-template></xsl:when>
</xsl:choose>
</xsl:template>
</xsl:output>
</xsl:stylesheet>
TSVを出力する場合は「,」とある箇所をタブに書き換えればよい.2012年11月24日土曜日
SPARQL ResultをSIMILE Timeline Widgetで表示する
昔からあるツールだけど時系列データの可視化ツール「SIMILE Timeline Widget」はSPARQLによる問い合わせ結果をそのまま表示できるらしい.
さっそく試してみる.
問い合わせは先はdbpedia.org.
SPARQL endpointはhttp://dbpedia.org/sparqlを利用する.
問い合わせ文は以下のとおり.
Timelineの方は以下のとおり.
結果波以下のとおり.
うーん.こんなことが簡単にできるなんて,
色々と使えそう.
さっそく試してみる.
問い合わせは先はdbpedia.org.
SPARQL endpointはhttp://dbpedia.org/sparqlを利用する.
問い合わせ文は以下のとおり.
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>室町時代の日本人の生没年と解説を検索する.
PREFIX rdfs:<http://www.w3.org/2000/01/rdf-schema#>
PREFIX foaf:<http://xmlns.com/foaf/0.1/>
PREFIX dbpprop: <http://dbpedia.org/property/>
select * where {
?s rdf:type foaf:Person .
?s rdf:type <http://dbpedia.org/class/yago/PeopleOfMuromachiPeriodJapan> .
?s rdfs:label ?title.
filter(lang(?title)="ja").
?s rdfs:comment ?description.
filter(lang(?description)="ja").
?s dbpprop:dateOfBirth ?start .
?s dbpprop:dateOfDeath ?end .
?s foaf:isPrimaryTopicOf ?link .
}
Timelineの方は以下のとおり.
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <meta charset="utf-8" /> | |
| <title>Timeline demo</title> | |
| <!--[if IE]> | |
| <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script> | |
| <![endif]--> | |
| <script src="libs/timeline/api/timeline-api.js" type="text/javascript"></script> | |
| <script type="text/javascript"> | |
| var tl; | |
| function onLoad() { | |
| var eventSource = new Timeline.DefaultEventSource(); | |
| var bandInfos = [ | |
| Timeline.createBandInfo({ | |
| eventSource: eventSource, | |
| date: "Jun 1 1534 00:00:00", | |
| width: "80%", | |
| intervalUnit: Timeline.DateTime.YEAR, | |
| intervalPixels: 10 | |
| }), | |
| Timeline.createBandInfo({ | |
| overview: true, | |
| eventSource: eventSource, | |
| date: "Jun 1 1534 00:00:00", | |
| width: "20%", | |
| intervalUnit: Timeline.DateTime.YEAR, | |
| intervalPixels: 200 | |
| }) | |
| ]; | |
| bandInfos[1].syncWith = 0; | |
| bandInfos[1].highlight = true; | |
| tl = Timeline.create(document.getElementById("my-timeline"), bandInfos); | |
| //var sparql="/lod/result01.xml"; var sparql="http://dbpedia.org/sparql?query=以降省略"; | |
| Timeline.loadXML(sparql,function(xml,url){eventSource.loadSPARQL(xml,url);}); | |
| } | |
| var resizeTimerID = null; | |
| function onResize() { | |
| if (resizeTimerID == null) { | |
| resizeTimerID = window.setTimeout(function() { | |
| resizeTimerID = null; | |
| tl.layout(); | |
| }, 500); | |
| } | |
| } | |
| </script> | |
| <style> | |
| </style> | |
| </head> | |
| <body onload="onLoad();" onresize="onResize();"> | |
| <div id="my-timeline" style="height: 600px; width: 800px; border:1px solid #aaa"></div> | |
| <noscript> | |
| This page uses Javascript to show you a Timeline. Please enable Javascript in your browser to see the full page. Thank you. | |
| </noscript> | |
| </body> | |
| </html> |
結果波以下のとおり.
うーん.こんなことが簡単にできるなんて,
色々と使えそう.
登録:
投稿 (Atom)

