Revision 28 org.gvsig.lrs/trunk/org.gvsig.lrs/org.gvsig.lrs.lib/org.gvsig.lrs.lib.impl/src/main/java/org/gvsig/lrs/lib/impl/LrsCreateRouteAlgorithm.java

View differences:

LrsCreateRouteAlgorithm.java
311 311
        boolean ignoreSpatialGaps = parameters.ignoreSpatialGaps();
312 312
        for (MStructure mStructure : mList) {
313 313
            mStructure.fromField =
314
                getGeometryLength(mStructure.geometry, ignoreSpatialGaps);
314
                LrsAlgorithmUtils.getGeometryLength(mStructure.geometry, ignoreSpatialGaps);
315 315
        }
316 316
        return calculateGeometryByOneField(mList);
317 317
    }
......
336 336

  
337 337
        for (MStructure mStructure : mList) {
338 338
            Geometry geometry = mStructure.geometry;
339
            List<Line> geometryLines=extractLines(geometry);
340
            Double geometryLength=getGeometryLength(geometry, ignoreSpatialGaps);
339
            List<Line> geometryLines=LrsAlgorithmUtils.extractLines(geometry);
340
            Double geometryLength=LrsAlgorithmUtils.getGeometryLength(geometry, ignoreSpatialGaps);
341 341
            boolean firstLineInGeometry=true;
342 342

  
343 343
            if (!ignoreSpatialGaps) {
......
357 357
            Double distance=Double.valueOf(0);;
358 358
            for (Line line:geometryLines){
359 359
                Double gapBetweenLines = Double.valueOf(0);
360
                Double lineLength=getLineLength(line);
360
                Double lineLength=LrsAlgorithmUtils.getLineLength(line);
361 361
                if (!ignoreSpatialGaps&&!firstLineInGeometry) {
362 362
                    gapBetweenLines = calculateGap(lastPoint, line);
363 363
                    distance+=gapBetweenLines;
......
408 408
    }
409 409

  
410 410
    private Double getCalculatedMValueForFirstPoint(Geometry mGeometry,Geometry geometry){
411
        List<Point> mPoints=extractPoints(mGeometry);
412
        Point point=extractFirstPoint(geometry);
411
        List<Point> mPoints=LrsAlgorithmUtils.extractPoints(mGeometry);
412
        Point point=LrsAlgorithmUtils.extractFirstPoint(geometry);
413 413
        for (Point mPoint:mPoints){
414 414
            if ( mPoint.getX()==point.getX() && mPoint.getY()==point.getY() ){
415 415
                int dimensionM=mPoint.getDimension()-1;
......
428 428

  
429 429
        for (MStructure mStructure : mList) {
430 430
            Geometry geometry = mStructure.geometry;
431
            List<Line> geometryLines=extractLines(geometry);
432
            Double geometryLength=getGeometryLength(geometry);
431
            List<Line> geometryLines=LrsAlgorithmUtils.extractLines(geometry);
432
            Double geometryLength=LrsAlgorithmUtils.getGeometryLength(geometry);
433 433

  
434 434
            Double geometryMFirstPoint = mStructure.fromField;
435 435
            Double geometryMLastPoint=mStructure.toField;
436 436

  
437 437
            Double distance=Double.valueOf(0);
438 438
            for (Line line:geometryLines){
439
                Double lineLength=getLineLength(line);
439
                Double lineLength=LrsAlgorithmUtils.getLineLength(line);
440 440
                Double mFirstPoint =
441 441
                    calculateM(geometryMLastPoint, geometryMFirstPoint, distance, geometryLength);
442 442
                distance+=lineLength;
......
513 513
        throws CreateGeometryException, GeometryOperationNotSupportedException,
514 514
        GeometryOperationException {
515 515
        GeometryManager geomanager = GeometryLocator.getGeometryManager();
516
        Double lineLength = getLineLength(line);
516
        Double lineLength = LrsAlgorithmUtils.getLineLength(line);
517 517
        Line lineM = (Line) geomanager.create(Geometry.TYPES.LINE,
518 518
            Geometry.SUBTYPES.GEOM2DM);
519 519
        Double inLineDistance = Double.valueOf(0);
......
867 867
        List <Route> routes=new ArrayList<Route>();
868 868
        for (MStructure mStructure:mList){
869 869
            Geometry geometry=mStructure.geometry;
870
            List<Line> lines=extractLines(geometry);
870
            List<Line> lines=LrsAlgorithmUtils.extractLines(geometry);
871 871
            for (Line initialLine:lines){
872 872
                if (!isAddedLine(routes,initialLine)){
873 873
                    Route route=new Route();
......
896 896
                        boolean lineAdded=false;
897 897
                        MStructure mStructureAdded=mList.get(i);
898 898
                        Geometry geometryAdded=mStructureAdded.geometry;
899
                        List<Line> linesAdded=extractLines(geometryAdded);
899
                        List<Line> linesAdded=LrsAlgorithmUtils.extractLines(geometryAdded);
900 900
                        for (Line addedLine:linesAdded){
901 901
                            if (!isAddedLine(routes,addedLine)){
902 902

  
......
985 985
    private boolean isTriplePoint(List <MStructure> mList,Point point){
986 986
        List <Point> points=new ArrayList<Point>();
987 987
        for (MStructure structure:mList){
988
            points.addAll(extractPoints(structure.geometry));
988
            points.addAll(LrsAlgorithmUtils.extractPoints(structure.geometry));
989 989
        }
990 990

  
991 991
        int cont=0;
......
1000 1000
        return false;
1001 1001
    }
1002 1002

  
1003
    private Point extractFirstPoint(Geometry geometry){
1004
        Point firstPoint=null;
1005
        List<Line> lines= extractLines(geometry);
1006
        if (lines!=null && !lines.isEmpty()){
1007
            firstPoint=lines.get(0).getVertex(0);
1008
        }
1009
        return firstPoint;
1010
    }
1011 1003

  
1012
    private List<Point> extractPoints(Geometry geometry){
1013
        List<Point> points=new ArrayList<Point>();
1014
        List<Line> lines=extractLines(geometry);
1015
        for (Line line: lines){
1016
            for (int i=0;i<line.getNumVertices();i++){
1017
                points.add(line.getVertex(i));
1018
            }
1019
        }
1020
        return points;
1021
    }
1022 1004

  
1023
    private List<Line> extractLines(Geometry geometry){
1024
        List<Line> lines=new ArrayList<Line>();
1025
        if (geometry instanceof  Line){
1026
            lines.add((Line) geometry);
1027
        }
1028
        if (geometry instanceof MultiLine){
1029
            MultiLine multiline=(MultiLine)geometry;
1030
            for (int i=0;i<multiline.getPrimitivesNumber();i++){
1031
                lines.add((Line) multiline.getPrimitiveAt(i));
1032
            }
1033
        }
1034
        return lines;
1035
    }
1036

  
1037 1005
    private boolean isAddedLine(List<Route> routes,Line line){
1038 1006
        for (Route route:routes){
1039 1007
            if (route.lines.contains(line)){
......
1174 1142
        return refs;
1175 1143
    }
1176 1144

  
1177
    /**
1178
     * @param line
1179
     * @return lenght
1180
     * @throws GeometryOperationException
1181
     * @throws GeometryOperationNotSupportedException
1182
     */
1183
    private double getLineLength(Line line)
1184
        throws GeometryOperationNotSupportedException,
1185
        GeometryOperationException {
1186
        double lenght = 0;
1187
        Point previousVertex = null;
1188
        for (int i = 0; i < line.getNumVertices(); i++) {
1189
            Point vertex = line.getVertex(i);
1190
            if (previousVertex != null) {
1191
                lenght += previousVertex.distance(vertex);
1192
            }
1193
            previousVertex = vertex;
1194
        }
1195
        return lenght;
1196
    }
1197 1145

  
1198
    private Double getGeometryLength(Geometry geometry) throws GeometryOperationNotSupportedException, GeometryOperationException{
1199
        return getGeometryLength(geometry, true);
1200
    }
1201 1146

  
1202
    private Double getGeometryLength(Geometry geometry,
1203
        boolean ignoreSpatialGaps)
1204
            throws GeometryOperationNotSupportedException,
1205
            GeometryOperationException {
1206
        if (geometry instanceof Line) {
1207
            Line line = (Line) geometry;
1208
            return getLineLength(line);
1209
        }
1210
        if (geometry instanceof MultiLine) {
1211
            MultiLine multiLine = (MultiLine) geometry;
1212
            return getMultiLineLength(multiLine, ignoreSpatialGaps);
1213
        }
1214
        return Double.NaN;
1215
    }
1216

  
1217
    /**
1218
     * @param multiLine
1219
     * @return lenght
1220
     * @throws GeometryOperationException
1221
     * @throws GeometryOperationNotSupportedException
1222
     */
1223
    private double getMultiLineLength(MultiLine multiLine,
1224
        boolean ignoreSpatialGaps)
1225
            throws GeometryOperationNotSupportedException,
1226
            GeometryOperationException {
1227
        double lenght = 0;
1228
        Point previousVertex = null;
1229
        for (int j = 0; j < multiLine.getPrimitivesNumber(); j++) {
1230
            Line line = (Line) multiLine.getPrimitiveAt(j);
1231
            if (ignoreSpatialGaps) {
1232
                previousVertex = null;
1233
            }
1234
            for (int i = 0; i < line.getNumVertices(); i++) {
1235
                Point vertex = line.getVertex(i);
1236
                if (previousVertex != null) {
1237
                    lenght += previousVertex.distance(vertex);
1238
                }
1239
                previousVertex = vertex;
1240
            }
1241
        }
1242
        return lenght;
1243
    }
1244

  
1245 1147
    /*
1246 1148
     * (non-Javadoc)
1247 1149
     *

Also available in: Unified diff