Geotools系列说明之LineString仿高德航路截取说明

需求分析

我们在做webgl的时候经常会遇到这样的需求,计算给定航路的拥堵情况,不同的拥堵显示不同的颜色,航路截取计算等等。基于这类问题统一都可以使用LineString进行处理

实现思路

在这里插入图片描述
如上图所示,航路是几个关键的点然后练成线,如果要实现上述的需求,就需要把每段航路进行划分成为线段,然后计算通过处理每个线段,最后汇总数据得到结果。

实现

下面是实现方案

  1. 首先拿到所有的线段的经纬度的点
  2. 通过calculator.getGeodeticPath(i);的截断方法获取起点和终点的截断
  3. 根据截断得到新的LineString
  4. 实现了把一个航路分段成多个小段
  5. 然后计算点到线段的距离知道每个线段的数量就可以实现线段变色了
 /**
     * 分段算法
     * @param pointList
     * @return
     */
    public List<LineString> getRouteSegment(List<RoutePointDTO> pointList)  {

        List<LineString> lineStrings = new ArrayList<>();
        for (int i = 0; i < pointList.size()-1; i++) {
            RoutePointDTO startRoute = pointList.get(i);
            RoutePointDTO endRoute = pointList.get(i+1);
            Coordinate start = new Coordinate(startRoute.getLongitude(), startRoute.getLatitude(),
                    startRoute.getHeight());
            log.debug("开始的位置:{}",start);
            Coordinate end = new Coordinate(endRoute.getLongitude(),endRoute.getLatitude(),
                    endRoute.getHeight());
            log.debug("结束的位置:{}",end);
            List<Point2D> point2DS = GeoTools.point2DList(start, end, 5000);
            for (int j = 0; j < point2DS.size()-1; j++) {
                Point2D startPoint = point2DS.get(j);
                Point2D endPoint = point2DS.get(j + 1);
                Coordinate[] coordinates = new Coordinate[]{
                        new Coordinate(startPoint.getX(),startPoint.getY(),start.z),
                        new Coordinate(endPoint.getX(),endPoint.getY(),end.z)
                };
                LineString lineString = GeometryUtil.createLineString(coordinates);
                lineStrings.add(lineString);
                log.debug("截取后的:{}", Arrays.toString(coordinates));
            }
        }

        return lineStrings;
    }
  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要使用 Geotools 计算高德经纬度范围的面积并将其转换为平方米,您可以按照以下步骤进行操作: 1. 首先,您需要将高德经纬度坐标转换为 Geotools 中的几何对象。您可以使用 Geotools 的 `GeometryFactory` 类来创建几何对象。假设您有一个包含高德经纬度坐标的列表 `List<Point2D.Double> coordinates`,可以按照以下方式创建几何对象: ```java import com.vividsolutions.jts.geom.Coordinate; import com.vividsolutions.jts.geom.Geometry; import com.vividsolutions.jts.geom.GeometryFactory; // 创建 GeometryFactory 对象 GeometryFactory geometryFactory = new GeometryFactory(); // 创建 Coordinate 数组 Coordinate[] coordinateArray = new Coordinate[coordinates.size()]; for (int i = 0; i < coordinates.size(); i++) { Point2D.Double point = coordinates.get(i); coordinateArray[i] = new Coordinate(point.getX(), point.getY()); } // 创建几何对象 Geometry polygon = geometryFactory.createPolygon(coordinateArray); ``` 2. 接下来,您可以使用 Geotools 的 `Area` 类来计算面积。请注意,`Area` 类返回的面积单位是平方度(square degrees),而不是平方米。为了将其转换为平方米,您需要使用 Geotools 的 `MeasureUtilities` 类进行单位转换。以下是示例代码: ```java import org.geotools.measure.Measure; import org.geotools.measure.MeasureUtilities; // 创建 Area 对象 org.geotools.geometry.jts.Area area = new org.geotools.geometry.jts.Area(polygon); // 获取面积值(单位为平方度) double areaValue = area.getArea(); // 创建面积测量对象 Measure<Double, org.geotools.measure.MeasureUtilities.Area> areaMeasure = Measure.valueOf(areaValue, org.geotools.measure.MeasureUtilities.SQUARE_DEGREE); // 转换为平方米 double areaInSquareMeters = MeasureUtilities.convert(areaMeasure, org.geotools.measure.MeasureUtilities.SQUARE_METRE); System.out.println("面积:" + areaInSquareMeters + " 平方米"); ``` 请确保在使用该代码之前,已经正确导入 GeotoolsJTS 的相关包,并且已经设置好 Geotools 的环境。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

stayhungerstayflush

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值