当前位置: 首页 > news >正文

【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;}
}

http://www.lryc.cn/news/93545.html

相关文章:

  • 从Window中先多瞥几眼
  • 【STM32训练—WiFi模块】第二篇、STM32驱动ESP8266WiFi模块获取天气
  • Maven私服
  • 手写RPC总结篇
  • c++11 标准模板(STL)(std::ios_base)成员类型与常量
  • 我用 ChatGPT 写 2023 高考语文作文:全国卷(一)
  • 4.java转义符,javadoc 标签
  • PinYin4j库的使用
  • 日志框架 --- Logback
  • QML 与 Python 交互
  • React基础教程(三):JSX语法
  • 软件测试岗位都是女孩子在做吗?
  • ARP协议,带你了解ARP协议
  • 基于Java汽车客运站管理系统设计实现(源码+lw+部署文档+讲解等)
  • 0203使用规则-索引-MySQL
  • LVS+Keepalived群集
  • 抖音矩阵系统源代码开发部署--源码搭建
  • 如何用Jmeter进行接口测试 ,这应该是全网最详细的教程了
  • C语言---malloc(0)会产生什么结果,真的是空指针吗?
  • 建模助手618 | 谁不囤点Revit插件我都会生气!
  • 【“职场程序员是否会保护自己的隐私?为何要求程序员之间保密薪资?”】
  • 企业工程管理系统源码之提高工程项目管理软件的效率
  • 创新指南 | 推动销售的17个可落地的集客式营销示例
  • ASEMI代理光宝光耦LTV-0314的应用与优势
  • Apikit 自学日记: Apikit 如何发起测试
  • python-glob模块_表格及代码样例
  • GitHub Copilot 最全安装、使用
  • C语言 指针(特别篇)
  • 【计算机视觉 | 目标检测】arxiv 计算机视觉关于目标检测的学术速递(5月26日论文合集)
  • 网络编程与自动化(python)