svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.file / org.gvsig.fmap.dal.file.csv / src / main / java / org / gvsig / fmap / dal / store / csv / CSVStoreProvider.java @ 41006
History | View | Annotate | Download (24.5 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.dal.store.csv; |
25 |
|
26 |
import java.io.File; |
27 |
import java.io.FileReader; |
28 |
import java.io.FileWriter; |
29 |
import java.io.IOException; |
30 |
import java.util.ArrayList; |
31 |
import java.util.HashMap; |
32 |
import java.util.Iterator; |
33 |
import java.util.List; |
34 |
import java.util.Properties; |
35 |
|
36 |
import org.apache.commons.io.FilenameUtils; |
37 |
import org.cresques.cts.IProjection; |
38 |
import org.gvsig.fmap.dal.DALLocator; |
39 |
import org.gvsig.fmap.dal.DataManager; |
40 |
import org.gvsig.fmap.dal.DataServerExplorer; |
41 |
import org.gvsig.fmap.dal.DataStore; |
42 |
import org.gvsig.fmap.dal.DataStoreNotification; |
43 |
import org.gvsig.fmap.dal.DataTypes; |
44 |
import org.gvsig.fmap.dal.FileHelper; |
45 |
import org.gvsig.fmap.dal.exception.DataException; |
46 |
import org.gvsig.fmap.dal.exception.InitializeException; |
47 |
import org.gvsig.fmap.dal.exception.OpenException; |
48 |
import org.gvsig.fmap.dal.exception.ReadException; |
49 |
import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
50 |
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor; |
51 |
import org.gvsig.fmap.dal.feature.EditableFeatureType; |
52 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
53 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
54 |
import org.gvsig.fmap.dal.feature.FeatureType; |
55 |
import org.gvsig.fmap.dal.feature.exception.PerformEditingException; |
56 |
import org.gvsig.fmap.dal.feature.spi.FeatureProvider; |
57 |
import org.gvsig.fmap.dal.feature.spi.FeatureStoreProviderServices; |
58 |
import org.gvsig.fmap.dal.feature.spi.memory.AbstractMemoryStoreProvider; |
59 |
import org.gvsig.fmap.dal.resource.ResourceAction; |
60 |
import org.gvsig.fmap.dal.resource.file.FileResource; |
61 |
import org.gvsig.fmap.dal.resource.spi.ResourceConsumer; |
62 |
import org.gvsig.fmap.dal.resource.spi.ResourceProvider; |
63 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorer; |
64 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorerParameters; |
65 |
import org.gvsig.fmap.dal.spi.DataStoreProviderServices; |
66 |
import org.gvsig.fmap.geom.Geometry; |
67 |
import org.gvsig.fmap.geom.GeometryLocator; |
68 |
import org.gvsig.fmap.geom.GeometryManager; |
69 |
import org.gvsig.fmap.geom.primitive.Envelope; |
70 |
import org.gvsig.fmap.geom.primitive.Point; |
71 |
import org.gvsig.fmap.geom.type.GeometryType; |
72 |
import org.gvsig.fmap.geom.type.GeometryTypeNotSupportedException; |
73 |
import org.gvsig.fmap.geom.type.GeometryTypeNotValidException; |
74 |
import org.gvsig.tools.ToolsLocator; |
75 |
import org.gvsig.tools.dataTypes.CoercionException; |
76 |
import org.gvsig.tools.dataTypes.DataTypesManager; |
77 |
import org.gvsig.tools.dataTypes.DataTypesManager.Coercion; |
78 |
import org.gvsig.tools.dispose.DisposableIterator; |
79 |
import org.gvsig.tools.dynobject.DynObject; |
80 |
import org.gvsig.tools.dynobject.exception.DynFieldNotFoundException; |
81 |
import org.gvsig.tools.evaluator.AbstractEvaluator; |
82 |
import org.gvsig.tools.evaluator.EvaluatorData; |
83 |
import org.gvsig.tools.evaluator.EvaluatorException; |
84 |
import org.gvsig.tools.exception.NotYetImplemented; |
85 |
import org.gvsig.tools.persistence.PersistentState; |
86 |
import org.gvsig.tools.persistence.exception.PersistenceException; |
87 |
import org.gvsig.tools.task.SimpleTaskStatus; |
88 |
import org.gvsig.tools.task.TaskStatusManager; |
89 |
import org.slf4j.Logger; |
90 |
import org.slf4j.LoggerFactory; |
91 |
import org.supercsv.comment.CommentStartsWith; |
92 |
import org.supercsv.io.CsvListReader; |
93 |
import org.supercsv.io.CsvListWriter; |
94 |
import org.supercsv.prefs.CsvPreference; |
95 |
import org.supercsv.quote.QuoteMode; |
96 |
|
97 |
public class CSVStoreProvider extends AbstractMemoryStoreProvider implements |
98 |
ResourceConsumer { |
99 |
private static final Logger logger = LoggerFactory.getLogger(CSVStoreProvider.class); |
100 |
|
101 |
public static final String NAME = "CSV"; |
102 |
public static final String DESCRIPTION = "CSV file"; |
103 |
|
104 |
public static final String METADATA_DEFINITION_NAME = NAME; |
105 |
|
106 |
private ResourceProvider resource;
|
107 |
|
108 |
private long counterNewsOIDs = 0; |
109 |
private Envelope envelope;
|
110 |
private SimpleTaskStatus taskStatus;
|
111 |
|
112 |
|
113 |
public CSVStoreProvider(CSVStoreParameters parameters,
|
114 |
DataStoreProviderServices storeServices) throws InitializeException {
|
115 |
super(
|
116 |
parameters, |
117 |
storeServices, |
118 |
FileHelper.newMetadataContainer(METADATA_DEFINITION_NAME) |
119 |
); |
120 |
|
121 |
TaskStatusManager manager = ToolsLocator.getTaskStatusManager(); |
122 |
this.taskStatus = manager.createDefaultSimpleTaskStatus("CSV"); |
123 |
|
124 |
counterNewsOIDs = 0;
|
125 |
|
126 |
File file = getCSVParameters().getFile();
|
127 |
resource = this.createResource(
|
128 |
FileResource.NAME, |
129 |
new Object[] { file.getAbsolutePath() } |
130 |
); |
131 |
|
132 |
resource.addConsumer(this);
|
133 |
initializeFeatureTypes(); |
134 |
} |
135 |
|
136 |
private CSVStoreParameters getCSVParameters() {
|
137 |
return (CSVStoreParameters) this.getParameters(); |
138 |
} |
139 |
|
140 |
public String getProviderName() { |
141 |
return NAME;
|
142 |
} |
143 |
|
144 |
public boolean allowWrite() { |
145 |
return true; |
146 |
} |
147 |
|
148 |
private String getFullFileName() { |
149 |
// Usar solo para mostrar mensajes en el logger.
|
150 |
String s = "(unknow)"; |
151 |
try {
|
152 |
s = getCSVParameters().getFile().getAbsolutePath(); |
153 |
} catch(Exception e2) { |
154 |
s = "(unknow)";
|
155 |
} |
156 |
return s;
|
157 |
} |
158 |
|
159 |
public void open() throws OpenException { |
160 |
if (this.data != null) { |
161 |
return;
|
162 |
} |
163 |
this.data = new ArrayList<FeatureProvider>(); |
164 |
resource.setData(new HashMap()); |
165 |
counterNewsOIDs = 0;
|
166 |
try {
|
167 |
loadFeatures(); |
168 |
} catch (RuntimeException e) { |
169 |
logger.warn("Can't load features from CSV '"+getFullFileName()+"'.", e); |
170 |
throw e;
|
171 |
} catch (Exception e) { |
172 |
logger.warn("Can't load features from CSV '"+getFullFileName()+"'.", e); |
173 |
throw new RuntimeException(e); |
174 |
} |
175 |
} |
176 |
|
177 |
public DataServerExplorer getExplorer() throws ReadException { |
178 |
DataManager manager = DALLocator.getDataManager(); |
179 |
FilesystemServerExplorerParameters params; |
180 |
try {
|
181 |
params = (FilesystemServerExplorerParameters) manager |
182 |
.createServerExplorerParameters(FilesystemServerExplorer.NAME); |
183 |
params.setRoot(this.getCSVParameters().getFile().getParent());
|
184 |
return manager.openServerExplorer(FilesystemServerExplorer.NAME,params);
|
185 |
} catch (DataException e) {
|
186 |
throw new ReadException(this.getProviderName(), e); |
187 |
} catch (ValidateDataParametersException e) {
|
188 |
throw new ReadException(this.getProviderName(), e); |
189 |
} |
190 |
|
191 |
} |
192 |
|
193 |
class Writer { |
194 |
private Envelope envelope = null; |
195 |
private boolean calculate_envelope = false; |
196 |
private CsvListWriter listWriter = null; |
197 |
private CsvPreference csvpreferences = null; |
198 |
private FileWriter fwriter = null; |
199 |
private FeatureType ftype;
|
200 |
private File file; |
201 |
private String[] values; |
202 |
private FeatureAttributeDescriptor[] descriptors; |
203 |
private Coercion convert = null; |
204 |
private int errorcounts =0; |
205 |
private Throwable lasterror = null; |
206 |
|
207 |
public void initialize(File file, FeatureType ftype, CsvPreference csvpreferences) { |
208 |
this.file = file;
|
209 |
this.ftype = ftype;
|
210 |
this.csvpreferences = csvpreferences;
|
211 |
if( csvpreferences == null ) { |
212 |
this.csvpreferences = CsvPreference.STANDARD_PREFERENCE;
|
213 |
} |
214 |
if( ftype.getDefaultGeometryAttributeName() != null ) { |
215 |
this.calculate_envelope = true; |
216 |
} |
217 |
this.descriptors = this.ftype.getAttributeDescriptors(); |
218 |
this.convert = ToolsLocator.getDataTypesManager().getCoercion(org.gvsig.tools.dataTypes.DataTypes.STRING);
|
219 |
this.errorcounts = 0; |
220 |
} |
221 |
|
222 |
public void begin() { |
223 |
try {
|
224 |
this.fwriter = new FileWriter(file); |
225 |
} catch (IOException e) { |
226 |
logger.warn("Can't open file for write ("+file.getAbsolutePath()+").",e); |
227 |
throw new RuntimeException(e); |
228 |
} |
229 |
this.listWriter = new CsvListWriter(this.fwriter,this.csvpreferences); |
230 |
int n = 0; |
231 |
for(int i=0; i<descriptors.length; i++ ) { |
232 |
FeatureAttributeDescriptor descriptor = descriptors[i]; |
233 |
if( descriptor.getEvaluator()== null ) { |
234 |
n++; |
235 |
} |
236 |
} |
237 |
|
238 |
String[] header = new String[n]; |
239 |
this.values = new String[n]; |
240 |
n = 0;
|
241 |
for(int i=0; i<descriptors.length; i++ ) { |
242 |
FeatureAttributeDescriptor descriptor = descriptors[i]; |
243 |
if( descriptor.getEvaluator()== null ) { |
244 |
String name = descriptor.getName();
|
245 |
String type = descriptor.getDataTypeName();
|
246 |
header[n++] = name + "__" + type;
|
247 |
} |
248 |
} |
249 |
try {
|
250 |
listWriter.writeHeader(header); |
251 |
} catch (Exception e) { |
252 |
logger.warn("Can't write header '"+header.toString()+"' file for write ("+file.getAbsolutePath()+").",e); |
253 |
throw new RuntimeException(e); |
254 |
} |
255 |
} |
256 |
|
257 |
public void add(FeatureProvider feature) { |
258 |
if (this.calculate_envelope) { |
259 |
Geometry geom = feature.getDefaultGeometry(); |
260 |
if (geom != null) { |
261 |
if (envelope == null) { |
262 |
try {
|
263 |
envelope = (Envelope) geom.getEnvelope().clone(); |
264 |
} catch (CloneNotSupportedException e) { |
265 |
logger.warn("Este error no deberia pasar, siempre se puede hacer un clone de un envelope.",e);
|
266 |
} |
267 |
} else {
|
268 |
envelope.add(geom.getEnvelope()); |
269 |
} |
270 |
} |
271 |
} |
272 |
int n = 0; |
273 |
for(int i=0; i<descriptors.length; i++ ) { |
274 |
FeatureAttributeDescriptor descriptor = descriptors[i]; |
275 |
if( descriptor.getEvaluator()== null ) { |
276 |
Object value = feature.get(i);
|
277 |
try {
|
278 |
n++; |
279 |
values[n] = (String) this.convert.coerce(value); |
280 |
} catch (CoercionException e) {
|
281 |
try {
|
282 |
values[n] = value.toString(); |
283 |
} catch(Exception ex) { |
284 |
values[n] = "";
|
285 |
} |
286 |
if( errorcounts++ <= 10 ) { |
287 |
this.lasterror = e;
|
288 |
logger.warn("Can't convert value of field "+i+" to string in CVS file '"+getFullFileName()+"'.",e); |
289 |
if( errorcounts == 10 ) { |
290 |
logger.warn("Too many error writing CVS file '"+getFullFileName()+"', don't output more."); |
291 |
} |
292 |
} |
293 |
} |
294 |
} |
295 |
} |
296 |
try {
|
297 |
this.listWriter.writeHeader(values);
|
298 |
} catch (IOException e) { |
299 |
if( errorcounts++ <= 10 ) { |
300 |
this.lasterror = e;
|
301 |
logger.warn("Can't write values to CVS file '"+getFullFileName()+"'.",e); |
302 |
if( errorcounts == 10 ) { |
303 |
logger.warn("Too many error writing CVS file '"+getFullFileName()+"', don't output more."); |
304 |
} |
305 |
} |
306 |
} |
307 |
|
308 |
} |
309 |
|
310 |
public void end() throws PerformEditingException { |
311 |
if( this.errorcounts>0 ) { |
312 |
throw new PerformEditingException(this.file.getAbsolutePath(), lasterror); |
313 |
} |
314 |
if( listWriter!=null ) { |
315 |
try {
|
316 |
listWriter.close(); |
317 |
} catch(Exception ex) { |
318 |
// Ignore error
|
319 |
} |
320 |
listWriter=null ;
|
321 |
} |
322 |
if( fwriter!=null ) { |
323 |
try {
|
324 |
fwriter.close(); |
325 |
} catch(Exception ex) { |
326 |
// Ignore error
|
327 |
} |
328 |
fwriter=null ;
|
329 |
} |
330 |
} |
331 |
|
332 |
public Envelope getEnvelope() {
|
333 |
return this.envelope; |
334 |
} |
335 |
} |
336 |
|
337 |
public void performChanges(Iterator deleteds, Iterator inserteds, Iterator updateds, Iterator originalFeatureTypesUpdated) throws PerformEditingException { |
338 |
|
339 |
try {
|
340 |
this.taskStatus.add();
|
341 |
taskStatus.message("_preparing");
|
342 |
getResource().execute(new ResourceAction() {
|
343 |
public Object run() throws Exception { |
344 |
FeatureSet features = null;
|
345 |
DisposableIterator it = null;
|
346 |
try {
|
347 |
File file = (File) resource.get(); |
348 |
|
349 |
Writer writer = new Writer(); |
350 |
writer.initialize(file,getStoreServices().getDefaultFeatureType(),null);
|
351 |
features = |
352 |
getStoreServices().getFeatureStore() |
353 |
.getFeatureSet(); |
354 |
List<FeatureProvider> newdata = new ArrayList<FeatureProvider>(); |
355 |
writer.begin(); |
356 |
it = features.fastIterator(); |
357 |
taskStatus.setRangeOfValues(0,0); |
358 |
long counter=0; |
359 |
while (it.hasNext()) {
|
360 |
taskStatus.setCurValue(counter++); |
361 |
FeatureProvider feature = getStoreServices().getFeatureProviderFromFeature( |
362 |
(org.gvsig.fmap.dal.feature.Feature) it.next()); |
363 |
writer.add(feature); |
364 |
if (feature.getOID() == null){ |
365 |
logger.warn("feature without OID");
|
366 |
feature.setOID(createNewOID()); |
367 |
} |
368 |
newdata.add(feature); |
369 |
} |
370 |
data = newdata; |
371 |
if (writer.getEnvelope() != null){ |
372 |
envelope = writer.getEnvelope().getGeometry().getEnvelope(); |
373 |
} |
374 |
resource.notifyChanges(); |
375 |
writer.end(); |
376 |
} finally {
|
377 |
if (it != null) { |
378 |
it.dispose(); |
379 |
} |
380 |
if (features != null) { |
381 |
features.dispose(); |
382 |
} |
383 |
} |
384 |
return null; |
385 |
} |
386 |
}); |
387 |
this.taskStatus.terminate();
|
388 |
} catch (Exception e) { |
389 |
this.taskStatus.abort();
|
390 |
throw new PerformEditingException(getResource().toString(), e); |
391 |
} finally {
|
392 |
this.taskStatus.remove();
|
393 |
} |
394 |
} |
395 |
|
396 |
public boolean closeResourceRequested(ResourceProvider resource) { |
397 |
return true; |
398 |
} |
399 |
|
400 |
public int getOIDType() { |
401 |
return DataTypes.LONG;
|
402 |
} |
403 |
|
404 |
public boolean supportsAppendMode() { |
405 |
return false; |
406 |
} |
407 |
|
408 |
public void append(FeatureProvider featureProvider) { |
409 |
throw new UnsupportedOperationException(); |
410 |
} |
411 |
|
412 |
public void beginAppend() { |
413 |
throw new UnsupportedOperationException(); |
414 |
} |
415 |
|
416 |
public void endAppend() { |
417 |
throw new UnsupportedOperationException(); |
418 |
} |
419 |
|
420 |
public void saveToState(PersistentState state) throws PersistenceException { |
421 |
throw new NotYetImplemented(); |
422 |
} |
423 |
|
424 |
public void loadFromState(PersistentState state) throws PersistenceException { |
425 |
throw new NotYetImplemented(); |
426 |
} |
427 |
|
428 |
public Object createNewOID() { |
429 |
return new Long(counterNewsOIDs++); |
430 |
} |
431 |
|
432 |
protected void initializeFeatureTypes() throws InitializeException { |
433 |
try {
|
434 |
this.open();
|
435 |
} catch (OpenException e) {
|
436 |
throw new InitializeException(this.getProviderName(), e); |
437 |
} |
438 |
} |
439 |
|
440 |
public Envelope getEnvelope() throws DataException { |
441 |
this.open();
|
442 |
return this.envelope; |
443 |
} |
444 |
|
445 |
public Object getDynValue(String name) throws DynFieldNotFoundException { |
446 |
if( DataStore.METADATA_ENVELOPE.equalsIgnoreCase(name) ) {
|
447 |
try {
|
448 |
return this.getEnvelope(); |
449 |
} catch (DataException e) {
|
450 |
return null; |
451 |
} |
452 |
} else {
|
453 |
if( DataStore.METADATA_CRS.equalsIgnoreCase(name) ) {
|
454 |
IProjection pro = CSVStoreParameters.getCRS(this.getCSVParameters());
|
455 |
if (pro != null){ |
456 |
return pro;
|
457 |
} |
458 |
} |
459 |
} |
460 |
return super.getDynValue(name); |
461 |
} |
462 |
|
463 |
|
464 |
public void resourceChanged(ResourceProvider resource) { |
465 |
this.getStoreServices().notifyChange(
|
466 |
DataStoreNotification.RESOURCE_CHANGED, |
467 |
resource); |
468 |
} |
469 |
|
470 |
|
471 |
public Object getSourceId() { |
472 |
return this.getCSVParameters().getFile(); |
473 |
} |
474 |
|
475 |
public String getName() { |
476 |
String name = this.getCSVParameters().getFile().getName(); |
477 |
return FilenameUtils.getBaseName(name);
|
478 |
} |
479 |
|
480 |
public String getFullName() { |
481 |
return this.getCSVParameters().getFile().getAbsolutePath(); |
482 |
} |
483 |
|
484 |
public ResourceProvider getResource() {
|
485 |
return resource;
|
486 |
} |
487 |
|
488 |
private boolean isEmpty(String s) { |
489 |
if( s==null ) { |
490 |
return true; |
491 |
} |
492 |
return s.trim().length()==0; |
493 |
} |
494 |
|
495 |
private CsvPreference getCSVPreferences() {
|
496 |
try {
|
497 |
String s = null; |
498 |
char quoteChar;
|
499 |
int delimiterChar;
|
500 |
String endOfLineSymbols;
|
501 |
|
502 |
DynObject params = this.getParameters();
|
503 |
|
504 |
CsvPreference.Builder builder = null;
|
505 |
|
506 |
CsvPreference defaultPreference = CSVStoreParameters |
507 |
.getPredefinedCSVPreferences(params); |
508 |
if (defaultPreference == null) { |
509 |
defaultPreference = CsvPreference.STANDARD_PREFERENCE; |
510 |
} |
511 |
|
512 |
endOfLineSymbols = CSVStoreParameters.getRecordSeparator(params); |
513 |
if (isEmpty(endOfLineSymbols)) {
|
514 |
endOfLineSymbols = defaultPreference.getEndOfLineSymbols(); |
515 |
} |
516 |
s = CSVStoreParameters.getQuoteCharacter(params); |
517 |
if (isEmpty(s)) {
|
518 |
quoteChar = (char) defaultPreference.getQuoteChar();
|
519 |
} else {
|
520 |
quoteChar = s.charAt(0);
|
521 |
} |
522 |
s = CSVStoreParameters.getDelimiter(params); |
523 |
if (isEmpty(s)) {
|
524 |
delimiterChar = defaultPreference.getDelimiterChar(); |
525 |
} else {
|
526 |
delimiterChar = s.charAt(0);
|
527 |
} |
528 |
|
529 |
builder = new CsvPreference.Builder(quoteChar, delimiterChar,
|
530 |
endOfLineSymbols); |
531 |
|
532 |
s = CSVStoreParameters.getCommentStartMarker(params); |
533 |
if (!isEmpty(s)) {
|
534 |
CommentStartsWith cs = new CommentStartsWith(s);
|
535 |
builder.skipComments(cs); |
536 |
} |
537 |
|
538 |
builder.surroundingSpacesNeedQuotes(CSVStoreParameters |
539 |
.getSurroundingSpacesNeedQuotes(params)); |
540 |
QuoteMode quoteMode = CSVStoreParameters.getQuoteMode(params); |
541 |
if( quoteMode != null ) { |
542 |
builder.useQuoteMode(quoteMode); |
543 |
} |
544 |
return builder.build();
|
545 |
} catch (Exception e) { |
546 |
logger.warn("Can't make preferences for CSV '" + getFullFileName()
|
547 |
+ "'.", e);
|
548 |
return null; |
549 |
} |
550 |
} |
551 |
|
552 |
private EditableFeatureType getFeatureType(String headers[]) { |
553 |
EditableFeatureType fType = getStoreServices().createFeatureType(this.getName());
|
554 |
fType.setHasOID(true);
|
555 |
DataTypesManager dataTypesManager = ToolsLocator.getDataTypesManager(); |
556 |
|
557 |
int[] types = new int[headers.length]; |
558 |
for( int i=0; i<types.length; i++) { |
559 |
String s = headers[i];
|
560 |
String typename = null; |
561 |
if( s.contains(":") ) { |
562 |
String[] ss = s.split(":"); |
563 |
headers[i] = ss[0];
|
564 |
typename = ss[1];
|
565 |
types[i] = dataTypesManager.getType(typename); |
566 |
} else if( s.contains("__") ) { |
567 |
String[] ss = s.split("__"); |
568 |
headers[i] = ss[0];
|
569 |
typename = ss[1];
|
570 |
types[i] = dataTypesManager.getType(typename); |
571 |
} else {
|
572 |
types[i] = DataTypes.STRING; |
573 |
typename = "string";
|
574 |
} |
575 |
if( types[i]==DataTypes.INVALID ) {
|
576 |
types[i] = DataTypes.STRING; |
577 |
logger.info("Type '"+typename+"' not valid for attribute '"+headers[i]+"' in CSV file '"+this.getFullFileName()+"'."); |
578 |
} |
579 |
} |
580 |
|
581 |
int[] param_types = CSVStoreParameters.getFieldTypes(this.getParameters()); |
582 |
if( param_types != null ) { |
583 |
for( int i=0; i<types.length && i<param_types.length; i++) { |
584 |
types[i] = param_types[i]; |
585 |
} |
586 |
} |
587 |
for(int i=0; i<types.length; i++) { |
588 |
int fieldType = types[i];
|
589 |
String fieldName = headers[i];
|
590 |
fType.add(fieldName, fieldType); |
591 |
if( fieldType == DataTypes.GEOMETRY && fType.getDefaultGeometryAttributeName() == null) { |
592 |
fType.setDefaultGeometryAttributeName(fieldName); |
593 |
} |
594 |
} |
595 |
String[] pointDimensionNames = CSVStoreParameters.getPointDimensionNames(this.getParameters()); |
596 |
if( pointDimensionNames!= null ) { |
597 |
EditableFeatureAttributeDescriptor attr = fType.add("GEOM", DataTypes.GEOMETRY, new ToPointEvaluaror(pointDimensionNames)); |
598 |
GeometryManager geommgr = GeometryLocator.getGeometryManager(); |
599 |
GeometryType gt; |
600 |
try {
|
601 |
gt = geommgr.getGeometryType(Geometry.TYPES.GEOMETRY, Geometry.SUBTYPES.GEOM3D); |
602 |
attr.setGeometryType(gt); |
603 |
} catch (Exception e) { |
604 |
logger.warn("Can't set geometry type for the calculated field in CSV file '"+getFullFileName()+"'.",e); |
605 |
} |
606 |
} |
607 |
return fType;
|
608 |
} |
609 |
|
610 |
static class ToPointEvaluaror extends AbstractEvaluator { |
611 |
|
612 |
private static final Logger logger = LoggerFactory.getLogger(ToPointEvaluaror.class); |
613 |
|
614 |
private GeometryManager geommgr = null; |
615 |
private String xname = null; |
616 |
private String yname = null; |
617 |
private String zname = null; |
618 |
private Coercion toDouble;
|
619 |
private int errorcount = 0; |
620 |
ToPointEvaluaror(String[] pointDimensionNames) { |
621 |
this.xname = pointDimensionNames[0]; |
622 |
this.yname = pointDimensionNames[1]; |
623 |
if( pointDimensionNames.length >2 ) { |
624 |
this.yname = pointDimensionNames[2]; |
625 |
} |
626 |
this.geommgr = GeometryLocator.getGeometryManager();
|
627 |
this.toDouble = ToolsLocator.getDataTypesManager().getCoercion(DataTypes.DOUBLE);
|
628 |
} |
629 |
|
630 |
public Object evaluate(EvaluatorData data) throws EvaluatorException { |
631 |
try {
|
632 |
double x = ((Double) toDouble.coerce(data.getDataValue(xname))).doubleValue(); |
633 |
double y = ((Double) toDouble.coerce(data.getDataValue(yname))).doubleValue(); |
634 |
Point point = geommgr.createPoint(x, y,Geometry.SUBTYPES.GEOM3D);
|
635 |
if (zname != null) { |
636 |
double z = ((Double) toDouble.coerce(data.getDataValue(zname))).doubleValue(); |
637 |
point.setCoordinateAt(2, z);
|
638 |
} |
639 |
return point;
|
640 |
} catch (Exception ex) { |
641 |
if( ++errorcount <5 ) { |
642 |
logger.warn("["+errorcount+"] Can't create point in CSV provider. XNAME='"+ |
643 |
xname + "', YNAME='"+yname+"', ZNAME='"+zname+"', data="+data.toString()); |
644 |
} |
645 |
return null; |
646 |
} |
647 |
} |
648 |
|
649 |
public String getName() { |
650 |
return "ToPointEvaluaror"; |
651 |
} |
652 |
|
653 |
} |
654 |
|
655 |
private void loadFeatures() throws IOException, DataException, |
656 |
CoercionException, CloneNotSupportedException {
|
657 |
//
|
658 |
// http://supercsv.sourceforge.net/examples_reading.html
|
659 |
// http://supercsv.sourceforge.net/apidocs/index.html
|
660 |
//
|
661 |
FileReader in = null; |
662 |
CsvListReader reader = null;
|
663 |
try {
|
664 |
Envelope envelope = null;
|
665 |
String headers[] = null; |
666 |
FeatureStoreProviderServices store = this.getStoreServices();
|
667 |
|
668 |
// Initiaize the CSV parser
|
669 |
CsvPreference preferences = getCSVPreferences(); |
670 |
in = new FileReader(this.getCSVParameters().getFile()); |
671 |
|
672 |
reader = new CsvListReader(in, preferences);
|
673 |
headers = CSVStoreParameters.getHeaders(getCSVParameters()); |
674 |
if (headers == null) { |
675 |
headers = reader.getHeader(true);
|
676 |
if (headers == null) { |
677 |
String msg = "Can't retrieve header from csv file '" |
678 |
+ this.getCSVParameters().getFile()
|
679 |
.getAbsolutePath() |
680 |
+ "' and not specified in the parameters.";
|
681 |
logger.warn(msg); |
682 |
throw new RuntimeException(msg); |
683 |
} |
684 |
} |
685 |
|
686 |
// Initialize the feature types
|
687 |
FeatureType ftype = this.getFeatureType(headers)
|
688 |
.getNotEditableCopy(); |
689 |
List<FeatureType> ftypes = new ArrayList<FeatureType>(); |
690 |
ftypes.add(ftype); |
691 |
store.setFeatureTypes(ftypes, ftype); |
692 |
|
693 |
Coercion coercion[] = new Coercion[ftype.size()]; |
694 |
for (int i = 0; i < ftype.size(); i++) { |
695 |
coercion[i] = ftype.getAttributeDescriptor(i).getDataType() |
696 |
.getCoercion(); |
697 |
} |
698 |
boolean calculate_envelope = (ftype
|
699 |
.getDefaultGeometryAttributeName() != null);
|
700 |
taskStatus.message("_loading");
|
701 |
int count = 0; |
702 |
|
703 |
List<String> row = reader.read(); |
704 |
while (row != null) { |
705 |
taskStatus.setCurValue(++count); |
706 |
FeatureProvider feature = this.createFeatureProvider(ftype);
|
707 |
for (int i = 0; i < row.size(); i++) { |
708 |
feature.set(i, coercion[i].coerce(row.get(i))); |
709 |
} |
710 |
if (calculate_envelope) {
|
711 |
Geometry geom = feature.getDefaultGeometry(); |
712 |
if (geom != null) { |
713 |
if (envelope == null) { |
714 |
envelope = (Envelope) geom.getEnvelope().clone(); |
715 |
} else {
|
716 |
envelope.add(geom.getEnvelope()); |
717 |
} |
718 |
} |
719 |
} |
720 |
this.addFeatureProvider(feature);
|
721 |
row = reader.read(); |
722 |
} |
723 |
taskStatus.terminate(); |
724 |
} finally {
|
725 |
if( reader != null ) { |
726 |
try {
|
727 |
reader.close(); |
728 |
} catch(Exception ex) { |
729 |
// Do nothing
|
730 |
} |
731 |
reader = null;
|
732 |
} |
733 |
if( in != null ) { |
734 |
try {
|
735 |
in.close(); |
736 |
} catch(Exception ex) { |
737 |
// Do nothing
|
738 |
} |
739 |
in = null;
|
740 |
} |
741 |
} |
742 |
} |
743 |
|
744 |
} |