<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <title>Mollusca Code - Home</title>
  <id>tag:blog.mollusca.ch,2009:mephisto/</id>
  <generator uri="http://mephistoblog.com" version="0.8.0">Mephisto Drax</generator>
  <link href="http://blog.mollusca.ch/feed/atom.xml" rel="self" type="application/atom+xml"/>
  <link href="http://blog.mollusca.ch/" rel="alternate" type="text/html"/>
  <updated>2009-08-22T12:59:48Z</updated>
  <entry xml:base="http://blog.mollusca.ch/">
    <author>
      <name>silvio</name>
    </author>
    <id>tag:blog.mollusca.ch,2009-08-22:992</id>
    <published>2009-08-22T12:44:00Z</published>
    <updated>2009-08-22T12:59:48Z</updated>
    <category term="Java"/>
    <category term="jRuby"/>
    <category term="Ruby"/>
    <category term="imports"/>
    <category term="java"/>
    <category term="jruby"/>
    <link href="http://blog.mollusca.ch/2009/8/22/importer-v0-2" rel="alternate" type="text/html"/>
    <title>Importer v0.2</title>
<content type="html">
            &lt;p&gt;Danlucraft pointed something out the other day: Why would Importer require that you use a hash?
And of course he was right. So after a little review: Importer, second iteration:&lt;/p&gt;


&lt;pre class=&quot;ruby&quot;&gt;
module Importer

  module ClassMethods
    def importer(package, classes)
        classes.each do |item|
          path = package + &quot;.&quot; + item
          import path
        end
    end
  end

  def self.included(receiver)
    receiver.extend ClassMethods
  end

end
&lt;/pre&gt;

	&lt;p&gt;This will remove the pesky hash notation:&lt;/p&gt;


&lt;pre class=&quot;ruby&quot;&gt;
require 'importer'

class Banzaiii
  include Importer
  importer 'org.eclipse.swt.widgets', \
      %w{Display, Shell, Button, Canvas}
  importer 'org.eclipse.swt', ['SWT']

  def build
    puts SWT::BOLD | SWT::ITALIC 
  end
end
&lt;/pre&gt;

Maybe this will evolve into something that enables you to do the follwing:
&lt;pre class=&quot;ruby&quot;&gt;
class Widgets
  include Importer
  importer 'org.eclipse.swt.widgets.*'
end

# and thus
canvas = Widgets::Canvas.new
&lt;/pre&gt;

	&lt;p&gt;Which is exactly what is currently missing from jruby&#8230;&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://blog.mollusca.ch/">
    <author>
      <name>silvio</name>
    </author>
    <id>tag:blog.mollusca.ch,2009-08-13:868</id>
    <published>2009-08-13T14:01:00Z</published>
    <updated>2009-08-13T14:23:18Z</updated>
    <category term="jRuby"/>
    <category term="Ruby"/>
    <category term="import"/>
    <category term="jruby"/>
    <category term="ruby"/>
    <link href="http://blog.mollusca.ch/2009/8/13/cleaning-up-imports-in-jruby" rel="alternate" type="text/html"/>
    <title>Cleaning up imports in jRuby</title>
<content type="html">
            &lt;h2&gt;Import Puts Noise In My Class&lt;/h2&gt;


	&lt;p&gt;Today I stumbled accross something like this:&lt;/p&gt;


&lt;pre class=&quot;ruby&quot;&gt;
class SwtBuilder

  import org.eclipse.swt.widgets.Display
  import org.eclipse.swt.widgets.Shell
  import org.eclipse.swt.widgets.Button
  import org.eclipse.swt.widgets.Canvas
  import org.eclipse.swt.SWT

  import ch.mollusca.foo.Bar
  import ch.mollusca.foo.Jiffy

  def build
    puts SWT::BOLD | SWT::ITALIC
  end

end

SwtBuilder.new.build
&lt;/pre&gt;

Alas, jRuby doesn&#8217;t support
&lt;pre class=&quot;ruby&quot;&gt;
import org.eclipse.swt.widgets.*
&lt;/pre&gt;
there is plenty of noise throughout this class definition. It&#8217;s  a lot of vertical clutter and I don&#8217;t like it. This will even get worse for any class that ties together a few java classes.

	&lt;h2&gt;Enter Importer&lt;/h2&gt;


	&lt;p&gt;I tried to figure out a way to clean this up. Maybe I&#8217;ve hit the spot, but I think it&#8217;s not quite there yet. Anyway:&lt;/p&gt;


&lt;pre class=&quot;ruby&quot;&gt;
module Importer

  module ClassMethods
    def importer(hash)
      unless hash[:path] &#38;&#38; hash[:items]
        puts &quot;need to specify path and items!&quot; 
      else
        hash[:items].each do |item|
          path = hash[:path] + &quot;.&quot; + item
          import path
        end
      end
    end
  end

  def self.included(receiver)
    receiver.extend ClassMethods
  end

