Creating a Point
Points are the building blocks for most other geometries. The following sections explain how to manipulate Points:
There are a few ways you can go about creating a point. GeometryBuilder has many utility methods for creating geometries, so you don’t have to worry about factories. But you can also use factories directly, or you can also use a WKT parser for creating points.
There are several createPoint methods provided as part of GeometryBuilder. Here is an example using one of them:
GeometryBuilder builder = new GeometryBuilder( DefaultGeographicCRS.WGS84 );
Point point = builder.createPoint( 48.44, -123.37 );
Using Factories
In some environments you are restricted to just using formal gt-opengis interfaces, here is an example of using the PositionFactory and PrimitiveFactory as is:
Hints hints = new Hints( Hints.CRS, DefaultGeographicCRS.WGS84 );
PositionFactory positionFactory = GeometryFactoryFinder.getPositionFactory( hints );
PrimitiveFactory primitiveFactory = GeometryFactoryFinder.getPrimitiveFactory( hints );
DirectPosition here = positionFactory.createDirectPosition( new double[]{48.44, -123.37} );
Point point1 = primitiveFactory.createPoint( here );
PositionFactory has a helper method allowing you to save one step:
Hints hints = new Hints( Hints.CRS, DefaultGeographicCRS.WGS84 );
PrimitiveFactory primitiveFactory = GeometryFactoryFinder.getPrimitiveFactory( hints );
Point point2 = primitiveFactory.createPoint( new double[]{48.44, -123.37} );
System.out.println( point2 );
Using WKT
You can use the WKTParser to create a point from a well known text:
WKTParser parser = new WKTParser( DefaultGeographicCRS.WGS84 );
Point point = (Point) parser.parse("POINT( 48.44 -123.37)");
You can also create the WKTParser to use a specific set of factories:
Hints hints = new Hints( Hints.CRS, DefaultGeographicCRS.WGS84 );
PositionFactory positionFactory = GeometryFactoryFinder.getPositionFactory(hints);
GeometryFactory geometryFactory = GeometryFactoryFinder.getGeometryFactory(hints);
PrimitiveFactory primitiveFactory = GeometryFactoryFinder.getPrimitiveFactory(hints);
AggregateFactory aggregateFactory = GeometryFactoryFinder.getAggregateFactory(hints);
WKTParser parser = new WKTParser( geometryFactory, primitiveFactory, positionFactory, aggregateFactory );
Point point = (Point) parser.parse("POINT( 48.44 -123.37)");