【Turfjs的java版本JTS】前面讲了Turfjs可以实现几何计算,空间计算的功能,如果后端要做这项功能也有类似的类库,JTS

JTS = Java Topology Suite

几何计算:

1. 前端js就用这个 Turfjs的类库。参考网站: 计算两线段相交点 | Turf.js中文网

2. 后端java语言就可以用 JTS这个类库,参考网站:

 JTS参考网站:

1. https://github.com/locationtech/jts

GitHub - locationtech/jts: The JTS Topology Suite is a Java library for creating and manipulating vector geometry.The JTS Topology Suite is a Java library for creating and manipulating vector geometry. - GitHub - locationtech/jts: The JTS Topology Suite is a Java library for creating and manipulating vector geometry.https://github.com/locationtech/jts 2. https://locationtech.github.io/jts/JTS | Documentationhttps://locationtech.github.io/jts/

1. https://github.com/locationtech/jts

2. https://locationtech.github.io/jts/

POM文件:

<dependency>
    <groupId>org.locationtech.jts</groupId>
    <artifactId>jts-core</artifactId>
    <version>1.18.0</version>
</dependency>

实例代码:

可以通过JTS

要使用JTS(Java Topology Suite)库计算

1. 某个点是否在另外一个闭合的空间内
2. 计算某个闭合的空间的中心的的位置

3. 已知两个点的经纬度,计算他们之间的距离

4. 已知某点的经纬度坐标,计算其他点的经纬度坐标

 简单实例如下:

package com.simulate.jts;

import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.GeometryFactory;
import org.locationtech.jts.geom.Point;
import org.locationtech.jts.geom.Polygon;
import org.locationtech.jts.io.ParseException;
import org.locationtech.jts.io.WKTReader;
import org.locationtech.jts.linearref.LengthIndexedLine;


public class JtsExample {


    public static void main(String[] args) throws ParseException {

        // 1. 某个点是否在另外一个闭合的空间内
        // isInside();

        // 2. 计算某个闭合的空间的中心的的位置
        // calcCentPoint();

        //3. 已知两个点的经纬度,计算他们之间的距离
        // pointDistance();

        // 4. 已知某点的经纬度坐标,计算其他点的经纬度坐标
        calcCoordinate();

        // 5.


    }

    // 1. 某个点是否在另外一个闭合的空间内
    static void isInside() throws ParseException{
        // 创建 GeometryFactory 对象
        GeometryFactory geometryFactory = new GeometryFactory();

        // 创建点对象
        Coordinate pointCoord = new Coordinate(2.0, 2.0);
        Point point = geometryFactory.createPoint(pointCoord);

        // 创建多边形对象
        WKTReader wktReader = new WKTReader(geometryFactory);
        Polygon polygon = (Polygon) wktReader.read("POLYGON((0 0, 0 4, 4 4, 4 0, 0 0))");

        // 判断点是否在多边形内部
        boolean isInside = polygon.contains(point);

        // 输出结果
        System.out.println("Point: " + point.toText());
        System.out.println("Polygon: " + polygon.toText());
        System.out.println("Is inside: " + isInside);
    }


    // 2. 计算某个闭合的空间的中心的的位置
    static void calcCentPoint(){
        int pointCount = 5;
        Coordinate[] coordinates = new Coordinate[pointCount];

        // 填充Coordinate数组
        coordinates[0] = new Coordinate(1.0, 1.0);
        coordinates[1] = new Coordinate(2.0, 3.0);
        coordinates[2] = new Coordinate(4.0, 1.0);
        coordinates[3] = new Coordinate(3.0, 4.0);
        coordinates[4] = new Coordinate(1.0, 1.0);

        // 创建Polygon对象
        GeometryFactory factory = new GeometryFactory();
        Polygon polygon = factory.createPolygon(coordinates);
        polygon.getDimension();

        // 计算中心点坐标
        Coordinate centerCoordinate = polygon.getCentroid().getCoordinate();
        double centerX = centerCoordinate.x;
        double centerY = centerCoordinate.y;

        // 输出结果
        System.out.println("Center point: (" + centerX + ", " + centerY + ")");
    }


    //3. 已知两个点的经纬度,计算他们之间的距离
    static void pointDistance(){
        double lon1 = 115.8575; // 第一个点的经度
        double lat1 = 28.6829;  // 第一个点的纬度
        double lon2 = 116.4074; // 第二个点的经度
        double lat2 = 39.9042;  // 第二个点的纬度

        // 创建GeometryFactory对象
        GeometryFactory factory = new GeometryFactory();

        // 创建Coordinate对象
        Coordinate coordinate1 = new Coordinate(lon1, lat1);
        Coordinate coordinate2 = new Coordinate(lon2, lat2);

        // 创建Point对象
        Point point1 = factory.createPoint(coordinate1);
        Point point2 = factory.createPoint(coordinate2);

        // 计算两点之间的距离
        double distance = point1.distance(point2);


        // 输出结果
        System.out.println("Distance between the two points: " + distance);
    }



