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 @ 45690
History | View | Annotate | Download (7.59 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.MULTILINE:
|
121 |
geom = geom.toLines(); |
122 |
break;
|
123 |
case Geometry.TYPES.MULTIPOLYGON:
|
124 |
geom = geom.toPolygons(); |
125 |
break;
|
126 |
case Geometry.TYPES.POINT:
|
127 |
geom = this.convertToPoint(targetGeometryType, geom);
|
128 |
break;
|
129 |
case Geometry.TYPES.LINE:
|
130 |
geom = this.convertToLine(targetGeometryType, geom);
|
131 |
break;
|
132 |
case Geometry.TYPES.POLYGON:
|
133 |
geom = this.convertToPolygon(targetGeometryType, geom);
|
134 |
break;
|
135 |
case Geometry.TYPES.GEOMETRY:
|
136 |
//Do not convert
|
137 |
break;
|
138 |
default:
|
139 |
geom = null;
|
140 |
break;
|
141 |
} |
142 |
if (geom != null) { |
143 |
// Return converted geometry
|
144 |
return geom;
|
145 |
} |
146 |
switch(mode) {
|
147 |
case MODE_ONERROR_NULL:
|
148 |
return null; |
149 |
case MODE_ONERROR_DONTCONVERT:
|
150 |
return sourcegeom;
|
151 |
case MODE_ONERROR_THROW:
|
152 |
default:
|
153 |
throw new CoercionException(); |
154 |
} |
155 |
} catch (Exception e) { |
156 |
switch(mode) {
|
157 |
case MODE_ONERROR_NULL:
|
158 |
return null; |
159 |
case MODE_ONERROR_DONTCONVERT:
|
160 |
return sourcegeom;
|
161 |
case MODE_ONERROR_THROW:
|
162 |
default:
|
163 |
throw new CoercionException(); |
164 |
} |
165 |
} |
166 |
} |
167 |
|
168 |
private static GeometryManager getManager() { |
169 |
if (manager == null) { |
170 |
manager = GeometryLocator.getGeometryManager(); |
171 |
} |
172 |
return manager;
|
173 |
} |
174 |
|
175 |
private Geometry convertToPoint(GeometryType geometryType, Geometry geom) {
|
176 |
try {
|
177 |
Point target = null; |
178 |
MultiPoint points = geom.toPoints(); |
179 |
if(points.getPrimitivesNumber() == 1){ |
180 |
target = (Point) points.getPrimitiveAt(0); |
181 |
} |
182 |
return target;
|
183 |
} catch (Exception ex) { |
184 |
return null; |
185 |
} |
186 |
} |
187 |
|
188 |
private Geometry convertToLine(GeometryType geometryType, Geometry geom) {
|
189 |
try {
|
190 |
Line target = null; |
191 |
MultiLine lines = geom.toLines(); |
192 |
if(lines.getPrimitivesNumber() == 1){ |
193 |
target = (Line) lines.getPrimitiveAt(0); |
194 |
} |
195 |
return target;
|
196 |
} catch (Exception ex) { |
197 |
return null; |
198 |
} |
199 |
} |
200 |
|
201 |
private Geometry convertToPolygon(GeometryType geometryType, Geometry geom) {
|
202 |
try {
|
203 |
Polygon target = null; |
204 |
MultiPolygon polygons = geom.toPolygons(); |
205 |
if(polygons.getPrimitivesNumber() == 1){ |
206 |
target = (Polygon) polygons.getPrimitiveAt(0); |
207 |
} |
208 |
return target;
|
209 |
} catch (Exception ex) { |
210 |
return null; |
211 |
} |
212 |
} |
213 |
|
214 |
} |