Revision 41006 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 | ||
---|---|---|
25 | 25 |
|
26 | 26 |
import java.io.File; |
27 | 27 |
import java.io.FileReader; |
28 |
import java.io.FileWriter; |
|
28 | 29 |
import java.io.IOException; |
29 | 30 |
import java.util.ArrayList; |
30 | 31 |
import java.util.HashMap; |
32 |
import java.util.Iterator; |
|
31 | 33 |
import java.util.List; |
34 |
import java.util.Properties; |
|
32 | 35 |
|
36 |
import org.apache.commons.io.FilenameUtils; |
|
33 | 37 |
import org.cresques.cts.IProjection; |
34 | 38 |
import org.gvsig.fmap.dal.DALLocator; |
35 | 39 |
import org.gvsig.fmap.dal.DataManager; |
... | ... | |
43 | 47 |
import org.gvsig.fmap.dal.exception.OpenException; |
44 | 48 |
import org.gvsig.fmap.dal.exception.ReadException; |
45 | 49 |
import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
50 |
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor; |
|
46 | 51 |
import org.gvsig.fmap.dal.feature.EditableFeatureType; |
52 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
|
53 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
|
47 | 54 |
import org.gvsig.fmap.dal.feature.FeatureType; |
55 |
import org.gvsig.fmap.dal.feature.exception.PerformEditingException; |
|
48 | 56 |
import org.gvsig.fmap.dal.feature.spi.FeatureProvider; |
49 | 57 |
import org.gvsig.fmap.dal.feature.spi.FeatureStoreProviderServices; |
50 | 58 |
import org.gvsig.fmap.dal.feature.spi.memory.AbstractMemoryStoreProvider; |
59 |
import org.gvsig.fmap.dal.resource.ResourceAction; |
|
51 | 60 |
import org.gvsig.fmap.dal.resource.file.FileResource; |
52 | 61 |
import org.gvsig.fmap.dal.resource.spi.ResourceConsumer; |
53 | 62 |
import org.gvsig.fmap.dal.resource.spi.ResourceProvider; |
... | ... | |
55 | 64 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorerParameters; |
56 | 65 |
import org.gvsig.fmap.dal.spi.DataStoreProviderServices; |
57 | 66 |
import org.gvsig.fmap.geom.Geometry; |
58 |
import org.gvsig.fmap.geom.Geometry.SUBTYPES; |
|
59 | 67 |
import org.gvsig.fmap.geom.GeometryLocator; |
60 | 68 |
import org.gvsig.fmap.geom.GeometryManager; |
61 |
import org.gvsig.fmap.geom.exception.CreateEnvelopeException; |
|
62 | 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; |
|
63 | 74 |
import org.gvsig.tools.ToolsLocator; |
64 | 75 |
import org.gvsig.tools.dataTypes.CoercionException; |
76 |
import org.gvsig.tools.dataTypes.DataTypesManager; |
|
65 | 77 |
import org.gvsig.tools.dataTypes.DataTypesManager.Coercion; |
78 |
import org.gvsig.tools.dispose.DisposableIterator; |
|
66 | 79 |
import org.gvsig.tools.dynobject.DynObject; |
67 | 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; |
|
68 | 84 |
import org.gvsig.tools.exception.NotYetImplemented; |
69 | 85 |
import org.gvsig.tools.persistence.PersistentState; |
70 | 86 |
import org.gvsig.tools.persistence.exception.PersistenceException; |
... | ... | |
74 | 90 |
import org.slf4j.LoggerFactory; |
75 | 91 |
import org.supercsv.comment.CommentStartsWith; |
76 | 92 |
import org.supercsv.io.CsvListReader; |
93 |
import org.supercsv.io.CsvListWriter; |
|
77 | 94 |
import org.supercsv.prefs.CsvPreference; |
95 |
import org.supercsv.quote.QuoteMode; |
|
78 | 96 |
|
79 | 97 |
public class CSVStoreProvider extends AbstractMemoryStoreProvider implements |
80 | 98 |
ResourceConsumer { |
... | ... | |
89 | 107 |
|
90 | 108 |
private long counterNewsOIDs = 0; |
91 | 109 |
private Envelope envelope; |
92 |
private GeometryManager geomManager = GeometryLocator.getGeometryManager(); |
|
93 | 110 |
private SimpleTaskStatus taskStatus; |
94 | 111 |
|
95 | 112 |
|
... | ... | |
113 | 130 |
); |
114 | 131 |
|
115 | 132 |
resource.addConsumer(this); |
116 |
|
|
133 |
initializeFeatureTypes(); |
|
117 | 134 |
} |
118 | 135 |
|
119 | 136 |
private CSVStoreParameters getCSVParameters() { |
... | ... | |
125 | 142 |
} |
126 | 143 |
|
127 | 144 |
public boolean allowWrite() { |
128 |
return false;
|
|
145 |
return true;
|
|
129 | 146 |
} |
130 | 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 |
|
|
131 | 159 |
public void open() throws OpenException { |
132 | 160 |
if (this.data != null) { |
133 | 161 |
return; |
134 | 162 |
} |
135 |
|
|
136 |
List<FeatureProvider> features = null; |
|
163 |
this.data = new ArrayList<FeatureProvider>(); |
|
164 |
resource.setData(new HashMap()); |
|
165 |
counterNewsOIDs = 0; |
|
137 | 166 |
try { |
138 |
features = loadFeatures(); |
|
167 |
loadFeatures(); |
|
168 |
} catch (RuntimeException e) { |
|
169 |
logger.warn("Can't load features from CSV '"+getFullFileName()+"'.", e); |
|
170 |
throw e; |
|
139 | 171 |
} catch (Exception e) { |
140 |
// PETA --ojo-- |
|
172 |
logger.warn("Can't load features from CSV '"+getFullFileName()+"'.", e); |
|
173 |
throw new RuntimeException(e); |
|
141 | 174 |
} |
142 |
|
|
143 |
CSVData csvData = null; |
|
144 |
resource.setData(new HashMap()); |
|
145 |
csvData = new CSVData(); |
|
146 |
csvData.data = features; |
|
147 |
data = csvData.data; |
|
148 |
counterNewsOIDs = 0; |
|
149 |
|
|
150 | 175 |
} |
151 | 176 |
|
152 |
private class CSVData { |
|
153 |
public List data = null; |
|
154 |
public FeatureType defaultFType = null; |
|
155 |
public List fTypes = null; |
|
156 |
public Envelope envelope = null; |
|
157 |
public IProjection projection; |
|
158 |
public Envelope getEnvelopeCopy() throws CreateEnvelopeException { |
|
159 |
if (envelope == null) { |
|
160 |
return null; |
|
161 |
} |
|
162 |
Envelope newEnvelope; |
|
163 |
if (envelope.getDimension() == 2) { |
|
164 |
newEnvelope = geomManager.createEnvelope(SUBTYPES.GEOM2D); |
|
165 |
} else { |
|
166 |
newEnvelope = geomManager.createEnvelope(SUBTYPES.GEOM3D); |
|
167 |
|
|
168 |
} |
|
169 |
newEnvelope.setLowerCorner(envelope.getLowerCorner()); |
|
170 |
newEnvelope.setUpperCorner(envelope.getUpperCorner()); |
|
171 |
return newEnvelope; |
|
172 |
} |
|
173 |
} |
|
174 |
/* |
|
175 |
private void openEver() throws OpenException { |
|
176 |
try { |
|
177 |
this.taskStatus.add(); |
|
178 |
getResource().execute(new ResourceAction() { |
|
179 |
public Object run() throws Exception { |
|
180 |
CSVData csvData = null; |
|
181 |
resource.setData(new HashMap()); |
|
182 |
FeatureStoreProviderServices store = getStoreServices(); |
|
183 |
csvData = new CSVData(); |
|
184 |
csvData.data = new ArrayList(); |
|
185 |
data = csvData.data; |
|
186 |
counterNewsOIDs = 0; |
|
187 |
Reader reader = new Reader().initialice( |
|
188 |
getMemoryProvider(), |
|
189 |
(File) resource.get(), |
|
190 |
projection |
|
191 |
); |
|
192 |
reader.begin(store); |
|
193 |
csvData.defaultFType = reader.getDefaultType().getNotEditableCopy(); |
|
194 |
ArrayList types = new ArrayList(); |
|
195 |
Iterator it = reader.getTypes().iterator(); |
|
196 |
EditableFeatureType fType; |
|
197 |
while (it.hasNext()) { |
|
198 |
fType = (EditableFeatureType) it.next(); |
|
199 |
if (fType.getId().equals(csvData.defaultFType.getId())) { |
|
200 |
types.add(csvData.defaultFType); |
|
201 |
} else { |
|
202 |
types.add(fType.getNotEditableCopy()); |
|
203 |
} |
|
204 |
} |
|
205 |
csvData.fTypes = types; |
|
206 |
|
|
207 |
resource.notifyOpen(); |
|
208 |
store.setFeatureTypes(csvData.fTypes, csvData.defaultFType); |
|
209 |
reader.load(); |
|
210 |
|
|
211 |
csvData.envelope = reader.getEnvelope(); |
|
212 |
|
|
213 |
csvData.projection = projection; |
|
214 |
|
|
215 |
reader.end(); |
|
216 |
resource.notifyClose(); |
|
217 |
((Map) resource.getData()).put(projection.getAbrev(), |
|
218 |
csvData); // OJO la reproyeccion |
|
219 |
|
|
220 |
data = csvData.data; |
|
221 |
store.setFeatureTypes(csvData.fTypes, csvData.defaultFType); |
|
222 |
envelope= csvData.getEnvelopeCopy(); |
|
223 |
// setDynValue("CRS", projection.getAbrev()); |
|
224 |
counterNewsOIDs = data.size(); |
|
225 |
return null; |
|
226 |
} |
|
227 |
}); |
|
228 |
this.taskStatus.terminate(); |
|
229 |
} catch (Exception e) { |
|
230 |
data = null; |
|
231 |
this.taskStatus.abort(); |
|
232 |
try { |
|
233 |
throw new OpenException(resource.getName(), e); |
|
234 |
} catch (AccessResourceException e1) { |
|
235 |
throw new OpenException(getProviderName(), e); |
|
236 |
} |
|
237 |
} finally { |
|
238 |
this.taskStatus.remove(); |
|
239 |
} |
|
240 |
} |
|
241 |
|
|
242 |
*/ |
|
243 | 177 |
public DataServerExplorer getExplorer() throws ReadException { |
244 | 178 |
DataManager manager = DALLocator.getDataManager(); |
245 |
FilesystemServerExplorerParameters params;// static boolean getIgnoreEmptyLines(DynObject dynobj) { |
|
246 |
// Boolean b = (Boolean) dynobj.getDynValue(IGNOREEMPTYLINES); |
|
247 |
// return b.booleanValue(); |
|
248 |
// } |
|
179 |
FilesystemServerExplorerParameters params; |
|
249 | 180 |
try { |
250 | 181 |
params = (FilesystemServerExplorerParameters) manager |
251 | 182 |
.createServerExplorerParameters(FilesystemServerExplorer.NAME); |
... | ... | |
259 | 190 |
|
260 | 191 |
} |
261 | 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 |
} |
|
262 | 336 |
|
263 |
/* |
|
264 | 337 |
public void performChanges(Iterator deleteds, Iterator inserteds, Iterator updateds, Iterator originalFeatureTypesUpdated) throws PerformEditingException { |
265 | 338 |
|
266 | 339 |
try { |
... | ... | |
272 | 345 |
DisposableIterator it = null; |
273 | 346 |
try { |
274 | 347 |
File file = (File) resource.get(); |
275 |
String fileName = file.getAbsolutePath(); |
|
276 |
writer = new Writer().initialice(file, projection); |
|
348 |
|
|
349 |
Writer writer = new Writer(); |
|
350 |
writer.initialize(file,getStoreServices().getDefaultFeatureType(),null); |
|
277 | 351 |
features = |
278 | 352 |
getStoreServices().getFeatureStore() |
279 | 353 |
.getFeatureSet(); |
280 |
List newdata = new ArrayList();
|
|
354 |
List<FeatureProvider> newdata = new ArrayList<FeatureProvider>();
|
|
281 | 355 |
writer.begin(); |
282 | 356 |
it = features.fastIterator(); |
283 | 357 |
taskStatus.setRangeOfValues(0,0); |
... | ... | |
294 | 368 |
newdata.add(feature); |
295 | 369 |
} |
296 | 370 |
data = newdata; |
297 |
writer.end(); |
|
298 | 371 |
if (writer.getEnvelope() != null){ |
299 | 372 |
envelope = writer.getEnvelope().getGeometry().getEnvelope(); |
300 | 373 |
} |
301 | 374 |
resource.notifyChanges(); |
302 |
// counterNewsOIDs = 0;
|
|
375 |
writer.end();
|
|
303 | 376 |
} finally { |
304 | 377 |
if (it != null) { |
305 | 378 |
it.dispose(); |
... | ... | |
320 | 393 |
} |
321 | 394 |
} |
322 | 395 |
|
323 |
public static EditableFeatureType createFeatureType(IProjection projection, int geometrySubtype){ |
|
324 |
DataManager manager = DALLocator.getDataManager(); |
|
325 |
EditableFeatureType featureType = manager.createFeatureType(); |
|
326 |
initialice |
|
327 |
featureType.setHasOID(true); |
|
328 |
|
|
329 |
// ID_FIELD_ID = 0; |
|
330 |
featureType.add(NAME_FIELD_ID, DataTypes.INT) |
|
331 |
.setDefaultValue(Integer.valueOf(0)) |
|
332 |
.getIndex(); |
|
333 |
|
|
334 |
EditableFeatureAttributeDescriptor attr = featureType.add( |
|
335 |
NAME_FIELD_GEOMETRY, DataTypes.GEOMETRY); |
|
336 |
attr.setSRS(projection); |
|
337 |
attr.setGeometryType(Geometry.TYPES.GEOMETRY); |
|
338 |
//If is a 3D file, set the geometry subtype |
|
339 |
attr.setGeometrySubType(geometrySubtype);initialice |
|
340 |
|
|
341 |
// ID_FIELD_GEOMETRY = 1; //attr.getIndex(); |
|
342 |
|
|
343 |
featureType.setDefaultGeometryAttributeName(NAME_FIELD_GEOMETRY); |
|
344 |
|
|
345 |
// FIXME: Cual es el size y el valor por defecto para Entityinitialice ? |
|
346 |
// ID_FIELD_ENTITY = 2; |
|
347 |
featureType.add(NAME_FIELD_ENTITY, |
|
348 |
DataTypes.STRING, 100) |
|
349 |
.setDefaultValue("") |
|
350 |
.getIndex(); |
|
351 |
|
|
352 |
// FIXME: Cual es el size de Layer ? |
|
353 |
// ID_FIELD_LAYER = 3;// static boolean getIgnoreEmptyLines(DynObject dynobj) { |
|
354 |
// Boolean b = (Boolean) dynobj.getDynValue(IGNOREEMPTYLINES); |
|
355 |
// return b.booleanValue(); |
|
356 |
// } |
|
357 |
featureType.add(NAME_FIELD_LAYER,initialice |
|
358 |
DataTypes.STRING, 100) |
|
359 |
.setDefaultValue( |
|
360 |
"default").getIndex(); |
|
361 |
|
|
362 |
// ID_FIELD_COLOR = 4; |
|
363 |
featureType.add(NAME_FIELD_COLOR, |
|
364 |
DataTypes.INT) |
|
365 |
.setDefaultValue( |
|
366 |
Integer.valueOf(0)).getIndex(); |
|
367 |
|
|
368 |
// ID_FIELD_ELEVATION = 5; |
|
369 |
featureType.add(NAME_FIELD_ELEVATION, |
|
370 |
DataTypes.DOUBLE) |
|
371 |
.setDefaultValue( |
|
372 |
Double.valueOf(0)).getIndex(); |
|
373 |
|
|
374 |
// ID_FIELD_THICKNESS = 6; |
|
375 |
featureType.add(NAME_FIELD_THICKNESS, |
|
376 |
DataTypes.DOUBLE) |
|
377 |
.setDefaultValue( |
|
378 |
Double.valueOf(0)).getIndex(); |
|
379 |
|
|
380 |
// FIXME: Cual es el size de Text ? |
|
381 |
// ID_FIELD_TEXT = 7; |
|
382 |
featureType.add(NAME_FIELD_TEXT, |
|
383 |
DataTypes.STRING, 100) |
|
384 |
.setDefaultValue("") |
|
385 |
.getIndex(); |
|
386 |
|
|
387 |
// ID_FIELD_HEIGHTTEXT = 8; |
|
388 |
featureType.add(NAME_FIELD_HEIGHTTEXT, |
|
389 |
DataTypes.DOUBLE).setDefaultValue( |
|
390 |
Double.valueOf(10)).getIndex(); |
|
391 |
|
|
392 |
// ID_FIELD_ROTATIONTEXT = 9; |
|
393 |
featureType.add(NAME_FIELD_ROTATIONTEXT, |
|
394 |
DataTypes.DOUBLE).setDefaultValue( |
|
395 |
Double.valueOf(0)).getIndex(); |
|
396 |
|
|
397 |
// FIXME: Parece que el DXF puede tener mas atributos opcionales. |
|
398 |
// Habria que ver de pillarlos ? |
|
399 |
|
|
400 |
return featureType; |
|
401 |
} |
|
402 |
public class Reader { |
|
403 |
private File file; |
|
404 |
private String fileName; |
|
405 |
private IProjection projection; |
|
406 |
private List types; |
|
407 |
private AbstractMemoryStoreProvider store; |
|
408 |
private Envelope envelope; |
|
409 |
|
|
410 |
public Reader initialice(AbstractMemoryStoreProvider store, File file, |
|
411 |
IProjection projection) { |
|
412 |
this.store = store; |
|
413 |
this.file = file; |
|
414 |
this.fileName = file.getAbsolutePath(); |
|
415 |
this.projection = projection; |
|
416 |
return this; |
|
417 |
} |
|
418 |
|
|
419 |
public Envelope getEnvelope() { |
|
420 |
return this.envelope; |
|
421 |
} |
|
422 |
|
|
423 |
public void begin(FeatureStoreProviderServices store) throws LoadException { |
|
424 |
taskStatus.message("_preloading_data"); |
|
425 |
|
|
426 |
|
|
427 |
try { |
|
428 |
dxfFeatureFile.load(); |
|
429 |
} catch (Exception e1) { |
|
430 |
throw new LoadException(e1, fileName); |
|
431 |
} |
|
432 |
|
|
433 |
taskStatus.message("_preparing_featureType"); |
|
434 |
int geometrySubtype; |
|
435 |
if (featureMaker.isDxf3DFile() && headerManager.isWritedDxf3D()){ |
|
436 |
geometrySubtype = Geometry.SUBTYPES.GEOM3D; |
|
437 |
}else{ |
|
438 |
geometrySubtype = Geometry.SUBTYPES.GEOM2D; |
|
439 |
} |
|
440 |
EditableFeatureType featureType = createFeatureType(this.projection, geometrySubtype); |
|
441 |
|
|
442 |
types = new ArrayList(); |
|
443 |
types.add(featureType); |
|
444 |
|
|
445 |
} |
|
446 |
|
|
447 |
public void end() { |
|
448 |
} |
|
449 |
|
|
450 |
public List getTypes() { |
|
451 |
return types; |
|
452 |
} |
|
453 |
|
|
454 |
public EditableFeatureType getDefaultType() { |
|
455 |
return (EditableFeatureType) types.get(0); |
|
456 |
} |
|
457 |
|
|
458 |
public void load() throws DataException { |
|
459 |
|
|
460 |
this.envelope = null; |
|
461 |
|
|
462 |
IObjList.vector features = (IObjList.vector) ((DxfFeatureMaker) featureMaker) |
|
463 |
.getObjects(); |
|
464 |
String acadVersion = ((DxfHeaderManager) headerManager) |
|
465 |
.getAcadVersion(); |
|
466 |
|
|
467 |
|
|
468 |
logger.info("load: acadVersion = '" + acadVersion + "'"); |
|
469 |
|
|
470 |
GeometryManager gManager = GeometryLocator.getGeometryManager(); |
|
471 |
|
|
472 |
taskStatus.setRangeOfValues(0,features.size()); |
|
473 |
|
|
474 |
if (!featureMaker.isDxf3DFile() && !headerManager.isWritedDxf3D()) { |
|
475 |
// y no est?n todos a 9999 |
|
476 |
taskStatus.message("_fixing_3dgeometries"); |
|
477 |
Feature[] features2D = new Feature[features.size()]; |
|
478 |
for (int i = 0; i < features.size(); i++) { |
|
479 |
taskStatus.setCurValue(i); |
|
480 |
Feature fea = (Feature) features.get(i); |
|
481 |
if (fea.getGeometry() instanceof org.gvsig.dxf.px.gml.Point3D) { |
|
482 |
Point point = (Point) fea.getGeometry(); |
|
483 |
Point point2 = new Point(); |
|
484 |
for (int j = 0; j < point.pointNr(); j++) { |
|
485 |
point2.add(point.get(j)); |
|
486 |
} |
|
487 |
point2.setTextPoint(point.isTextPoint()); |
|
488 |
fea.setGeometry(point2); |
|
489 |
features2D[i] = fea; |
|
490 |
|
|
491 |
} else if (fea.getGeometry() instanceof LineString3D) { |
|
492 |
LineString lineString = (LineString) fea.getGeometry(); |
|
493 |
LineString lineString2 = new LineString(); |
|
494 |
for (int j = 0; j < lineString.pointNr(); j++) { |
|
495 |
lineString2.add(lineString.get(j)); |
|
496 |
} |
|
497 |
fea.setGeometry(lineString2); |
|
498 |
features2D[i] = fea; |
|
499 |
} else if (fea.getGeometry() instanceof Polygon3D) { |
|
500 |
Polygon polygon = (Polygon) fea.getGeometry(); |
|
501 |
Polygon polygon2 = new Polygon(); |
|
502 |
for (int j = 0; j < polygon.pointNr(); j++) { |
|
503 |
polygon2.add(polygon.get(j)); |
|
504 |
} |
|
505 |
fea.setGeometry(polygon2); |
|
506 |
features2D[i] = fea; |
|
507 |
} |
|
508 |
} |
|
509 |
features.clear(); |
|
510 |
for (int i = 0; i < features2D.length; i++) { |
|
511 |
features.add(features2D[i]); |
|
512 |
} |
|
513 |
} |
|
514 |
|
|
515 |
|
|
516 |
|
|
517 |
taskStatus.message("_loading"); |
|
518 |
for (int i = 0; i < features.size(); i++) { |
|
519 |
|
|
520 |
taskStatus.setCurValue(i); |
|
521 |
|
|
522 |
FeatureProvider feature = store.createFeatureProvider(store |
|
523 |
.getStoreServices().getDefaultFeatureType()); |
|
524 |
|
|
525 |
Feature fea = (Feature) features.get(i); |
|
526 |
try { |
|
527 |
|
|
528 |
feature.setOID(new Long(i)); |
|
529 |
feature.set(ID_FIELD_ID, Integer.valueOf(i)); |
|
530 |
feature.set(ID_FIELD_ENTITY, fea.getProp("dxfEntity")); |
|
531 |
feature.set(ID_FIELD_LAYER, fea.getProp("layer")); |
|
532 |
feature.set(ID_FIELD_COLOR, Integer.valueOf(fea |
|
533 |
.getProp("color"))); |
|
534 |
feature.set(ID_FIELD_TEXT, fea.getProp("text")); |
|
535 |
feature.set(ID_FIELD_HEIGHTTEXT, toDouble(fea |
|
536 |
.getProp("textHeight"))); |
|
537 |
feature.set(ID_FIELD_ROTATIONTEXT, toDouble(fea |
|
538 |
.getProp("textRotation"))); |
|
539 |
feature.set(ID_FIELD_ELEVATION, toDouble(fea |
|
540 |
.getProp("elevation"))); |
|
541 |
feature.set(ID_FIELD_THICKNESS, toDouble(fea |
|
542 |
.getProp("thickness"))); |
|
543 |
feature.set(ID_FIELD_GEOMETRY, null); |
|
544 |
// FIXME: Abria que pillar el resto de atributos del DXF. |
|
545 |
|
|
546 |
// FIXME: Habia una incongruencia en el codigo ya que al |
|
547 |
// campo |
|
548 |
// ID_FIELD_GEOMETRY igual le asignaba una geometria que un |
|
549 |
// valor de cadena como 'Point3D', 'Polyline2D' o |
|
550 |
// 'Polyline3D' |
|
551 |
// Faltaria un atributo ID_FIELD_FSHAPE ? |
|
552 |
// |
|
553 |
|
|
554 |
Geometry geometry = null; |
|
555 |
|
|
556 |
if (fea.getGeometry() instanceof Point |
|
557 |
&& !(fea.getGeometry() instanceof org.gvsig.dxf.px.gml.Point3D)) { |
|
558 |
Point point = (Point) fea.getGeometry(); |
|
559 |
Point2D pto = new Point2D.Double(); |
|
560 |
pto = point.get(0); |
|
561 |
|
|
562 |
geometry = gManager.createPoint(pto.getX(), pto.getY(),SUBTYPES.GEOM2D); |
|
563 |
|
|
564 |
if (point.isTextPoint()) { |
|
565 |
/// TODO labeling |
|
566 |
} |
|
567 |
} else if (fea.getGeometry() instanceof org.gvsig.dxf.px.gml.Point3D) { |
|
568 |
org.gvsig.dxf.px.gml.Point3D point = (org.gvsig.dxf.px.gml.Point3D) fea.getGeometry(); |
|
569 |
Point3D pto = new Point3D(); |
|
570 |
pto = point.getPoint3D(0); |
|
571 |
org.gvsig.fmap.geom.primitive.Point geom = gManager.createPoint(pto.getX(), pto.getY(),SUBTYPES.GEOM3D); |
|
572 |
geom.setCoordinateAt(Geometry.DIMENSIONS.Z, pto.getZ()); |
|
573 |
|
|
574 |
geometry = geom; |
|
575 |
|
|
576 |
if (point.isTextPoint()) { |
|
577 |
/// TODO labeling |
|
578 |
} |
|
579 |
} else if (fea.getGeometry() instanceof LineString |
|
580 |
&& !(fea.getGeometry() instanceof LineString3D)) { |
|
581 |
Point2D[] pts = new Point2D[fea.getGeometry().pointNr()]; |
|
582 |
for (int j = 0; j < fea.getGeometry().pointNr(); j++) { |
|
583 |
pts[j] = fea.getGeometry().get(j); |
|
584 |
} |
|
585 |
|
|
586 |
Curve curve = (Curve)gManager.create(TYPES.CURVE , SUBTYPES.GEOM2D); |
|
587 |
curve.addMoveToVertex(gManager.createPoint(pts[0].getX(), pts[0].getY(), SUBTYPES.GEOM2D)); |
|
588 |
for (int j = 1; j < pts.length; j++) { |
|
589 |
curve.addVertex(gManager.createPoint(pts[j].getX(), pts[j].getY(), SUBTYPES.GEOM2D)); |
|
590 |
} |
|
591 |
|
|
592 |
geometry = curve; |
|
593 |
|
|
594 |
} else if (fea.getGeometry() instanceof LineString3D) { |
|
595 |
Point3D[] pts = new Point3D[fea.getGeometry().pointNr()]; |
|
596 |
for (int j = 0; j < fea.getGeometry().pointNr(); j++) { |
|
597 |
pts[j] = ((LineString3D) fea.getGeometry()).getPoint3D(j); |
|
598 |
} |
|
599 |
double[] elevations = new double[pts.length]; |
|
600 |
for (int j = 0; j < pts.length; j++) { |
|
601 |
elevations[j] = pts[j].getZ(); |
|
602 |
} |
|
603 |
|
|
604 |
Curve curve = (Curve)gManager.create(TYPES.CURVE , SUBTYPES.GEOM2D); |
|
605 |
org.gvsig.fmap.geom.primitive.Point point = gManager.createPoint(pts[0].getX(), pts[0].getY(), SUBTYPES.GEOM3D); |
|
606 |
point.setCoordinateAt(Geometry.DIMENSIONS.Z, elevations[0]); |
|
607 |
curve.addMoveToVertex(point); |
|
608 |
for (int j = 1; j < pts.length; j++) { |
|
609 |
point = gManager.createPoint(pts[j].getX(), pts[j].getY(), SUBTYPES.GEOM3D); |
|
610 |
point.setCoordinateAt(Geometry.DIMENSIONS.Z, elevations[j]); |
|
611 |
curve.addVertex(point); |
|
612 |
} |
|
613 |
geometry = curve; |
|
614 |
|
|
615 |
} else if (fea.getGeometry() instanceof Polygon |
|
616 |
&& !(fea.getGeometry() instanceof Polygon3D)) { |
|
617 |
|
|
618 |
Point2D firstPt = new Point2D.Double(); |
|
619 |
firstPt = fea.getGeometry().get(0); |
|
620 |
Point2D[] pts = new Point2D[fea.getGeometry().pointNr() + 1]; |
|
621 |
for (int j = 0; j < fea.getGeometry().pointNr(); j++) { |
|
622 |
pts[j] = fea.getGeometry().get(j); |
|
623 |
} |
|
624 |
pts[fea.getGeometry().pointNr()] = firstPt; |
|
625 |
|
|
626 |
Surface surface = (Surface)gManager.create(TYPES.SURFACE , SUBTYPES.GEOM2D); |
|
627 |
surface.addMoveToVertex(gManager.createPoint(pts[0].getX(), pts[0].getY(), SUBTYPES.GEOM2D)); |
|
628 |
for (int j = 1; j < pts.length; j++) { |
|
629 |
surface.addVertex(gManager.createPoint(pts[j].getX(), pts[j].getY(), SUBTYPES.GEOM2D)); |
|
630 |
} |
|
631 |
surface.closePrimitive(); |
|
632 |
|
|
633 |
geometry = surface; |
|
634 |
|
|
635 |
} else if (fea.getGeometry() instanceof Polygon3D) { |
|
636 |
Point3D firstPt = new Point3D(); |
|
637 |
firstPt = ((Polygon3D) fea.getGeometry()).getPoint3D(0); |
|
638 |
Point3D[] pts = new Point3D[fea.getGeometry().pointNr() + 1]; |
|
639 |
for (int j = 0; j < fea.getGeometry().pointNr(); j++) { |
|
640 |
pts[j] = ((Polygon3D) fea.getGeometry()).getPoint3D(j); |
|
641 |
} |
|
642 |
pts[fea.getGeometry().pointNr()] = firstPt; |
|
643 |
double[] elevations = new double[pts.length]; |
|
644 |
for (int j = 0; j < pts.length; j++) { |
|
645 |
elevations[j] = pts[j].getZ(); |
|
646 |
} |
|
647 |
|
|
648 |
Surface surface = (Surface)gManager.create(TYPES.SURFACE , SUBTYPES.GEOM3D); |
|
649 |
org.gvsig.fmap.geom.primitive.Point point = gManager.createPoint(pts[0].getX(), pts[0].getY(), SUBTYPES.GEOM3D); |
|
650 |
point.setCoordinateAt(Geometry.DIMENSIONS.Z, elevations[0]); |
|
651 |
surface.addMoveToVertex(point); |
|
652 |
|
|
653 |
for (int j = 1; j < pts.length; j++) { |
|
654 |
point = gManager.createPoint(pts[j].getX(), pts[j].getY(), SUBTYPES.GEOM3D); |
|
655 |
point.setCoordinateAt(Geometry.DIMENSIONS.Z, elevations[0]); |
|
656 |
surface.addVertex(point); |
|
657 |
} |
|
658 |
surface.closePrimitive(); |
|
659 |
|
|
660 |
geometry = surface; |
|
661 |
} else { |
|
662 |
logger.warn( |
|
663 |
MessageFormat.format( |
|
664 |
"load: geometry type {1} not supported", |
|
665 |
new Object[] { fea.getGeometry().getClass().getName() } |
|
666 |
) |
|
667 |
); |
|
668 |
} |
|
669 |
|
|
670 |
if (geometry != null){ |
|
671 |
feature.set(ID_FIELD_GEOMETRY, geometry); |
|
672 |
feature.setDefaultGeometry(geometry); |
|
673 |
if (this.envelope == null) { |
|
674 |
this.envelope = geometry.getEnvelope(); |
|
675 |
} else { |
|
676 |
this.envelope.add(geometry.getEnvelope()); |
|
677 |
} |
|
678 |
}else{ |
|
679 |
geometry = gManager.createNullGeometry(SUBTYPES.GEOM2D); |
|
680 |
} |
|
681 |
|
|
682 |
//Add the feature to the store |
|
683 |
store.addFeatureProvider(feature); |
|
684 |
|
|
685 |
} catch (Exception e) { |
|
686 |
//throw new LoadException(e, fileName); |
|
687 |
// FIXME: add to log max 10 |
|
688 |
} |
|
689 |
if (leyendBuilder != null) { |
|
690 |
try { |
|
691 |
leyendBuilder.process(feature); |
|
692 |
} catch (Exception e) { |
|
693 |
logger.warn( |
|
694 |
MessageFormat.format( |
|
695 |
"load: legendBuilder process fails in the feature {1}", |
|
696 |
fea |
|
697 |
) |
|
698 |
); |
|
699 |
} |
|
700 |
} |
|
701 |
|
|
702 |
} |
|
703 |
} |
|
704 |
|
|
705 |
} |
|
706 |
|
|
707 |
*/ |
|
708 | 396 |
public boolean closeResourceRequested(ResourceProvider resource) { |
709 | 397 |
return true; |
710 | 398 |
} |
... | ... | |
786 | 474 |
|
787 | 475 |
public String getName() { |
788 | 476 |
String name = this.getCSVParameters().getFile().getName(); |
789 |
int n = name.lastIndexOf("."); |
|
790 |
if( n<1 ) { |
|
791 |
return name; |
|
792 |
} |
|
793 |
return name.substring(0, n); |
|
477 |
return FilenameUtils.getBaseName(name); |
|
794 | 478 |
} |
795 | 479 |
|
796 | 480 |
public String getFullName() { |
... | ... | |
808 | 492 |
return s.trim().length()==0; |
809 | 493 |
} |
810 | 494 |
|
811 |
private CsvPreference getCSVPreferences() { |
|
812 |
String s = null; |
|
813 |
char quoteChar; |
|
814 |
int delimiterChar; |
|
815 |
String endOfLineSymbols; |
|
816 |
|
|
817 |
DynObject params = this.getParameters(); |
|
818 |
|
|
819 |
CsvPreference.Builder builder = null; |
|
820 |
|
|
821 |
CsvPreference defaultPreference = CSVStoreParameters.getPredefinedCSVPreferences(params); |
|
822 |
if( defaultPreference == null ) { |
|
823 |
defaultPreference = CsvPreference.STANDARD_PREFERENCE; |
|
824 |
} |
|
825 |
|
|
826 |
endOfLineSymbols = CSVStoreParameters.getRecordSeparator(params); |
|
827 |
if( isEmpty(endOfLineSymbols) ) { |
|
828 |
endOfLineSymbols = defaultPreference.getEndOfLineSymbols(); |
|
829 |
} |
|
830 |
s = CSVStoreParameters.getQuoteCharacter(params); |
|
831 |
if( isEmpty(s) ) { |
|
832 |
quoteChar = (char) defaultPreference.getQuoteChar(); |
|
833 |
} else { |
|
834 |
quoteChar = s.charAt(0); |
|
835 |
} |
|
836 |
s = CSVStoreParameters.getDelimiter(params); |
|
837 |
if( isEmpty(s) ) { |
|
838 |
delimiterChar = defaultPreference.getDelimiterChar(); |
|
839 |
} else { |
|
840 |
delimiterChar = s.charAt(0); |
|
841 |
} |
|
842 |
|
|
843 |
builder = new CsvPreference.Builder(quoteChar,delimiterChar,endOfLineSymbols); |
|
844 |
|
|
845 |
s = CSVStoreParameters.getCommentStartMarker(params); |
|
846 |
if( !isEmpty(s) ) { |
|
847 |
CommentStartsWith cs = new CommentStartsWith(s); |
|
848 |
builder.skipComments(cs); |
|
849 |
} |
|
495 |
private CsvPreference getCSVPreferences() { |
|
496 |
try { |
|
497 |
String s = null; |
|
498 |
char quoteChar; |
|
499 |
int delimiterChar; |
|
500 |
String endOfLineSymbols; |
|
850 | 501 |
|
851 |
builder.surroundingSpacesNeedQuotes(CSVStoreParameters.getSurroundingSpacesNeedQuotes(params)); |
|
852 |
return builder.build(); |
|
853 |
} |
|
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 |
} |
|
854 | 551 |
|
855 | 552 |
private EditableFeatureType getFeatureType(String headers[]) { |
856 | 553 |
EditableFeatureType fType = getStoreServices().createFeatureType(this.getName()); |
857 |
|
|
554 |
fType.setHasOID(true); |
|
555 |
DataTypesManager dataTypesManager = ToolsLocator.getDataTypesManager(); |
|
556 |
|
|
858 | 557 |
int[] types = new int[headers.length]; |
859 | 558 |
for( int i=0; i<types.length; i++) { |
860 |
types[i] = DataTypes.STRING; |
|
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 |
} |
|
861 | 579 |
} |
580 |
|
|
862 | 581 |
int[] param_types = CSVStoreParameters.getFieldTypes(this.getParameters()); |
863 | 582 |
if( param_types != null ) { |
864 | 583 |
for( int i=0; i<types.length && i<param_types.length; i++) { |
... | ... | |
873 | 592 |
fType.setDefaultGeometryAttributeName(fieldName); |
874 | 593 |
} |
875 | 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 |
} |
|
876 | 607 |
return fType; |
877 | 608 |
} |
878 | 609 |
|
879 |
private List<FeatureProvider> loadFeatures() throws IOException, DataException, CoercionException, CloneNotSupportedException { |
|
880 |
// |
|
881 |
// http://supercsv.sourceforge.net/examples_reading.html |
|
882 |
// http://supercsv.sourceforge.net/apidocs/index.html |
|
883 |
// |
|
884 |
List<FeatureProvider> features = new ArrayList(); |
|
885 |
Envelope envelope = null; |
|
886 |
CsvListReader reader = null; |
|
887 |
String headers[] = null; |
|
888 |
FeatureStoreProviderServices store = this.getStoreServices(); |
|
610 |
static class ToPointEvaluaror extends AbstractEvaluator { |
|
889 | 611 |
|
890 |
// Initiaize the CSV parser |
|
891 |
CsvPreference preferences = getCSVPreferences(); |
|
892 |
FileReader in = new FileReader(this.getCSVParameters().getFile()); |
|
612 |
private static final Logger logger = LoggerFactory.getLogger(ToPointEvaluaror.class); |
|
893 | 613 |
|
894 |
reader = new CsvListReader(in, preferences); |
|
895 |
headers = CSVStoreParameters.getHeaders(getCSVParameters ()); |
|
896 |
if( headers == null ) { |
|
897 |
headers = reader.getHeader(true); |
|
898 |
if( headers == null ) { |
|
899 |
String msg = "Can't retrieve header from csv file '"+this.getCSVParameters().getFile().getAbsolutePath()+"' and not specified in the parameters."; |
|
900 |
logger.warn(msg); |
|
901 |
throw new RuntimeException(msg); |
|
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]; |
|
902 | 625 |
} |
626 |
this.geommgr = GeometryLocator.getGeometryManager(); |
|
627 |
this.toDouble = ToolsLocator.getDataTypesManager().getCoercion(DataTypes.DOUBLE); |
|
903 | 628 |
} |
904 | 629 |
|
905 |
// Initialize the feature types |
|
906 |
FeatureType ftype = this.getFeatureType(headers); |
|
907 |
List ftypes = new ArrayList<FeatureType>(); |
|
908 |
ftypes.add(ftypes); |
|
909 |
store.setFeatureTypes(ftypes, ftype); |
|
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 |
} |
|
910 | 648 |
|
911 |
Coercion coercion[] = new Coercion[ftype.size()]; |
|
912 |
for(int i=0; i<ftype.size(); i++ ) { |
|
913 |
coercion[i] = ftype.getAttributeDescriptor(i).getDataType().getCoercion(); |
|
914 |
} |
|
915 |
boolean calculate_envelope = ( ftype.getDefaultGeometryAttributeName() != null ); |
|
916 |
taskStatus.message("_loading"); |
|
917 |
int count = 0; |
|
649 |
public String getName() { |
|
650 |
return "ToPointEvaluaror"; |
|
651 |
} |
|
918 | 652 |
|
919 |
List<String> row = reader.read(); |
|
920 |
while( row != null ) { |
|
921 |
taskStatus.setCurValue(++count); |
|
922 |
FeatureProvider feature = store.createDefaultFeatureProvider(ftype); |
|
923 |
feature.setOID(createNewOID()); |
|
924 |
for( int i=0; i<coercion.length; i++ ) { |
|
925 |
feature.set(i, coercion[i].coerce(row.get(i))); |
|
926 |
} |
|
927 |
if( calculate_envelope ) { |
|
928 |
Geometry geom = feature.getDefaultGeometry(); |
|
929 |
if( geom != null ) { |
|
930 |
if( envelope == null ) { |
|
931 |
envelope = (Envelope) geom.getEnvelope().clone(); |
|
932 |
} else { |
|
933 |
envelope.add(geom.getEnvelope()); |
|
934 |
} |
|
935 |
} |
|
936 |
} |
|
937 |
features.add(feature); |
|
938 |
} |
|
939 |
taskStatus.terminate(); |
|
940 |
in.close(); |
|
941 |
return features; |
|
942 | 653 |
} |
943 | 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 |
|
|
944 | 744 |
} |
Also available in: Unified diff