Revision 41617 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
CSVStoreProvider.java | ||
---|---|---|
10 | 10 |
* |
11 | 11 |
* This program is distributed in the hope that it will be useful, |
12 | 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 | 14 |
* GNU General Public License for more details. |
15 | 15 |
* |
16 | 16 |
* You should have received a copy of the GNU General Public License |
17 | 17 |
* along with this program; if not, write to the Free Software |
18 | 18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
19 |
* MA 02110-1301, USA.
|
|
19 |
* MA 02110-1301, USA. |
|
20 | 20 |
* |
21 | 21 |
* For any additional information, do not hesitate to contact us |
22 | 22 |
* at info AT gvsig.com, or visit our website www.gvsig.com. |
... | ... | |
69 | 69 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorer; |
70 | 70 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorerParameters; |
71 | 71 |
import org.gvsig.fmap.dal.spi.DataStoreProviderServices; |
72 |
import org.gvsig.fmap.dal.store.csv.simplereaders.CSVReader; |
|
73 |
import org.gvsig.fmap.dal.store.csv.simplereaders.FixedLenReader; |
|
74 |
import org.gvsig.fmap.dal.store.csv.simplereaders.SimpleReader; |
|
72 | 75 |
import org.gvsig.fmap.geom.Geometry; |
73 | 76 |
import org.gvsig.fmap.geom.GeometryLocator; |
74 | 77 |
import org.gvsig.fmap.geom.GeometryManager; |
... | ... | |
83 | 86 |
import org.gvsig.tools.dataTypes.DataTypesManager.Coercion; |
84 | 87 |
import org.gvsig.tools.dataTypes.DataTypesManager.CoercionWithLocale; |
85 | 88 |
import org.gvsig.tools.dispose.DisposableIterator; |
86 |
import org.gvsig.tools.dynobject.DynObject; |
|
87 | 89 |
import org.gvsig.tools.dynobject.exception.DynFieldNotFoundException; |
88 | 90 |
import org.gvsig.tools.evaluator.AbstractEvaluator; |
89 | 91 |
import org.gvsig.tools.evaluator.EvaluatorData; |
... | ... | |
98 | 100 |
import org.gvsig.tools.visitor.Visitor; |
99 | 101 |
import org.slf4j.Logger; |
100 | 102 |
import org.slf4j.LoggerFactory; |
101 |
import org.supercsv.comment.CommentStartsWith; |
|
102 |
import org.supercsv.io.CsvListReader; |
|
103 | 103 |
import org.supercsv.io.CsvListWriter; |
104 | 104 |
import org.supercsv.prefs.CsvPreference; |
105 |
import org.supercsv.quote.QuoteMode; |
|
106 | 105 |
|
107 | 106 |
public class CSVStoreProvider extends AbstractMemoryStoreProvider implements |
108 |
ResourceConsumer { |
|
107 |
ResourceConsumer { |
|
108 |
|
|
109 | 109 |
private static final Logger logger = LoggerFactory.getLogger(CSVStoreProvider.class); |
110 | 110 |
|
111 | 111 |
public static final String NAME = "CSV"; |
... | ... | |
120 | 120 |
private boolean need_calculate_envelope = false; |
121 | 121 |
private SimpleTaskStatus taskStatus; |
122 | 122 |
|
123 |
|
|
124 | 123 |
public CSVStoreProvider(CSVStoreParameters parameters, |
125 |
DataStoreProviderServices storeServices) throws InitializeException { |
|
124 |
DataStoreProviderServices storeServices) throws InitializeException {
|
|
126 | 125 |
super( |
127 |
parameters,
|
|
128 |
storeServices, |
|
129 |
FileHelper.newMetadataContainer(METADATA_DEFINITION_NAME) |
|
126 |
parameters,
|
|
127 |
storeServices,
|
|
128 |
FileHelper.newMetadataContainer(METADATA_DEFINITION_NAME)
|
|
130 | 129 |
); |
131 | 130 |
|
132 | 131 |
TaskStatusManager manager = ToolsLocator.getTaskStatusManager(); |
... | ... | |
136 | 135 |
|
137 | 136 |
File file = getCSVParameters().getFile(); |
138 | 137 |
resource = this.createResource( |
139 |
FileResource.NAME, |
|
140 |
new Object[] { file.getAbsolutePath() }
|
|
138 |
FileResource.NAME,
|
|
139 |
new Object[]{file.getAbsolutePath()}
|
|
141 | 140 |
); |
142 | 141 |
|
143 | 142 |
resource.addConsumer(this); |
... | ... | |
157 | 156 |
} |
158 | 157 |
|
159 | 158 |
private String getFullFileName() { |
160 |
// Usar solo para mostrar mensajes en el logger.
|
|
161 |
String s = "(unknow)";
|
|
162 |
try {
|
|
163 |
s = getCSVParameters().getFile().getAbsolutePath();
|
|
164 |
} catch(Exception e2) {
|
|
165 |
s = "(unknow)";
|
|
166 |
}
|
|
167 |
return s;
|
|
159 |
// Usar solo para mostrar mensajes en el logger.
|
|
160 |
String s = "(unknow)";
|
|
161 |
try {
|
|
162 |
s = getCSVParameters().getFile().getAbsolutePath();
|
|
163 |
} catch (Exception e2) {
|
|
164 |
s = "(unknow)";
|
|
165 |
}
|
|
166 |
return s;
|
|
168 | 167 |
} |
169 |
|
|
168 |
|
|
170 | 169 |
public void open() throws OpenException { |
171 |
if (this.data != null) {
|
|
170 |
if ( this.data != null ) {
|
|
172 | 171 |
return; |
173 | 172 |
} |
174 | 173 |
this.data = new ArrayList<FeatureProvider>(); |
175 | 174 |
resource.setData(new HashMap()); |
176 | 175 |
counterNewsOIDs = 0; |
177 |
try {
|
|
178 |
loadFeatures();
|
|
179 |
} catch (RuntimeException e) {
|
|
180 |
logger.warn("Can't load features from CSV '"+getFullFileName()+"'.", e);
|
|
181 |
throw e;
|
|
182 |
} catch (Exception e) {
|
|
183 |
logger.warn("Can't load features from CSV '"+getFullFileName()+"'.", e);
|
|
184 |
throw new RuntimeException(e);
|
|
185 |
}
|
|
176 |
try {
|
|
177 |
loadFeatures();
|
|
178 |
} catch (RuntimeException e) {
|
|
179 |
logger.warn("Can't load features from CSV '" + getFullFileName() + "'.", e);
|
|
180 |
throw e;
|
|
181 |
} catch (Exception e) {
|
|
182 |
logger.warn("Can't load features from CSV '" + getFullFileName() + "'.", e);
|
|
183 |
throw new RuntimeException(e);
|
|
184 |
}
|
|
186 | 185 |
} |
187 | 186 |
|
188 | 187 |
public DataServerExplorer getExplorer() throws ReadException { |
... | ... | |
190 | 189 |
FilesystemServerExplorerParameters params; |
191 | 190 |
try { |
192 | 191 |
params = (FilesystemServerExplorerParameters) manager |
193 |
.createServerExplorerParameters(FilesystemServerExplorer.NAME); |
|
192 |
.createServerExplorerParameters(FilesystemServerExplorer.NAME);
|
|
194 | 193 |
params.setRoot(this.getCSVParameters().getFile().getParent()); |
195 |
return manager.openServerExplorer(FilesystemServerExplorer.NAME,params); |
|
194 |
return manager.openServerExplorer(FilesystemServerExplorer.NAME, params);
|
|
196 | 195 |
} catch (DataException e) { |
197 | 196 |
throw new ReadException(this.getProviderName(), e); |
198 | 197 |
} catch (ValidateDataParametersException e) { |
... | ... | |
202 | 201 |
} |
203 | 202 |
|
204 | 203 |
class Writer { |
205 |
private Envelope envelope = null; |
|
206 |
private boolean calculate_envelope = false; |
|
207 |
private CsvListWriter listWriter = null; |
|
208 |
private CsvPreference csvpreferences = null; |
|
209 |
private FileWriter fwriter = null; |
|
210 |
private FeatureType ftype; |
|
211 |
private File file; |
|
212 |
private String[] values; |
|
213 |
private FeatureAttributeDescriptor[] descriptors; |
|
214 |
private Coercion convert = null; |
|
215 |
private int errorcounts =0; |
|
216 |
private Throwable lasterror = null; |
|
217 |
private Locale locale = null; |
|
218 |
|
|
219 |
public void initialize(File file, FeatureType ftype, CsvPreference csvpreferences) { |
|
220 |
this.file = file; |
|
221 |
this.ftype = ftype; |
|
222 |
this.csvpreferences = csvpreferences; |
|
223 |
this.locale = CSVStoreParameters.getLocale(getCSVParameters()); |
|
224 |
if( csvpreferences == null ) { |
|
225 |
this.csvpreferences = CsvPreference.STANDARD_PREFERENCE; |
|
226 |
} |
|
227 |
if( ftype.getDefaultGeometryAttributeName() != null ) { |
|
228 |
this.calculate_envelope = true; |
|
229 |
} |
|
230 |
this.descriptors = this.ftype.getAttributeDescriptors(); |
|
231 |
this.convert = ToolsLocator.getDataTypesManager().getCoercion(org.gvsig.tools.dataTypes.DataTypes.STRING); |
|
232 |
this.errorcounts = 0; |
|
233 |
} |
|
234 |
|
|
235 |
public void begin() { |
|
236 |
try { |
|
237 |
this.fwriter = new FileWriter(file); |
|
238 |
} catch (IOException e) { |
|
239 |
logger.warn("Can't open file for write ("+file.getAbsolutePath()+").",e); |
|
240 |
throw new RuntimeException(e); |
|
241 |
} |
|
242 |
this.listWriter = new CsvListWriter(this.fwriter,this.csvpreferences); |
|
243 |
int n = 0; |
|
244 |
for(int i=0; i<descriptors.length; i++ ) { |
|
245 |
FeatureAttributeDescriptor descriptor = descriptors[i]; |
|
246 |
if( descriptor.getEvaluator()== null ) { |
|
247 |
n++; |
|
248 |
} |
|
249 |
} |
|
250 |
|
|
251 |
String[] header = new String[n]; |
|
252 |
this.values = new String[n]; |
|
253 |
n = 0; |
|
254 |
for(int i=0; i<descriptors.length; i++ ) { |
|
255 |
FeatureAttributeDescriptor descriptor = descriptors[i]; |
|
256 |
if( descriptor.getEvaluator()== null ) { |
|
257 |
String name = descriptor.getName(); |
|
258 |
String typeName = descriptor.getDataTypeName(); |
|
259 |
if( descriptor.getDataType().getType() == DataTypes.STRING ) { |
|
260 |
header[n++] = name + "__" + typeName + "__" + descriptor.getSize(); |
|
261 |
} else { |
|
262 |
header[n++] = name + "__" + typeName; |
|
263 |
} |
|
264 |
} |
|
265 |
} |
|
204 |
|
|
205 |
private Envelope envelope = null; |
|
206 |
private boolean calculate_envelope = false; |
|
207 |
private CsvListWriter listWriter = null; |
|
208 |
private CsvPreference csvpreferences = null; |
|
209 |
private FileWriter fwriter = null; |
|
210 |
private FeatureType ftype; |
|
211 |
private File file; |
|
212 |
private String[] values; |
|
213 |
private FeatureAttributeDescriptor[] descriptors; |
|
214 |
private Coercion convert = null; |
|
215 |
private int errorcounts = 0; |
|
216 |
private Throwable lasterror = null; |
|
217 |
private Locale locale = null; |
|
218 |
|
|
219 |
public void initialize(File file, FeatureType ftype, CsvPreference csvpreferences) { |
|
220 |
this.file = file; |
|
221 |
this.ftype = ftype; |
|
222 |
this.csvpreferences = csvpreferences; |
|
223 |
this.locale = CSVStoreParameters.getLocale(getCSVParameters()); |
|
224 |
if ( csvpreferences == null ) { |
|
225 |
this.csvpreferences = CsvPreference.STANDARD_PREFERENCE; |
|
226 |
} |
|
227 |
if ( ftype.getDefaultGeometryAttributeName() != null ) { |
|
228 |
this.calculate_envelope = true; |
|
229 |
} |
|
230 |
this.descriptors = this.ftype.getAttributeDescriptors(); |
|
231 |
this.convert = ToolsLocator.getDataTypesManager().getCoercion(org.gvsig.tools.dataTypes.DataTypes.STRING); |
|
232 |
this.errorcounts = 0; |
|
233 |
} |
|
234 |
|
|
235 |
public void begin() { |
|
266 | 236 |
try { |
267 |
listWriter.writeHeader(header); |
|
268 |
} catch (Exception e) { |
|
269 |
logger.warn("Can't write header '"+header.toString()+"' file for write ("+file.getAbsolutePath()+").",e); |
|
270 |
throw new RuntimeException(e); |
|
271 |
} |
|
272 |
} |
|
273 |
|
|
274 |
public void add(FeatureProvider feature) { |
|
275 |
if (this.calculate_envelope) { |
|
276 |
Geometry geom = feature.getDefaultGeometry(); |
|
277 |
if (geom != null) { |
|
278 |
if (envelope == null) { |
|
279 |
try { |
|
280 |
envelope = (Envelope) geom.getEnvelope().clone(); |
|
281 |
} catch (CloneNotSupportedException e) { |
|
282 |
logger.warn("Este error no deberia pasar, siempre se puede hacer un clone de un envelope.",e); |
|
283 |
} |
|
284 |
} else { |
|
285 |
envelope.add(geom.getEnvelope()); |
|
286 |
} |
|
287 |
} |
|
288 |
} |
|
289 |
int n = 0; |
|
290 |
for(int i=0; i<descriptors.length; i++ ) { |
|
291 |
FeatureAttributeDescriptor descriptor = descriptors[i]; |
|
292 |
if( descriptor.getEvaluator()== null ) { |
|
293 |
Object value = feature.get(i); |
|
294 |
try { |
|
295 |
n++; |
|
296 |
if( this.convert!=null && this.convert instanceof CoercionWithLocale ) { |
|
297 |
values[n] = (String) ((CoercionWithLocale)this.convert).coerce(value,this.locale); |
|
298 |
} else { |
|
299 |
values[n] = (String) this.convert.coerce(value); |
|
300 |
} |
|
301 |
} catch (CoercionException e) { |
|
302 |
try { |
|
303 |
values[n] = value.toString(); |
|
304 |
} catch(Exception ex) { |
|
305 |
values[n] = ""; |
|
306 |
} |
|
307 |
if( errorcounts++ <= 10 ) { |
|
308 |
this.lasterror = e; |
|
309 |
logger.warn("Can't convert value of field "+i+" to string in CVS file '"+getFullFileName()+"'.",e); |
|
310 |
if( errorcounts == 10 ) { |
|
311 |
logger.warn("Too many error writing CVS file '"+getFullFileName()+"', don't output more."); |
|
312 |
} |
|
313 |
} |
|
314 |
} |
|
315 |
} |
|
316 |
} |
|
317 |
try { |
|
318 |
this.listWriter.writeHeader(values); |
|
319 |
} catch (IOException e) { |
|
320 |
if( errorcounts++ <= 10 ) { |
|
321 |
this.lasterror = e; |
|
322 |
logger.warn("Can't write values to CVS file '"+getFullFileName()+"'.",e); |
|
323 |
if( errorcounts == 10 ) { |
|
324 |
logger.warn("Too many error writing CVS file '"+getFullFileName()+"', don't output more."); |
|
325 |
} |
|
326 |
} |
|
327 |
} |
|
328 |
|
|
329 |
} |
|
330 |
|
|
331 |
public void end() throws PerformEditingException { |
|
332 |
if( this.errorcounts>0 ) { |
|
333 |
throw new PerformEditingException(this.file.getAbsolutePath(), lasterror); |
|
334 |
} |
|
335 |
if( listWriter!=null ) { |
|
336 |
try { |
|
337 |
listWriter.close(); |
|
338 |
} catch(Exception ex) { |
|
339 |
// Ignore error |
|
340 |
} |
|
341 |
listWriter=null ; |
|
342 |
} |
|
343 |
if( fwriter!=null ) { |
|
344 |
try { |
|
345 |
fwriter.close(); |
|
346 |
} catch(Exception ex) { |
|
347 |
// Ignore error |
|
348 |
} |
|
349 |
fwriter=null ; |
|
350 |
} |
|
351 |
} |
|
352 |
|
|
353 |
public Envelope getEnvelope() { |
|
354 |
return this.envelope; |
|
355 |
} |
|
237 |
this.fwriter = new FileWriter(file); |
|
238 |
} catch (IOException e) { |
|
239 |
logger.warn("Can't open file for write (" + file.getAbsolutePath() + ").", e); |
|
240 |
throw new RuntimeException(e); |
|
241 |
} |
|
242 |
this.listWriter = new CsvListWriter(this.fwriter, this.csvpreferences); |
|
243 |
int n = 0; |
|
244 |
for ( int i = 0; i < descriptors.length; i++ ) { |
|
245 |
FeatureAttributeDescriptor descriptor = descriptors[i]; |
|
246 |
if ( descriptor.getEvaluator() == null ) { |
|
247 |
n++; |
|
248 |
} |
|
249 |
} |
|
250 |
|
|
251 |
String[] header = new String[n]; |
|
252 |
this.values = new String[n]; |
|
253 |
n = 0; |
|
254 |
for ( int i = 0; i < descriptors.length; i++ ) { |
|
255 |
FeatureAttributeDescriptor descriptor = descriptors[i]; |
|
256 |
if ( descriptor.getEvaluator() == null ) { |
|
257 |
String name = descriptor.getName(); |
|
258 |
String typeName = descriptor.getDataTypeName(); |
|
259 |
if ( descriptor.getDataType().getType() == DataTypes.STRING ) { |
|
260 |
header[n++] = name + "__" + typeName + "__" + descriptor.getSize(); |
|
261 |
} else { |
|
262 |
header[n++] = name + "__" + typeName; |
|
263 |
} |
|
264 |
} |
|
265 |
} |
|
266 |
try { |
|
267 |
listWriter.writeHeader(header); |
|
268 |
} catch (Exception e) { |
|
269 |
logger.warn("Can't write header '" + header.toString() + "' file for write (" + file.getAbsolutePath() + ").", e); |
|
270 |
throw new RuntimeException(e); |
|
271 |
} |
|
272 |
} |
|
273 |
|
|
274 |
public void add(FeatureProvider feature) { |
|
275 |
if ( this.calculate_envelope ) { |
|
276 |
Geometry geom = feature.getDefaultGeometry(); |
|
277 |
if ( geom != null ) { |
|
278 |
if ( envelope == null ) { |
|
279 |
try { |
|
280 |
envelope = (Envelope) geom.getEnvelope().clone(); |
|
281 |
} catch (CloneNotSupportedException e) { |
|
282 |
logger.warn("Este error no deberia pasar, siempre se puede hacer un clone de un envelope.", e); |
|
283 |
} |
|
284 |
} else { |
|
285 |
envelope.add(geom.getEnvelope()); |
|
286 |
} |
|
287 |
} |
|
288 |
} |
|
289 |
int n = 0; |
|
290 |
for ( int i = 0; i < descriptors.length; i++ ) { |
|
291 |
FeatureAttributeDescriptor descriptor = descriptors[i]; |
|
292 |
if ( descriptor.getEvaluator() == null ) { |
|
293 |
Object value = feature.get(i); |
|
294 |
try { |
|
295 |
n++; |
|
296 |
if ( this.convert != null && this.convert instanceof CoercionWithLocale ) { |
|
297 |
values[n] = (String) ((CoercionWithLocale) this.convert).coerce(value, this.locale); |
|
298 |
} else { |
|
299 |
values[n] = (String) this.convert.coerce(value); |
|
300 |
} |
|
301 |
} catch (CoercionException e) { |
|
302 |
try { |
|
303 |
values[n] = value.toString(); |
|
304 |
} catch (Exception ex) { |
|
305 |
values[n] = ""; |
|
306 |
} |
|
307 |
if ( errorcounts++ <= 10 ) { |
|
308 |
this.lasterror = e; |
|
309 |
logger.warn("Can't convert value of field " + i + " to string in CVS file '" + getFullFileName() + "'.", e); |
|
310 |
if ( errorcounts == 10 ) { |
|
311 |
logger.warn("Too many error writing CVS file '" + getFullFileName() + "', don't output more."); |
|
312 |
} |
|
313 |
} |
|
314 |
} |
|
315 |
} |
|
316 |
} |
|
317 |
try { |
|
318 |
this.listWriter.writeHeader(values); |
|
319 |
} catch (IOException e) { |
|
320 |
if ( errorcounts++ <= 10 ) { |
|
321 |
this.lasterror = e; |
|
322 |
logger.warn("Can't write values to CVS file '" + getFullFileName() + "'.", e); |
|
323 |
if ( errorcounts == 10 ) { |
|
324 |
logger.warn("Too many error writing CVS file '" + getFullFileName() + "', don't output more."); |
|
325 |
} |
|
326 |
} |
|
327 |
} |
|
328 |
|
|
329 |
} |
|
330 |
|
|
331 |
public void end() throws PerformEditingException { |
|
332 |
if ( this.errorcounts > 0 ) { |
|
333 |
throw new PerformEditingException(this.file.getAbsolutePath(), lasterror); |
|
334 |
} |
|
335 |
if ( listWriter != null ) { |
|
336 |
try { |
|
337 |
listWriter.close(); |
|
338 |
} catch (Exception ex) { |
|
339 |
// Ignore error |
|
340 |
} |
|
341 |
listWriter = null; |
|
342 |
} |
|
343 |
if ( fwriter != null ) { |
|
344 |
try { |
|
345 |
fwriter.close(); |
|
346 |
} catch (Exception ex) { |
|
347 |
// Ignore error |
|
348 |
} |
|
349 |
fwriter = null; |
|
350 |
} |
|
351 |
} |
|
352 |
|
|
353 |
public Envelope getEnvelope() { |
|
354 |
return this.envelope; |
|
355 |
} |
|
356 | 356 |
} |
357 | 357 |
|
358 | 358 |
public void performChanges(Iterator deleteds, Iterator inserteds, Iterator updateds, Iterator originalFeatureTypesUpdated) throws PerformEditingException { |
... | ... | |
366 | 366 |
DisposableIterator it = null; |
367 | 367 |
try { |
368 | 368 |
File file = (File) resource.get(); |
369 |
|
|
369 |
|
|
370 | 370 |
Writer writer = new Writer(); |
371 |
writer.initialize(file,getStoreServices().getDefaultFeatureType(),getCSVPreferences());
|
|
372 |
features =
|
|
373 |
getStoreServices().getFeatureStore() |
|
374 |
.getFeatureSet(); |
|
371 |
writer.initialize(file, getStoreServices().getDefaultFeatureType(), getCSVPreferences());
|
|
372 |
features |
|
373 |
= getStoreServices().getFeatureStore()
|
|
374 |
.getFeatureSet();
|
|
375 | 375 |
List<FeatureProvider> newdata = new ArrayList<FeatureProvider>(); |
376 | 376 |
writer.begin(); |
377 | 377 |
it = features.fastIterator(); |
378 |
taskStatus.setRangeOfValues(0,0); |
|
379 |
long counter=0;
|
|
380 |
while (it.hasNext()) {
|
|
378 |
taskStatus.setRangeOfValues(0, 0);
|
|
379 |
long counter = 0;
|
|
380 |
while ( it.hasNext() ) {
|
|
381 | 381 |
taskStatus.setCurValue(counter++); |
382 | 382 |
FeatureProvider feature = getStoreServices().getFeatureProviderFromFeature( |
383 |
(org.gvsig.fmap.dal.feature.Feature) it.next()); |
|
383 |
(org.gvsig.fmap.dal.feature.Feature) it.next());
|
|
384 | 384 |
writer.add(feature); |
385 |
if (feature.getOID() == null){
|
|
385 |
if ( feature.getOID() == null ) {
|
|
386 | 386 |
logger.warn("feature without OID"); |
387 | 387 |
feature.setOID(createNewOID()); |
388 | 388 |
} |
389 | 389 |
newdata.add(feature); |
390 | 390 |
} |
391 | 391 |
data = newdata; |
392 |
if (writer.getEnvelope() != null){
|
|
392 |
if ( writer.getEnvelope() != null ) {
|
|
393 | 393 |
envelope = writer.getEnvelope().getGeometry().getEnvelope(); |
394 | 394 |
} |
395 | 395 |
resource.notifyChanges(); |
396 | 396 |
writer.end(); |
397 | 397 |
} finally { |
398 |
if (it != null) {
|
|
398 |
if ( it != null ) {
|
|
399 | 399 |
it.dispose(); |
400 | 400 |
} |
401 |
if (features != null) {
|
|
401 |
if ( features != null ) {
|
|
402 | 402 |
features.dispose(); |
403 | 403 |
} |
404 | 404 |
} |
405 | 405 |
return null; |
406 | 406 |
} |
407 |
|
|
408 |
private CsvPreference getCSVPreferences() { |
|
409 |
CSVReader reader = new CSVReader(getCSVParameters()); |
|
410 |
return reader.getCSVPreferences(); |
|
411 |
} |
|
412 |
|
|
407 | 413 |
}); |
408 | 414 |
this.taskStatus.terminate(); |
409 | 415 |
} catch (Exception e) { |
... | ... | |
427 | 433 |
} |
428 | 434 |
|
429 | 435 |
public void append(FeatureProvider featureProvider) { |
430 |
throw new UnsupportedOperationException();
|
|
436 |
throw new UnsupportedOperationException();
|
|
431 | 437 |
} |
432 | 438 |
|
433 | 439 |
public void beginAppend() { |
434 |
throw new UnsupportedOperationException();
|
|
440 |
throw new UnsupportedOperationException();
|
|
435 | 441 |
} |
436 | 442 |
|
437 | 443 |
public void endAppend() { |
438 |
throw new UnsupportedOperationException();
|
|
444 |
throw new UnsupportedOperationException();
|
|
439 | 445 |
} |
440 | 446 |
|
441 | 447 |
public void saveToState(PersistentState state) throws PersistenceException { |
... | ... | |
460 | 466 |
|
461 | 467 |
public Envelope getEnvelope() throws DataException { |
462 | 468 |
this.open(); |
463 |
if( this.envelope!= null ) {
|
|
464 |
return this.envelope;
|
|
469 |
if ( this.envelope != null ) {
|
|
470 |
return this.envelope;
|
|
465 | 471 |
} |
466 |
if( !this.need_calculate_envelope ) { |
|
467 |
return null;
|
|
472 |
if ( !this.need_calculate_envelope ) {
|
|
473 |
return null;
|
|
468 | 474 |
} |
469 | 475 |
FeatureStore fs = this.getFeatureStore(); |
470 | 476 |
FeatureType ft = fs.getDefaultFeatureType(); |
... | ... | |
472 | 478 |
|
473 | 479 |
try { |
474 | 480 |
this.envelope = GeometryLocator.getGeometryManager().createEnvelope(fad.getGeomType().getSubType()); |
475 |
fs.accept(new Visitor() {
|
|
476 |
public void visit(Object obj) throws VisitCanceledException, BaseException {
|
|
477 |
Feature f = (Feature) obj;
|
|
478 |
Geometry geom = f.getDefaultGeometry();
|
|
479 |
if( geom!=null ) {
|
|
480 |
envelope.add(geom.getEnvelope());
|
|
481 |
}
|
|
482 |
}
|
|
483 |
});
|
|
484 |
} catch (BaseException e) {
|
|
485 |
logger.warn("Can't calculate the envelope of CSV file '"+this.getFullName()+"'.",e);
|
|
486 |
this.envelope = null;
|
|
487 |
}
|
|
488 |
|
|
481 |
fs.accept(new Visitor() {
|
|
482 |
public void visit(Object obj) throws VisitCanceledException, BaseException {
|
|
483 |
Feature f = (Feature) obj;
|
|
484 |
Geometry geom = f.getDefaultGeometry();
|
|
485 |
if ( geom != null ) {
|
|
486 |
envelope.add(geom.getEnvelope()); |
|
487 |
} |
|
488 |
}
|
|
489 |
});
|
|
490 |
} catch (BaseException e) {
|
|
491 |
logger.warn("Can't calculate the envelope of CSV file '" + this.getFullName() + "'.", e);
|
|
492 |
this.envelope = null;
|
|
493 |
}
|
|
494 |
|
|
489 | 495 |
this.need_calculate_envelope = false; |
490 | 496 |
return this.envelope; |
491 | 497 |
} |
492 | 498 |
|
493 | 499 |
public Object getDynValue(String name) throws DynFieldNotFoundException { |
494 |
if( DataStore.METADATA_ENVELOPE.equalsIgnoreCase(name) ) { |
|
500 |
if ( DataStore.METADATA_ENVELOPE.equalsIgnoreCase(name) ) {
|
|
495 | 501 |
try { |
496 | 502 |
return this.getEnvelope(); |
497 | 503 |
} catch (DataException e) { |
498 | 504 |
return null; |
499 | 505 |
} |
500 | 506 |
} else { |
501 |
if( DataStore.METADATA_CRS.equalsIgnoreCase(name) ) { |
|
507 |
if ( DataStore.METADATA_CRS.equalsIgnoreCase(name) ) {
|
|
502 | 508 |
IProjection pro = CSVStoreParameters.getCRS(this.getCSVParameters()); |
503 |
if (pro != null){
|
|
509 |
if ( pro != null ) {
|
|
504 | 510 |
return pro; |
505 | 511 |
} |
506 | 512 |
} |
... | ... | |
508 | 514 |
return super.getDynValue(name); |
509 | 515 |
} |
510 | 516 |
|
511 |
|
|
512 | 517 |
public void resourceChanged(ResourceProvider resource) { |
513 | 518 |
this.getStoreServices().notifyChange( |
514 |
DataStoreNotification.RESOURCE_CHANGED, |
|
515 |
resource); |
|
519 |
DataStoreNotification.RESOURCE_CHANGED,
|
|
520 |
resource);
|
|
516 | 521 |
} |
517 | 522 |
|
518 |
|
|
519 | 523 |
public Object getSourceId() { |
520 | 524 |
return this.getCSVParameters().getFile(); |
521 | 525 |
} |
... | ... | |
534 | 538 |
} |
535 | 539 |
|
536 | 540 |
private boolean isEmpty(String s) { |
537 |
if( s==null ) {
|
|
538 |
return true;
|
|
539 |
}
|
|
540 |
return s.trim().length()==0;
|
|
541 |
if ( s == null ) {
|
|
542 |
return true;
|
|
543 |
}
|
|
544 |
return s.trim().length() == 0;
|
|
541 | 545 |
} |
542 |
|
|
543 |
private CsvPreference getCSVPreferences() { |
|
544 |
try { |
|
545 |
String s = null; |
|
546 |
char quoteChar; |
|
547 |
int delimiterChar; |
|
548 |
String endOfLineSymbols; |
|
549 | 546 |
|
550 |
DynObject params = this.getParameters(); |
|
551 |
|
|
552 |
CsvPreference.Builder builder = null; |
|
553 |
|
|
554 |
CsvPreference defaultPreference = CSVStoreParameters |
|
555 |
.getPredefinedCSVPreferences(params); |
|
556 |
if (defaultPreference == null) { |
|
557 |
defaultPreference = CsvPreference.STANDARD_PREFERENCE; |
|
558 |
} |
|
559 |
|
|
560 |
endOfLineSymbols = CSVStoreParameters.getRecordSeparator(params); |
|
561 |
if (isEmpty(endOfLineSymbols)) { |
|
562 |
endOfLineSymbols = defaultPreference.getEndOfLineSymbols(); |
|
563 |
} |
|
564 |
s = CSVStoreParameters.getQuoteCharacter(params); |
|
565 |
if (isEmpty(s)) { |
|
566 |
quoteChar = (char) defaultPreference.getQuoteChar(); |
|
567 |
} else { |
|
568 |
quoteChar = s.charAt(0); |
|
569 |
} |
|
570 |
s = CSVStoreParameters.getDelimiter(params); |
|
571 |
if (isEmpty(s)) { |
|
572 |
delimiterChar = defaultPreference.getDelimiterChar(); |
|
573 |
} else { |
|
574 |
delimiterChar = s.charAt(0); |
|
575 |
} |
|
576 |
|
|
577 |
builder = new CsvPreference.Builder(quoteChar, delimiterChar, |
|
578 |
endOfLineSymbols); |
|
579 |
|
|
580 |
s = CSVStoreParameters.getCommentStartMarker(params); |
|
581 |
if (!isEmpty(s)) { |
|
582 |
CommentStartsWith cs = new CommentStartsWith(s); |
|
583 |
builder.skipComments(cs); |
|
584 |
} |
|
585 |
|
|
586 |
builder.surroundingSpacesNeedQuotes(CSVStoreParameters |
|
587 |
.getSurroundingSpacesNeedQuotes(params)); |
|
588 |
QuoteMode quoteMode = CSVStoreParameters.getQuoteMode(params); |
|
589 |
if( quoteMode != null ) { |
|
590 |
builder.useQuoteMode(quoteMode); |
|
591 |
} |
|
592 |
return builder.build(); |
|
593 |
} catch (Exception e) { |
|
594 |
logger.warn("Can't make preferences for CSV '" + getFullFileName() |
|
595 |
+ "'.", e); |
|
596 |
return null; |
|
597 |
} |
|
598 |
} |
|
599 |
|
|
600 | 547 |
private class FieldTypeParser { |
548 |
|
|
601 | 549 |
public String name = null; |
602 | 550 |
public int type = DataTypes.STRING; |
603 | 551 |
public int size = 0; |
604 | 552 |
public boolean allowNulls = true; |
605 | 553 |
|
606 | 554 |
private String typename = "string"; |
607 |
|
|
555 |
|
|
608 | 556 |
FieldTypeParser() { |
609 | 557 |
} |
610 |
|
|
558 |
|
|
611 | 559 |
private int getType(String value) { |
612 |
DataTypesManager dataTypesManager = ToolsLocator.getDataTypesManager();
|
|
560 |
DataTypesManager dataTypesManager = ToolsLocator.getDataTypesManager(); |
|
613 | 561 |
return dataTypesManager.getType(typename); |
614 | 562 |
} |
615 |
|
|
563 |
|
|
616 | 564 |
// El formato seria: |
617 | 565 |
// name[:typename[:size[:notnull|null]]] |
618 | 566 |
// name[__typename[__size[__notnull|null]]] |
... | ... | |
620 | 568 |
public boolean parse(String value) { |
621 | 569 |
String typename = null; |
622 | 570 |
String[] ss = null; |
623 |
if( value.contains(":") ) { |
|
571 |
if ( value.contains(":") ) {
|
|
624 | 572 |
ss = value.split(":"); |
625 |
} else if( value.contains("__") ) { |
|
573 |
} else if ( value.contains("__") ) {
|
|
626 | 574 |
ss = value.split("__"); |
627 | 575 |
} |
628 |
if( ss==null ) {
|
|
576 |
if ( ss == null ) {
|
|
629 | 577 |
this.name = value; |
630 | 578 |
return true; |
631 | 579 |
} |
632 |
switch(ss.length) { |
|
580 |
switch (ss.length) {
|
|
633 | 581 |
case 4: |
634 |
if(ss[3].length()>0) {
|
|
635 |
if( "notnull".equalsIgnoreCase(ss[3]) ) { |
|
582 |
if ( ss[3].length() > 0 ) {
|
|
583 |
if ( "notnull".equalsIgnoreCase(ss[3]) ) {
|
|
636 | 584 |
this.allowNulls = false; |
637 | 585 |
} else { |
638 | 586 |
this.allowNulls = true; |
639 | 587 |
} |
640 | 588 |
} |
641 | 589 |
case 3: |
642 |
if(ss[2].length()>0) {
|
|
590 |
if ( ss[2].length() > 0 ) {
|
|
643 | 591 |
try { |
644 | 592 |
this.size = Integer.parseInt(ss[2]); |
645 |
} catch(Exception ex) { |
|
646 |
logger.warn("Ignore incorrect field size for field "+value+" in CSV header of '"+getFullFileName()+"'.",ex);
|
|
593 |
} catch (Exception ex) {
|
|
594 |
logger.warn("Ignore incorrect field size for field " + value + " in CSV header of '" + getFullFileName() + "'.", ex);
|
|
647 | 595 |
} |
648 | 596 |
} |
649 | 597 |
case 2: |
650 |
if(ss[1].length()>0) {
|
|
598 |
if ( ss[1].length() > 0 ) {
|
|
651 | 599 |
this.typename = ss[1]; |
652 |
this.type = this.getType(this.typename);
|
|
653 |
if( this.type==DataTypes.INVALID ) {
|
|
600 |
this.type = this.getType(this.typename); |
|
601 |
if ( this.type == DataTypes.INVALID ) {
|
|
654 | 602 |
this.type = DataTypes.STRING; |
655 |
logger.info("Type '"+typename+"' not valid for attribute '"+value+"' in CSV file '"+getFullFileName()+"'.");
|
|
603 |
logger.info("Type '" + typename + "' not valid for attribute '" + value + "' in CSV file '" + getFullFileName() + "'.");
|
|
656 | 604 |
} |
657 | 605 |
} |
658 | 606 |
case 1: |
659 | 607 |
this.name = ss[0]; |
660 | 608 |
break; |
661 | 609 |
} |
662 |
|
|
663 |
if( this.type != DataTypes.STRING ) { |
|
610 |
|
|
611 |
if ( this.type != DataTypes.STRING ) {
|
|
664 | 612 |
this.size = 0; |
665 | 613 |
} |
666 | 614 |
return true; |
667 | 615 |
} |
668 |
|
|
616 |
|
|
669 | 617 |
} |
670 |
|
|
618 |
|
|
671 | 619 |
private EditableFeatureType getFeatureType(String headers[], int automaticTypes[]) { |
672 | 620 |
EditableFeatureType fType = getStoreServices().createFeatureType(this.getName()); |
673 | 621 |
fType.setHasOID(true); |
674 | 622 |
DataTypesManager dataTypesManager = ToolsLocator.getDataTypesManager(); |
675 | 623 |
|
676 | 624 |
FieldTypeParser[] fieldTypes = new FieldTypeParser[headers.length]; |
677 |
//
|
|
625 |
//
|
|
678 | 626 |
// Calculamos cuales pueden ser los tipos de datos |
679 | 627 |
// |
680 | 628 |
for ( int i = 0; i < fieldTypes.length; i++ ) { |
681 | 629 |
fieldTypes[i] = new FieldTypeParser(); |
682 | 630 |
} |
683 | 631 |
|
684 |
// Asuminos los tipos pasados por parametro, que se supone
|
|
632 |
// Asuminos los tipos pasados por parametro, que se supone
|
|
685 | 633 |
// son los detectados automaticamente. |
686 | 634 |
if ( automaticTypes != null ) { |
687 | 635 |
for ( int i = 0; i < fieldTypes.length && i < automaticTypes.length; i++ ) { |
... | ... | |
697 | 645 |
|
698 | 646 |
} |
699 | 647 |
|
700 |
// Y por ultimo hacemos caso a lo que se haya especificado en los parametros
|
|
648 |
// Y por ultimo hacemos caso a lo que se haya especificado en los parametros
|
|
701 | 649 |
// de apertura del CSV, teniendo esto prioridad sobre todo. |
702 | 650 |
int[] param_types = CSVStoreParameters.getFieldTypes(this.getParameters()); |
703 | 651 |
if ( param_types != null ) { |
... | ... | |
714 | 662 |
} |
715 | 663 |
} |
716 | 664 |
} |
717 |
//
|
|
665 |
//
|
|
718 | 666 |
// Una vez ya sabemos los tipos de datos rellenamos el feature-type |
719 | 667 |
// |
720 | 668 |
for ( int i = 0; i < fieldTypes.length; i++ ) { |
... | ... | |
745 | 693 |
} |
746 | 694 |
return fType; |
747 | 695 |
} |
748 |
|
|
696 |
|
|
749 | 697 |
static class PointAttributeEmulator implements FeatureAttributeEmulator { |
750 | 698 |
|
751 | 699 |
private static final Logger logger = LoggerFactory.getLogger(ToPointEvaluaror.class); |
... | ... | |
753 | 701 |
private static final int XNAME = 0; |
754 | 702 |
private static final int YNAME = 1; |
755 | 703 |
private static final int ZNAME = 2; |
756 |
|
|
704 |
|
|
757 | 705 |
private final GeometryManager geommgr; |
758 | 706 |
private final String[] fieldNames; |
759 | 707 |
private final Coercion toDouble; |
... | ... | |
775 | 723 |
this.toDouble = datatypeManager.getCoercion(DataTypes.DOUBLE); |
776 | 724 |
this.dataType = datatypeManager.get(DataTypes.GEOMETRY); |
777 | 725 |
} |
778 |
|
|
726 |
|
|
779 | 727 |
public Object get(Feature feature) { |
780 | 728 |
try { |
781 | 729 |
Object valueX = feature.get(this.fieldNames[XNAME]); |
782 | 730 |
valueX = toDouble.coerce(valueX); |
783 |
if( valueX == null ) { |
|
731 |
if ( valueX == null ) {
|
|
784 | 732 |
return null; |
785 | 733 |
} |
786 | 734 |
Object valueY = feature.get(this.fieldNames[YNAME]); |
787 | 735 |
valueY = toDouble.coerce(valueY); |
788 |
if( valueY == null ) { |
|
736 |
if ( valueY == null ) {
|
|
789 | 737 |
return null; |
790 | 738 |
} |
791 | 739 |
Object valueZ = null; |
792 |
if( this.fieldNames.length>2 ) {
|
|
740 |
if ( this.fieldNames.length > 2 ) {
|
|
793 | 741 |
valueZ = toDouble.coerce(feature.get(this.fieldNames[ZNAME])); |
794 |
if( valueZ == null ) { |
|
742 |
if ( valueZ == null ) {
|
|
795 | 743 |
return null; |
796 | 744 |
} |
797 | 745 |
} |
798 |
|
|
799 |
double x = ((Double)valueX).doubleValue(); |
|
800 |
double y = ((Double)valueY).doubleValue(); |
|
746 |
|
|
747 |
double x = ((Double) valueX).doubleValue();
|
|
748 |
double y = ((Double) valueY).doubleValue();
|
|
801 | 749 |
Point point = geommgr.createPoint(x, y, Geometry.SUBTYPES.GEOM3D); |
802 |
if( this.fieldNames.length>2 ) {
|
|
803 |
double z = ((Double)valueZ).doubleValue(); |
|
750 |
if ( this.fieldNames.length > 2 ) {
|
|
751 |
double z = ((Double) valueZ).doubleValue();
|
|
804 | 752 |
point.setCoordinateAt(2, z); |
805 | 753 |
} |
806 | 754 |
return point; |
807 | 755 |
} catch (Exception ex) { |
808 | 756 |
if ( ++errorcount < 5 ) { |
809 | 757 |
logger.warn("[" + errorcount + "] Can't create point in CSV provider. XNAME='" |
810 |
+ this.fieldNames[XNAME] + "', YNAME='" + this.fieldNames[XNAME] + "' feature=" + feature.toString(),ex); |
|
758 |
+ this.fieldNames[XNAME] + "', YNAME='" + this.fieldNames[XNAME] + "' feature=" + feature.toString(), ex);
|
|
811 | 759 |
} |
812 | 760 |
return null; |
813 | 761 |
} |
814 | 762 |
} |
815 | 763 |
|
816 | 764 |
public void set(EditableFeature feature, Object value) { |
817 |
if( value == null ) { |
|
765 |
if ( value == null ) {
|
|
818 | 766 |
return; |
819 | 767 |
} |
820 | 768 |
Point point = null; |
821 |
if( value instanceof MultiPoint ) { |
|
822 |
point = (Point) ((MultiPoint)value).getPrimitiveAt(0); |
|
769 |
if ( value instanceof MultiPoint ) {
|
|
770 |
point = (Point) ((MultiPoint) value).getPrimitiveAt(0);
|
|
823 | 771 |
} else { |
824 | 772 |
point = (Point) value; |
825 | 773 |
} |
826 | 774 |
feature.set(this.fieldNames[XNAME], point.getX()); |
827 | 775 |
feature.set(this.fieldNames[YNAME], point.getY()); |
828 |
if( this.fieldNames.length>2 ) {
|
|
776 |
if ( this.fieldNames.length > 2 ) {
|
|
829 | 777 |
feature.set(this.fieldNames[ZNAME], point.getCoordinateAt(2)); |
830 | 778 |
} |
831 | 779 |
} |
... | ... | |
837 | 785 |
public String[] getRequiredFieldNames() { |
838 | 786 |
return this.fieldNames; |
839 | 787 |
} |
840 |
|
|
841 |
|
|
788 |
|
|
842 | 789 |
} |
843 |
|
|
790 |
|
|
844 | 791 |
static class ToPointEvaluaror extends AbstractEvaluator { |
845 | 792 |
|
846 | 793 |
private static final Logger logger = LoggerFactory.getLogger(ToPointEvaluaror.class); |
... | ... | |
887 | 834 |
|
888 | 835 |
} |
889 | 836 |
|
890 |
private void loadFeatures() throws IOException, DataException, |
|
891 |
CoercionException, CloneNotSupportedException { |
|
892 |
// |
|
893 |
// http://supercsv.sourceforge.net/examples_reading.html |
|
894 |
// http://supercsv.sourceforge.net/apidocs/index.html |
|
895 |
// |
|
896 |
FileReader in = null; |
|
897 |
CsvListReader reader = null; |
|
898 |
try { |
|
899 |
String headers[] = null; |
|
900 |
FeatureStoreProviderServices store = this.getStoreServices(); |
|
837 |
private SimpleReader getSimpleReader(FileReader in) { |
|
838 |
SimpleReader reader; |
|
839 |
if ( CSVStoreParameters.getRawFieldsDefinition(getCSVParameters()) != null ) { |
|
840 |
reader = new FixedLenReader(in, getCSVParameters()); |
|
841 |
} else { |
|
842 |
reader = new CSVReader(in, getCSVParameters()); |
|
843 |
} |
|
844 |
return reader; |
|
845 |
} |
|
901 | 846 |
|
902 |
boolean ignore_errors = CSVStoreParameters.getIgnoreErrors(getCSVParameters()); |
|
903 |
|
|
904 |
// Initiaize the CSV parser |
|
905 |
CsvPreference preferences = getCSVPreferences(); |
|
906 |
in = new FileReader(this.getCSVParameters().getFile()); |
|
847 |
private void loadFeatures() throws IOException, DataException, |
|
848 |
CoercionException, CloneNotSupportedException { |
|
849 |
FileReader in = null; |
|
850 |
SimpleReader reader = null; |
|
851 |
try { |
|
852 |
String headers[] = null; |
|
853 |
FeatureStoreProviderServices store = this.getStoreServices(); |
|
907 | 854 |
|
908 |
reader = new CsvListReader(in, preferences); |
|
909 |
headers = CSVStoreParameters.getHeaders(getCSVParameters()); |
|
910 |
if (headers == null) { |
|
911 |
headers = reader.getHeader(true); |
|
912 |
if (headers == null) { |
|
913 |
String msg = "Can't retrieve header from csv file '" |
|
914 |
+ this.getCSVParameters().getFile() |
|
915 |
.getAbsolutePath() |
|
916 |
+ "' and not specified in the parameters."; |
|
917 |
logger.warn(msg); |
|
918 |
throw new RuntimeException(msg); |
|
919 |
} |
|
920 |
} |
|
855 |
boolean ignore_errors = CSVStoreParameters.getIgnoreErrors(getCSVParameters()); |
|
856 |
|
|
857 |
in = new FileReader(this.getCSVParameters().getFile()); |
|
921 | 858 |
|
922 |
// Initialize the feature types |
|
923 |
EditableFeatureType edftype = this.getFeatureType(headers, automaticDetectionOfTypes()); |
|
924 |
FeatureType ftype = edftype.getNotEditableCopy(); |
|
925 |
List<FeatureType> ftypes = new ArrayList<FeatureType>(); |
|
926 |
ftypes.add(ftype); |
|
927 |
store.setFeatureTypes(ftypes, ftype); |
|
859 |
reader = getSimpleReader(in); |
|
928 | 860 |
|
929 |
Coercion coercion[] = new Coercion[ftype.size()]; |
|
930 |
int sizes[] = new int[ftype.size()]; |
|
931 |
for (int i = 0; i < ftype.size(); i++) { |
|
932 |
sizes[i] = -1; |
|
933 |
FeatureAttributeDescriptor ad = ftype.getAttributeDescriptor(i); |
|
934 |
coercion[i] = ad.getDataType().getCoercion(); |
|
935 |
if( ad.getDataType().getType() == DataTypes.STRING ) { |
|
936 |
if( ad.getSize() == 0 ) { |
|
937 |
// Es un string y no tiene un size asignado. |
|
938 |
// Lo ponemos a cero para calcularlo. |
|
939 |
sizes[i] = 0; |
|
940 |
} |
|
941 |
} |
|
942 |
} |
|
943 |
if( ftype.getDefaultGeometryAttributeName() != null) { |
|
944 |
this.need_calculate_envelope = true; |
|
945 |
} |
|
946 |
|
|
947 |
Locale locale = CSVStoreParameters.getLocale(getCSVParameters()); |
|
948 |
taskStatus.message("_loading"); |
|
949 |
int count = 0; |
|
861 |
headers = CSVStoreParameters.getHeaders(getCSVParameters()); |
|
862 |
if ( headers == null ) { |
|
863 |
headers = reader.getHeader(); |
|
864 |
if ( headers == null ) { |
|
865 |
String msg = "Can't retrieve header from csv file '" |
|
866 |
+ this.getCSVParameters().getFile() |
|
867 |
.getAbsolutePath() |
|
868 |
+ "' and not specified in the parameters."; |
|
869 |
logger.warn(msg); |
|
870 |
throw new RuntimeException(msg); |
|
871 |
} |
|
872 |
} |
|
950 | 873 |
|
951 |
int count_errors = 0; |
|
952 |
List<String> row = reader.read(); |
|
953 |
|
|
954 |
while (row != null) { |
|
955 |
taskStatus.setCurValue(++count); |
|
956 |
FeatureProvider feature = this.createFeatureProvider(ftype); |
|
957 |
for (int i = 0; i < row.size(); i++) { |
|
958 |
Object rawvalue = row.get(i); |
|
959 |
try { |
|
960 |
Object value = null; |
|
961 |
if( locale != null && coercion[i] instanceof CoercionWithLocale ) { |
|
962 |
value = ((CoercionWithLocale)(coercion[i])).coerce(rawvalue, locale); |
|
963 |
} else { |
|
964 |
value = coercion[i].coerce(rawvalue); |
|
965 |
} |
|
966 |
feature.set(i, value); |
|
967 |
if (sizes[i] >= 0 && value != null) { |
|
968 |
int x = ((String) value).length(); |
|
969 |
if (sizes[i] < x) { |
|
970 |
sizes[i] = x; |
|
971 |
} |
|
972 |
} |
|
973 |
} catch (RuntimeException ex) { |
|
974 |
if (!ignore_errors) { |
|
975 |
throw ex; |
|
976 |
} |
|
977 |
if( count_errors++ <10 ) { |
|
978 |
logger.warn("Can't load value of attribute "+i+" in row "+count+".",ex); |
|
979 |
} |
|
980 |
if( count_errors==10 ) { |
|
981 |
logger.info("Too many errors, suppress messages."); |
|
982 |
} |
|
983 |
} |
|
984 |
} |
|
985 |
this.addFeatureProvider(feature); |
|
986 |
row = reader.read(); |
|
987 |
} |
|
988 |
for (int i = 0; i < ftype.size(); i++) { |
|
989 |
if( sizes[i]>0 ) { |
|
990 |
EditableFeatureAttributeDescriptor efad = ((EditableFeatureAttributeDescriptor)edftype.getAttributeDescriptor(i)); |
|
991 |
efad.setSize(sizes[i]); |
|
992 |
} |
|
993 |
} |
|
994 |
// Volvemos a asignar al store el featuretype, ya que puede |
|
995 |
// haber cambiado. |
|
996 |
ftype = edftype.getNotEditableCopy(); |
|
997 |
ftypes = new ArrayList<FeatureType>(); |
|
998 |
ftypes.add(ftype); |
|
999 |
store.setFeatureTypes(ftypes, ftype); |
|
874 |
// Initialize the feature types |
|
875 |
EditableFeatureType edftype = this.getFeatureType(headers, automaticDetectionOfTypes()); |
|
876 |
FeatureType ftype = edftype.getNotEditableCopy(); |
|
877 |
List<FeatureType> ftypes = new ArrayList<FeatureType>(); |
|
878 |
ftypes.add(ftype); |
|
879 |
store.setFeatureTypes(ftypes, ftype); |
|
1000 | 880 |
|
1001 |
taskStatus.terminate(); |
|
1002 |
} finally { |
|
1003 |
if( reader != null ) { |
|
1004 |
try { |
|
1005 |
reader.close(); |
|
1006 |
} catch(Exception ex) { |
|
1007 |
// Do nothing |
|
1008 |
} |
|
1009 |
reader = null; |
|
1010 |
} |
|
1011 |
if( in != null ) { |
|
1012 |
try { |
|
1013 |
in.close(); |
|
1014 |
} catch(Exception ex) { |
|
1015 |
// Do nothing |
|
1016 |
} |
|
1017 |
in = null; |
|
1018 |
} |
|
1019 |
} |
|
1020 |
} |
|
1021 |
|
|
1022 |
private int[] automaticDetectionOfTypes() throws IOException { |
|
1023 |
boolean automatic_types_detection = CSVStoreParameters.getAutomaticTypesDetection(getCSVParameters()); |
|
1024 |
if( !automatic_types_detection ) { |
|
1025 |
return null; |
|
1026 |
} |
|
1027 |
|
|
1028 |
final int T_INT = 0; |
|
1029 |
final int T_FLOAT = 1; |
|
1030 |
final int T_DOUBLE = 2; |
|
1031 |
final int T_LONG = 3; |
|
1032 |
final int T_URL = 4; |
|
1033 |
final int T_DATE = 5; |
|
1034 |
boolean possibleDataTypes[][] = null; |
|
1035 |
Locale locale = null; |
|
1036 |
int[] types = null; |
|
881 |
Coercion coercion[] = new Coercion[ftype.size()]; |
|
882 |
int sizes[] = new int[ftype.size()]; |
|
883 |
for ( int i = 0; i < ftype.size(); i++ ) { |
|
884 |
sizes[i] = -1; |
|
885 |
FeatureAttributeDescriptor ad = ftype.getAttributeDescriptor(i); |
|
886 |
coercion[i] = ad.getDataType().getCoercion(); |
|
887 |
if ( ad.getDataType().getType() == DataTypes.STRING ) { |
|
888 |
if ( ad.getSize() == 0 ) { |
|
889 |
// Es un string y no tiene un size asignado. |
|
890 |
// Lo ponemos a cero para calcularlo. |
|
891 |
sizes[i] = 0; |
|
892 |
} |
|
893 |
} |
|
894 |
} |
|
895 |
if ( ftype.getDefaultGeometryAttributeName() != null ) { |
|
896 |
this.need_calculate_envelope = true; |
|
897 |
} |
|
1037 | 898 |
|
1038 |
FileReader in = null; |
|
1039 |
CsvListReader reader = null; |
|
1040 |
String headers[] = null; |
|
1041 |
SimpleDateFormat dateFormat = new SimpleDateFormat(); |
|
899 |
Locale locale = CSVStoreParameters.getLocale(getCSVParameters()); |
|
900 |
taskStatus.message("_loading"); |
|
901 |
int count = 0; |
|
1042 | 902 |
|
1043 |
try { |
|
1044 |
CsvPreference preferences = getCSVPreferences(); |
|
1045 |
in = new FileReader(this.getCSVParameters().getFile()); |
|
903 |
int count_errors = 0; |
|
1046 | 904 |
|
1047 |
reader = new CsvListReader(in, preferences); |
|
1048 |
headers = reader.getHeader(true); |
|
1049 |
if (headers == null) { |
|
1050 |
headers = CSVStoreParameters.getHeaders(getCSVParameters()); |
|
1051 |
} |
|
1052 |
types = new int[headers.length]; |
|
905 |
List<String> row = reader.read(); |
|
1053 | 906 |
|
1054 |
possibleDataTypes = new boolean[headers.length][6]; |
|
1055 |
for (int i = 0; i < possibleDataTypes.length; i++) { |
|
1056 |
for( int j=0; j<4; j++ ) { |
|
1057 |
possibleDataTypes[i][j] = true; |
|
1058 |
} |
|
1059 |
} |
|
1060 |
locale = CSVStoreParameters.getLocale(getCSVParameters()); |
|
1061 |
if( locale == null ) { |
|
1062 |
locale = Locale.getDefault(); |
|
907 |
int skipLines = CSVStoreParameters.getSkipLines(getCSVParameters()); |
|
908 |
if ( skipLines > 0 ) { |
|
909 |
row = reader.skip(skipLines); |
|
910 |
} |
|
911 |
|
|
912 |
while ( row != null ) { |
|
913 |
taskStatus.setCurValue(++count); |
|
914 |
FeatureProvider feature = this.createFeatureProvider(ftype); |
|
915 |
for ( int i = 0; i < row.size(); i++ ) { |
|
916 |
Object rawvalue = row.get(i); |
|
917 |
try { |
|
918 |
Object value = null; |
|
919 |
if ( locale != null && coercion[i] instanceof CoercionWithLocale ) { |
|
920 |
value = ((CoercionWithLocale) (coercion[i])).coerce(rawvalue, locale); |
|
921 |
} else { |
|
922 |
value = coercion[i].coerce(rawvalue); |
|
1063 | 923 |
} |
1064 |
|
|
1065 |
DataTypesManager typeManager = ToolsLocator.getDataTypesManager(); |
|
1066 |
CoercionWithLocale toDouble = (CoercionWithLocale) typeManager.getCoercion(DataTypes.DOUBLE); |
|
1067 |
CoercionWithLocale toFloat = (CoercionWithLocale) typeManager.getCoercion(DataTypes.FLOAT); |
|
1068 |
|
|
1069 |
List<String> row = reader.read(); |
|
924 |
feature.set(i, value); |
|
925 |
if ( sizes[i] >= 0 && value != null ) { |
|
926 |
int x = ((String) value).length(); |
|
927 |
if ( sizes[i] < x ) { |
|
928 |
sizes[i] = x; |
|
929 |
} |
|
930 |
} |
|
931 |
} catch (RuntimeException ex) { |
|
932 |
if ( !ignore_errors ) { |
|
933 |
throw ex; |
|
934 |
} |
|
935 |
if ( count_errors++ < 10 ) { |
|
936 |
logger.warn("Can't load value of attribute " + i + " in row " + count + ".", ex); |
|
937 |
} |
|
938 |
if ( count_errors == 10 ) { |
|
939 |
logger.info("Too many errors, suppress messages."); |
|
940 |
} |
|
941 |
} |
|
942 |
} |
|
943 |
this.addFeatureProvider(feature); |
|
944 |
row = reader.read(); |
|
945 |
} |
|
946 |
for ( int i = 0; i < ftype.size(); i++ ) { |
|
947 |
if ( sizes[i] > 0 ) { |
|
948 |
EditableFeatureAttributeDescriptor efad = ((EditableFeatureAttributeDescriptor) edftype.getAttributeDescriptor(i)); |
|
949 |
efad.setSize(sizes[i]); |
|
950 |
} |
|
951 |
} |
|
952 |
// Volvemos a asignar al store el featuretype, ya que puede |
|
953 |
// haber cambiado. |
|
954 |
ftype = edftype.getNotEditableCopy(); |
|
955 |
ftypes = new ArrayList<FeatureType>(); |
|
956 |
ftypes.add(ftype); |
|
957 |
store.setFeatureTypes(ftypes, ftype); |
|
1070 | 958 |
|
1071 |
while (row != null) { |
|
1072 |
for (int i = 0; i < row.size(); i++) { |
|
1073 |
Object rawvalue = row.get(i); |
|
1074 |
Object value = null; |
|
1075 |
if( possibleDataTypes[i][T_DOUBLE] ) { |
|
1076 |
try { |
|
1077 |
value = toDouble.coerce(rawvalue, locale); |
|
1078 |
possibleDataTypes[i][T_DOUBLE] = true; |
|
1079 |
} catch (Exception ex) { |
|
1080 |
possibleDataTypes[i][T_DOUBLE] = false; |
|
1081 |
} |
|
1082 |
} |
|
1083 |
if( possibleDataTypes[i][T_FLOAT] ) { |
|
1084 |
try { |
|
1085 |
value = toFloat.coerce(rawvalue, locale); |
|
1086 |
possibleDataTypes[i][T_FLOAT] = true; |
|
1087 |
} catch (Exception ex) { |
|
1088 |
possibleDataTypes[i][T_FLOAT] = false; |
|
1089 |
} |
|
1090 |
} |
|
1091 |
if (possibleDataTypes[i][T_LONG]) { |
|
1092 |
try { |
|
1093 |
value = Long.parseLong((String) rawvalue); |
|
1094 |
possibleDataTypes[i][T_LONG] = true; |
|
1095 |
} catch (Exception ex) { |
|
1096 |
possibleDataTypes[i][T_LONG] = false; |
|
1097 |
} |
|
1098 |
} |
|
1099 |
if (possibleDataTypes[i][T_INT]) { |
|
1100 |
try { |
|
1101 |
value = Integer.parseInt((String) rawvalue); |
|
1102 |
possibleDataTypes[i][T_INT] = true; |
|
1103 |
} catch (Exception ex) { |
|
1104 |
possibleDataTypes[i][T_INT] = false; |
|
1105 |
} |
|
1106 |
} |
|
1107 |
if (possibleDataTypes[i][T_DATE]) { |
|
1108 |
try { |
|
1109 |
value = dateFormat.parse((String) rawvalue); |
|
1110 |
possibleDataTypes[i][T_DATE] = true; |
|
1111 |
} catch (Exception ex) { |
|
1112 |
possibleDataTypes[i][T_DATE] = false; |
|
1113 |
} |
|
1114 |
} |
|
1115 |
if (possibleDataTypes[i][T_URL]) { |
|
1116 |
try { |
|
1117 |
value = new URL((String) rawvalue); |
|
1118 |
possibleDataTypes[i][T_URL] = true; |
|
1119 |
} catch (Exception ex) { |
|
1120 |
possibleDataTypes[i][T_URL] = false; |
|
1121 |
} |
|
1122 |
} |
|
1123 |
} |
|
1124 |
row = reader.read(); |
|
1125 |
} |
|
1126 |
} finally { |
|
1127 |
if (reader != null) { |
|
1128 |
try { |
|
1129 |
reader.close(); |
|
1130 |
} catch (Exception ex) { |
|
1131 |
// Do nothing |
|
1132 |
} |
|
1133 |
reader = null; |
|
1134 |
} |
|
1135 |
if (in != null) { |
|
1136 |
try { |
|
1137 |
in.close(); |
|
1138 |
} catch (Exception ex) { |
|
1139 |
// Do nothing |
|
1140 |
} |
|
1141 |
in = null; |
|
1142 |
} |
|
959 |
taskStatus.terminate(); |
|
960 |
} finally { |
|
961 |
if ( reader != null ) { |
|
962 |
try { |
|
963 |
reader.close(); |
|
964 |
} catch (Exception ex) { |
|
965 |
// Do nothing |
|
966 |
} |
|
967 |
reader = null; |
|
968 |
} |
|
969 |
if ( in != null ) { |
|
970 |
try { |
|
971 |
in.close(); |
|
972 |
} catch (Exception ex) { |
|
973 |
// Do nothing |
|
974 |
} |
|
975 |
in = null; |
|
976 |
} |
|
977 |
} |
|
978 |
} |
|
1143 | 979 |
|
1144 |
} |
|
1145 |
for (int i = 0; i < possibleDataTypes.length ; i++) { |
|
1146 |
if( possibleDataTypes[i][T_INT] ) { |
|
1147 |
types[i] = DataTypes.INT; |
|
1148 |
continue; |
|
1149 |
} |
|
1150 |
if( possibleDataTypes[i][T_LONG] ) { |
|
1151 |
types[i] = DataTypes.LONG; |
|
1152 |
continue; |
|
1153 |
} |
|
1154 |
if( possibleDataTypes[i][T_FLOAT] ) { |
|
1155 |
types[i] = DataTypes.FLOAT; |
|
1156 |
continue; |
|
1157 |
} |
|
1158 |
if( possibleDataTypes[i][T_DOUBLE] ) { |
|
1159 |
types[i] = DataTypes.DOUBLE; |
|
1160 |
continue; |
|
1161 |
} |
|
1162 |
if( possibleDataTypes[i][T_URL] ) { |
|
1163 |
types[i] = DataTypes.URL; |
|
1164 |
continue; |
|
1165 |
} |
|
1166 |
if( possibleDataTypes[i][T_DATE] ) { |
|
1167 |
types[i] = DataTypes.DATE; |
|
1168 |
continue; |
|
1169 |
} |
|
1170 |
types[i] = DataTypes.STRING; |
|
1171 |
} |
|
1172 |
return types; |
|
1173 |
} |
|
980 |
private int[] automaticDetectionOfTypes() throws IOException { |
|
981 |
boolean automatic_types_detection = CSVStoreParameters.getAutomaticTypesDetection(getCSVParameters()); |
|
982 |
if ( !automatic_types_detection ) { |
|
983 |
return null; |
|
984 |
} |
|
1174 | 985 |
|
986 |
final int T_INT = 0; |
|
987 |
final int T_FLOAT = 1; |
|
988 |
final int T_DOUBLE = 2; |
|
989 |
final int T_LONG = 3; |
|
990 |
final int T_URL = 4; |
|
991 |
final int T_DATE = 5; |
|
992 |
boolean possibleDataTypes[][] = null; |
|
993 |
Locale locale = null; |
|
994 |
int[] types = null; |
|
995 |
|
|
996 |
FileReader in = null; |
|
997 |
SimpleReader reader = null; |
|
998 |
String headers[] = null; |
|
999 |
SimpleDateFormat dateFormat = new SimpleDateFormat(); |
|
1000 |
|
|
1001 |
try { |
|
1002 |
|
|
1003 |
in = new FileReader(this.getCSVParameters().getFile()); |
|
1004 |
|
|
1005 |
reader = getSimpleReader(in); |
|
1006 |
headers = reader.getHeader(); |
|
1007 |
if ( headers == null ) { |
|
1008 |
headers = CSVStoreParameters.getHeaders(getCSVParameters()); |
|
1009 |
} |
|
1010 |
types = new int[headers.length]; |
|
1011 |
|
|
1012 |
possibleDataTypes = new boolean[headers.length][6]; |
|
1013 |
for ( int i = 0; i < possibleDataTypes.length; i++ ) { |
|
1014 |
for ( int j = 0; j < 4; j++ ) { |
|
1015 |
possibleDataTypes[i][j] = true; |
|
1016 |
} |
|
1017 |
} |
|
1018 |
locale = CSVStoreParameters.getLocale(getCSVParameters()); |
|
1019 |
if ( locale == null ) { |
|
1020 |
locale = Locale.getDefault(); |
|
1021 |
} |
|
1022 |
|
|
1023 |
DataTypesManager typeManager = ToolsLocator.getDataTypesManager(); |
|
1024 |
CoercionWithLocale toDouble = (CoercionWithLocale) typeManager.getCoercion(DataTypes.DOUBLE); |
|
1025 |
CoercionWithLocale toFloat = (CoercionWithLocale) typeManager.getCoercion(DataTypes.FLOAT); |
|
1026 |
|
|
1027 |
List<String> row = reader.read(); |
|
1028 |
|
|
1029 |
while ( row != null ) { |
|
1030 |
for ( int i = 0; i < row.size(); i++ ) { |
|
1031 |
Object rawvalue = row.get(i); |
|
1032 |
Object value = null; |
|
1033 |
if ( possibleDataTypes[i][T_DOUBLE] ) { |
|
1034 |
try { |
|
1035 |
value = toDouble.coerce(rawvalue, locale); |
|
1036 |
possibleDataTypes[i][T_DOUBLE] = true; |
|
1037 |
} catch (Exception ex) { |
|
1038 |
possibleDataTypes[i][T_DOUBLE] = false; |
|
1039 |
} |
|
1040 |
} |
|
1041 |
if ( possibleDataTypes[i][T_FLOAT] ) { |
|
1042 |
try { |
|
1043 |
value = toFloat.coerce(rawvalue, locale); |
|
1044 |
possibleDataTypes[i][T_FLOAT] = true; |
|
1045 |
} catch (Exception ex) { |
|
1046 |
possibleDataTypes[i][T_FLOAT] = false; |
|
1047 |
} |
|
1048 |
} |
|
1049 |
if ( possibleDataTypes[i][T_LONG] ) { |
|
1050 |
try { |
|
1051 |
value = Long.parseLong((String) rawvalue); |
|
1052 |
possibleDataTypes[i][T_LONG] = true; |
|
1053 |
} catch (Exception ex) { |
|
1054 |
possibleDataTypes[i][T_LONG] = false; |
|
1055 |
} |
|
1056 |
} |
|
1057 |
if ( possibleDataTypes[i][T_INT] ) { |
|
1058 |
try { |
|
1059 |
value = Integer.parseInt((String) rawvalue); |
|
1060 |
possibleDataTypes[i][T_INT] = true; |
|
1061 |
} catch (Exception ex) { |
|
1062 |
possibleDataTypes[i][T_INT] = false; |
|
1063 |
} |
|
1064 |
} |
|
1065 |
if ( possibleDataTypes[i][T_DATE] ) { |
|
1066 |
try { |
|
1067 |
value = dateFormat.parse((String) rawvalue); |
|
1068 |
possibleDataTypes[i][T_DATE] = true; |
|
1069 |
} catch (Exception ex) { |
|
1070 |
possibleDataTypes[i][T_DATE] = false; |
|
1071 |
} |
|
1072 |
} |
|
1073 |
if ( possibleDataTypes[i][T_URL] ) { |
|
1074 |
try { |
|
1075 |
value = new URL((String) rawvalue); |
|
1076 |
possibleDataTypes[i][T_URL] = true; |
|
1077 |
} catch (Exception ex) { |
|
1078 |
possibleDataTypes[i][T_URL] = false; |
|
1079 |
} |
|
1080 |
} |
|
1081 |
} |
|
1082 |
row = reader.read(); |
|
1083 |
} |
|
1084 |
} finally { |
|
1085 |
if ( reader != null ) { |
|
1086 |
try { |
|
1087 |
reader.close(); |
|
1088 |
} catch (Exception ex) { |
|
1089 |
// Do nothing |
|
1090 |
} |
|
1091 |
reader = null; |
|
1092 |
} |
|
1093 |
if ( in != null ) { |
|
1094 |
try { |
|
1095 |
in.close(); |
|
1096 |
} catch (Exception ex) { |
|
1097 |
// Do nothing |
|
1098 |
} |
|
1099 |
in = null; |
|
1100 |
} |
|
1101 |
|
|
1102 |
} |
|
1103 |
for ( int i = 0; i < possibleDataTypes.length; i++ ) { |
|
1104 |
if ( possibleDataTypes[i][T_INT] ) { |
|
1105 |
types[i] = DataTypes.INT; |
|
1106 |
continue; |
|
1107 |
} |
|
1108 |
if ( possibleDataTypes[i][T_LONG] ) { |
|
1109 |
types[i] = DataTypes.LONG; |
|
1110 |
continue; |
|
1111 |
} |
|
1112 |
if ( possibleDataTypes[i][T_FLOAT] ) { |
|
1113 |
types[i] = DataTypes.FLOAT; |
|
1114 |
continue; |
|
1115 |
} |
|
1116 |
if ( possibleDataTypes[i][T_DOUBLE] ) { |
|
1117 |
types[i] = DataTypes.DOUBLE; |
|
1118 |
continue; |
|
1119 |
} |
|
1120 |
if ( possibleDataTypes[i][T_URL] ) { |
|
1121 |
types[i] = DataTypes.URL; |
|
1122 |
continue; |
|
1123 |
} |
|
1124 |
if ( possibleDataTypes[i][T_DATE] ) { |
|
1125 |
types[i] = DataTypes.DATE; |
|
1126 |
continue; |
|
1127 |
} |
|
1128 |
types[i] = DataTypes.STRING; |
|
1129 |
} |
|
1130 |
return types; |
|
1131 |
} |
Also available in: Unified diff