Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.geometry / org.gvsig.fmap.geometry.jts / src / main / java / org / gvsig / fmap / geom / jts / coerce / CoerceToGeometry.java @ 47432

History | View | Annotate | Download (8.76 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
package org.gvsig.fmap.geom.jts.coerce;
25

    
26
import java.util.Iterator;
27
import java.util.logging.Level;
28
import java.util.logging.Logger;
29
import org.apache.commons.lang3.StringUtils;
30
import org.gvsig.fmap.geom.Geometry;
31
import org.gvsig.fmap.geom.GeometryCoercionContext;
32
import static org.gvsig.fmap.geom.GeometryCoercionContext.MODE_ONERROR_DONTCONVERT;
33
import static org.gvsig.fmap.geom.GeometryCoercionContext.MODE_ONERROR_NULL;
34
import static org.gvsig.fmap.geom.GeometryCoercionContext.MODE_ONERROR_THROW;
35
import org.gvsig.fmap.geom.GeometryLocator;
36
import org.gvsig.fmap.geom.GeometryManager;
37
import org.gvsig.fmap.geom.aggregate.MultiLine;
38
import org.gvsig.fmap.geom.aggregate.MultiPoint;
39
import org.gvsig.fmap.geom.aggregate.MultiPolygon;
40
import org.gvsig.fmap.geom.jts.AbstractGeometry;
41
import org.gvsig.fmap.geom.operation.GeometryOperationException;
42
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException;
43
import org.gvsig.fmap.geom.primitive.Line;
44
import org.gvsig.fmap.geom.primitive.Point;
45
import org.gvsig.fmap.geom.primitive.Polygon;
46
import org.gvsig.fmap.geom.primitive.Primitive;
47
import org.gvsig.fmap.geom.type.GeometryType;
48
import org.gvsig.tools.dataTypes.AbstractCoercion;
49
import org.gvsig.tools.dataTypes.CoercionException;
50
import org.gvsig.tools.dataTypes.CoercionContext;
51

    
52
/**
53
 * Convert a object to Geometry.
54
 *
55
 * Support convert: - Geometry to Geometry (do nothing) - WKB (byte[]) to
56
 * Geometry - WKT (String/toString) to Geometry
57
 *
58
 *
59
 * @author gvSIG Team
60
 * @version $Id$
61
 *
62
 */
63
@SuppressWarnings("UseSpecificCatch")
64
public class CoerceToGeometry extends AbstractCoercion {
65

    
66
    private static GeometryManager manager = null;
67
    
68
    @Override
69
    public Object coerce(Object value, CoercionContext context) throws CoercionException {
70
        if (value == null) {
71
            return value;
72
        }
73
        int mode = MODE_ONERROR_NULL;
74
        Geometry geom;
75
        GeometryType targetGeometryType = null;
76
        Geometry sourcegeom = null;
77
        try {
78
//            TODO: descomentarizar para que tengan efecto los cambios.
79
            if ( context instanceof GeometryCoercionContext) {
80
                GeometryCoercionContext geomContext = (GeometryCoercionContext) context;
81
                targetGeometryType = geomContext.getGeometryType();
82
                mode = geomContext.getMode();
83
            }
84
            
85
            if ( value instanceof Geometry ) {
86
                geom = (Geometry) value;
87
            } else {
88
                if (value instanceof byte[]) {
89
                    geom = getManager().createFrom((byte[]) value);
90
                } else {
91
                    String wkt = value.toString();
92
                    if (StringUtils.isBlank(wkt)) {
93
                        return null;
94
                    }
95
                    geom = getManager().createFrom(value.toString());
96
                }
97
                if (geom == null) {
98
                    // Can't parse from WKB/WKT
99
                    switch(mode) {
100
                        case MODE_ONERROR_NULL:
101
                        case MODE_ONERROR_DONTCONVERT:
102
                            return null;
103
                        case MODE_ONERROR_THROW:
104
                        default:
105
                            throw new CoercionException();
106
                    }
107
                }
108
            }
109
            if ( targetGeometryType==null) {
110
                return geom;
111
            }
112
            sourcegeom = geom;
113
            if (geom.getGeometryType().equals(targetGeometryType)) {
114
                return geom;
115
            }
116
            switch (targetGeometryType.getType()) {
117
                case Geometry.TYPES.MULTIPOINT:
118
                    geom = geom.toPoints();
119
                    break;
120
                case Geometry.TYPES.MULTICURVE:
121
                    if(geom.getGeometryType().getSubType() != Geometry.TYPES.MULTICURVE){
122
                        geom = geom.toLines();
123
                    }
124
                    break;
125
                case Geometry.TYPES.MULTILINE:
126
                    geom = geom.toLines();
127
                    break;
128
                case Geometry.TYPES.MULTISURFACE:
129
                    if(geom.getGeometryType().getSubType() != Geometry.TYPES.MULTISURFACE){
130
                        geom = geom.toPolygons();
131
                    }
132
                    break;
133
                case Geometry.TYPES.MULTIPOLYGON:
134
                    geom = geom.toPolygons();
135
                    break;
136
                case Geometry.TYPES.POINT:
137
                    geom = this.convertToPoint(targetGeometryType, geom);
138
                    break;
139
                case Geometry.TYPES.CURVE:
140
                    if(geom.getGeometryType().getSubType() != Geometry.TYPES.CURVE){
141
                        geom = this.convertToLine(targetGeometryType, geom);
142
                    }
143
                    break;
144
                case Geometry.TYPES.LINE:
145
                    geom = this.convertToLine(targetGeometryType, geom);
146
                    break;
147
                case Geometry.TYPES.SURFACE:
148
                    if(geom.getGeometryType().getSubType() != Geometry.TYPES.SURFACE){
149
                        geom = this.convertToPolygon(targetGeometryType, geom);
150
                    }
151
                    break;
152
                case Geometry.TYPES.POLYGON:
153
                    geom = this.convertToPolygon(targetGeometryType, geom);
154
                    break;
155
                case Geometry.TYPES.GEOMETRY:
156
                    //Do not convert
157
                    break;
158
                default:
159
                    geom = null;
160
                    break;
161
            }
162
            
163
            if (geom.getGeometryType().getSubType() != targetGeometryType.getSubType()) {
164
                geom = geom.forceSubtype(targetGeometryType.getSubType());
165
            }
166
            
167
            if (geom != null) {
168
                // Return converted geometry
169
                return geom;
170
            }
171
            switch(mode) {
172
                case MODE_ONERROR_NULL:
173
                    return null;
174
                case MODE_ONERROR_DONTCONVERT:
175
                    return sourcegeom;
176
                case MODE_ONERROR_THROW:
177
                default:
178
                    throw new CoercionException();
179
            }
180
        } catch (Exception e) {
181
            switch(mode) {
182
                case MODE_ONERROR_NULL:
183
                    return null;
184
                case MODE_ONERROR_DONTCONVERT:
185
                    return sourcegeom;
186
                case MODE_ONERROR_THROW:
187
                default:
188
                    throw new CoercionException();
189
            }
190
        }
191
    }
192

    
193
    private static GeometryManager getManager() {
194
        if (manager == null) {
195
            manager = GeometryLocator.getGeometryManager();
196
        }
197
        return manager;
198
    }
199

    
200
    private Geometry convertToPoint(GeometryType geometryType, Geometry geom) {
201
        try {
202
            Point target = null;
203
            MultiPoint points = geom.toPoints();
204
            if(points.getPrimitivesNumber() == 1){
205
               target = (Point) points.getPrimitiveAt(0); 
206
            }
207
            return target;
208
        } catch (Exception ex) {
209
            return null;
210
        }
211
    }
212

    
213
    private Geometry convertToLine(GeometryType geometryType, Geometry geom) {
214
        try {
215
            Line target = null;
216
            MultiLine lines = geom.toLines();
217
            if(lines.getPrimitivesNumber() == 1){
218
               target = (Line) lines.getPrimitiveAt(0); 
219
            }
220
            return target;
221
        } catch (Exception ex) {
222
            return null;
223
        }
224
    }
225

    
226
    private Geometry convertToPolygon(GeometryType geometryType, Geometry geom) {
227
        try {
228
            Polygon target = null;
229
            MultiPolygon polygons = geom.toPolygons();
230
            if(polygons.getPrimitivesNumber() == 1){
231
               target = (Polygon) polygons.getPrimitiveAt(0); 
232
            }
233
            return target;
234
        } catch (Exception ex) {
235
            return null;
236
        }
237
    }
238

    
239
}