root / trunk / libraries / libTopology / src / org / gvsig / fmap / core / FeatureUtil.java @ 23048
History | View | Annotate | Download (8.89 KB)
1 |
/*
|
---|---|
2 |
* Created on 10-abr-2006
|
3 |
*
|
4 |
* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
5 |
*
|
6 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
7 |
*
|
8 |
* This program is free software; you can redistribute it and/or
|
9 |
* modify it under the terms of the GNU General Public License
|
10 |
* as published by the Free Software Foundation; either version 2
|
11 |
* of the License, or (at your option) any later version.
|
12 |
*
|
13 |
* This program is distributed in the hope that it will be useful,
|
14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
16 |
* GNU General Public License for more details.
|
17 |
*
|
18 |
* You should have received a copy of the GNU General Public License
|
19 |
* along with this program; if not, write to the Free Software
|
20 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
21 |
*
|
22 |
* For more information, contact:
|
23 |
*
|
24 |
* Generalitat Valenciana
|
25 |
* Conselleria d'Infraestructures i Transport
|
26 |
* Av. Blasco Ib??ez, 50
|
27 |
* 46010 VALENCIA
|
28 |
* SPAIN
|
29 |
*
|
30 |
* +34 963862235
|
31 |
* gvsig@gva.es
|
32 |
* www.gvsig.gva.es
|
33 |
*
|
34 |
* or
|
35 |
*
|
36 |
* IVER T.I. S.A
|
37 |
* Salamanca 50
|
38 |
* 46005 Valencia
|
39 |
* Spain
|
40 |
*
|
41 |
* +34 963163400
|
42 |
* dac@iver.es
|
43 |
*/
|
44 |
/* CVS MESSAGES:
|
45 |
*
|
46 |
* $Id:
|
47 |
* $Log:
|
48 |
*/
|
49 |
package org.gvsig.fmap.core; |
50 |
|
51 |
import java.io.IOException; |
52 |
import java.sql.Types; |
53 |
import java.util.Date; |
54 |
|
55 |
import org.apache.log4j.Logger; |
56 |
import org.gvsig.jts.JtsUtil; |
57 |
|
58 |
import com.hardcode.gdbms.engine.values.BooleanValue; |
59 |
import com.hardcode.gdbms.engine.values.DateValue; |
60 |
import com.hardcode.gdbms.engine.values.DoubleValue; |
61 |
import com.hardcode.gdbms.engine.values.FloatValue; |
62 |
import com.hardcode.gdbms.engine.values.IntValue; |
63 |
import com.hardcode.gdbms.engine.values.LongValue; |
64 |
import com.hardcode.gdbms.engine.values.StringValue; |
65 |
import com.hardcode.gdbms.engine.values.Value; |
66 |
import com.hardcode.gdbms.engine.values.ValueFactory; |
67 |
import com.iver.cit.gvsig.fmap.core.DefaultFeature; |
68 |
import com.iver.cit.gvsig.fmap.core.IFeature; |
69 |
import com.iver.cit.gvsig.fmap.core.IGeometry; |
70 |
import com.iver.cit.gvsig.fmap.drivers.WKBParser2; |
71 |
import com.iver.utiles.XMLEntity; |
72 |
import com.vividsolutions.jts.geom.Geometry; |
73 |
import com.vividsolutions.jts.geom.GeometryCollection; |
74 |
import com.vividsolutions.jts.precision.EnhancedPrecisionOp; |
75 |
|
76 |
/**
|
77 |
* Class with utility methods to work with FMap features.
|
78 |
*
|
79 |
* @author Alvaro Zabala
|
80 |
*
|
81 |
*/
|
82 |
public class FeatureUtil { |
83 |
|
84 |
private static Logger logger = Logger.getLogger(FeatureUtil.class); |
85 |
|
86 |
private static WKBParser2 wkbParser = new WKBParser2(); |
87 |
|
88 |
public static XMLEntity getValueAsXMLEntity(Object value){ |
89 |
XMLEntity solution = new XMLEntity();
|
90 |
solution.putProperty("value", value);
|
91 |
return solution;
|
92 |
} |
93 |
|
94 |
public static Object getValueFromXMLEntity(XMLEntity xml){ |
95 |
if(xml.contains("value")) |
96 |
return xml.getObjectProperty("value"); |
97 |
else
|
98 |
return null; |
99 |
} |
100 |
|
101 |
|
102 |
public static Value getValue(Object object, int fieldType) { |
103 |
if(object == null) |
104 |
return ValueFactory.createNullValue();
|
105 |
Value solution = null;
|
106 |
switch (fieldType) {
|
107 |
case Types.VARCHAR: |
108 |
String str = (String) object; |
109 |
solution = ValueFactory.createValue(str); |
110 |
break;
|
111 |
case Types.FLOAT: |
112 |
float fval = ((Float) object).floatValue(); |
113 |
solution = ValueFactory.createValue(fval); |
114 |
break;
|
115 |
case Types.DOUBLE: |
116 |
double dval = ((Double) object).doubleValue(); |
117 |
solution = ValueFactory.createValue(dval); |
118 |
break;
|
119 |
case Types.INTEGER: |
120 |
int ival = ((Integer) object).intValue(); |
121 |
solution = ValueFactory.createValue(ival); |
122 |
break;
|
123 |
case Types.BIGINT: |
124 |
long lval = ((Long) object).longValue(); |
125 |
solution = ValueFactory.createValue(lval); |
126 |
break;
|
127 |
case Types.BIT: |
128 |
boolean bval = ((Boolean) object).booleanValue(); |
129 |
solution = ValueFactory.createValue(bval); |
130 |
break;
|
131 |
case Types.DATE: |
132 |
Date dtval = (Date) object; |
133 |
solution = ValueFactory.createValue(dtval); |
134 |
break;
|
135 |
default:
|
136 |
solution = ValueFactory.createNullValue(); |
137 |
} |
138 |
return solution;
|
139 |
} |
140 |
|
141 |
public static Object getJavaTypeFromValue(Value value, int fieldType) { |
142 |
Object solution = null; |
143 |
switch (fieldType) {
|
144 |
case Types.VARCHAR: |
145 |
StringValue str = (StringValue) value; |
146 |
solution = str.getValue(); |
147 |
break;
|
148 |
case Types.FLOAT: |
149 |
FloatValue fval = (FloatValue) value; |
150 |
solution = new Float(fval.floatValue()); |
151 |
break;
|
152 |
case Types.DOUBLE: |
153 |
DoubleValue dval = (DoubleValue) value; |
154 |
solution = new Double(dval.doubleValue()); |
155 |
break;
|
156 |
case Types.INTEGER: |
157 |
IntValue intval = (IntValue) value; |
158 |
solution = new Integer(intval.intValue()); |
159 |
break;
|
160 |
case Types.BIGINT: |
161 |
LongValue lval = (LongValue) value; |
162 |
solution = new Long(lval.longValue()); |
163 |
break;
|
164 |
case Types.BIT: |
165 |
BooleanValue bval = (BooleanValue) value; |
166 |
solution = new Boolean(bval.getValue()); |
167 |
break;
|
168 |
case Types.DATE: |
169 |
DateValue dtval = (DateValue) value; |
170 |
solution = dtval.getValue(); |
171 |
break;
|
172 |
} |
173 |
return solution;
|
174 |
} |
175 |
|
176 |
|
177 |
|
178 |
public static void setXMLEntity(IFeature feature, XMLEntity xml){ |
179 |
if(xml.contains("fid")){ |
180 |
feature.setID(xml.getStringProperty("fid"));
|
181 |
} |
182 |
|
183 |
if(xml.contains("geometry")){ |
184 |
byte[] wkbErrorGeometry = (byte[]) xml.getObjectProperty("geometry"); |
185 |
feature.setGeometry( wkbParser.parse(wkbErrorGeometry) ); |
186 |
} |
187 |
|
188 |
if(xml.contains("numberOfAttributes")){ |
189 |
int numberOfAttributes = xml.getIntProperty("numberOfAttributes"); |
190 |
if(numberOfAttributes > 0){ |
191 |
Value[] values = new Value[numberOfAttributes]; |
192 |
for(int i = 0; i < numberOfAttributes; i++){ |
193 |
int type = xml.getIntProperty("sqlType"+i); |
194 |
Object obj = xml.getObjectProperty("value"+i); |
195 |
Value value = getValue(obj, type); |
196 |
values[i] = value; |
197 |
}//for
|
198 |
feature.setAttributes(values); |
199 |
}//if
|
200 |
}//if numberOfAttrs
|
201 |
} |
202 |
|
203 |
public static IFeature getFeatureFromXmlEntity(XMLEntity xml){ |
204 |
DefaultFeature solution = null;
|
205 |
String fid = null; |
206 |
IGeometry geometry = null;
|
207 |
Value[] values = null; |
208 |
|
209 |
if(xml.contains("fid")){ |
210 |
fid = xml.getStringProperty("fid");
|
211 |
} |
212 |
|
213 |
if(xml.contains("geometry")){ |
214 |
byte[] wkbErrorGeometry = (byte[]) xml.getObjectProperty("geometry"); |
215 |
geometry = wkbParser.parse(wkbErrorGeometry); |
216 |
} |
217 |
|
218 |
if(xml.contains("numberOfAttributes")){ |
219 |
int numberOfAttributes = xml.getIntProperty("numberOfAttributes"); |
220 |
if(numberOfAttributes > 0){ |
221 |
values = new Value[numberOfAttributes];
|
222 |
for(int i = 0; i < numberOfAttributes; i++){ |
223 |
int type = xml.getIntProperty("sqlType"+i); |
224 |
Object obj = xml.getObjectProperty("value"+i); |
225 |
Value value = getValue(obj, type); |
226 |
values[i] = value; |
227 |
}//for
|
228 |
}//if
|
229 |
} |
230 |
|
231 |
solution = new DefaultFeature(geometry, values, fid);
|
232 |
return solution;
|
233 |
} |
234 |
|
235 |
|
236 |
public static XMLEntity getAsXmlEntity(IFeature feature){ |
237 |
XMLEntity solution = new XMLEntity();
|
238 |
String fid = feature.getID();
|
239 |
solution.putProperty("fid", fid);
|
240 |
|
241 |
IGeometry errorGeometry = feature.getGeometry(); |
242 |
try {
|
243 |
byte[] wkbErrorGeometry = errorGeometry.toWKB(); |
244 |
solution.putProperty("geometry", wkbErrorGeometry);
|
245 |
} catch (IOException e) { |
246 |
logger.error("Error trying to serialize feature to xml ");
|
247 |
return null;//FIXME Revisar si puede haber problemas devolviendo NULL |
248 |
} |
249 |
|
250 |
Value[] attributes = feature.getAttributes();
|
251 |
if(attributes != null){ |
252 |
solution.putProperty("numberOfAttributes", attributes.length);
|
253 |
for(int i = 0; i < attributes.length; i++){ |
254 |
Value value = attributes[i]; |
255 |
int type = value.getSQLType();
|
256 |
Object objectValue = FeatureUtil.getJavaTypeFromValue(value, type);
|
257 |
solution.putProperty("sqlType"+i, type);
|
258 |
solution.putProperty("value"+i, objectValue);
|
259 |
|
260 |
} |
261 |
} |
262 |
return solution;
|
263 |
} |
264 |
|
265 |
public static IFeature removeOverlappingArea(IFeature featureToEdit, |
266 |
Geometry originalGeo, |
267 |
Geometry errorGeo){ |
268 |
Geometry[] first = null; |
269 |
if(originalGeo instanceof GeometryCollection){ |
270 |
first = JtsUtil.extractGeometries((GeometryCollection) originalGeo); |
271 |
}else
|
272 |
{ |
273 |
first = new Geometry[]{originalGeo}; |
274 |
} |
275 |
|
276 |
Geometry[] second = null; |
277 |
if(errorGeo instanceof GeometryCollection){ |
278 |
second = JtsUtil.extractGeometries((GeometryCollection) errorGeo); |
279 |
}else
|
280 |
{ |
281 |
second = new Geometry[]{errorGeo}; |
282 |
} |
283 |
|
284 |
for (int i = 0; i < first.length; i++) { |
285 |
Geometry geom = first[i]; |
286 |
Geometry partialSolution = null;
|
287 |
for (int j = 0; j < second.length; j++) { |
288 |
Geometry aux = EnhancedPrecisionOp.difference(geom, second[j]); |
289 |
if(partialSolution == null) |
290 |
partialSolution = aux; |
291 |
else
|
292 |
partialSolution = EnhancedPrecisionOp.union(partialSolution, aux); |
293 |
}//for
|
294 |
first[i] = partialSolution; |
295 |
}//for i
|
296 |
GeometryCollection geomCol = JtsUtil.GEOMETRY_FACTORY.createGeometryCollection(first); |
297 |
IGeometry newFGeo = NewFConverter.toFMap(geomCol); |
298 |
featureToEdit.setGeometry(newFGeo); |
299 |
return featureToEdit;
|
300 |
} |
301 |
} |