当前位置: 首页 > 工具软件 > Spatial4J > 使用案例 >

GIS-Spatial4j-对象创建说明(二)

商飞龙
2023-12-01

对象创建说明

示例代码库

1.创建几何工厂

1.1 ShapeFactory

spatial4j创建工厂 SpatialContext GEO模式(通过球计算公式计算相关关系)

SpatialContext ctx = SpatialContext.GEO;
ShapeFactory shapeFactory = ctx.getShapeFactory();

1.2 JtsShapeFactory

JtsSpatialContext jtsSpatialContext = JtsSpatialContext.GEO;
JtsShapeFactory jtsShapeFactory = jtsSpatialContext.getShapeFactory();

2.创建几何对象

2.1 spatial4j shape对象创建

spatial4j原生几何对象创建 circle、GeoCircle[-180,180]其它值将进行转换、Rectangle、PointImpl

2.1.1 方式一通过shapeFactory

ShapeFactory.LineStringBuilder lineStringBuilder = shapeFactory.lineString();
Shape shapeLine = lineStringBuilder.pointXYZ(1,2,3).pointXYZ(2,3,4).build();

2.1.2 方式二通过 impl实现类

Circle circle = new CircleImpl(new PointImpl(0,0,sCtx),0.01,sCtx);
Rectangle rectangle = new RectangleImpl(new PointImpl(3.0,4.0, sCtx), new PointImpl(7.0,8.0,sCtx),sCtx);
Rectangle rectangle2 = new RectangleImpl(new PointImpl(5.0,2.0, sCtx), new PointImpl(10.0,7.0,sCtx),sCtx);

2.2 JTS对象创建

创建jts对象 Point LineString Polygon等

GeometryFactory geometryFactory = jtsShapeFactory.getGeometryFactory();
Coordinate coordinate = new Coordinate(0.0,1.1,1);
Point point = geometryFactory.createPoint(coordinate);

3. Shape对象与JTS对象转换

shape对象转换为JTS,将shape对象转换为相应的JTS对象,可以利用JTS TestBuilder等工具查看几何形状等;JTS对象转换为Shape对象,可以利用shape接口进行几何运算

3.1 Shape转JTS对象

通过JtsShapeFactory.getGeometryFrom

Geometry circleGeo = jtsShapeFactory.getGeometryFrom(circle);
System.out.println("shape circle to jts geometry:"+circleGeo.toText());
Geometry rectangleGeo = jtsShapeFactory.getGeometryFrom(rectangle);
System.out.println("shape rectangle to jts geometry:"+rectangleGeo.toText());
Geometry rectangle2Geo = jtsShapeFactory.getGeometryFrom(rectangle2);
System.out.println("shape rectangle2 to jts geometry:"+rectangle2Geo.toText());

输出(此WKT形状可以通过JTS TestBuilder展示几何形状):

