Friday, March 9, 2012

Battle of the SSLs - Round 2

In a comment to Battle of the Spatial Scripting Languages, Sean suggested that a slightly more in-depth test of language expressiveness might be this task, involving a sequence of CRS transformation and geometry cleaning.  Here's the JEQL equivalent:

ShapefileReader t file: "test_uk.shp";

trans = select * except GEOMETRY, 
               CRS.transform(GEOMETRY, "epsg:4326", "epsg:27700") geom from t;

clean = select * except geom, isValid ? 1 : 0 isValid, cleanGeom
    with { 
        isValid = Geom.isValid(geom);
        cleanGeom = isValid ? geom : geom; //Geom.buffer(geom, 0);
    } from trans;

ShapefileWriter clean file: "test_uk_valid.shp";


The code is spread out a bit for clarity.  It could have been done in single SELECT statement, apart from the isValid flag added for reporting purposes.  Note the use of the JEQL extension with clause, which improves code clarity, and also allows a more imperative style of coding which can sometimes be preferable.

Fiona gets points for being able to define any function directly in the language, but there's a price to be paid in code complexity.

Here's the result (seen in the new JEQL Workbench Geometry Viewer):




6 comments:

sgillies said...

Martin, is ``with`` new? I don't see it in the docs. It looks familiar ... is it related to the :let modifier in Clojure's list comprehensions?

http://clojuredocs.org/clojure_core/clojure.core/for

Jared Erickson said...

Martin, JEQL is looking very cool. Here is a Groovy GeoScript version.

Dr JTS said...

@sean:

Yes, the JEQL with block is exactly analogous to the let: block in Clojure. It's an extension to standard SQL which is sorely needed IMHO. It allows factoring out and naming common subexpressions for legibility and performance.

I've added this as doc about it here:

http://tsusiatsoftware.net/jeql/spec/jeql-query.html

Dr JTS said...

@Jared:

Cool, I saw that. Wow, lots of SSLs to choose from!

sgillies said...

Hmm, "SSL"... I may have to blog about what distinguishes Python and Fiona from a "mere" scripting language.

The JEQL with block is handy, for sure. It's interesting how we're going in opposite directions here: you adding imperative features to a declarative language, me factoring functional forms out of imperative programs.

Dr JTS said...

That would be interesting to read. I personally don't have a clear idea of what constitutes a "scripting language". Isn't it common to refer to Python as a scripting language? (Although I would agree that this sounds overly ghettoizing). Surely there must be prior discussion of this to refer to?

I think I can say that whatever a scripting language is, JEQL has no further aspirations than being one of them!

As for extending in opposite directions to meet in the middle, isn't this the eventual fate of all languages? There's just too much goodness on either side to completely ignore.