end
&lt;/pre&gt;

	&lt;p&gt;This allows me to write:&lt;/p&gt;


&lt;pre class=&quot;ruby&quot;&gt;
class SwtBuilder

  include Importer
  importer :path =&amp;gt; 'org.eclipse.swt.widgets', \
    :items =&amp;gt; ['Display', 'Shell', 'Button', 'Canvas']
  importer :path =&amp;gt; 'org.eclipse.swt', :items =&amp;gt; ['SWT']

  def build
    puts SWT::BOLD | SWT::ITALIC
  end

end

SwtBuilder.new.build
&lt;/pre&gt;

	&lt;p&gt;It&#8217;s formatted in two lines for layout reasons, but in principle can nicely be written on one line.
Any comments on the idea?&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://blog.mollusca.ch/">
    <author>
      <name>silvio</name>
    </author>
    <id>tag:blog.mollusca.ch,2009-06-17:524</id>
    <published>2009-06-17T10:09:00Z</published>
    <updated>2009-06-17T10:36:40Z</updated>
    <category term="Grails"/>
    <category term="Groovy"/>
    <category term="Java"/>
    <category term="classnotfoundexception"/>
    <category term="exception"/>
    <category term="grails"/>
    <category term="jdbc"/>
    <link href="http://blog.mollusca.ch/2009/6/17/osx-java-update-breaks-postgresql-jdbc4" rel="alternate" type="text/html"/>
    <title>OSX Java Update breaks PostgreSQL JDBC4</title>
<content type="html">
            &lt;p&gt;It seems like the latest Java update from Apple breaks the &lt;span class=&quot;caps&quot;&gt;JDBC4&lt;/span&gt; version of the postgresql database driver.&lt;/p&gt;


	&lt;p&gt;I currently have two Grails apps doing similar things. One (by accident or luck, your call) uses postgresql-604.jdbc3 as it&#8217;s database driver.
The other one uses a different driver, namely postgres-604-jdbc4.&lt;/p&gt;


	&lt;p&gt;Today I updated my installed Java on the Mac using software update.&lt;/p&gt;


	&lt;p&gt;Suddenly one of the Grails apps was broken.&lt;/p&gt;


	&lt;p&gt;.... &lt;strong&gt;tries to work out what has gone wrong&lt;/strong&gt;&lt;/p&gt;


	&lt;p&gt;I got a ClassNotFound Exception while loading the driver. So I tried to figure out where the two apps differ.
It turns out that the only relevant difference between the two apps (database-driver-wise) is that one uses jdbc3 and the other jdbc4.&lt;/p&gt;


	&lt;p&gt;I&#8217;m not blaming anyone, but I suspect the update broke it.&lt;/p&gt;


	&lt;p&gt;So if anyone faces the same problem while running a Grails app, there you go: Replace jdbc4 with jdbc3!&lt;/p&gt;


	&lt;p&gt;And now part of the stacktrace for googleability:&lt;/p&gt;


&lt;pre&gt;
Starting integration tests ...
Cannot load JDBC driver class 'org.postgresql.Driver'
java.lang.ClassNotFoundException: org.postgresql.Driver
    at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:316)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:280)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
    at org.codehaus.groovy.grails.cli.support.GrailsRootLoader.loadClass(GrailsRootLoader.java:45)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
    at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:374)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:164)
&lt;/pre&gt;

&lt;h2&gt;Addendum&lt;/h2&gt;


	&lt;p&gt;And &lt;strong&gt;yes&lt;/strong&gt;, I&#8217;m aware of the fact that the &lt;span class=&quot;caps&quot;&gt;JVM&lt;/span&gt; on &lt;span class=&quot;caps&quot;&gt;OSX&lt;/span&gt; is 1.5. But previously it used to work, so I figured this might still be of some use to some people. Maybe &lt;span class=&quot;caps&quot;&gt;JDBC4&lt;/span&gt; was a bad idea all along the way, however.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://blog.mollusca.ch/">
    <author>
      <name>silvio</name>
    </author>
    <id>tag:blog.mollusca.ch,2009-05-11:484</id>
    <published>2009-05-11T07:59:00Z</published>
    <updated>2009-05-11T08:06:21Z</updated>
    <category term="GIS"/>
    <category term="Grails"/>
    <category term="Groovy"/>
    <category term="grails"/>
    <category term="postgis"/>
    <category term="spatial"/>
    <link href="http://blog.mollusca.ch/2009/5/11/grails-spatial-data-2-0" rel="alternate" type="text/html"/>
    <title>Grails + Spatial Data 2.0</title>
