2012年12月17日月曜日

Processing.jsを使ってみる

遅まきながらProcessingにハマり中.

Androidアプリに変換できるし,
Processing.jsがあればhtml5環境でもうごく.

まだまだ簡単なことしかできないけど,
色々と出来そうな感じが素晴らしい.


int angle;
void setup(){
  frameRate(100);
  size(400,400);
  PFont myFont = createFont("Osaka", 32);
  textFont(myFont);
  angle=0;
}

void draw(){
  background(0);
  pushMatrix();
  translate(width/2, height/2);
  angle=angle+5;
  rotate(angle*PI/180);
  text("おいしい", 0, 0);
  popMatrix();
  if(angle>360){
    angle=0;
  }
}
これが,こうなる↓

2012年12月3日月曜日

dbpedia.orgの内容をopenstreetmapに表示する

dbpedia.orgの内容をopenstreetmapに表示する実験.

本当はリアルタイムで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を出力する場合は「,」とある箇所をタブに書き換えればよい.