本文介绍了如何使用Java JTS将线串每x米分成几部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将JTS和postgis用于基于spring的后端.虽然我了解JTS的基础知识,但我不知道如何实现

I'm trying to use JTS and postgis for backend based on spring.While I know basics of JTS I don't know how to achieve

推荐答案

我还需要编写split函数,因此我在stackoverflow上找到了解决方案,但找不到一些有用的信息.因此,我编写了follow方法并回答了您的问题;

i also need to write the split function,so i find solution on stackoverflow,but not find some useful information.so i write follow method and answer your question;

public List<LineString> lineSplit(LineString lineString,double meters) {
    List<LineString> results = new ArrayList<>();
    List<LineSegment> segments = new ArrayList<>();
    // first split linestring to segements[]
    for(int i = 1; i < lineString.getCoordinates().length; i++){
        segments.add(new LineSegment(lineString.getCoordinates()[i-1], lineString.getCoordinates()[i]));
    }
    // remainLegnth means that last segment's length dont enough to split to one segement which length is meters
    // neededLength means that current segment need how many meters to create a new segment
    double remainLength = 0D;
    double neededLength = 0D;
    // remainCoors means that if the last iteartor dont create a new segment,also mean  last segment
    // is too short ,even add remains length can't create a new segment;so, we should add this segment's start
    // point and end point to remainCoors
    List<Coordinate> remainCoors = new ArrayList<>();
    // netxStartPoint to store the next segment's start point 
    Coordinate netxStartPoint = null;
    for(int i=0;i<segments.size();i++) {
        LineSegment seg = segments.get(i);
        neededLength = meters-remainLength;
        remainLength += seg.getLength();
        netxStartPoint = seg.p0;
        while(remainLength>=meters) {
            remainCoors.add(netxStartPoint);
            Coordinate endPoint = seg.pointAlong(neededLength/seg.getLength());
            // to remove the adjacent and same vertx
            for(int j=0;j<remainCoors.size()-1;j++) {
                if(remainCoors.get(j).equals(remainCoors.get(j+1))) {
                    remainCoors.remove(j);
                }
            }
            remainCoors.add(endPoint);
            results.add(lineString.getFactory().createLineString(remainCoors.toArray(new Coordinate[remainCoors.size()])));
            remainCoors = new ArrayList<>();
            netxStartPoint = endPoint;
            remainLength -= meters;
            neededLength += meters;
        }
        remainCoors.add(netxStartPoint);
        remainCoors.add(seg.p1);
    }
    for(int j=0;j<remainCoors.size()-1;j++) {
        if(remainCoors.get(j).equals(remainCoors.get(j+1))) {
            remainCoors.remove(j);
        }
    }
    if(remainCoors.size()>=2) {
        results.add(lineString.getFactory().createLineString(remainCoors.toArray(new Coordinate[remainCoors.size()])));
    }
    return results;
}

这篇关于如何使用Java JTS将线串每x米分成几部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 13:39