Added by jgarnett, last edited by jgarnett on May 14, 2008  (view change)

Labels

 
(None)

You create a Geometry using a GeometryFactory. A GeometryFactory has a bunch of create methods that take Coordiante instances (and arrays) and wrap them up as a Geometry object.

You can create your own GeometryFactory with a specific PrecisionModel and CoordinateSequenceFactory. These configuration options are only of interest if you are wanting your Coordinates to store values as floats rather than doubles.

The GeometryFactory created by default works just fine.

Creating a Point

This time we are using a JTS GeometryFactory, although you can create one yourself (if you want to fiddle with Precision}} there is a global one available using the FactoryFinder.

GeometryFactory geometryFactory = FactoryFinder.getGeometryFactory( null );

Coordinate coord = new Coordinate( 1, 1 );
Point point = geometryFactory.createPoint( coord );

Alternative - Reading a Point from WKT

"Well Known Text" is a simple text format defined by the Simple Feature for SQL specification:

GeometryFactory geometryFactory = FactoryFinder.getGeometryFactory( null );

WKTReader reader = new WKTReader( geometryFactory );
Point point = (Point) reader.read("POINT (1 1)");

Creating a LineString

The following makes a line string in the shape of a check mark:

GeometryFactory geometryFactory = FactoryFinder.getGeometryFactory( null );

Coordinate[] coords  =
    new Coordinate[] {new Coordinate(0, 2), new Coordinate(2, 0), new Coordinate(8, 6) };

LineString line = geometryFactory.createLineString(coordinates);

Alternative - Reading a LineString from WKT

GeometryFactory geometryFactory = FactoryFinder.getGeometryFactory( null );

WKTReader reader = new WKTReader( geometryFactory );
LineString line = (LineString) reader.read("LINESTRING(0 2, 2 0, 8 6)");

Creating a Polygon

TBA

Alternative - Reading a Polygon from WKT

GeometryFactory geometryFactory = FactoryFinder.getGeometryFactory( null );

WKTReader reader = new WKTReader( geometryFactory );
Polygon polygon = (Polygon) reader.read("POLYGON((20 10, 30 0, 40 10, 30 20, 20 10))");

Arcs, Circles and Curves

The JTS Topology Suite does not have any constructs to represent a "curve" or "circle" - it is strictly limited to geometry made up of straight lines.

In order to represent circles and curves you will need to produce them (yourself) using a little bit of math. If there is general interest in these kinds of functions we can add them to the GeoTools library.

Creating a Circle

private static Geometry createCircle(double x, double y, final double RADIUS) {
    final int SIDES = 32;
    Coordinate coords[] = new Coordinate[S];
    for( int i = 0; i < S; i++){
        double angle = ((double) i / (double) SIDES) * 360.0;
        double dx = Math.cos( angle ) * RADIUS;
        double dy = Math.sin( angle ) * RADIUS;
        coords[i] = new Coordinate( (double) x + dx, (double) y + dy );  
    }
    coords[coords.length-1] = coords[0];
         
    LinearRing ring = factory.createLinearRing( coords );
    Polygon polygon = factory.createPolygon( ring, null );
         
    return polygon;
}