How to split linestring into parts every x meters with java JTS

折月煮酒 提交于 2019-12-11 20:14:04

问题


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


回答1:


Simply use the Java implementation of geographiclib - I believe you want GeodesicLine; then use JTS and postgis to get the linestring coordinates you generate that way into postgresql. This geodesic path example in JavaScript should get you started.




回答2:


I wrote the following method that is capable of what you are asking for and uses JTS LineSegment objects to calculate the splitting points:

public ArrayList<LineString> splitLineStringIntoParts(LineString ls, double length){
    // result list for linestrings
    ArrayList<LineString> resultList = new ArrayList();
    // list for linesegments from input linestring
    ArrayList<LineSegment> lineSegmentList = new ArrayList();
    // create LineSegment objects from input linestring and add them to list
    for(int i = 1; i < ls.getCoordinates().length; i++){
        lineSegmentList.add(new LineSegment(ls.getCoordinates()[i-1], ls.getCoordinates()[i]));
    }
    LineString currentLineString = null;
    double neededLength = length;
    for(LineSegment s : lineSegmentList){
        while(s.getLength() > 0){
            // case: current segment is small enough to be added to the linestring
            if(s.getLength() <= neededLength){
                // create linestring if it does not exist 
                if(currentLineString == null){
                    currentLineString = new GeometryFactory().createLineString(new Coordinate[]{new Coordinate(s.p0), new Coordinate(s.p1)});
                // just add the new endpoint otherwise
                } else {
                    Coordinate[] coords = new Coordinate[currentLineString.getCoordinates().length + 1];
                    // copy old coordinates
                    System.arraycopy(currentLineString.getCoordinates(), 0, coords, 0, currentLineString.getCoordinates().length);
                    // add new coordinate at the end
                    coords[coords.length-1] = new Coordinate(s.p1);
                    // create new linestring
                    currentLineString = new GeometryFactory().createLineString(coords);
                }
                neededLength -= s.getLength();
                s.setCoordinates(s.p1, s.p1);
                // add linestring to result list if needed length is 0
                if(neededLength == 0){
                    resultList.add(currentLineString);
                    currentLineString = null;
                    neededLength = length;
                }
            // current segment needs to be cut and added to the linestring
            } else {
                // get coordinate at desired distance (endpoint of linestring)
                Coordinate endPoint = s.pointAlong(neededLength/s.getLength());
                // create linestring if it does not exist 
                if(currentLineString == null){
                    currentLineString = new GeometryFactory().createLineString(new Coordinate[]{new Coordinate(s.p0), endPoint});
                // just add the new endpoint otherwise
                } else {
                    // add new coordinate to linestring
                    Coordinate[] coords = new Coordinate[currentLineString.getCoordinates().length + 1];
                    // copy old coordinates
                    System.arraycopy(currentLineString.getCoordinates(), 0, coords, 0, currentLineString.getCoordinates().length);
                    // add new coordinate at the end
                    coords[coords.length-1] = endPoint;
                    currentLineString = new GeometryFactory().createLineString(coords);
                }
                // add linestring to result list
                resultList.add(currentLineString);
                // reset needed length
                neededLength = length;
                // reset current linestring
                currentLineString = null;
                // adjust segment (calculated endpoint is the new startpoint)
                s.setCoordinates(endPoint, s.p1);
            }
        }
    }
    // add last linestring if there is a rest
    if(neededLength < length){
        resultList.add(currentLineString);
    }
    return resultList;
}



回答3:


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


来源:https://stackoverflow.com/questions/33549915/how-to-split-linestring-into-parts-every-x-meters-with-java-jts

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!