You are on page 1of 7

OSGeo Journal Volume 9 (2010 Annual Report) January 2012

Angles and library although he is no longer working

Directions: An
for the company. JTS is used by popular
geospatial software written in Java, and

Introduction to
is the geometry library used by the
open source desktop GIS OpenJUMP.

JTS Warped What is JTS Warped?


By Landon Blake JTS Warped is a library of utility code
related to JTS. It adds to the
Introduction functionality of JTS but is not included in
the main JTS distribution. JTS Warped is
This article provides an introduction to written by Landon Blake. The code for
the JTS Warped software library. It JTS is released under the GPL and is
begins with a brief discussion of the JTS managed by the SurveyOS Project, a
Topology Suite (JTS) and explains what member of the Free Software
functionality JTS Warped adds to the JTS Conservancy. JTS Warped is currently in
Topology Suite. It then provides an Alpha status. You can download the
overview of the code in JTS Warped that source code, via SVN, for JTS Warped
allows programmers to easily work with from here:
angles and bearings in JTS. It concludes
with some code examples. https://surveyos.svn.sourceforge.net/sv
nroot/surveyos
What Is JTS?
In this article we are going to focus our
JTS is a software library and set of tools discussion on the code in JTS Warped
that support geometry calculations on that allows programmers to work with
the 2D Cartesian plane. JTS conforms to angles and bearings in JTS.
the Simple Features Specification for
SQL published by the OGC. JTS strives Overview of Angles and
to be (1) fast, (2) robust, and (3) Directions Code in JTS
implemented in pure Java. JTS was Warped
originally written by Martin Davis while
at Vivid Solutions. He still maintains the Land surveyors frequently work with

Page 1 of 10 http://www.osgeo.org/journal
OSGeo Journal Volume 9 (2010 Annual Report) January 2012

angles and directions when moving coordinate systems. The angles and
data between 2D grid (also known as directions code has only two (2) key
2D Cartesian or rectangular coordinate classes used to implement this design.
systems) and polar coordinate systems. The BasicSurveyorsAngle class
represents angular measurements in a
polar coordinate system, and the
Here is one example: Optical BasicSurveyorsDirection class
instruments used in terrestrial represents direction measurements in a
surveying employ polar coordinate polar coordinate system. Both classes
systems to collect their measurements. provide basic implementations of
In a polar coordinate system points are corresponding Java interfaces, which
located in relation to the instrument allow the library to support alternate
point. This involves two measurements. implementations of the interfaces if
The first is the measurement of the desired.
angle between the instrument point, a
backsight point and the point being The Basic
located (foresight point). The second is Surveyors Angle Class
the distance between the instrument
point and the point being located. The BasicSurveyorsAngle is essentially
Software is then used to transform the a value class. It represents an angle
collected measurements in to point measurement by storing the value of
coordinates on the project grid the angle in degrees, minutes, and
coordinate system. seconds. The degrees value, minutes
value, and seconds value are passed in
Here is another example: Descriptions and out of the class as ints. The
for land parcel boundaries often use fractional seconds value is passed in
bearings and distances to describe the and out of the class as a double. As an
parcel geometry. It is often useful to alternative, you can wrap these values
convert this bearing and distance in a DegreesMinutesSecondsValue
information into 2D vector geometry in object for passage in and out of the
a 2D grid coordinate system. BasicSurveyorsAngle methods.
The angles and directions code in JTS
Warped was designed to make it easier There are four (4) ways to create a
to perform the transformations between BasicSurveyorsAngle class. The default,
polar coordinate systems and 2D grid no-argument constructor will create an

Page 3 of 10 http://www.osgeo.org/journal
OSGeo Journal Volume 9 (2010 Annual Report) January 2012

BasicSurveyorsAngle class with a convenience methods. Convenience


