Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.geometry / org.gvsig.fmap.geometry.impl / src / main / java / org / gvsig / fmap / geom / impl / DefaultValidationStatus.java @ 41440

History | View | Annotate | Download (4.84 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24

    
25
package org.gvsig.fmap.geom.impl;
26

    
27
import com.vividsolutions.jts.geom.Coordinate;
28
import com.vividsolutions.jts.operation.valid.TopologyValidationError;
29
import org.gvsig.fmap.geom.Geometry;
30
import org.gvsig.fmap.geom.GeometryLocator;
31
import org.gvsig.fmap.geom.GeometryManager;
32
import org.gvsig.fmap.geom.exception.CreateGeometryException;
33
import org.gvsig.fmap.geom.primitive.Point;
34
import org.gvsig.fmap.geom.primitive.impl.AbstractPrimitive;
35
import org.slf4j.Logger;
36
import org.slf4j.LoggerFactory;
37

    
38
public class DefaultValidationStatus implements Geometry.ValidationStatus {
39

    
40
    private static final Logger logger = LoggerFactory.getLogger(AbstractPrimitive.class);
41

    
42
    private int statusCode = VALID;
43
    private Point problemLocation = null;
44
    private String message = null;
45

    
46
    public DefaultValidationStatus() {
47

    
48
    }
49

    
50
    public DefaultValidationStatus(int code, String message) {
51
        this.statusCode = code;
52
        this.message = message;
53
    }
54

    
55
    public void setMesage(String message) {
56
        this.message = message;
57
    }
58

    
59
    public void setStatusCode(int statusCode) {
60
        this.statusCode = statusCode;
61
    }
62

    
63
    public void setValidationError(TopologyValidationError validationError) {
64
        if( validationError == null ) {
65
            statusCode = VALID;
66
            message = null;
67
            problemLocation = null;
68
            return;
69
        }
70
        this.message = validationError.getMessage();
71
        Coordinate coord = validationError.getCoordinate();
72
        try {
73
            GeometryManager geomManager = GeometryLocator.getGeometryManager();
74
            if ( Double.isNaN(coord.z) ) {
75
                this.problemLocation = geomManager.createPoint(coord.x, coord.y, Geometry.SUBTYPES.GEOM2D);
76
            } else {
77
                this.problemLocation = geomManager.createPoint(coord.x, coord.y, Geometry.SUBTYPES.GEOM3D);
78
                this.problemLocation.setCoordinateAt(2, coord.z);
79
            }
80
        } catch (CreateGeometryException ex) {
81
            String coordstr;
82
            try {
83
                coordstr = coord.toString();
84
            } catch (Exception ex1) {
85
                coordstr = "unknow";
86
            }
87
            logger.warn("Can't create a point from jts Coordinate (" + coordstr + ").", ex);
88
            this.problemLocation = null;
89
        }
90
        switch (validationError.getErrorType()) {
91
        case TopologyValidationError.DISCONNECTED_INTERIOR:
92
            this.statusCode = DISCONNECTED_INTERIOR;
93
            break;
94
        case TopologyValidationError.DUPLICATE_RINGS:
95
            this.statusCode = DUPLICATE_RINGS;
96
            break;
97
        case TopologyValidationError.HOLE_OUTSIDE_SHELL:
98
            this.statusCode = HOLE_OUTSIDE_SHELL;
99
            break;
100
        case TopologyValidationError.INVALID_COORDINATE:
101
            this.statusCode = INVALID_COORDINATE;
102
            break;
103
        case TopologyValidationError.NESTED_HOLES:
104
            this.statusCode = NESTED_HOLES;
105
            break;
106
        case TopologyValidationError.NESTED_SHELLS:
107
            this.statusCode = NESTED_SHELLS;
108
            break;
109
        case TopologyValidationError.RING_NOT_CLOSED:
110
            this.statusCode = RING_NOT_CLOSED;
111
            break;
112
        case TopologyValidationError.RING_SELF_INTERSECTION:
113
            this.statusCode = RING_SELF_INTERSECTION;
114
            break;
115
        case TopologyValidationError.SELF_INTERSECTION:
116
            this.statusCode = SELF_INTERSECTION;
117
            break;
118
        case TopologyValidationError.TOO_FEW_POINTS:
119
            this.statusCode = TOO_FEW_POINTS;
120
            break;
121
        default:
122
            this.statusCode = UNKNOW;
123
            break;
124
        }
125
    }
126

    
127
    public boolean isValid() {
128
        return statusCode == VALID;
129
    }
130

    
131
    public int getStatusCode() {
132
        return this.statusCode;
133
    }
134

    
135
    public Point getProblemLocation() {
136
        return this.problemLocation;
137
    }
138

    
139
    public String getMessage() {
140
        return this.message;
141
    }
142

    
143
}