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 @ 45721
History | View | Annotate | Download (24.2 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 modify it under
|
7 |
* the terms of the GNU General Public License as published by the Free Software
|
8 |
* Foundation; either version 3 of the License, or (at your option) any later
|
9 |
* version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful, but WITHOUT
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
13 |
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
14 |
* details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License along with
|
17 |
* this program; if not, write to the Free Software Foundation, Inc., 51
|
18 |
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
19 |
*
|
20 |
* For any additional information, do not hesitate to contact us at info AT
|
21 |
* gvsig.com, or visit our website www.gvsig.com.
|
22 |
*/
|
23 |
package org.gvsig.fmap.dal.store.csv; |
24 |
|
25 |
import java.io.File; |
26 |
import java.io.InputStreamReader; |
27 |
import java.net.URI; |
28 |
import java.net.URL; |
29 |
import java.util.ArrayList; |
30 |
import java.util.HashMap; |
31 |
import java.util.Iterator; |
32 |
import java.util.List; |
33 |
import java.util.Locale; |
34 |
|
35 |
import org.apache.commons.io.FilenameUtils; |
36 |
import org.apache.commons.lang3.StringUtils; |
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.feature.EditableFeatureAttributeDescriptor; |
50 |
import org.gvsig.fmap.dal.feature.EditableFeatureType; |
51 |
import org.gvsig.fmap.dal.feature.Feature; |
52 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
53 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
54 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
55 |
import org.gvsig.fmap.dal.feature.FeatureType; |
56 |
import org.gvsig.fmap.dal.feature.exception.PerformEditingException; |
57 |
import org.gvsig.fmap.dal.feature.spi.FeatureProvider; |
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.dal.store.csv.simplereaders.CSVReaderSuperCSV; |
67 |
import org.gvsig.fmap.dal.store.csv.simplereaders.SimpleReader; |
68 |
import org.gvsig.fmap.geom.Geometry; |
69 |
import org.gvsig.fmap.geom.GeometryLocator; |
70 |
import org.gvsig.fmap.geom.GeometryManager; |
71 |
import org.gvsig.fmap.geom.primitive.Envelope; |
72 |
import org.gvsig.fmap.geom.primitive.Point; |
73 |
import org.gvsig.tools.ToolsLocator; |
74 |
import org.gvsig.tools.dataTypes.Coercion; |
75 |
import org.gvsig.tools.dispose.DisposableIterator; |
76 |
import org.gvsig.tools.dynobject.exception.DynFieldNotFoundException; |
77 |
import org.gvsig.tools.evaluator.AbstractEvaluator; |
78 |
import org.gvsig.tools.evaluator.EvaluatorData; |
79 |
import org.gvsig.tools.evaluator.EvaluatorException; |
80 |
import org.gvsig.tools.exception.BaseException; |
81 |
import org.gvsig.tools.exception.NotYetImplemented; |
82 |
import org.gvsig.tools.persistence.PersistentState; |
83 |
import org.gvsig.tools.persistence.exception.PersistenceException; |
84 |
import org.gvsig.tools.task.SimpleTaskStatus; |
85 |
import org.gvsig.tools.task.TaskStatusManager; |
86 |
import org.gvsig.tools.visitor.VisitCanceledException; |
87 |
import org.gvsig.tools.visitor.Visitor; |
88 |
import org.slf4j.Logger; |
89 |
import org.slf4j.LoggerFactory; |
90 |
import org.supercsv.prefs.CsvPreference; |
91 |
import org.gvsig.tools.dataTypes.CoercionContext; |
92 |
|
93 |
@SuppressWarnings("UseSpecificCatch") |
94 |
public class CSVStoreProvider extends AbstractMemoryStoreProvider implements |
95 |
ResourceConsumer { |
96 |
|
97 |
private static final Logger LOGGER = LoggerFactory.getLogger(CSVStoreProvider.class); |
98 |
|
99 |
public static final String NAME = DataStore.CSV_PROVIDER_NAME; |
100 |
public static final String DESCRIPTION = "CSV file"; |
101 |
|
102 |
public static final String METADATA_DEFINITION_NAME = NAME; |
103 |
|
104 |
private final ResourceProvider resource; |
105 |
|
106 |
private long counterNewsOIDs = 0; |
107 |
private Envelope envelope;
|
108 |
private boolean need_calculate_envelope = false; |
109 |
private final SimpleTaskStatus taskStatus; |
110 |
private final CSVFeatureWriter writer; |
111 |
private FeatureType featureType;
|
112 |
|
113 |
@SuppressWarnings({"OverridableMethodCallInConstructor", "LeakingThisInConstructor"}) |
114 |
public CSVStoreProvider(CSVStoreParameters parameters,
|
115 |
DataStoreProviderServices storeServices) throws InitializeException {
|
116 |
super(
|
117 |
parameters, |
118 |
storeServices, |
119 |
FileHelper.newMetadataContainer(METADATA_DEFINITION_NAME) |
120 |
); |
121 |
this.writer = new CSVFeatureWriter(); |
122 |
TaskStatusManager manager = ToolsLocator.getTaskStatusManager(); |
123 |
this.taskStatus = manager.createDefaultSimpleTaskStatus("CSV"); |
124 |
|
125 |
counterNewsOIDs = 0;
|
126 |
|
127 |
File file = getCSVParameters().getFile();
|
128 |
resource = this.createResource(
|
129 |
FileResource.NAME, |
130 |
new Object[]{file.getAbsolutePath()} |
131 |
); |
132 |
|
133 |
resource.addConsumer(this);
|
134 |
|
135 |
initializeFeatureTypes(); |
136 |
} |
137 |
|
138 |
private CSVStoreParameters getCSVParameters() {
|
139 |
return (CSVStoreParameters) this.getParameters(); |
140 |
} |
141 |
|
142 |
@Override
|
143 |
public String getProviderName() { |
144 |
return NAME;
|
145 |
} |
146 |
|
147 |
@Override
|
148 |
public boolean allowWrite() { |
149 |
return true; |
150 |
} |
151 |
|
152 |
private String getFullFileName() { |
153 |
// Usar solo para mostrar mensajes en el logger.
|
154 |
String s;
|
155 |
try {
|
156 |
s = getCSVParameters().getFile().getAbsolutePath(); |
157 |
} catch (Exception e2) { |
158 |
s = "(unknow)";
|
159 |
} |
160 |
return s;
|
161 |
} |
162 |
|
163 |
@Override
|
164 |
public void open() throws OpenException { |
165 |
if (this.data != null) { |
166 |
return;
|
167 |
} |
168 |
this.data = new ArrayList<>(); |
169 |
resource.setData(new HashMap()); |
170 |
counterNewsOIDs = 0;
|
171 |
try {
|
172 |
loadFeatures(); |
173 |
} catch (RuntimeException e) { |
174 |
LOGGER.debug("Can't load features from CSV '" + getFullFileName() + "'.", e); |
175 |
throw e;
|
176 |
} catch (Exception e) { |
177 |
LOGGER.debug("Can't load features from CSV '" + getFullFileName() + "'.", e); |
178 |
throw new RuntimeException(e); |
179 |
} |
180 |
} |
181 |
|
182 |
@Override
|
183 |
public DataServerExplorer getExplorer() throws ReadException { |
184 |
DataManager manager = DALLocator.getDataManager(); |
185 |
FilesystemServerExplorerParameters params; |
186 |
try {
|
187 |
params = (FilesystemServerExplorerParameters) manager |
188 |
.createServerExplorerParameters(FilesystemServerExplorer.NAME); |
189 |
params.setRoot(this.getCSVParameters().getFile().getParent());
|
190 |
return manager.openServerExplorer(FilesystemServerExplorer.NAME, params);
|
191 |
} catch (Exception e) { |
192 |
throw new ReadException(this.getProviderName(), e); |
193 |
} |
194 |
|
195 |
} |
196 |
|
197 |
@Override
|
198 |
@SuppressWarnings("Convert2Lambda") |
199 |
public void performChanges(Iterator deleteds, Iterator inserteds, Iterator updateds, Iterator originalFeatureTypesUpdated) throws PerformEditingException { |
200 |
|
201 |
try {
|
202 |
this.taskStatus.add();
|
203 |
taskStatus.message("_preparing");
|
204 |
this.getResource().closeRequest();
|
205 |
getResource().execute(new ResourceAction() {
|
206 |
@Override
|
207 |
public Object run() throws Exception { |
208 |
FeatureSet features = null;
|
209 |
DisposableIterator it = null;
|
210 |
try {
|
211 |
File file = (File) resource.get(); |
212 |
|
213 |
features |
214 |
= getStoreServices().getFeatureStore() |
215 |
.getFeatureSet(); |
216 |
List<FeatureProvider> newdata = new ArrayList<>(); |
217 |
FeatureType ftype = getStoreServices().getDefaultFeatureType(); |
218 |
writer.initialize(getCSVParameters(), file, ftype, getCSVPreferences()); |
219 |
writer.begin(); |
220 |
it = features.fastIterator(); |
221 |
taskStatus.setRangeOfValues(0, 0); |
222 |
long counter = 0; |
223 |
while (it.hasNext()) {
|
224 |
taskStatus.setCurValue(counter++); |
225 |
FeatureProvider feature = getStoreServices().getFeatureProviderFromFeature( |
226 |
(org.gvsig.fmap.dal.feature.Feature) it.next()); |
227 |
writer.add(feature); |
228 |
if (feature.getOID() == null) { |
229 |
LOGGER.warn("feature without OID");
|
230 |
feature.setOID(createNewOID()); |
231 |
} |
232 |
newdata.add(feature); |
233 |
} |
234 |
data = newdata; |
235 |
if (writer.getEnvelope() != null) { |
236 |
envelope = writer.getEnvelope().getGeometry().getEnvelope(); |
237 |
} |
238 |
resource.notifyChanges(); |
239 |
writer.end(); |
240 |
} finally {
|
241 |
if (it != null) { |
242 |
it.dispose(); |
243 |
} |
244 |
if (features != null) { |
245 |
features.dispose(); |
246 |
} |
247 |
} |
248 |
return null; |
249 |
} |
250 |
|
251 |
}); |
252 |
this.taskStatus.terminate();
|
253 |
} catch (Exception e) { |
254 |
this.taskStatus.abort();
|
255 |
throw new PerformEditingException(getResource().toString(), e); |
256 |
} finally {
|
257 |
this.taskStatus.remove();
|
258 |
} |
259 |
} |
260 |
|
261 |
private CsvPreference getCSVPreferences() {
|
262 |
CSVReaderSuperCSV reader = new CSVReaderSuperCSV(getCSVParameters());
|
263 |
return reader.getCSVPreferences();
|
264 |
} |
265 |
|
266 |
@Override
|
267 |
public boolean closeResourceRequested(ResourceProvider resource) { |
268 |
return true; |
269 |
} |
270 |
|
271 |
@Override
|
272 |
public int getOIDType() { |
273 |
return DataTypes.LONG;
|
274 |
} |
275 |
|
276 |
@Override
|
277 |
public boolean supportsAppendMode() { |
278 |
return true; |
279 |
} |
280 |
|
281 |
@Override
|
282 |
@SuppressWarnings("Convert2Lambda") |
283 |
public void append(final FeatureProvider featureProvider) { |
284 |
//todo
|
285 |
getResource().execute(new ResourceAction() {
|
286 |
@Override
|
287 |
public Object run() throws Exception { |
288 |
//writer.append(getStoreServices().createFeature(featureProvider));
|
289 |
writer.add(featureProvider); |
290 |
return null; |
291 |
} |
292 |
}); |
293 |
} |
294 |
|
295 |
@Override
|
296 |
@SuppressWarnings("Convert2Lambda") |
297 |
public void beginAppend() throws DataException { |
298 |
this.close();
|
299 |
getResource().execute(new ResourceAction() {
|
300 |
@Override
|
301 |
public Object run() throws Exception { |
302 |
writer.initialize( |
303 |
getCSVParameters(), |
304 |
getCSVParameters().getFile(), |
305 |
getFeatureStore().getDefaultFeatureType(), |
306 |
getCSVPreferences() |
307 |
); |
308 |
writer.beginAppend(); |
309 |
return null; |
310 |
} |
311 |
}); |
312 |
|
313 |
} |
314 |
|
315 |
@Override
|
316 |
@SuppressWarnings("Convert2Lambda") |
317 |
public void endAppend() { |
318 |
try {
|
319 |
getResource().execute(new ResourceAction() {
|
320 |
@Override
|
321 |
public Object run() throws Exception { |
322 |
writer.end(); |
323 |
resource.notifyChanges(); //resourcesNotifyChanges();
|
324 |
counterNewsOIDs = -1;
|
325 |
return null; |
326 |
} |
327 |
}); |
328 |
writer.end(); |
329 |
} catch (PerformEditingException ex) {
|
330 |
LOGGER.warn("Not been able to end append '" + this.getFullName() + "'.", ex); |
331 |
} |
332 |
} |
333 |
|
334 |
public void saveToState(PersistentState state) throws PersistenceException { |
335 |
throw new NotYetImplemented(); |
336 |
} |
337 |
|
338 |
public void loadFromState(PersistentState state) throws PersistenceException { |
339 |
throw new NotYetImplemented(); |
340 |
} |
341 |
|
342 |
@Override
|
343 |
public Object createNewOID() { |
344 |
return counterNewsOIDs++;
|
345 |
} |
346 |
|
347 |
protected void initializeFeatureTypes() throws InitializeException { |
348 |
try {
|
349 |
this.open();
|
350 |
} catch (OpenException e) {
|
351 |
throw new InitializeException(this.getProviderName(), e); |
352 |
} |
353 |
} |
354 |
|
355 |
@Override
|
356 |
@SuppressWarnings("Convert2Lambda") |
357 |
public Envelope getEnvelope() throws DataException { |
358 |
this.open();
|
359 |
if (this.envelope != null) { |
360 |
return this.envelope; |
361 |
} |
362 |
if (!this.need_calculate_envelope) { |
363 |
return null; |
364 |
} |
365 |
FeatureStore fs = this.getFeatureStore();
|
366 |
FeatureType ft = fs.getDefaultFeatureType(); |
367 |
FeatureAttributeDescriptor fad = ft.getAttributeDescriptor(ft.getDefaultGeometryAttributeIndex()); |
368 |
|
369 |
try {
|
370 |
this.envelope = GeometryLocator.getGeometryManager().createEnvelope(fad.getGeomType().getSubType());
|
371 |
fs.accept(new Visitor() {
|
372 |
@Override
|
373 |
public void visit(Object obj) throws VisitCanceledException, BaseException { |
374 |
Feature f = (Feature) obj; |
375 |
Geometry geom = f.getDefaultGeometry(); |
376 |
if (geom != null) { |
377 |
envelope.add(geom.getEnvelope()); |
378 |
} |
379 |
} |
380 |
}); |
381 |
} catch (BaseException e) {
|
382 |
LOGGER.warn("Can't calculate the envelope of CSV file '" + this.getFullName() + "'.", e); |
383 |
this.envelope = null; |
384 |
} |
385 |
|
386 |
this.need_calculate_envelope = false; |
387 |
return this.envelope; |
388 |
} |
389 |
|
390 |
@Override
|
391 |
public Object getDynValue(String name) throws DynFieldNotFoundException { |
392 |
if (DataStore.METADATA_ENVELOPE.equalsIgnoreCase(name)) {
|
393 |
try {
|
394 |
return this.getEnvelope(); |
395 |
} catch (DataException e) {
|
396 |
return null; |
397 |
} |
398 |
} else {
|
399 |
if (DataStore.METADATA_CRS.equalsIgnoreCase(name)) {
|
400 |
IProjection pro = CSVStoreParameters.getCRS(this.getCSVParameters());
|
401 |
if (pro != null) { |
402 |
return pro;
|
403 |
} |
404 |
} |
405 |
} |
406 |
return super.getDynValue(name); |
407 |
} |
408 |
|
409 |
@Override
|
410 |
public void resourceChanged(ResourceProvider resource) { |
411 |
this.getStoreServices().notifyChange(
|
412 |
DataStoreNotification.RESOURCE_CHANGED, |
413 |
resource); |
414 |
} |
415 |
|
416 |
@Override
|
417 |
public Object getSourceId() { |
418 |
return this.getCSVParameters().getFile(); |
419 |
} |
420 |
|
421 |
@Override
|
422 |
public String getName() { |
423 |
String name = this.getCSVParameters().getFile().getName(); |
424 |
return FilenameUtils.getBaseName(name);
|
425 |
} |
426 |
|
427 |
@Override
|
428 |
public String getFullName() { |
429 |
return this.getCSVParameters().getFile().getAbsolutePath(); |
430 |
} |
431 |
|
432 |
@Override
|
433 |
public ResourceProvider getResource() {
|
434 |
return resource;
|
435 |
} |
436 |
|
437 |
private boolean isEmpty(String s) { |
438 |
if (s == null) { |
439 |
return true; |
440 |
} |
441 |
return s.trim().length() == 0; |
442 |
} |
443 |
|
444 |
private void init(CSVStoreParameters parameters, DataStoreProviderServices storeServices) { |
445 |
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. |
446 |
} |
447 |
|
448 |
|
449 |
|
450 |
static class ToPointEvaluaror extends AbstractEvaluator { |
451 |
|
452 |
private static final Logger logger = LoggerFactory.getLogger(ToPointEvaluaror.class); |
453 |
|
454 |
private GeometryManager geommgr = null; |
455 |
private String xname = null; |
456 |
private String yname = null; |
457 |
private String zname = null; |
458 |
private final Coercion toDouble; |
459 |
private int errorcount = 0; |
460 |
|
461 |
ToPointEvaluaror(String[] pointDimensionNames) { |
462 |
this.xname = pointDimensionNames[0]; |
463 |
this.yname = pointDimensionNames[1]; |
464 |
if (pointDimensionNames.length > 2) { |
465 |
this.zname = pointDimensionNames[2]; |
466 |
} |
467 |
this.geommgr = GeometryLocator.getGeometryManager();
|
468 |
this.toDouble = ToolsLocator.getDataTypesManager().getCoercion(DataTypes.DOUBLE);
|
469 |
} |
470 |
|
471 |
@Override
|
472 |
public Object evaluate(EvaluatorData data) throws EvaluatorException { |
473 |
try {
|
474 |
double x = ((Double) toDouble.coerce(data.getDataValue(xname))); |
475 |
double y = ((Double) toDouble.coerce(data.getDataValue(yname))); |
476 |
Point point = geommgr.createPoint(x, y, Geometry.SUBTYPES.GEOM3D);
|
477 |
if (zname != null) { |
478 |
double z = ((Double) toDouble.coerce(data.getDataValue(zname))); |
479 |
point.setCoordinateAt(2, z);
|
480 |
} |
481 |
return point;
|
482 |
} catch (Exception ex) { |
483 |
if (++errorcount < 5) { |
484 |
logger.warn("[" + errorcount + "] Can't create point in CSV provider. XNAME='" |
485 |
+ xname + "', YNAME='" + yname + "', ZNAME='" + zname + "', data=" + data.toString()); |
486 |
} |
487 |
return null; |
488 |
} |
489 |
} |
490 |
|
491 |
@Override
|
492 |
public String getName() { |
493 |
return "ToPointEvaluaror"; |
494 |
} |
495 |
|
496 |
} |
497 |
|
498 |
private void loadFeatures() { |
499 |
InputStreamReader in = null; |
500 |
SimpleReader reader = null;
|
501 |
try {
|
502 |
boolean ignore_errors = CSVStoreParameters.getIgnoreErrors(getCSVParameters());
|
503 |
|
504 |
// Initialize the feature types
|
505 |
EditableFeatureType edftype = getStoreServices().createFeatureType(this.getName());
|
506 |
CSVUtils.loadFeatureType(getCSVParameters(), edftype, true);
|
507 |
FeatureType ftype = edftype.getNotEditableCopy(); |
508 |
this.setFeatureType(ftype);
|
509 |
|
510 |
in = CSVUtils.openFile( |
511 |
this.getCSVParameters().getFile(),
|
512 |
CSVStoreParameters.getCharset(this.getCSVParameters())
|
513 |
); |
514 |
reader = CSVUtils.getSimpleReader(getCSVParameters(), in); |
515 |
if (CSVStoreParameters.isFirstLineHeader(getCSVParameters())) {
|
516 |
reader.getHeader(); // Skip and ignore the header of file
|
517 |
} |
518 |
Coercion coercion[] = new Coercion[ftype.size()]; |
519 |
CoercionContext coercionContext[] = new CoercionContext[ftype.size()]; |
520 |
int sizes[] = new int[ftype.size()]; |
521 |
for (int i = 0; i < ftype.size(); i++) { |
522 |
sizes[i] = -1;
|
523 |
FeatureAttributeDescriptor ad = ftype.getAttributeDescriptor(i); |
524 |
coercion[i] = ad.getCoercion(); |
525 |
coercionContext[i] = ad.getCoercionContext(); |
526 |
switch (ad.getDataType().getType()) {
|
527 |
case DataTypes.INT:
|
528 |
sizes[i] = 0;
|
529 |
break;
|
530 |
case DataTypes.LONG:
|
531 |
sizes[i] = 0;
|
532 |
break;
|
533 |
case DataTypes.STRING:
|
534 |
if (ad.getSize() == 0) { |
535 |
// Es un string y no tiene un size asignado.
|
536 |
// Lo ponemos a cero para calcularlo.
|
537 |
sizes[i] = 0;
|
538 |
} |
539 |
break;
|
540 |
case DataTypes.URL:
|
541 |
case DataTypes.URI:
|
542 |
case DataTypes.FILE:
|
543 |
sizes[i] = 0;
|
544 |
} |
545 |
} |
546 |
if (ftype.getDefaultGeometryAttributeName() != null) { |
547 |
this.need_calculate_envelope = true; |
548 |
} |
549 |
|
550 |
Locale locale = CSVStoreParameters.getLocale(getCSVParameters());
|
551 |
taskStatus.message("_loading");
|
552 |
int count = 0; |
553 |
|
554 |
int count_errors = 0; |
555 |
|
556 |
List<String> row = reader.read(); |
557 |
|
558 |
int skipLines = CSVStoreParameters.getSkipLines(getCSVParameters());
|
559 |
if (skipLines > 0) { |
560 |
row = reader.skip(skipLines); |
561 |
} |
562 |
int limit = CSVStoreParameters.getLimit(getCSVParameters());
|
563 |
while (row != null) { |
564 |
taskStatus.setCurValue(++count); |
565 |
FeatureProvider feature = this.createFeatureProvider(ftype);
|
566 |
for (int i = 0; i < row.size(); i++) { |
567 |
Object rawvalue = row.get(i);
|
568 |
try {
|
569 |
Object value = null; |
570 |
if( coercion[i]!=null ) { |
571 |
value = coercion[i].coerce(rawvalue, coercionContext[i]); |
572 |
} |
573 |
feature.set(i, value); |
574 |
if (sizes[i] >= 0 |
575 |
&& (value instanceof String || value instanceof URL |
576 |
|| value instanceof URI || value instanceof File)) { |
577 |
int x = value.toString().length();
|
578 |
if (sizes[i] < x) {
|
579 |
sizes[i] = x; |
580 |
} |
581 |
} |
582 |
} catch (Exception ex) { |
583 |
if (!ignore_errors) {
|
584 |
throw ex;
|
585 |
} |
586 |
if (count_errors++ < 10) { |
587 |
LOGGER.warn("Can't load value of attribute " + i + " in row " + count + ".", ex); |
588 |
} |
589 |
if (count_errors == 10) { |
590 |
LOGGER.info("Too many errors, suppress messages.");
|
591 |
} |
592 |
} |
593 |
} |
594 |
this.addFeatureProvider(feature);
|
595 |
if (limit > 0) { |
596 |
if (limit < this.data.size()) { |
597 |
break;
|
598 |
} |
599 |
} |
600 |
row = reader.read(); |
601 |
} |
602 |
for (int i = 0; i < ftype.size(); i++) { |
603 |
if (sizes[i] > 0) { |
604 |
EditableFeatureAttributeDescriptor efad = ((EditableFeatureAttributeDescriptor) edftype.getAttributeDescriptor(i)); |
605 |
efad.setSize(sizes[i]); |
606 |
} |
607 |
} |
608 |
// Volvemos a asignar al store el featuretype, ya que puede
|
609 |
// haber cambiado.
|
610 |
ftype = edftype.getNotEditableCopy(); |
611 |
this.setFeatureType(ftype);
|
612 |
|
613 |
|
614 |
taskStatus.terminate(); |
615 |
} catch (Throwable ex) { |
616 |
int lineno = 0; |
617 |
if (reader != null) { |
618 |
lineno = reader.getLine(); |
619 |
} |
620 |
throw new RuntimeException("Problems reading file '" + getFullFileName() + "' near line " + lineno + ".", ex); |
621 |
|
622 |
} finally {
|
623 |
if (reader != null) { |
624 |
try {
|
625 |
reader.close(); |
626 |
} catch (Exception ex) { |
627 |
// Do nothing
|
628 |
} |
629 |
// reader = null;
|
630 |
} |
631 |
if (in != null) { |
632 |
try {
|
633 |
in.close(); |
634 |
} catch (Exception ex) { |
635 |
// Do nothing
|
636 |
} |
637 |
// in = null;
|
638 |
} |
639 |
} |
640 |
} |
641 |
|
642 |
@Override
|
643 |
public void fixFeatureTypeFromParameters() { |
644 |
String param_types_def = CSVStoreParameters.getRawFieldTypes(this.getParameters()); |
645 |
String[] pointDimensionNames = CSVStoreParameters.getPointDimensionNames(this.getParameters()); |
646 |
String geometry_column = CSVStoreParameters.getGeometryColumn(this.getParameters()); |
647 |
if (StringUtils.isNotBlank(param_types_def) ||
|
648 |
pointDimensionNames != null ||
|
649 |
!StringUtils.isBlank(geometry_column)) { |
650 |
this.setFeatureType(featureType);
|
651 |
} |
652 |
} |
653 |
|
654 |
private void setFeatureType(FeatureType ftype) { |
655 |
List<FeatureType> ftypes = new ArrayList<>(); |
656 |
ftypes.add(ftype); |
657 |
this.featureType = ftype;
|
658 |
this.getStoreServices().setFeatureTypes(ftypes, ftype);
|
659 |
} |
660 |
|
661 |
} |