Revision 44664
trunk/org.gvsig.desktop/org.gvsig.desktop.plugin/org.gvsig.daltransform.app/org.gvsig.daltransform.app.eventtheme/src/main/java/org/gvsig/app/eventtheme/dal/feature/EventThemeTransform.java | ||
---|---|---|
29 | 29 |
|
30 | 30 |
import java.util.Arrays; |
31 | 31 |
|
32 |
import javax.swing.JOptionPane; |
|
33 | 32 |
import org.apache.commons.lang3.StringUtils; |
34 | 33 |
|
35 | 34 |
import org.cresques.cts.IProjection; |
36 | 35 |
|
37 |
import org.gvsig.app.ApplicationLocator; |
|
38 | 36 |
import org.gvsig.fmap.dal.DataStore; |
39 | 37 |
import org.gvsig.fmap.dal.DataTypes; |
40 | 38 |
import org.gvsig.fmap.dal.exception.DataException; |
... | ... | |
44 | 42 |
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor; |
45 | 43 |
import org.gvsig.fmap.dal.feature.EditableFeatureType; |
46 | 44 |
import org.gvsig.fmap.dal.feature.Feature; |
47 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
|
|
45 |
import org.gvsig.fmap.dal.feature.FeatureAttributeEmulator;
|
|
48 | 46 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
49 | 47 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
50 | 48 |
import org.gvsig.fmap.dal.feature.FeatureType; |
51 |
import org.gvsig.fmap.dal.feature.exception.SetReadOnlyAttributeException; |
|
52 | 49 |
import org.gvsig.fmap.geom.Geometry; |
53 | 50 |
import org.gvsig.fmap.geom.Geometry.SUBTYPES; |
54 | 51 |
import org.gvsig.fmap.geom.Geometry.TYPES; |
... | ... | |
60 | 57 |
import org.gvsig.fmap.geom.primitive.Point; |
61 | 58 |
import org.gvsig.fmap.geom.type.GeometryTypeNotSupportedException; |
62 | 59 |
import org.gvsig.fmap.geom.type.GeometryTypeNotValidException; |
63 |
import org.gvsig.i18n.Messages; |
|
64 | 60 |
import org.gvsig.tools.ToolsLocator; |
65 | 61 |
import org.gvsig.tools.dispose.DisposableIterator; |
66 | 62 |
import org.gvsig.tools.dynobject.DynStruct; |
... | ... | |
68 | 64 |
import org.gvsig.tools.persistence.PersistenceManager; |
69 | 65 |
import org.gvsig.tools.persistence.PersistentState; |
70 | 66 |
import org.gvsig.tools.persistence.exception.PersistenceException; |
71 |
import org.gvsig.tools.task.AbstractMonitorableTask; |
|
72 |
import org.gvsig.tools.task.TaskStatusManager; |
|
73 | 67 |
import org.slf4j.Logger; |
74 | 68 |
import org.slf4j.LoggerFactory; |
75 | 69 |
|
... | ... | |
86 | 80 |
public class EventThemeTransform |
87 | 81 |
extends AbstractFeatureStoreTransform { |
88 | 82 |
|
89 |
private static Logger logger = LoggerFactory.getLogger( |
|
90 |
EventThemeTransform.class); |
|
83 |
private static Logger LOGGER = LoggerFactory.getLogger(EventThemeTransform.class); |
|
91 | 84 |
|
92 | 85 |
public static final String PERSISTENCE_DEFINITION_NAME = "EventThemeTransform"; |
93 | 86 |
/** |
... | ... | |
142 | 135 |
@Override |
143 | 136 |
public void setUp() throws Exception { |
144 | 137 |
|
145 |
this.originalFeatureType = this.getFeatureStore() |
|
146 |
.getDefaultFeatureType(); |
|
138 |
this.originalFeatureType = this.getFeatureStore().getDefaultFeatureType(); |
|
147 | 139 |
|
148 | 140 |
EditableFeatureType type = originalFeatureType.getEditable(); |
149 | 141 |
if( type.get(this.geometryFieldName) == null ) { |
150 | 142 |
EditableFeatureAttributeDescriptor attributeDescriptor = type.add(this.geometryFieldName, DataTypes.GEOMETRY); |
151 | 143 |
try { |
152 | 144 |
attributeDescriptor.setGeometryType(geometryManager.getGeometryType(TYPES.POINT, SUBTYPES.GEOM2D)); |
153 |
} catch (GeometryTypeNotSupportedException e) { |
|
145 |
} catch (GeometryTypeNotSupportedException | GeometryTypeNotValidException e) {
|
|
154 | 146 |
throw new InitializeException(e); |
155 |
} catch (GeometryTypeNotValidException e) { |
|
156 |
throw new InitializeException(e); |
|
157 | 147 |
} |
158 | 148 |
attributeDescriptor.setSRS(projection); |
149 |
attributeDescriptor.setFeatureAttributeEmulator(new FeatureAttributeEmulator() { |
|
150 |
@Override |
|
151 |
public Object get(Feature feature) { |
|
152 |
Geometry point = null; |
|
153 |
Object xval = feature.get(xFieldName); |
|
154 |
Object yval = feature.get(yFieldName); |
|
155 |
if( xval == null || yval == null ) { |
|
156 |
LOGGER.info("Found row with null coordinates in event theme (created null geometry)"); |
|
157 |
return null; |
|
158 |
} else { |
|
159 |
try { |
|
160 |
point = geometryManager.createPoint( |
|
161 |
new Double(xval.toString()), |
|
162 |
new Double(yval.toString()), |
|
163 |
SUBTYPES.GEOM2D); |
|
164 |
} catch (CreateGeometryException ex) { |
|
165 |
} |
|
166 |
return point; |
|
167 |
} |
|
168 |
} |
|
169 |
|
|
170 |
@Override |
|
171 |
public void set(EditableFeature feature, Object value) { |
|
172 |
throw new UnsupportedOperationException("Not supported yet."); |
|
173 |
} |
|
174 |
|
|
175 |
@Override |
|
176 |
public boolean allowSetting() { |
|
177 |
return false; |
|
178 |
} |
|
179 |
|
|
180 |
@Override |
|
181 |
public String[] getRequiredFieldNames() { |
|
182 |
return new String[] { xFieldName, yFieldName }; |
|
183 |
} |
|
184 |
}); |
|
159 | 185 |
} |
160 | 186 |
|
161 | 187 |
try { |
... | ... | |
176 | 202 |
* creates and updates envelope with all features |
177 | 203 |
* |
178 | 204 |
*/ |
179 |
private void initEnvelope(FeatureStore fsto, int max_feats) throws CreateEnvelopeException {
|
|
205 |
private void initEnvelope(FeatureStore theStore, int max_feats) throws CreateEnvelopeException {
|
|
180 | 206 |
|
181 | 207 |
envelope = geometryManager.createEnvelope(SUBTYPES.GEOM2D); |
182 | 208 |
FeatureSet fset = null; |
183 | 209 |
DisposableIterator diter = null; |
184 | 210 |
Feature feat = null; |
185 | 211 |
try { |
186 |
fset = fsto.getFeatureSet();
|
|
212 |
fset = theStore.getFeatureSet();
|
|
187 | 213 |
diter = fset.fastIterator(); |
188 | 214 |
|
189 | 215 |
// int count = 0; |
... | ... | |
235 | 261 |
this.envelope = env; |
236 | 262 |
} |
237 | 263 |
|
238 |
/* |
|
239 |
* Currently not used |
|
240 |
*/ |
|
241 |
private void launchFullExtentThread(DisposableIterator diter) { |
|
242 |
|
|
243 |
ComputeExtentTask task = new ComputeExtentTask(diter, this); |
|
244 |
task.start(); |
|
245 |
} |
|
246 |
|
|
247 | 264 |
@Override |
248 | 265 |
public void applyTransform(Feature source, EditableFeature target) |
249 |
throws DataException { |
|
250 |
|
|
251 |
this.copySourceToTarget(source, target); |
|
252 |
|
|
253 |
try { |
|
254 |
|
|
255 |
Geometry point; |
|
256 |
Object xval = source.get(xFieldName); |
|
257 |
Object yval = source.get(yFieldName); |
|
258 |
if( xval == null || yval == null ) { |
|
259 |
logger.info("Found row with null coordinates in event theme (created null geometry)"); |
|
260 |
target.set(geometryFieldName, null); |
|
261 |
target.setDefaultGeometry(null); |
|
262 |
} else { |
|
263 |
point = geometryManager.createPoint( |
|
264 |
new Double(xval.toString()), |
|
265 |
new Double(yval.toString()), |
|
266 |
SUBTYPES.GEOM2D); |
|
267 |
target.set(geometryFieldName, point); |
|
268 |
target.setDefaultGeometry(point); |
|
269 |
envelope.add(point.getEnvelope()); |
|
270 |
} |
|
271 |
} catch (SetReadOnlyAttributeException e1) { |
|
272 |
// Do nothing |
|
273 |
|
|
274 |
} catch (Exception e) { |
|
275 |
throw new org.gvsig.fmap.dal.feature.exception.CreateGeometryException( |
|
276 |
TYPES.POINT, SUBTYPES.GEOM2D, e); |
|
277 |
} |
|
278 |
|
|
266 |
throws DataException { |
|
267 |
target.copyFrom(source); |
|
279 | 268 |
} |
280 | 269 |
|
281 | 270 |
/** |
... | ... | |
291 | 280 |
envelope.add(point.getEnvelope()); |
292 | 281 |
} |
293 | 282 |
|
294 |
/** |
|
295 |
* @param source |
|
296 |
* @param target |
|
297 |
*/ |
|
298 |
private void copySourceToTarget(Feature source, EditableFeature target) { |
|
299 |
FeatureAttributeDescriptor attr, attrTrg; |
|
300 |
FeatureType ftSrc = source.getType(); |
|
301 |
FeatureType ftTrg = target.getType(); |
|
302 |
|
|
303 |
for( int i = 0; i < source.getType().size(); i++ ) { |
|
304 |
attr = ftSrc.getAttributeDescriptor(i); |
|
305 |
if( ftTrg.getIndex(attr.getName()) > -1 ) { |
|
306 |
attrTrg = ftTrg.getAttributeDescriptor(attr.getName()); |
|
307 |
try { |
|
308 |
if ( !attrTrg.isReadOnly() ) { |
|
309 |
target.set(attr.getName(), source.get(i)); |
|
310 |
} |
|
311 |
} catch (IllegalArgumentException e) { |
|
312 |
target.set(attrTrg.getIndex(), attrTrg.getDefaultValue()); |
|
313 |
} |
|
314 |
|
|
315 |
} |
|
316 |
} |
|
317 |
|
|
318 |
} |
|
319 |
|
|
320 | 283 |
@Override |
321 | 284 |
public FeatureType getSourceFeatureTypeFrom(FeatureType targetFeatureType) { |
322 | 285 |
return this.originalFeatureType; |
... | ... | |
389 | 352 |
|| (DataStore.METADATA_ENVELOPE.equals(name))); |
390 | 353 |
} |
391 | 354 |
|
392 |
/** |
|
393 |
* |
|
394 |
* A thread to compute the true extent (in case it has a lot of features) |
|
395 |
* Currently not used. |
|
396 |
* |
|
397 |
* @author jldominguez |
|
398 |
* |
|
399 |
* @deprecated This is not used because it causes issues with |
|
400 |
* ConsurrentModificationException because the store is notified of a change |
|
401 |
* (the transformation). Anyway, this is not very important I think. |
|
402 |
*/ |
|
403 |
private class ComputeExtentTask |
|
404 |
extends AbstractMonitorableTask { |
|
405 |
|
|
406 |
private DisposableIterator disp_iter = null; |
|
407 |
private EventThemeTransform tra_toupdate = null; |
|
408 |
|
|
409 |
public ComputeExtentTask(DisposableIterator diter, EventThemeTransform ettra) { |
|
410 |
/* |
|
411 |
* Auto-added by task manager |
|
412 |
*/ |
|
413 |
super(Messages.getText("_Extent_of_event_theme"), true); |
|
414 |
disp_iter = diter; |
|
415 |
tra_toupdate = ettra; |
|
416 |
} |
|
417 |
|
|
418 |
public void run() { |
|
419 |
|
|
420 |
Envelope env = null; |
|
421 |
Feature feat = null; |
|
422 |
Point point = null; |
|
423 |
int count = 99; |
|
424 |
|
|
425 |
try { |
|
426 |
while( disp_iter.hasNext() ) { |
|
427 |
feat = (Feature) disp_iter.next(); |
|
428 |
point = geometryManager.createPoint( |
|
429 |
Double.parseDouble(feat.get(xFieldName).toString()), |
|
430 |
Double.parseDouble(feat.get(yFieldName).toString()), |
|
431 |
SUBTYPES.GEOM2D); |
|
432 |
if( env == null ) { |
|
433 |
env = (Envelope) point.getEnvelope().clone(); |
|
434 |
} else { |
|
435 |
env.add(point.getEnvelope()); |
|
436 |
} |
|
437 |
count++; |
|
438 |
Thread.sleep(10); |
|
439 |
if( count % 100 == 0 ) { |
|
440 |
System.out.println("COUNT = " + count); |
|
441 |
} |
|
442 |
} |
|
443 |
} catch (Exception exc) { |
|
444 |
|
|
445 |
ApplicationLocator.getManager().message( |
|
446 |
Messages.getText("_Error_while_getting_extent"), |
|
447 |
JOptionPane.ERROR_MESSAGE); |
|
448 |
logger.info("Error while getting extent in thread.", exc); |
|
449 |
|
|
450 |
} |
|
451 |
|
|
452 |
disp_iter.dispose(); |
|
453 |
// ================= |
|
454 |
if( env != null ) { |
|
455 |
Envelope curr_env = (Envelope) tra_toupdate.getDynValue( |
|
456 |
DataStore.METADATA_ENVELOPE); |
|
457 |
curr_env.add(env); |
|
458 |
} |
|
459 |
// =========== End |
|
460 |
TaskStatusManager man = this.getTaskStatus().getManager(); |
|
461 |
man.remove(this.getTaskStatus()); |
|
462 |
} |
|
463 |
|
|
464 |
} |
|
465 | 355 |
} |
Also available in: Unified diff