degree value of 0, a minutes value of 0, methods are also included to return the
a whole seconds value of zero, and a trig ratios for the sin, cosin, and tangent
fractional seconds value of 0. A second of the angle. These methods handle the
constructor allows you to pass these internal conversions of the angle values
values as three ints and a single to and from radians so the trig ratios
double. A third constructor sets the can be calculated. Three additional
value during construction using an methods provide information about the
instance of the type of angle. These are the isAcute
DegreesMinutesSecondsValue passed method, isObtuse method and
as an argument. The final constructor is isRightAngle method.
a “copy constructor” which accepts
another instance of SurveyorsAngle as A few standard utility methods are also
an argument. The value of this included in the class. There are three
argument is used to set the value of the methods to compare angle values for
new angle. (This constructor is a tool for equality and a toString method. The
cloning angle values.) toString method returns the angle value
in the following format: degrees-
Code Listing #1 shows examples of how minutes-seconds. For example: 282-12-
to create instances of the 11.12
BasicSurveyorsAngle class.
The last method of the
The BasicSurveyorsAngle class is BasicSurveyorsAngle class allows you to
immutable. You can obtain its degree, rotate a JTS geometry. You provide the
minute, second, and fractional second geometry, center of rotation coordinate,
components using traditional getter and a boolean flag that indicates the
methods of the class. The decrease and direction of the method. Note that this
increase methods allow you to increase method modifies the JTS geometry it is
or decrease the value of an angle by passed in-place. It does not create and
applying other angles as a rotation. modify a copy of the geometry.
These methods do not modify the
subject angle, but create a clone, The Basic Surveyors
modify and return it. The value of the Direction Class
angle object can also be obtained in
radians or decimal degrees through The BasicSurveyorsDirection class is

Page 3 of 10 http://www.osgeo.org/journal
OSGeo Journal Volume 9 (2010 Annual Report) January 2012

also essentially a value class. It bearing, or as an angle value in


represents the direction of a line degrees, minutes, seconds, and
segment or line in reference to a fractional seconds stored in a string.
system for angle measurement. In JTS The format for the returned string is the
Warped, the direction of a line is same as output by the toString method
measured as an azimuth in degrees, of the BasicSurveyorsAngle class.
minutes, seconds, and fractional Another method returns the quadrant of
seconds. A direction of 0-0-0.0 the direction.
corresponds to cardinal north or the Y
axis of the 2D coordinate grid. The The BasicSurveyorsDirection class is not
degrees value, minutes value, and immutable. (It will be made immutable
seconds value are passed in and out of in the next release.) It can be modified
the class as ints. The fractional seconds by four (4) methods. Three of these are
value is passed in and out of the class convenience methods that allow the
as a double. BasicSurveyorsDirection to be flopped
180 degrees or rotated forward and
There are five (5) ways to create a backward 90 degrees. The last method
BasicSurveyorsDirection object. The allows you to rotate the
default constructor creates a direction BasicSurveyorsDirection by passing in a
with a value that corresponds to 0, or rotation angle.
true north. Two additional constructors
allow you to create a There are two (2) methods of the
BasicSurveyorsDirection object from an BasicSurveyorsDirection that create JTS
angle object. You can also create a geometry objects. The
BasicSurveyorsDirection by passing two getLineStringAlongDirection method
JTS coordinate objects or a string accepts a JTS Coordinate object and a
representing the azimuth. The format double as arguments. It then creates a
for the string argument passed to this JTS LineString along the direction, using
last constructor is the same as output the Coordinate object as a start point
by the toString method of the and the double as the length of the
BasicSurveyorsAngle class. LineString. The
getCoordinateAtEndOfVector object
You can obtain the value of the accepts a JTS Coordinate object and a
BasicSurveyorsDirection object as an double as arguments. It returns a
Angle object, as a string formatted as a Coordinate object at the end of the

Page 3 of 10 http://www.osgeo.org/journal
OSGeo Journal Volume 9 (2010 Annual Report) January 2012

vector represented by the direction string in the appropriate format.


