Points are the building blocks for most other geometries. The following sections explain how to manipulate Points:
Creating a Point
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.
Using GeometryBuilder
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 GeoAPI 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:
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)");
Dissecting a Point
Sometimes it is useful to take apart a geometry and get the pieces that are used to build it. The following shows how you can get the ordinates of a point:
double[] ords = point.getCentroid().getCoordinates();