Pages

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 jts-1.13.zip -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 testrunner.sh test_robust.bat testbuilder.bat testbuilder.sh testrunner.properties
  4. Time to fix permissions and give that a go:
    >chmod 777 *.sh
    >./testbuilder.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 testbuilder.sh script actually does on the inside:

#!/bin/sh
#to change L&F if desired.  Blank is default
JAVA_LOOKANDFEEL="-Dswing.defaultlaf=javax.swing.plaf.metal.MetalLookAndFeel"
#JAVA_LOOKANDFEEL=""
JAVA_OPTS="-Xms256M -Xmx1024M"
APP_OPTS=""
if test "x$JTS_LIB_DIR" = "x"; then
        JTS_LIB_DIR=`dirname $0`/../lib/
fi
#---------------------------------#
# dynamically build the classpath #
#---------------------------------#
CP=
for i in `ls ${JTS_LIB_DIR}/*.jar`
do
  CP=${CP}:${i}
done
#---------------------------#
# run the program           #
#---------------------------#
MAIN=com.vividsolutions.jtstest.testbuilder.JTSTestBuilder
java -cp ".:${CP}" $JAVA_OPTS  $JAVA_LOOKANDFEEL $MAIN $APP_OPTS

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">
        <tstamp/>
        <mkdir dir="${build}"/>
      </target>
      <target name="jar" depends="init"
              description="package as executable jar" >
        <jar destfile="${build}/jts-runner.jar" basedir="${build}">
           <zipgroupfileset dir="${lib}"
             includes="*.jar"
             excludes="jtsio*.jar,junit.jar"/>
           <manifest>
              <attribute name="Main-Class"
              value="com.vividsolutions.jtstest.testbuilder.JTSTestBuilder"/>
           </manifest>
        </jar>
      </target>
      <target name="clean" >
        <delete dir="${build}"/>
      </target>
    </project>
  2. Giving that a go:
    >ant
    Buildfile: /Users/jody/Downloads/jts/build.xml
    init:
        [mkdir] Created dir: /Users/jody/Downloads/jts/build
    jar:
          [jar] Building jar: /Users/jody/Downloads/jts/build/jts-runner.jar
    BUILD SUCCESSFUL
    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!