Using GeoRss

I started using Marc’s GeoRss module as part of a webapp that needed to send geometries to an OpenLayers (JavaScript) client. Here are the steps I took to get GeoRss working.

* Read the documentation at http://georss.geonames.org/

* Used the following maven command to setup a basic project.

#!/bin/bash

PROJECT=twotrack

mvn archetype:generate \
   -DarchetypeGroupId=org.apache.maven.archetypes \
   -DarchetypeArtifactId=maven-archetype-profiles \
   -Dversion=1.0.0-SNAPSHOT \
   -DartifactId=$PROJECT-georss \
   -DgroupId=net.fnarg.$PROJECT

* Added GeoRss dependencies to the maven generated pom.xml

    <dependency>
      <groupId>rome</groupId>
      <artifactId>rome</artifactId>
      <version>0.9</version>
    </dependency>
    <dependency>
      <groupId>org.geonames</groupId>
      <artifactId>georss-rome</artifactId>
      <version>0.9.8</version>
    </dependency>

* Grabbed the jars for Eclipse

mvn eclipse:eclipse

* Extended Marc’s examples by wrapping some setters and getters for Spring

package net.fnarg.twotrack;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import com.sun.syndication.io.XmlReader;
import com.sun.syndication.io.SyndFeedInput;
import com.sun.syndication.io.SyndFeedOutput;

import com.sun.syndication.feed.synd.SyndEntry;
import com.sun.syndication.feed.synd.SyndEntryImpl;
import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.feed.synd.SyndFeedImpl;

import com.sun.syndication.feed.module.georss.GeoRSSUtils;
import com.sun.syndication.feed.module.georss.GeoRSSModule;
import com.sun.syndication.feed.module.georss.W3CGeoModuleImpl;
import com.sun.syndication.feed.module.georss.geometries.Position;

public class GeoRssFeed {
    private SyndFeed feed;
    
    public GeoRssFeed() {
 	    feed = new SyndFeedImpl();
    }

    public GeoRssFeed(String url) throws Exception {
 	    feed = new SyndFeedImpl();
 	    this.setEntries(url);
    }
    
    public String getFeed() throws Exception {
	    return new SyndFeedOutput().outputString(feed);
    }

    public void setFeed() throws Exception {
 	    feed.setFeedType("rss_2.0");
	    feed.setTitle("Two Track");
	    feed.setDescription("A tool for modelling toy train tracks");
	    feed.setLink("http://www.fnarg.net/twotrack/");
    }
	
    public void setEntries(String url) throws Exception {
	    SyndFeedInput input = new SyndFeedInput();
	    feed = input.build(new XmlReader(new URL(url)));
    }
    
    public List<SyndEntry> getEntries() {
    	return feed.getEntries();
    }
    
	public void setEntries(List<Position> positions) {
	    List<SyndEntry> entries = new ArrayList<SyndEntry>();
	    for (Position p : positions) {
			SyndEntry entry = new SyndEntryImpl();
		    GeoRSSModule geoRSSModule = new W3CGeoModuleImpl();
		    geoRSSModule.setPosition(p);
		    entry.getModules().add(geoRSSModule);
		    entries.add(entry);
	    }
	    feed.setEntries(entries);
    }
}

* Wrote a test class to verify inputs/outputs

package net.fnarg.twotrack;

import java.util.ArrayList;
import java.util.List;

import com.sun.syndication.feed.module.georss.GeoRSSModule;
import com.sun.syndication.feed.module.georss.GeoRSSUtils;
import com.sun.syndication.feed.module.georss.geometries.Position;
import com.sun.syndication.feed.synd.SyndEntry;

public class TestGeoRssReadWriter {
	
	private static GeoRssFeed geoRssFeed;

	/**
	 * Tester for the GeoRssFeed class
	 * @author gavin
	 */
	public static void main(String[] args) throws Exception {
		// load XML file into memory
		geoRssFeed = new GeoRssFeed("http://www.openlayers.org/dev/examples/xml/track1.xml");
		// get and display the geometries
		List<SyndEntry> entries = geoRssFeed.getEntries();
	    for (SyndEntry entry : entries) {
	         GeoRSSModule geoRSSModule = GeoRSSUtils.getGeoRSS(entry);
	         System.out.println(entry.getTitle() + " : lat="
	               + geoRSSModule.getPosition().getLatitude() + ",lng="
	               + geoRSSModule.getPosition().getLongitude() + ", desc="
	               + entry.getDescription().getValue() + "; time="
	               + entry.getPublishedDate());
	    }
	    // set mandatory XML fields with our Two Track headers
	    geoRssFeed.setFeed();
	    // output
	    System.out.println(geoRssFeed.getFeed());
	    // update the feed
	    	    Position p1 = new Position(54.2, 12.4);
	    Position p2 = new Position(55.2, 13.4);
	    List<Position> newEntries = new ArrayList<Position>();
	    newEntries.add(p1);
	    newEntries.add(p2);
	    // replace the geometries
	    geoRssFeed.setEntries(newEntries);
	    // check new output
	    System.out.println(geoRssFeed.getFeed());
	}
}

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.