<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Ilya Boyandin &#187; scala</title>
	<atom:link href="http://blog.boyandi.net/category/scala/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.boyandi.net</link>
	<description>notes about ui, web development, visualization. links, tips and tricks</description>
	<lastBuildDate>Sat, 30 Jul 2011 22:04:35 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Resolving geographical entities with Scala</title>
		<link>http://blog.boyandi.net/2010/06/17/resolving-geographical-entities-with-scala/</link>
		<comments>http://blog.boyandi.net/2010/06/17/resolving-geographical-entities-with-scala/#comments</comments>
		<pubDate>Thu, 17 Jun 2010 09:25:00 +0000</pubDate>
		<dc:creator>Ilya Boyandin</dc:creator>
				<category><![CDATA[geo]]></category>
		<category><![CDATA[scala]]></category>

		<guid isPermaLink="false">http://blog.boyandi.net/?p=92</guid>
		<description><![CDATA[<p>Here is a chunk of Scala code for getting lat/lon coordinates of geographical locations (cities or villages) using the wonderful <a href="http://www.geonames.org/export/geonames-search.html">geonames.org</a> webservice which allows to search for geo entities by name. The getCoords() method returns a triple of (resolved entity name, lat, lon). The countryCode parameter is optional and will limit the search within [...]]]></description>
			<content:encoded><![CDATA[<p>Here is a chunk of Scala code for getting lat/lon coordinates of geographical locations (cities or villages) using the wonderful <a href="http://www.geonames.org/export/geonames-search.html">geonames.org</a> webservice which allows to search for geo entities by name. The getCoords() method returns a triple of (resolved entity name, lat, lon). The countryCode parameter is optional and will limit the search within the country when specified. The webservice often returns more than one result, in this case the method will return the coordinates of the first one.</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">import</span> scala.<span style="color: #000000;">xml</span>.<span style="color: #000080;">_</span>
<span style="color: #0000ff; font-weight: bold;">import</span> java.<span style="color: #000000;">io</span>.<span style="color: #000080;">_</span>
<span style="color: #0000ff; font-weight: bold;">import</span> java.<span style="color: #000000;">net</span>.<span style="color: #F78811;">&#123;</span>URLEncoder, URL<span style="color: #F78811;">&#125;</span>
&nbsp;
<span style="color: #0000ff; font-weight: bold;">def</span> getCoords<span style="color: #F78811;">&#40;</span>query<span style="color: #000080;">:</span>String, countryCode<span style="color: #000080;">:</span>String<span style="color: #F78811;">&#41;</span><span style="color: #000080;">:</span><span style="color: #F78811;">&#40;</span>String, Double, Double<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>
  <span style="color: #0000ff; font-weight: bold;">val</span> geonames <span style="color: #000080;">=</span> XML.<span style="color: #000000;">loadString</span><span style="color: #F78811;">&#40;</span>readUrl<span style="color: #F78811;">&#40;</span>
    <span style="color: #6666FF;">&quot;http://ws.geonames.org/search?featureClass=P&amp;q=&quot;</span> + URLEncoder.<span style="color: #000000;">encode</span><span style="color: #F78811;">&#40;</span>query<span style="color: #F78811;">&#41;</span> +
            <span style="color: #F78811;">&#40;</span><span style="color: #0000ff; font-weight: bold;">if</span> <span style="color: #F78811;">&#40;</span>countryCode <span style="color: #000080;">!=</span> <span style="color: #0000ff; font-weight: bold;">null</span><span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;&amp;country=&quot;</span> + countryCode<span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span>
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">if</span> <span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#40;</span>geonames \ <span style="color: #6666FF;">&quot;geoname&quot;</span><span style="color: #F78811;">&#41;</span>.<span style="color: #000000;">size</span>  <span style="color: #000080;">&gt;</span> <span style="color: #F78811;">0</span><span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>
    <span style="color: #0000ff; font-weight: bold;">var</span> firstGeoname <span style="color: #000080;">=</span> <span style="color: #F78811;">&#40;</span>geonames \ <span style="color: #6666FF;">&quot;geoname&quot;</span><span style="color: #F78811;">&#41;</span>.<span style="color: #000000;">first</span>
&nbsp;
    <span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#40;</span>firstGeoname \ <span style="color: #6666FF;">&quot;name&quot;</span><span style="color: #F78811;">&#41;</span>.<span style="color: #000000;">first</span>.<span style="color: #000000;">text</span> + <span style="color: #6666FF;">&quot; (&quot;</span> +
          <span style="color: #F78811;">&#40;</span>firstGeoname \ <span style="color: #6666FF;">&quot;countryCode&quot;</span><span style="color: #F78811;">&#41;</span>.<span style="color: #000000;">first</span>.<span style="color: #000000;">text</span> + <span style="color: #6666FF;">&quot;)&quot;</span>,
     <span style="color: #F78811;">&#40;</span>firstGeoname \ <span style="color: #6666FF;">&quot;lat&quot;</span><span style="color: #F78811;">&#41;</span>.<span style="color: #000000;">first</span>.<span style="color: #000000;">text</span>.<span style="color: #000000;">toDouble</span>,
     <span style="color: #F78811;">&#40;</span>firstGeoname \ <span style="color: #6666FF;">&quot;lng&quot;</span><span style="color: #F78811;">&#41;</span>.<span style="color: #000000;">first</span>.<span style="color: #000000;">text</span>.<span style="color: #000000;">toDouble</span><span style="color: #F78811;">&#41;</span>
  <span style="color: #F78811;">&#125;</span> <span style="color: #0000ff; font-weight: bold;">else</span> <span style="color: #F78811;">&#123;</span>
    <span style="color: #0000ff; font-weight: bold;">null</span>
  <span style="color: #F78811;">&#125;</span>
<span style="color: #F78811;">&#125;</span>
&nbsp;
<span style="color: #0000ff; font-weight: bold;">def</span> readUrl<span style="color: #F78811;">&#40;</span>url<span style="color: #000080;">:</span>String<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>
  <span style="color: #0000ff; font-weight: bold;">val</span> in <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">new</span> BufferedReader<span style="color: #F78811;">&#40;</span><span style="color: #0000ff; font-weight: bold;">new</span> InputStreamReader<span style="color: #F78811;">&#40;</span><span style="color: #0000ff; font-weight: bold;">new</span> URL<span style="color: #F78811;">&#40;</span>url<span style="color: #F78811;">&#41;</span>.<span style="color: #000000;">openStream</span><span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span>, <span style="color: #6666FF;">&quot;utf-8&quot;</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span>
  <span style="color: #0000ff; font-weight: bold;">val</span> response <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">new</span> StringBuilder
  <span style="color: #0000ff; font-weight: bold;">var</span> inputLine<span style="color: #000080;">:</span>String <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">null</span>
  <span style="color: #0000ff; font-weight: bold;">do</span> <span style="color: #F78811;">&#123;</span>
    inputLine <span style="color: #000080;">=</span> in.<span style="color: #000000;">readLine</span>
    <span style="color: #0000ff; font-weight: bold;">if</span> <span style="color: #F78811;">&#40;</span>inputLine <span style="color: #000080;">!=</span> <span style="color: #0000ff; font-weight: bold;">null</span><span style="color: #F78811;">&#41;</span> response.<span style="color: #000000;">append</span><span style="color: #F78811;">&#40;</span>inputLine<span style="color: #F78811;">&#41;</span>
  <span style="color: #F78811;">&#125;</span> <span style="color: #0000ff; font-weight: bold;">while</span> <span style="color: #F78811;">&#40;</span>inputLine <span style="color: #000080;">!=</span> <span style="color: #0000ff; font-weight: bold;">null</span><span style="color: #F78811;">&#41;</span>
  in.<span style="color: #000000;">close</span>
&nbsp;
  response.<span style="color: #000000;">toString</span>
<span style="color: #F78811;">&#125;</span></pre></div></div>

<p>Calling getCoords(&#8220;Neuchatel&#8221;, &#8220;CH&#8221;) returns a triple (Neuchâtel (CH),46.99179,6.931).</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.boyandi.net/2010/06/17/resolving-geographical-entities-with-scala/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