shape circle to jts geometry:POLYGON ((0.01 0, 0.0099802672842827 0.0006279051952931, 0.0099211470131448 0.001253332335643, 0.0098228725072869 0.0018738131458572, 0.0096858316112863 0.0024868988716485, 0.0095105651629515 0.0030901699437495, 0.0092977648588825 0.0036812455268468, 0.0090482705246602 0.0042577929156507, 0.0087630668004386 0.0048175367410172, 0.0084432792550202 0.00535826794979, 0.0080901699437495 0.0058778525229247, 0.0077051324277579 0.0063742398974869, 0.0072896862742141 0.0068454710592869, 0.0068454710592869 0.0072896862742141, 0.0063742398974869 0.0077051324277579, 0.0058778525229247 0.0080901699437495, 0.00535826794979 0.0084432792550202, 0.0048175367410172 0.0087630668004386, 0.0042577929156507 0.0090482705246602, 0.0036812455268468 0.0092977648588825, 0.0030901699437495 0.0095105651629515, 0.0024868988716485 0.0096858316112863, 0.0018738131458572 0.0098228725072869, 0.001253332335643 0.0099211470131448, 0.0006279051952931 0.0099802672842827, -0 0.01, -0.0006279051952931 0.0099802672842827, -0.001253332335643 0.0099211470131448, -0.0018738131458572 0.0098228725072869, -0.0024868988716485 0.0096858316112863, -0.0030901699437495 0.0095105651629515, -0.0036812455268468 0.0092977648588825, -0.0042577929156507 0.0090482705246602, -0.0048175367410172 0.0087630668004386, -0.00535826794979 0.0084432792550202, -0.0058778525229247 0.0080901699437495, -0.0063742398974869 0.0077051324277579, -0.0068454710592869 0.0072896862742141, -0.0072896862742141 0.0068454710592869, -0.0077051324277579 0.0063742398974869, -0.0080901699437495 0.0058778525229247, -0.0084432792550202 0.00535826794979, -0.0087630668004386 0.0048175367410172, -0.0090482705246602 0.0042577929156507, -0.0092977648588825 0.0036812455268468, -0.0095105651629515 0.0030901699437495, -0.0096858316112863 0.0024868988716485, -0.0098228725072869 0.0018738131458572, -0.0099211470131448 0.001253332335643, -0.0099802672842827 0.0006279051952931, -0.01 -0, -0.0099802672842827 -0.0006279051952931, -0.0099211470131448 -0.001253332335643, -0.0098228725072869 -0.0018738131458572, -0.0096858316112863 -0.0024868988716486, -0.0095105651629515 -0.0030901699437495, -0.0092977648588825 -0.0036812455268468, -0.0090482705246602 -0.0042577929156507, -0.0087630668004386 -0.0048175367410172, -0.0084432792550202 -0.00535826794979, -0.0080901699437495 -0.0058778525229247, -0.0077051324277579 -0.0063742398974869, -0.0072896862742141 -0.0068454710592869, -0.0068454710592869 -0.0072896862742141, -0.0063742398974869 -0.0077051324277579, -0.0058778525229247 -0.0080901699437495, -0.00535826794979 -0.0084432792550202, -0.0048175367410172 -0.0087630668004386, -0.0042577929156507 -0.0090482705246602, -0.0036812455268468 -0.0092977648588825, -0.0030901699437495 -0.0095105651629515, -0.0024868988716485 -0.0096858316112863, -0.0018738131458572 -0.0098228725072869, -0.001253332335643 -0.0099211470131448, -0.0006279051952931 -0.0099802672842827, -0 -0.01, 0.0006279051952931 -0.0099802672842827, 0.001253332335643 -0.0099211470131448, 0.0018738131458573 -0.0098228725072869, 0.0024868988716485 -0.0096858316112863, 0.0030901699437495 -0.0095105651629515, 0.0036812455268468 -0.0092977648588825, 0.0042577929156507 -0.0090482705246602, 0.0048175367410172 -0.0087630668004386, 0.00535826794979 -0.0084432792550202, 0.0058778525229247 -0.0080901699437495, 0.0063742398974869 -0.0077051324277579, 0.0068454710592869 -0.0072896862742141, 0.0072896862742141 -0.0068454710592869, 0.0077051324277579 -0.0063742398974869, 0.0080901699437495 -0.0058778525229247, 0.0084432792550202 -0.00535826794979, 0.0087630668004386 -0.0048175367410172, 0.0090482705246602 -0.0042577929156507, 0.0092977648588825 -0.0036812455268468, 0.0095105651629515 -0.0030901699437495, 0.0096858316112863 -0.0024868988716485, 0.0098228725072869 -0.0018738131458572, 0.0099211470131448 -0.001253332335643, 0.0099802672842827 -0.0006279051952931, 0.01 0))
shape rectangle to jts geometry:POLYGON ((3 4, 3 8, 7 8, 7 4, 3 4))
shape rectangle2 to jts geometry:POLYGON ((5 2, 5 7, 10 7, 10 2, 5 2))
shape rectangleBBox to jts geometry:POLYGON ((3 2, 3 8, 10 8, 10 2, 3 2))

3.2 JTS转Shape对象

通过JtsShapeFactory.makeShape(point);

JtsGeometry jtsPoint = jtsShapeFactory.makeShape(point);
 类似资料: