Tuesday 15 October 2013

JTS Downloaded

Introducing the concept of geometry is a fundamental stepping stone to entering our GIS profession. The trick is where to do the introducing?

Java developers are a tough lot, or at least their lot is tough with code examples and theory for an introduction. You can have a bit of fun with curl and a good web API, take the time to set up Open Layers and WPS, try your hand at SFSQL, or break out an desktop application.

In each case you get distracted by the application or technology stack used and lose sight of the point we are trying to make: POINT (160 280)

JTS Topology Suite

So what would it take to download and run the original JTS Topology Suite?
  1. The first step is finding JTS Topology Suite. The project exists on source forge and has a big fat download button.

    Martin does provide an actual home page for JTS, but often as not google will take you to the orphaned Vivid Solutions page from 2006.
  2. The home page comes with nice screenshots, alluding to an easy to use playground somewhere in that bundle.
    Lets see what comes out of the box:
    > unzip -d jts
    > ls jts
    bin doc jtsio lib src testxml
  3. This looks promising:
    >cd jts/bin
    > ls
    test.bat test_stmlf.bat testbuilder.command testrunner.bat test_robust.bat testbuilder.bat
  4. Time to fix permissions and give that a go:
    >chmod 777 *.sh
  5. A few short clicks later and you can bend it like Martin.

    This is the same application Martin uses in every other lin.ear th.inking blog post so it must be good for something.

Launching JTSTestBuilder

Lets see what that script actually does on the inside:

#to change L&F if desired.  Blank is default
JAVA_OPTS="-Xms256M -Xmx1024M"
if test "x$JTS_LIB_DIR" = "x"; then
        JTS_LIB_DIR=`dirname $0`/../lib/
# dynamically build the classpath #
for i in `ls ${JTS_LIB_DIR}/*.jar`
# run the program           #

So the usual amount of bash-ing about and then running the JTSTestBuilder class.

JTSTestBuilder Executable

Still all of that is not quite easy enough for use in a training course. Time to get to work:
  1. Since this is JTS (and Martin still does not believe in Maven) we will bust out a ANT script to create an executable jar:
    <project name="jts_runner" default="jar" basedir=".">
      <property name="lib" location="lib"/>
      <property name="build" location="build"/>
      <target name="init">
        <mkdir dir="${build}"/>
      <target name="jar" depends="init"
              description="package as executable jar" >
        <jar destfile="${build}/jts-runner.jar" basedir="${build}">
           <zipgroupfileset dir="${lib}"
              <attribute name="Main-Class"
      <target name="clean" >
        <delete dir="${build}"/>
  2. Giving that a go:
    Buildfile: /Users/jody/Downloads/jts/build.xml
        [mkdir] Created dir: /Users/jody/Downloads/jts/build
          [jar] Building jar: /Users/jody/Downloads/jts/build/jts-runner.jar
    Total time: 1 second
  3. And the result something we can double click and run:

1 comment:

Joe Lie said...

I recently downloaded the JTS, and it was straightforward to use. I found it helpful as it provided a comprehensive Guide on Management Case Study. It was a great resource to understand the concept better and gain more insight. I have highly recommended it!