<content type="html">
            &lt;p&gt;This is intended as a follow up to this artile: &lt;a href=&quot;http://blog.mollusca.ch/2008/10/4/grails-spatial-data-postgis&quot;&gt;Grails + Spatial Data&lt;/a&gt;&lt;/p&gt;


	&lt;p&gt;We recently discovered (actually we were nudged in the right direction&#8230;) that one can completely do away with the manual hibernate mapping, by adding the following code to the domain class that contains spatial data:&lt;/p&gt;


&lt;pre class=&quot;java&quot;&gt;
class Event {
  String title
  String description
  Url url
  Point spatial

  static mapping = {
    columns {
      spatial type: org.hibernatespatial.GeometryUserType
    }
  }
}
&lt;/pre&gt;

	&lt;p&gt;I think it doesn&#8217;t get any sweeter than that.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://blog.mollusca.ch/">
    <author>
      <name>silvio</name>
    </author>
    <id>tag:blog.mollusca.ch,2009-04-27:459</id>
    <published>2009-04-27T14:22:00Z</published>
    <updated>2009-04-27T14:29:23Z</updated>
    <category term="Random"/>
    <category term="code"/>
    <link href="http://blog.mollusca.ch/2009/4/27/the-rules-of-code-club" rel="alternate" type="text/html"/>
    <title>The rules of Code Club</title>
<content type="html">
            &lt;p&gt;Something I just came up with, while typing for my students:&lt;/p&gt;


&lt;pre&gt;
These are the rules of Code Club:
---------------------------------
1)  The first rule of Code Club is, you do talk at Code Club.
2)  The second rule of Code Club is, you DO TALK at Code Club.
3)  If someone forgets to write a test first, coding is over.
4)  Two guys to a screen.
5)  One thing at a time. Especially in classes and methods.
5)  Coding will go on as long, it takes to pass the tests.
6)  If this is your first day at Code Club, you have to code.
&lt;/pre&gt;
          </content>  </entry>
  <entry xml:base="http://blog.mollusca.ch/">
    <author>
      <name>silvio</name>
    </author>
    <id>tag:blog.mollusca.ch,2009-03-09:413</id>
    <published>2009-03-09T20:16:00Z</published>
    <updated>2009-03-09T20:35:52Z</updated>
    <category term="Image editing"/>
    <category term="Photography"/>
    <link href="http://blog.mollusca.ch/2009/3/9/how-much-info-is-there-in-a-raw-file" rel="alternate" type="text/html"/>
    <title>How much info is there in a RAW-File?</title>
<content type="html">
            &lt;p&gt;I&#8217;m simply amazed once again, how much information there is in a single &lt;span class=&quot;caps&quot;&gt;RAW&lt;/span&gt;-file. I with my humble editing skills am able to pull of stunts like the following:&lt;/p&gt;


	&lt;p&gt;Image the way that Canon &lt;span class=&quot;caps&quot;&gt;DPP&lt;/span&gt; and Lightroom both agreed more or less agreed should look like:&lt;/p&gt;


	&lt;p&gt;&lt;img src=&quot;http://blog.mollusca.ch/assets/2009/3/9/IMG_7926.jpg&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;


	&lt;p&gt;Here&#8217;s what there is to do to get the following result:&lt;/p&gt;


	&lt;p&gt;&lt;img src=&quot;http://blog.mollusca.ch/assets/2009/3/9/IMG_7926-Edit.jpg&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;


	&lt;ol&gt;
	&lt;li&gt;The white balance is off. I used a flash, but bounced it off a wall, so it&#8217;s not the cameras, DPPs or LRs fault, that it&#8217;s set to &#8220;flash&#8221; &lt;/li&gt;
		&lt;li&gt;Tweak in LR &#8216;till cooked right
	&lt;ol&gt;
	&lt;li&gt;Pull up exposure a bit.&lt;/li&gt;
		&lt;li&gt;Adjust white balance.&lt;/li&gt;
		&lt;li&gt;Pull up the exposure some more, so we have a black spot and a white spot (at least something close to both)&lt;/li&gt;
	&lt;/ol&gt;
	&lt;/li&gt;
		&lt;li&gt;Off to editing in &lt;span class=&quot;caps&quot;&gt;LAB&lt;/span&gt; mode
	&lt;ol&gt;
	&lt;li&gt;Tweak curve of L-channel to get the highlights punchy.&lt;/li&gt;
		&lt;li&gt;Some more monkeying around with the L-Curve&lt;/li&gt;
		&lt;li&gt;Apply very moderate unsharp mask&lt;/li&gt;
		&lt;li&gt;steepen the b-curve a tad, remove slight color cast at the same time.&lt;/li&gt;
	&lt;/ol&gt;
	&lt;/li&gt;
		&lt;li&gt;done.&lt;/li&gt;
	&lt;/ol&gt;
          </content>  </entry>
  <entry xml:base="http://blog.mollusca.ch/">
    <author>
      <name>silvio</name>
    </author>
    <id>tag:blog.mollusca.ch,2008-12-11:37</id>
    <published>2008-12-11T16:18:00Z</published>
    <updated>2008-12-11T16:23:03Z</updated>
    <category term="Books"/>
    <category term="Grails"/>
    <category term="Groovy"/>
    <category term="books"/>
    <category term="groovy"/>
    <category term="review"/>
    <link href="http://blog.mollusca.ch/2008/12/11/programming-groovy" rel="alternate" type="text/html"/>
    <title>Programming Groovy</title>