    /
    // 4. 已知某点的经纬度坐标,计算其他点的经纬度坐标
    static void calcCoordinate() {
        // 假设已知的参考点的坐标
        double lat1 = 40.7128;   // 参考点的纬度
        double lon1 = -74.0060;  // 参考点的经度

        // 假设要计算的距离和方向
        double distanceInMeters = 1000;  // 距离为 1000 米
        double bearingInDegrees = 45;    // 方向为 45 度

        // 创建参考点的坐标对象
        Coordinate referenceCoord = new Coordinate(lon1, lat1);

        // 计算目标点的坐标
        Coordinate targetCoord = calculateCoordinate(referenceCoord, distanceInMeters, bearingInDegrees);

        // 打印目标点的经纬度
        System.out.println("目标点的经度:" + targetCoord.x);
        System.out.println("目标点的纬度:" + targetCoord.y);
    }

    // 使用 JTS 计算目标坐标
    static Coordinate calculateCoordinate(Coordinate referenceCoord, double distance, double bearing) {
        // 将距离转换为度数
        double distanceInDegrees = Math.toDegrees(distance / 6371000.0); // 假设地球是一个球体,半径为 6371000 米

        // 根据参考点和距离创建线段对象
        // LengthIndexedLine line = new LengthIndexedLine(new Coordinate[] { referenceCoord });
        GeometryFactory factory = new GeometryFactory();
        Point referencePoint = factory.createPoint(referenceCoord);
        LengthIndexedLine line = new LengthIndexedLine(referencePoint);

        // 在线段上根据方向和距离计算目标点的索引
        double targetIndex = line.project(referenceCoord) + distanceInDegrees;

        // 根据目标索引获取目标点的坐标
        Coordinate targetCoord = line.extractPoint(targetIndex);
        return targetCoord;
    }
}

是的,Java也可以使用相似的实现泰森多边形的创建。下面是一个使用 JTS Topology Suite Java示例代码: ```java import java.util.ArrayList; import java.util.List; import org.locationtech.jts.algorithm.ConvexHull; import org.locationtech.jts.algorithm.DelaunayTriangulationBuilder; import org.locationtech.jts.geom.Coordinate; import org.locationtech.jts.geom.Geometry; import org.locationtech.jts.geom.GeometryFactory; import org.locationtech.jts.geom.MultiPolygon; import org.locationtech.jts.geom.Polygon; import org.locationtech.jts.geom.PrecisionModel; import org.locationtech.jts.geom.impl.PackedCoordinateSequenceFactory; public class Voronoi { public static void main(String[] args) { // 输入一组经纬度坐标点 Coordinate[] coords = new Coordinate[] { new Coordinate(116.404, 39.915), new Coordinate(116.418, 39.921), new Coordinate(116.434, 39.907), new Coordinate(116.388, 39.908), new Coordinate(116.401, 39.889), new Coordinate(116.382, 39.893), new Coordinate(116.387, 39.911), new Coordinate(116.412, 39.887), new Coordinate(116.408, 39.902), new Coordinate(116.441, 39.914), new Coordinate(116.428, 39.901), new Coordinate(116.456, 39.909) }; // 创建 GeometryFactory PackedCoordinateSequenceFactory coordSeqFactory = new PackedCoordinateSequenceFactory(PackedCoordinateSequenceFactory.DOUBLE); PrecisionModel precisionModel = new PrecisionModel(PrecisionModel.FLOATING); GeometryFactory geometryFactory = new GeometryFactory(coordSeqFactory, precisionModel); // 使用 Delaunay 三角剖分算法创建三角形网格 DelaunayTriangulationBuilder builder = new DelaunayTriangulationBuilder(); builder.setSites(coords); Geometry triangles = builder.getTriangles(geometryFactory); // 获取凸包信息 ConvexHull convexHull = new ConvexHull(coords, geometryFactory); Geometry hull = convexHull.getConvexHull(); // 计算泰森多边形 List<Polygon> voronoiPolygons = new ArrayList<>(); for (int i = 0; i < triangles.getNumGeometries(); i++) { Geometry triangle = triangles.getGeometryN(i); Coordinate[] triangleCoords = triangle.getCoordinates(); Polygon voronoiPolygon = geometryFactory.createPolygon(triangleCoords); if (hull.contains(voronoiPolygon)) { voronoiPolygons.add(voronoiPolygon); } } // 输出泰森多边形 MultiPolygon voronoiDiagram = geometryFactory.createMultiPolygon(voronoiPolygons.toArray(new Polygon[0])); System.out.println(voronoiDiagram); } } ``` 这个示例代码将输出一个由泰森多边形组成的多边形集合。你可以根据需要进一步处理这些多边形。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值