Skip to main content

JTS processing Grails servlet

Sometimes it is convenient to make certain topological operations available in a web-based gis. For a gis that makes heavy use of Javascript (like OpenLayers-based ones) it might be worth looking at jSTS, a Javascript port of JTS.

For all the rest and for those who don't want to load yet another library in the browser you can always write a Grails controller that encapsulates common JTS operations like buffer, intersection, union, etc.

Assuming you are familiar with Grails the steps are as follows:
  1. drop the jts jar in the lib directory
  2. create a controller and define the relevant methods
  3. define a url mapping to prettify the calls
Step 1 is trivial, so we'll go straight to 2. Create a controller and call it JtsController, then open the source file and paste this code:

import com.vividsolutions.jts.geom.*
import com.vividsolutions.jts.operation.overlay.snap.*
import grails.converters.JSON

import grails.plugins.springsecurity.Secured

class JtsController {
 def exec = {
  def pm = new PrecisionModel(PrecisionModel.FLOATING_SINGLE);
  def fact = new GeometryFactory(pm);
  def wktRdr = new WKTReader(fact); 
  def text = request.reader.text
  if(params.operation) {   
   def geometries = text.split("\\*")
                 Geometry A = selfSnap([0]))
   Geometry B = null
                  Geometry C = null
   if (geometries.length==2)
    B = selfSnap([1]))
   if ("area".equalsIgnoreCase(params.operation))
    C = A;
   else if ("intersection".equalsIgnoreCase(params.operation))
    C = A.intersection(B);
   else if ("union".equalsIgnoreCase(params.operation))
    C = A.union(B);
   else if ("buffer".equalsIgnoreCase(params.operation)) {
    // defaults to 25
    C = A.buffer(25);
   } else if (params.operation.startsWith("buffer")) {
    // parametric buffer
    def distance=(String)params.operation.substring(6)
    C = A.buffer(Double.parseDouble(distance));
   } else {
    render text: "${params.operation} not supported.", status: 400
    return false
   render(contentType: "text/json") {
  } else {
   render text: "Please supply an operation to be performed.", status: 400
   return false

 def selfSnap(Geometry g)
  double snapTol = GeometrySnapper.computeOverlaySnapTolerance(g);
  GeometrySnapper snapper = new GeometrySnapper(g);
  Geometry snapped = snapper.snapTo(g, snapTol);
  // need to "clean" snapped geometry - use buffer(0) as a simple way to do this
  Geometry fix = snapped.buffer(0);
  return fix;

the relevant points to note are:
  • the geometries (up to two) are sent in the POST body in WKT format, separated by a * (you may change that, I just happened to like the *)
  • both geometries are 'cleaned' with a self-snap operation to prevent invalid geometries from blocking the operation (in my case I had many, cleaning was not an option as I am not the owner of the dataset)
  • the operation is specified as part of the url, thanks to a custom url mapping
The url mapping (step 3) is as follows:

"/jts/$operation"(controller: "jts") {
   action = [GET: "exec", POST: "exec"]

A JTS buffer operation can then be invoked in Sproutcore as follows:

     .notify(this, 'didPerformGeoOperation')
     .send(geom1.toString() + "*");
A JTS intersection operation in jQuery :
  type: 'POST',
  url: "/app/jts/intersection",
  data: geom1.toString() + "*" + geom2.toString(),
  success: didPerformGeoOperation


Popular posts from this blog

From 0 to ZFS replication in 5m with syncoid

The ZFS filesystem has many features that once you try them you can never go back. One of the lesser known is probably the support for replicating a zfs filesystem by sending the changes over the network with zfs send/receive. Technically the filesystem changes don't even need to be sent over a network: you could as well dump them on a removable disk, then receive  from the same removable disk.

Mirth: recover space when mirthdb grows out of control

I was recently asked to recover a mirth instance whose embedded database had grown to fill all available space so this is just a note-to-self kind of post. Btw: the recovery, depending on db size and disk speed, is going to take long. The problem A 1.8 Mirth Connect instance was started, then forgotten (well neglected, actually). The user also forgot to setup pruning so the messages filled the embedded Derby database until it grew to fill all the available space on the disk. The SO is linux. The solution First of all: free some disk space so that the database can be started in embedded mode from the cli. You can also copy the whole mirth install to another server if you cannot free space. Depending on db size you will need a corresponding amount of space: in my case a 5GB db required around 2GB to start, process logs and then store the temp files during shrinking. Then open a shell as the user that mirth runs as (you're not running it as root, are you?) and cd in

A not so short guide to ZFS on Linux

Updated Oct 16 2013: shadow copies, memory settings and links for further learning. Updated Nov 15 2013: shadow copies example, samba tuning. Unless you've been living under a rock you should have by now heard many stories about how awesome ZFS is and the many ways it can help with saving your bacon . The downside is that ZFS is not available (natively) for Linux because the CDDL license under which it is released is incompatible with the GPL. Assuming you are not interested in converting to one of the many Illumos distributions or FreeBSD this guide might serve you as a starting point if you are attracted  by ZFS features but are reluctant to try it out on production systems. Basically in this post I note down both the tought process and the actual commands for implementing a fileserver for a small office. The fileserver will run as a virtual machine in a large ESXi host and use ZFS as the filesystem for shared data.