创建一个点
点是大多数其他几何图形的构建块。以下部分解释了如何操作点:
有几种方法可以创建一个点。GeometryBuilder 有许多用于创建几何图形的实用方法,因此您不必担心工厂。但是您也可以直接使用工厂,也可以使用 WKT 解析器来创建点。
作为 GeometryBuilder 的一部分提供了几个 createPoint 方法。这是使用其中之一的示例:
GeometryBuilder builder = new GeometryBuilder( DefaultGeographicCRS.WGS84 );
Point point = builder.createPoint( 48.44, -123.37 );
使用工厂 在某些环境中,您只能使用正式的 gt-opengis 接口,下面是使用 PositionFactory 和 PrimitiveFactory 的示例:
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 );
使用 WKT 您可以使用 WKTParser 从众所周知的文本中创建一个点:
WKTParser parser = new WKTParser( DefaultGeographicCRS.WGS84 );
Point point = (Point) parser.parse("POINT( 48.44 -123.37)");
您还可以创建 WKTParser 以使用一组特定的工厂:
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)");