Snippet: Get the timezone for a geo location with Groovy and Google

I have a list (table) of airport locations, but missing the timezone information (as used in Java, eg. ‘America/Los_Angeles’ which is the timezone Id in java.util.Timezone). We could use the geonames dump to retrieve the timezone from the cityname, but the cityname for some airports might not be unique or distinct, so I rather use the geographical location which I have for each airport and use a webservice to get the timzone for a specific location. We can use the geonames webservice (commercial usage allowed, but you should give credit) or the Google Timezone API (which is experimental and restricted to 2500 calls a day) .

We use Groovy to retrieve this information, using the browser you get this JSON reply for :
https://maps.googleapis.com/maps/api/timezone/json?location=39.6034810,-119.6822510&timestamp=1331161200&sensor=false

JSON

We put this into a Groovy Script to read the timeZoneId:

#!/usr/bin/env groovy
import groovyx.net.http.*
import static groovyx.net.http.ContentType.*

def timezone = new RESTClient('https://maps.googleapis.com/maps/api/timezone/json?location=39.6034810,-119.6822510&timestamp=1331161200&sensor=false')
def resp = timezone.get( contentType : JSON)

println resp.data.timeZoneName + " - " + resp.data.timeZoneId

We need the HTTP Builder library.
Please note the httpbuilder library has some dependencise ! Put all into your .groovy/lib folder.

  • commons-beanutils-1.8.3.jar
  • commons-codec-1.6.jar
  • commons-collections-3.2.1.jar
  • commons-lang3-3.1.jar
  • commons-logging-1.1.1.jar
  • fluent-hc-4.2.2.jar
  • http-builder-0.6.jar
  • httpclient-4.2.2.jar
  • httpclient-cache-4.2.2.jar
  • httpcore-4.2.2.jar
  • httpmime-4.2.2.jar
  • json-lib-2.4-jdk15.jar
  • xml-resolver-1.2.jar

Result:

Pacific Standard Time - America/Los_Angeles

Now we can iterate through our locations and complete the data.

Alternative with geonames:

geonames JSON

#!/usr/bin/env groovy
import groovyx.net.http.*
import static groovyx.net.http.ContentType.*

def timezone2 = new RESTClient('http://api.geonames.org/timezoneJSON?lat=47.01&lng=10.2&username=xxxxx')
def resp2 = timezone2.get( contentType : JSON)
println resp2.data.countryName + " - " + resp2.data.timezoneId
Advertisements

Creating a ZK Groovy Console

SECURITY WARNING: Please take note of the warning at the end of the tutorial !
UPDATE 2011-07-08: A good article on the security concern I recommend http://www.jroller.com/melix/entry/customizing_groovy_compilation_process

We use the Groovy Script Engine to run small Groovy scripts to prepare incoming interface messages for further processing. Here I share with you how to embed the engine into a ZK page. Very basic, though it shows you the concept and it is wasy to expand to your needs.

ZK Groovy Console

Continue reading

SnipplrScout Release 0.1

A little sunday afternoon project, mixing Java and Groovy into a Swing application that reads your favorite code snippets from snipplr.com. The website has an XML-RPC based API, so a good exercise with this rather old API technology. I am storing both sourcecode and jar file on bitBucket (which is now under Atlassian). Learning the Mercurial way.

Download the tool from: http://bitbucket.org/javadude/snipplrscout/downloads

SnipplrScout 0.1A

Project: Visualizing Trac Wiki Pages (Part 2)

More information before we start with the implementation:

Graph requirements:

  • Display all wikipages as nodes
  • Show direction (links are uni-directional)

Option Requirements:

  • Highlight direct connected wikipages
  • Display content (in separate browser window)

Wiki Graph

The XML for prefuse graphs look like this sample (our groovy script needs to produce this):

<?xml version="1.0" encoding="UTF-8"?><graphml xmlns="http://graphml.graphdrawing.org/xmlns">
 <graph edgedefault="directed">
 <key id="wname" for="node" attr.name="wname" attr.type="string"/>
 <node id="1">
 <data key="wname">WikiStart</data>
 </node>
 <node id="2">
 <data key="wname">IdeOverview</data>
 </node>
 <node id="3">
 <data key="wname">JavaEE</data>
 </node>
[..]
 <edge source="1" target="3"/>
 <edge source="1" target="4"/>
 <edge source="1" target="5"/>
 <edge source="1" target="6"/>
 <edge source="1" target="7"/>
 <edge source="1" target="13"/>
 <edge source="3" target="4"/>
 [..]
 </graph>
</graphml>

The prefuse xml file contains the metadata for the nodes, all nodes and all edges linking the nodes.

Using the backup function of Trac we need to process input like this sample:

INSERT INTO "wiki" VALUES('wikiPage1',2,1246948793,'user','220.255.7.193',' * MySQL on Server xyz * Evaluate Firebird and Oracle. (schema and timestamp issue)','',0);

The SQL file contains the complete wiki content. Following the sample we need to filter with

  • type: INSERT INTO ‘wiki’ (filter out all others)
  • page title: ‘wikiPage1
  • version: ‘2‘ (search for the highest version)
  • wiki user: ‘user‘ (filter out all ‘trac’ entries)
  • content: ‘* MySQL on Server xyz * Evaluate Firebird and Oracle. (schema and timestamp issue)

Project: Visualizing Trac Wiki Pages (Part 1)

We are using Trac as Wiki and after a couple of months using it as main know-how repository, we tend to loose the overview of the myriad of pages and how they are linked to each other. A graph representing the relation between the pages would be very useful.

I could not find any plugin or tool, so it would be a good exercise to create a tool that convert a trac wiki into a graph.

Options:

  • Use Java or Groovy. I choose Groovy because less boilerplate for standard tasks, such as creating xml, reading text files, etc.
  • XML-RPC to access the wiki directly. I tried to create a mockup in Java, but failed due to the cryptic error messages and hard to debug.
  • Trac backup to retrieve wiki data: You can create a backup in form of an INSERT sql file containing the complete structure of the wiki (inclusive of non-user content and all revisions of the wiki. I choose this as a starting point until I get the XML-RPC problem solved (maybe in Groovy).
  • Visualization in JGraph, InfoVis, Prefuse or other toolkits. I decide for Prefuse because it is feature complete, powerful and I created simple applications previously.

Current Implementation:

  • A simple Groovy script that works in 2 stages, a) reading and processing the sql backup file and b) creating a xml file that my prefuse viewer can display.

Future Plans:

  • Create a standalone SwingXBuilder application
  • Create a Netbeans Plugin

Groovy Blog

Over the Christmas break I decided to spend some time on in-deep groovy’ing some stuff. Time to start another blog on nothing but Groovy and Grails topics and all the snippets and tutorials I am collecting.

If you keen on Groovy, tune-in here:   groovy.bighugesystems.com

(running on an own server, in order to make use of some cool worpress plugins which are not available on worpress.com

News: Netbeans, Groovy

  • Netbeans 6.8 M1
    The first stable milestone is available for download. The first version being more EE6’ish with JSF 2.0, JPA 2.0 and finally a build-in browser. Of course there is more stuff in the box, so find infos on feature and download at Netbeans.org.
  • Groovy 1.7
    We can look forward to get our hands on 1.7 soon, beta version is available for download. For production a stable 1.6.4 is released with minor bug fixing. Find info and downloads at codehaus.org