stored internally in the class and the
length passed in to the method as a Conclusion
double.
JTS Warped contains code that enables
Code Listing #2 shows how to use the programmers to integrate angles and
BasicSurveyorsDirection class to create directions into their creation and
a new line segment offset 50 units in a manipulation of JTS geometries. In this
perpendicular direction from an existing article we looked at the two (2) most
line segment. important classes of this code, the
BasicSurveyorsAngle and the
Converting Between BasicSurveyorsDirection. JTS Warped
Angular Unit Systems includes other code to enhance JTS.
This includes classes to support
A number of different units systems are common coordinate geometry
used to measure angles. Surveyors operations, additional coordinate filter
typically measure angles in values implementations, and utility methods
recorded as degrees, minutes, and for manipulation of LineStrings and
seconds. Mathematicians record angles Coordinate objects.
as radians. Military surveyors record
angles as Grads. You may measure JTS Warped is an open source library
angles in revolutions. Latitude and released under the GPL. It is maintained
longitude values are often stored in by the SurveyOS Project, a member of
decimal degrees. JTS Warped provides a the Free Software Conservancy.
utility class that allows for the easy Contributions of code or documentation
conversion between these different for the library are welcome.
angle formats. This utility class is
named AngleFormatUtilties.

The class also contains three (3)


convenience methods. Two of these
provide BasicSurveyorsAngle objects for
a given value in radians or revolutions.
The third returns a
DegreesMinutesSecondsValue from a

Page 3 of 10 http://www.osgeo.org/journal
OSGeo Journal Volume 9 (2010 Annual Report) January 2012

Code Listing #1

// Use the default, no argument constructor.


BasicSurveyorsAngle angle1 = new BasicSurveyorsAngle();

// Create a BasicSurveyorsAngle with a specific value.


BasicSurveyorsAngle angle2 = new BasicSurveyorsAngle(35, 22, 11,
0.2116);

// Create a BasicSurveyorsAngle with using a


DegreesMinutesSecondsValue.
DegreesMinutesSecondsValue dmsvalue = new DegreesMinutesSeconsValue()
BasicSurveyorsAngle angle3 = new BasicSurveyorsAngle(dmsValue);

// Clone a BasicSurveyorsAngle using the copy constructor.


// The value of clone will be the same as the value of angle2.
BasicSurveyorsAngle clone = new BasicSurveyorsAngle(angle2);

Code Listing #3

// Rotate the target LineString 90 degrees.


// Create the angle needed to perform the rotation.
BasicSurveyorsAngle rotationAngle = new BasicSurveyorsAngle(90, 0, 0, 0.0);

// Apply the rotation. “target” holds the JTS LineString to be


rotated. “rotationBase” holds
// a JTS Coordinate object that is the basis of the rotation.
rotationAngle.rotateGeometry(target, baseCoordinate, true);

Code Listing #3

// Get the end coordinates of the existing LineString.


// “targetLine” holds copy of existing LineString to offset 50 units.
Coordinate coord1 = targetLine.getCoordinateN(0);
Coordinate coord2 = targetLine.getCoordinateN(1);

Page 3 of 10 http://www.osgeo.org/journal
OSGeo Journal Volume 9 (2010 Annual Report) January 2012

// Create a BasicSurveyorsDirection from the two (2) coordinates.


BasicSurveyorsDirection dir = new BasicSurveyorsDirection(coord1, coord2);

// Rotate the direction 90 degrees so we can create a point on a line


perpendicular to the existing
// LineString.
dir.rotateForward90Degrees();

// Create a point 50 units away on the perpendicular line.


Coordinate newStartPoint = dir.getCoordinateAtEndOfVector(coord1, 50.0);

// Create a parallel LineString that is 200 units long and offset 50


unites from the existing LineString.
LineString parallelLine = dir.getLineStringAlongDirection(newStartPoint, 200.0);

Page 3 of 10 http://www.osgeo.org/journal

You might also like