<content type="html">
            &lt;p&gt;I will probably not this a &lt;em&gt;lot&lt;/em&gt;, but this one is necessary. I just received my copy of the book &#8220;Programming Groovy&#8221; by Venkat Subramaniam, read it through in a few days and I gotta say:&lt;/p&gt;


	&lt;p&gt;&lt;strong&gt;It&#8217;s awesome!&lt;/strong&gt;&lt;/p&gt;


	&lt;p&gt;Very clear and concise explanations. Even MOPping is explained in very short and clear examples. I like that!&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://blog.mollusca.ch/">
    <author>
      <name>silvio</name>
    </author>
    <id>tag:blog.mollusca.ch,2008-10-17:10</id>
    <published>2008-10-17T20:15:00Z</published>
    <updated>2008-10-17T21:48:22Z</updated>
    <category term="Linux"/>
    <link href="http://blog.mollusca.ch/2008/10/17/my-new-notebook-has-arrived" rel="alternate" type="text/html"/>
    <title>My new notebook has arrived!</title>
<content type="html">
            &lt;p&gt;Yay! I&#8217;ll have another beer on that.&lt;/p&gt;


	&lt;p&gt;The new notebook has arrived. Behold! Here goes the Lenovo x301.&lt;/p&gt;


	&lt;p&gt;&lt;img src=&quot;http://blog.mollusca.ch/assets/2008/10/17/thinkpad-x301.jpg&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;


	&lt;p&gt;One of the nicest pieces of technology I&#8217;ve seen. This eats Airbooks for breakfast.&lt;/p&gt;


	&lt;p&gt;Here&#8217;s a few point to consider:&lt;/p&gt;


	&lt;ul&gt;
	&lt;li&gt;1440&#215;900 resolution&lt;/li&gt;
		&lt;li&gt;&lt;span class=&quot;caps&quot;&gt;DVD&lt;/span&gt;-Burner&lt;/li&gt;
		&lt;li&gt;Solid State Disk (hell yeah&#8230;)&lt;/li&gt;
		&lt;li&gt;more than one &lt;span class=&quot;caps&quot;&gt;USB&lt;/span&gt; port (pokes Airbook)&lt;/li&gt;
		&lt;li&gt;&lt;span class=&quot;caps&quot;&gt;VGA&lt;/span&gt; Port&lt;/li&gt;
		&lt;li&gt;DisplayPort port (that one will rock)&lt;/li&gt;
		&lt;li&gt;Awesome speakers. Best I&#8217;ve heard so far in any notebook.&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;Since I ordered it without an operating system, I installed my favourite i686-distro: &lt;a href=&quot;http://www.archlinux.org&quot;&gt;Archlinux&lt;/a&gt;.
Install went really smoothly. Everything that matters to me seems to be working. Had a few hickups of Xorg along the way, but I think it&#8217;s fine now.
Did I mention the keyboard rocks? And that it&#8217;s only 1.4kg?? :)&lt;/p&gt;


	&lt;p&gt;I will definitely post some more on this. &lt;span class=&quot;caps&quot;&gt;KDE 4&lt;/span&gt;.1 is running smothly and boot times are ridiculously short. The &lt;span class=&quot;caps&quot;&gt;SSD&lt;/span&gt; is definitely the way to go, if you want fast load times. That&#8217;s true for software loading time as well. This thingie loads firefox faster than my desktop&#8230;&lt;/p&gt;


	&lt;p&gt;Some more geeking on this mervelous piece now&#8230;&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://blog.mollusca.ch/">
    <author>
      <name>silvio</name>
    </author>
    <id>tag:blog.mollusca.ch,2008-10-16:9</id>
    <published>2008-10-16T08:18:00Z</published>
    <updated>2009-07-25T19:17:59Z</updated>
    <category term="GIS"/>
    <category term="Grails"/>
    <category term="Java"/>
    <link href="http://blog.mollusca.ch/2008/10/16/calling-stored-procedures-from-grails" rel="alternate" type="text/html"/>
    <title>Calling stored procedures from Grails</title>
<content type="html">
            &lt;p&gt;Calling stored procedures from Grails is not that hard to achieve. One needs to wade through the &lt;acronym title=&quot;Hibernate Query Language&quot;&gt;HQL&lt;/acronym&gt; documentation though, which I find slightly counter-intuitive. I&#8217;d rather see stuff like that on the grails page.&lt;/p&gt;


	&lt;p&gt;So, how do we call a stored procedure then?&lt;/p&gt;


	&lt;h2&gt;What stored procedure?&lt;/h2&gt;


Here&#8217;s the situation:
	&lt;ul&gt;
	&lt;li&gt;The events from &lt;a href=&quot;http://blog.mollusca.ch/2008/10/16/grails-spatial-data-postgis&quot;&gt;the last post&lt;/a&gt; are in the database now.&lt;/li&gt;
		&lt;li&gt;The database has been spatially enabled according to the PostGIS plan.&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;We now want to find the closest events, to a certain point on the earth. Let&#8217;s assume we have successfully geocoded the point, so we have its coordinates in lat;long form.&lt;/p&gt;


	&lt;p&gt;The basic function that we want to call in plain &lt;span class=&quot;caps&quot;&gt;SQL&lt;/span&gt; is:&lt;/p&gt;


&lt;pre class=&quot;sql&quot;&gt;
select * from event e
  order by distance_sphere(
  Geometry('POINT(lat, long)'),e.location_id)
&lt;/pre&gt;

	&lt;p&gt;The &lt;em&gt;distance_sphere()&lt;/em&gt; function calculates the distance of two points on the earth given in lat;long form. However the function takes two argumenty of type &lt;em&gt;geometry&lt;/em&gt;, so we have to create one first.&lt;/p&gt;


	&lt;h2&gt;Calling it from grails&lt;/h2&gt;


	&lt;p&gt;To call this function from grails and get a nice an clean Collection of objects back (we don&#8217;t want to mess with &lt;em&gt;ResultSet&lt;/em&gt;, do we?) we have to grab a hold of the current hibernate session. This is pretty easy: We just have it injected by grails:&lt;/p&gt;


Add the following line to the &lt;em&gt;EventController&lt;/em&gt; class:
&lt;pre class=&quot;ruby&quot;&gt;
def sessionFactory
&lt;/pre&gt;

	&lt;p&gt;The &lt;em&gt;sessionFactory&lt;/em&gt; object is now defined inside of this controller and will have its value injected by grails. Assuming that the function is called by some form using &lt;span class=&quot;caps&quot;&gt;POST&lt;/span&gt;, the code for our controller function boils down to this:&lt;/p&gt;


&lt;pre class=&quot;ruby&quot;&gt;
def searchByLocation = {
  if (request.method == &quot;POST&quot;){
     def lat = params['lat']
     def long = params['long']

     def session = sessionFactory.currentSession

     def sql = &quot;select * from &quot; +
     &quot;event e order by distance_sphere(setsrid&quot; +
     &quot;(geometry('POINT(${lat} ${long})'), 0),&quot; +
     &quot;e.location_id) limit 20;&quot; 

     def results = session.createSQLQuery(sql).
       addEntity(Event.class).list()
  }
}
&lt;/pre&gt;

	&lt;p&gt;The &lt;em&gt;setSRID()&lt;/em&gt; function from PostGIS has to be called, because by default grails in combination with hibernatespatial stores the points with the &lt;span class=&quot;caps&quot;&gt;SRID&lt;/span&gt; value set to 0. This is no problem, as long as all coordinates you save are in the same reference system and you do not have to do any coordinate transformation. Otherwise you would have to handle that as well.&lt;/p&gt;


	&lt;p&gt;What&#8217;s nice about this method of calling the stored procedure through hibernate is that by calling &lt;em&gt;addEntity()&lt;/em&gt; we can set the returned type and calling &lt;em&gt;list()&lt;/em&gt; we get a nice and clean java collection.&lt;/p&gt;


	&lt;p&gt;That&#8217;s it so far&#8230;&lt;/p&gt;


	&lt;h2&gt;Important note (This is new)&lt;/h2&gt;


	&lt;p&gt;As pointed out by Matt, this code is vulnerable to &lt;span class=&quot;caps&quot;&gt;SQL&lt;/span&gt;-Injection type attacks, if used directly like that in a setting where this is of any use to the attacker.&lt;/p&gt;


	&lt;p&gt;Please, with sugar on top, always sanitize form input data, before parsing it into a database query. In this case, this would have meant more code, but I should have added a note to the code. Mea culpa. To fix this simply make sure that the entered text looks exactly like float data and nothing else.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://blog.mollusca.ch/">
    <author>
      <name>silvio</name>
    </author>
    <id>tag:blog.mollusca.ch,2008-10-13:1</id>
    <published>2008-10-13T16:21:00Z</published>
    <updated>2008-10-17T09:50:19Z</updated>
    <category term="Rails"/>
    <category term="Ruby"/>
    <category term="passenger"/>
    <category term="rails"/>
    <category term="ruby"/>
    <link href="http://blog.mollusca.ch/2008/10/13/switch-mephisto" rel="alternate" type="text/html"/>
    <title>Switch to Mephisto</title>
<content type="html">
            &lt;p&gt;I just had to do this. Sorry to all those who already liked the old blog :D&lt;/p&gt;


	&lt;p&gt;Something had to be done about the &lt;span class=&quot;caps&quot;&gt;PHP&lt;/span&gt; bloat that was flying around before this. So I switched to this nice and clean ruby based blogging engine.&lt;/p&gt;


	&lt;p&gt;Thanks to Passenger (mod_rails) that was relatively easy to achieve. Alas, the server is running Debian, there where some problems during the initial setup of the ruby and rails environment. Why the hell does Debian ship a 1.5 years old package in the stable branch? I mean come on 1.8.5?? &lt;span class=&quot;caps&quot;&gt;WTF&lt;/span&gt;!&lt;/p&gt;


	&lt;p&gt;Anyway. Here we go now. Shiny new blog, leaner and meaner software, where I can actually read the code and edit it and where I can also add new features. We&#8217;ll see about that though.&lt;/p&gt;


	&lt;p&gt;I will soon move the old articles over here. Actually as soon as I have syntax highlighting like pastie.org :D&lt;/p&gt;


	&lt;p&gt;Ah, and yeah, mephisto is in fact not dead. Go see the &lt;a href=&quot;http://github.com/halorgium/mephisto/tree/master&quot;&gt;mephisto git repo&lt;/a&gt;.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://blog.mollusca.ch/">
    <author>
      <name>silvio</name>
    </author>
    <id>tag:blog.mollusca.ch,2008-10-04:8</id>
    <published>2008-10-04T07:41:00Z</published>
    <updated>2009-03-30T14:35:37Z</updated>
    <category term="GIS"/>
    <category term="Grails"/>
    <category term="Java"/>
    <link href="http://blog.mollusca.ch/2008/10/4/grails-spatial-data-postgis" rel="alternate" type="text/html"/>
    <title>Grails + Spatial Data (PostGIS)</title>
<summary type="html">&lt;p&gt;For a client/server application I&#8217;m currently developing, I chose grails (&lt;a href=&quot;http://www.grails.org&quot;&gt;www.grails.org&lt;/a&gt;) as the framework to develop the web application and webservice part.
Grails is a framework very similar in concepts to Ruby on Rails, which I personally like a lot for it&#8217;s convention over configuration attitude.&lt;/p&gt;


	&lt;p&gt;The main downside of grails, as far as I&#8217;m concerned is the huge amount of *.jar dependencies that it comes along with. This is, as long as applications do not stray from the standard data types of a web framework, very well hidden from the developer.&lt;/p&gt;


	&lt;p&gt;In this blog post I will outline a few points, on how to save geo-spatial points (no pun intended) using grails.&lt;/p&gt;</summary><content type="html">
            &lt;p&gt;For a client/server application I&#8217;m currently developing, I chose grails (&lt;a href=&quot;http://www.grails.org&quot;&gt;www.grails.org&lt;/a&gt;) as the framework to develop the web application and webservice part.
Grails is a framework very similar in concepts to Ruby on Rails, which I personally like a lot for it&#8217;s convention over configuration attitude.&lt;/p&gt;


	&lt;p&gt;The main downside of grails, as far as I&#8217;m concerned is the huge amount of *.jar dependencies that it comes along with. This is, as long as applications do not stray from the standard data types of a web framework, very well hidden from the developer.&lt;/p&gt;


	&lt;p&gt;In this blog post I will outline a few points, on how to save geo-spatial points (no pun intended) using grails.&lt;/p&gt;
&lt;p&gt;For said application, I have to save &lt;span class=&quot;caps&quot;&gt;GIS&lt;/span&gt; spatial data in the database and I want to access them from the web application. I also want to call the special &lt;span class=&quot;caps&quot;&gt;GIS&lt;/span&gt;-related stored procedures on the server.
This seemed to be very hard at first, since almost no documentation on the topic can be found, especially in the documentation of the grails framework (not only for &lt;span class=&quot;caps&quot;&gt;GIS&lt;/span&gt; data, but also for other custom data types, that is).
On the mailing lists of Hibernate and Spring (libraries both used by grails), no working solutions could be found, but only the problem statements.&lt;/p&gt;


	&lt;p&gt;Digging deeper however revealed the solution to the problem, which I&#8217;m going to present now.&lt;/p&gt;


	&lt;p&gt;Let&#8217;s say we want to create a webapp, that can do the following things:&lt;/p&gt;


Store upcoming and past events, whereas each event has:
	&lt;ul&gt;
	&lt;li&gt;A Date,&lt;/li&gt;
		&lt;li&gt;a title,&lt;/li&gt;
		&lt;li&gt;a description,&lt;/li&gt;
		&lt;li&gt;and a location (This will be the &lt;span class=&quot;caps&quot;&gt;GIS&lt;/span&gt; data)&lt;/li&gt;
	&lt;/ul&gt;


and store members with:
	&lt;ul&gt;
	&lt;li&gt;Events they are attending.&lt;/li&gt;
		&lt;li&gt;A name.&lt;/li&gt;
		&lt;li&gt;A birthday.&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;I suppose creating a web-app using grails is already clear to readers at this point.&lt;/p&gt;


	&lt;h2&gt;The domain classes&lt;/h2&gt;


Let&#8217;s have a look at the domain classes, shall we?
&lt;pre class=&quot;java&quot;&gt;
# Event.groovy
class Event {
    String name
    String description
    Date date

    static hasMany = [participants : Participant]
    static belongsTo = [creator : Participant]

    public void findWithinLimit(double distance){
    }
}
&lt;/pre&gt;

&lt;pre class=&quot;java&quot;&gt;
# Participant.groovy
class Participant {
    String name
    Date birthDate

    static hasMany = [events : Event]
}
&lt;/pre&gt;

	&lt;p&gt;I intentionally left out the &lt;span class=&quot;caps&quot;&gt;GIS&lt;/span&gt; attribute of the event class. You can easily start off with this as your domain classes and add &lt;span class=&quot;caps&quot;&gt;GIS&lt;/span&gt; functionality later.&lt;/p&gt;


	&lt;h2&gt;Adding &lt;span class=&quot;caps&quot;&gt;GIS&lt;/span&gt; functions to PostgreSQL&lt;/h2&gt;


	&lt;p&gt;Now, lets add the &lt;span class=&quot;caps&quot;&gt;GIS&lt;/span&gt; functionality to the database. Assuming you use PostgreSQL this can be done in the following way:&lt;/p&gt;


	&lt;ol&gt;
	&lt;li&gt;Add the plpgsql language to the database (createlang command on shell)&lt;/li&gt;
		&lt;li&gt;Execute the update script on the database (my database is called geoEvent) to add the PostGIS functions and data types. On my computer that script is called lwpostgis.sql and sits in /usr/share. I execute the script using pgadmin3, which is my method of choice to work with postgresql databases&#8230;&lt;/li&gt;
	&lt;/ol&gt;


	&lt;p&gt;This will make your database ready to store &lt;span class=&quot;caps&quot;&gt;GIS&lt;/span&gt; data.&lt;/p&gt;


	&lt;p&gt;Now, first things first. Let&#8217;s add all the libraries we need to make grails aware of the data types we&#8217;re going to be using.&lt;/p&gt;


	&lt;p&gt;You&#8217;ll need:&lt;/p&gt;


	&lt;ul&gt;
	&lt;li&gt;hibernate-spatial.jar (find this on &lt;a href=&quot;http://www.hibernatespatial.org/&quot;&gt;the hibernatespatial site&lt;/a&gt;)&lt;/li&gt;
		&lt;li&gt;hibernate-spatial-postgis.jar (same site)&lt;/li&gt;
		&lt;li&gt;jts-1.8.jar (&lt;a href=&quot;http://www.vividsolutions.com/jts/jtshome.htm&quot;&gt;&lt;span class=&quot;caps&quot;&gt;JTS&lt;/span&gt; site&lt;/a&gt;)&lt;/li&gt;
		&lt;li&gt;postgis_1.4.0.jar (PostGIS site)&lt;/li&gt;
		&lt;li&gt;postgresql-8.2-VERSION.jdbc3.jar (it may be possible to run this with jdbc4, but I haven&#8217;t tested that one yet. Version works fine, however)&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;Enough libraries. ;)&lt;/p&gt;


	&lt;h2&gt;Adding &lt;span class=&quot;caps&quot;&gt;GIS&lt;/span&gt; to the domain classes&lt;/h2&gt;


Now let&#8217;s get those &lt;span class=&quot;caps&quot;&gt;GIS&lt;/span&gt; data types into the domain classes.
&lt;pre class=&quot;java&quot;&gt;
# Event.groovy
import com.vividsolutions.jts.geom.Point
class Event {
    String name
    String description
    Date date
    Point location

    static hasMany = [participants : Participant]

    public void findWithinLimit(double distance){

    }
}
&lt;/pre&gt;

	&lt;p&gt;Hooooold your horses, we&#8217;re not yet ready to fire up the grails app again. :D&lt;/p&gt;


	&lt;h2&gt;Bind hibernate&lt;/h2&gt;


	&lt;p&gt;You will first have to tell hibernate, that there is new data types that are mapped to the database in a certain way.&lt;/p&gt;


In grails-app/conf/hibernate create a file called hibernate.cfg.xml
&lt;pre class=&quot;xml&quot;&gt;
&amp;lt;hibernate-configuration&amp;gt;
  &amp;lt;session-factory&amp;gt;
    &amp;lt;!-- SPATIAL SQL dialect --&amp;gt;
    &amp;lt;property name=&quot;dialect&quot;&amp;gt;
      org.hibernatespatial.postgis.PostgisDialect
    &amp;lt;/property&amp;gt;

    &amp;lt;!-- Echo all executed SQL to stdout --&amp;gt;
    &amp;lt;property name=&quot;show_sql&quot;&amp;gt;false&amp;lt;/property&amp;gt;

    &amp;lt;mapping resource=&quot;Event.hbm.xml&quot; /&amp;gt;
  &amp;lt;/session-factory&amp;gt;
&amp;lt;/hibernate-configuration&amp;gt;
&lt;/pre&gt;

	&lt;p&gt;This tells hibernate, that there is a special &lt;span class=&quot;caps&quot;&gt;SQL&lt;/span&gt; dialect to learn and that there is a mapping for the type Event (This is by convention, I&#8217;d say).&lt;/p&gt;


Event.hbm.xml looks like this:
&lt;pre class=&quot;xml&quot;&gt;
&amp;lt;hibernate-mapping&amp;gt;
  &amp;lt;class name=&quot;Event&quot; table=&quot;event&quot;&amp;gt;
    &amp;lt;id name=&quot;id&quot; column=&quot;id&quot;&amp;gt;
      &amp;lt;generator class=&quot;native&quot; /&amp;gt;
    &amp;lt;/id&amp;gt;
    &amp;lt;property name=&quot;location&quot; 
      type=&quot;org.hibernatespatial.GeometryUserType&quot;&amp;gt;
      &amp;lt;column name=&quot;location&quot; sql-type=&quot;GEOMETRY&quot; /&amp;gt;
    &amp;lt;/property&amp;gt;
    &amp;lt;property name=&quot;name&quot; /&amp;gt;
    &amp;lt;property name=&quot;description&quot; /&amp;gt;
    &amp;lt;property name=&quot;date&quot; /&amp;gt;
  &amp;lt;/class&amp;gt;
&amp;lt;/hibernate-mapping&amp;gt;
&lt;/pre&gt;

	&lt;p&gt;That&#8217;s it! Now you can fire up your application again and shine with &lt;span class=&quot;caps&quot;&gt;GIS&lt;/span&gt; data. At least it seems that way. The tables all get created in the database, the attributes are all there and so are the foreign keys and link tables.
Read on the next page how to add data to your database using your grails scaffolds.&lt;/p&gt;


	&lt;h2&gt;Ticket to forms and back&lt;/h2&gt;


	&lt;p&gt;To have grails render your Point in a way, that a user can add new data to the database using the scaffolded code you will have to add a PropertyEditor (which is a concept from the Spring framework&#8230;).&lt;/p&gt;


	&lt;p&gt;A property editor tells your application, how to translate a certain data type into String format and back.
This doesn&#8217;t look so bad after all:&lt;/p&gt;


&lt;pre class=&quot;java&quot;&gt;
package com.vividsolutions.jts.geom;

import java.beans.PropertyEditorSupport;
import com.vividsolutions.jts.geom.Point;
import com.vividsolutions.jts.io.ParseException;
import com.vividsolutions.jts.io.WKTReader;

public class PointEditor
        extends PropertyEditorSupport{

       private Point point;

    @Override
    public String getAsText() {
        point = (Point) super.getValue();
        return String.format(&quot;POINT(%f %f)&quot;,
                    point.getX(), point.getY());
    }

    @Override
    public void setAsText(String text)
                throws IllegalArgumentException {
        WKTReader reader = new WKTReader();
        try {
            point = (Point)reader.read(text);

            super.setValue(point);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
}
&lt;/pre&gt;

Now, grails will pick up this PropertyEditor automatically, iff you put it in the correct place. You will have to put it in same directory as the class you are trying to render and parse from a String.
So this goes into the src/java/com.vividsolutions.jts.geom package.
This will enable you to render and enter Points as Strings of the form:
&lt;pre class=&quot;sql&quot;&gt;
POINT(48.8 8.8)
POINT(20 30 40) // 3d-Point
&lt;/pre&gt;

	&lt;p&gt;That&#8217;s quite enough for today. I will follow this post up with a tutorial on how to use the stored procedures to find nearby Points efficiently, as is already hinted in the code by the function findWithinLimit(distance).&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://blog.mollusca.ch/">
    <author>
      <name>silvio</name>
    </author>
    <id>tag:blog.mollusca.ch,2008-10-04:7</id>
    <published>2008-10-04T07:37:00Z</published>
    <updated>2008-10-16T07:39:08Z</updated>
    <category term="Grails"/>
    <category term="Java"/>
    <link href="http://blog.mollusca.ch/2008/10/4/here-we-go" rel="alternate" type="text/html"/>
    <title>Here we go...</title>
<content type="html">
            &lt;p&gt;So this is it.&lt;/p&gt;


	&lt;p&gt;Grails + Hibernatespatial made me open a blog of my own.&lt;/p&gt;


	&lt;p&gt;Since grails in combination with spatial data is documented quite badly, I will try to fix this issue by posting my solution to the problem (Which basically boils down to a spring+hibernate problem). I hope this will make the string &#8220;grails hibernatespatial&#8221; google-able. At least all the solutions that pop up on google right now, are highly outdated or downright wrong XX(&lt;/p&gt;


	&lt;p&gt;The working solution will be up on monday, when I&#8217;m at work and have access to my code-base&#8230;&lt;/p&gt;
          </content>  </entry>
</feed>
