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

Indexing Apache access logs with ELK (Elasticsearch+Logstash+Kibana)

Who said that grepping Apache logs has to be boring?

The truth is that, as Enteprise applications move to the browser too, Apache access logs are a gold mine, it does not matter what your role is: developer, support or sysadmin. If you are not mining them you are most likely missing out a ton of information and, probably, making the wrong decisions.
ELK (Elasticsearch, Logstash, Kibana) is a terrific, Open Source stack for visually analyzing Apache (or nginx) logs (but also any other timestamped data).

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.

A not so short guide to TDD SaltStack formulas

One of the hardest parts about Infrastructure As Code and Configuration Management is establishing a discipline on developing, testing and deploying changes.
Developers follow established practices and tools have been built and perfected over the last decade and a half. On the other hand sysadmins and ops people do not have the same tooling and culture because estensive automation has only become a trend recently.

So if Infrastructure As Code allows you to version the infrastructure your code runs on, what good is it if then there are no tools or established practices to follow?

Luckily the situation is changing and in this post I'm outlining a methodology for test driven development of SaltStack Formulas.

The idea is that with a single command you can run your formula against a matrix of platforms (operating systems) and suites (or configurations). Each cell of the matrix will be tested and the result is a build failure or success much alike to what every half-decent developer of…