root / branches / v2_0_0_prep / libraries / libFMap_dalfile / src / org / gvsig / fmap / dal / store / dxf / DXFStoreProvider.java @ 24710
History | View | Annotate | Download (42.7 KB)
1 |
package org.gvsig.fmap.dal.store.dxf; |
---|---|
2 |
|
3 |
import java.awt.geom.PathIterator; |
4 |
import java.awt.geom.Point2D; |
5 |
import java.io.File; |
6 |
import java.io.IOException; |
7 |
import java.text.MessageFormat; |
8 |
import java.util.ArrayList; |
9 |
import java.util.Iterator; |
10 |
import java.util.List; |
11 |
import java.util.Vector; |
12 |
|
13 |
import org.cresques.cts.IProjection; |
14 |
import org.cresques.geo.Point3D; |
15 |
import org.cresques.io.DxfFile; |
16 |
import org.cresques.io.DxfGroup; |
17 |
import org.cresques.io.DxfGroupVector; |
18 |
import org.cresques.px.IObjList; |
19 |
import org.cresques.px.dxf.DxfEntityMaker; |
20 |
import org.cresques.px.dxf.DxfFeatureMaker; |
21 |
import org.cresques.px.dxf.DxfHeaderManager; |
22 |
import org.cresques.px.gml.Feature; |
23 |
import org.cresques.px.gml.LineString; |
24 |
import org.cresques.px.gml.LineString3D; |
25 |
import org.cresques.px.gml.Point; |
26 |
import org.cresques.px.gml.Polygon; |
27 |
import org.cresques.px.gml.Polygon3D; |
28 |
import org.gvsig.fmap.dal.DALLocator; |
29 |
import org.gvsig.fmap.dal.DataExplorer; |
30 |
import org.gvsig.fmap.dal.DataManager; |
31 |
import org.gvsig.fmap.dal.DataStoreParameters; |
32 |
import org.gvsig.fmap.dal.DataTypes; |
33 |
import org.gvsig.fmap.dal.NewDataStoreParameters; |
34 |
import org.gvsig.fmap.dal.exception.CreateException; |
35 |
import org.gvsig.fmap.dal.exception.DataException; |
36 |
import org.gvsig.fmap.dal.exception.FileNotFoundException; |
37 |
import org.gvsig.fmap.dal.exception.InitializeException; |
38 |
import org.gvsig.fmap.dal.exception.LoadException; |
39 |
import org.gvsig.fmap.dal.exception.OpenException; |
40 |
import org.gvsig.fmap.dal.exception.ReadException; |
41 |
import org.gvsig.fmap.dal.exception.RemoveException; |
42 |
import org.gvsig.fmap.dal.exception.WriteException; |
43 |
import org.gvsig.fmap.dal.explorer.filesystem.FilesystemExplorer; |
44 |
import org.gvsig.fmap.dal.explorer.filesystem.FilesystemExplorerParameters; |
45 |
import org.gvsig.fmap.dal.feature.EditableFeatureType; |
46 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
47 |
import org.gvsig.fmap.dal.feature.NewFeatureStoreParameters; |
48 |
import org.gvsig.fmap.dal.feature.exception.PerformEditingException; |
49 |
import org.gvsig.fmap.dal.feature.spi.FeatureData; |
50 |
import org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider; |
51 |
import org.gvsig.fmap.dal.feature.spi.FeatureStoreProviderServices; |
52 |
import org.gvsig.fmap.dal.feature.spi.LegendBuilder; |
53 |
import org.gvsig.fmap.dal.feature.spi.memory.AbstractMemoryStoreProvider; |
54 |
import org.gvsig.fmap.dal.resource.exception.AccessResourceException; |
55 |
import org.gvsig.fmap.dal.resource.file.FileResource; |
56 |
import org.gvsig.fmap.dal.resource.spi.ResourceConsumer; |
57 |
import org.gvsig.fmap.dal.resource.spi.ResourceProvider; |
58 |
import org.gvsig.fmap.geom.Geometry; |
59 |
import org.gvsig.fmap.geom.GeometryFactory; |
60 |
import org.gvsig.fmap.geom.GeometryManager; |
61 |
import org.gvsig.fmap.geom.primitive.AbstractPrimitive; |
62 |
import org.gvsig.fmap.geom.primitive.Arc2D; |
63 |
import org.gvsig.fmap.geom.primitive.Circle2D; |
64 |
import org.gvsig.fmap.geom.primitive.Curve2D; |
65 |
import org.gvsig.fmap.geom.primitive.Curve2DZ; |
66 |
import org.gvsig.fmap.geom.primitive.Ellipse2D; |
67 |
import org.gvsig.fmap.geom.primitive.GeneralPathX; |
68 |
import org.gvsig.fmap.geom.primitive.Point2DZ; |
69 |
import org.gvsig.fmap.geom.primitive.Surface2D; |
70 |
import org.gvsig.fmap.geom.primitive.Surface2DZ; |
71 |
import org.gvsig.fmap.geom.util.Converter; |
72 |
import org.gvsig.fmap.geom.util.UtilFunctions; |
73 |
import org.gvsig.tools.exception.NotYetImplemented; |
74 |
import org.gvsig.tools.operations.OperationNotSupportedException; |
75 |
import org.gvsig.tools.persistence.AbstractPersistenceManager; |
76 |
import org.gvsig.tools.persistence.PersistenceException; |
77 |
import org.gvsig.tools.persistence.PersistentState; |
78 |
|
79 |
public class DXFStoreProvider extends AbstractMemoryStoreProvider implements |
80 |
ResourceConsumer { |
81 |
|
82 |
final public static String NAME = "DXF"; |
83 |
final public static String DESCRIPTION = "DXF file"; |
84 |
|
85 |
private int ID_FIELD_ID = 0; |
86 |
private int ID_FIELD_GEOMETRY = 1; |
87 |
private int ID_FIELD_ENTITY = 2; |
88 |
private int ID_FIELD_LAYER = 3; |
89 |
private int ID_FIELD_COLOR = 4; |
90 |
private int ID_FIELD_ELEVATION = 5; |
91 |
private int ID_FIELD_THICKNESS = 6; |
92 |
private int ID_FIELD_TEXT = 7; |
93 |
private int ID_FIELD_HEIGHTTEXT = 8; |
94 |
private int ID_FIELD_ROTATIONTEXT = 9; |
95 |
|
96 |
private IProjection projection;
|
97 |
private ResourceProvider resource;
|
98 |
private LegendBuilder leyendBuilder;
|
99 |
|
100 |
private int counterNewsOIDs; |
101 |
|
102 |
private DXFStoreParameters getParameters() {
|
103 |
return (DXFStoreParameters) this.parameters; |
104 |
} |
105 |
|
106 |
public DXFStoreProvider(DXFStoreParameters parameters) {
|
107 |
super(parameters);
|
108 |
} |
109 |
|
110 |
public FeatureStoreProvider initialize(FeatureStoreProviderServices store)
|
111 |
throws InitializeException {
|
112 |
super.initialize(store);
|
113 |
|
114 |
counterNewsOIDs = 0;
|
115 |
// projection = CRSFactory.getCRS(getParameters().getSRSID());
|
116 |
|
117 |
File file = getParameters().getFile();
|
118 |
resource = this.createResource(
|
119 |
FileResource.NAME, |
120 |
new Object[] { file.getAbsolutePath() } |
121 |
); |
122 |
resource.addConsumer(this);
|
123 |
|
124 |
this.projection = this.getParameters().getCRS(); |
125 |
|
126 |
try {
|
127 |
leyendBuilder = (LegendBuilder) store.invokeOperation( |
128 |
LegendBuilder.OPERATION_NAME, null);
|
129 |
} catch (OperationNotSupportedException e) { |
130 |
leyendBuilder = null;
|
131 |
} catch (Exception e) { |
132 |
throw new InitializeException(e); |
133 |
} |
134 |
this.initializeFeatureTypes();
|
135 |
return this; |
136 |
} |
137 |
|
138 |
public String getName() { |
139 |
return NAME;
|
140 |
} |
141 |
|
142 |
public boolean allowWrite() { |
143 |
return true; |
144 |
} |
145 |
|
146 |
public boolean canCreate() { |
147 |
return true; |
148 |
} |
149 |
|
150 |
public Object getLeyend() throws OpenException { |
151 |
this.open();
|
152 |
if (leyendBuilder == null) { |
153 |
return null; |
154 |
} |
155 |
return leyendBuilder.getLegend();
|
156 |
} |
157 |
|
158 |
public void open() throws OpenException { |
159 |
if (this.data != null) { |
160 |
return;
|
161 |
} |
162 |
try {
|
163 |
this.resource.begin();
|
164 |
|
165 |
if (this.resource.getData() != null) { |
166 |
this.data = (ArrayList) this.resource.getData(); |
167 |
} else {
|
168 |
this.data = new ArrayList(); |
169 |
this.counterNewsOIDs = 0; |
170 |
Reader reader = new Reader().initialice( |
171 |
this,
|
172 |
new File((String) this.resource.get()), |
173 |
projection, |
174 |
this.leyendBuilder
|
175 |
); |
176 |
reader.begin(this.store);
|
177 |
|
178 |
ArrayList types = new ArrayList(); |
179 |
Iterator it = reader.getTypes().iterator();
|
180 |
while (it.hasNext()) {
|
181 |
types.add(((EditableFeatureType) it.next()) |
182 |
.getNotEditableCopy()); |
183 |
} |
184 |
|
185 |
this.store.setFeatureTypes(types, reader.getDefaultType()
|
186 |
.getNotEditableCopy()); |
187 |
resource.notifyOpen(); |
188 |
reader.load(); |
189 |
reader.end(); |
190 |
resource.notifyClose(); |
191 |
this.resource.setData(this.data); |
192 |
} |
193 |
} catch (Exception e) { |
194 |
this.data = null; |
195 |
try {
|
196 |
throw new OpenException(resource.getName(), e); |
197 |
} catch (AccessResourceException e1) {
|
198 |
throw new OpenException(this.getName(), e); |
199 |
} |
200 |
} finally {
|
201 |
this.resource.end();
|
202 |
} |
203 |
} |
204 |
|
205 |
|
206 |
public DataExplorer getExplorer() throws ReadException { |
207 |
DataManager manager = DALLocator.getDataManager(); |
208 |
FilesystemExplorerParameters params; |
209 |
try {
|
210 |
params = (FilesystemExplorerParameters) manager |
211 |
.createExplorerParameters(FilesystemExplorer.NAME); |
212 |
params.setRoot(this.getParameters().getFile().getParent());
|
213 |
return manager.createExplorer(params);
|
214 |
} catch (DataException e) {
|
215 |
throw new ReadException(this.getName(), e); |
216 |
} |
217 |
|
218 |
} |
219 |
|
220 |
public void create(NewDataStoreParameters parameters, boolean overwrite) |
221 |
throws CreateException {
|
222 |
|
223 |
|
224 |
|
225 |
IProjection projection = null;
|
226 |
DXFStoreParameters params = (DXFStoreParameters) parameters; |
227 |
|
228 |
if (params.getFile().exists()) {
|
229 |
if (overwrite) {
|
230 |
if (!params.getFile().delete()) {
|
231 |
throw new CreateException(this.getName(), new IOException( |
232 |
"cannot delete file"));
|
233 |
} |
234 |
} else {
|
235 |
throw new CreateException(this.getName(), new IOException( |
236 |
"file already exist"));
|
237 |
} |
238 |
} |
239 |
|
240 |
// projection = CRSFactory.getCRS(params.getSRSID());
|
241 |
|
242 |
File file = params.getFile();
|
243 |
FileResource resource; |
244 |
try {
|
245 |
resource = (FileResource) this.createResource(
|
246 |
FileResource.NAME, |
247 |
new Object[] { file.getAbsolutePath() }); |
248 |
} catch (InitializeException e1) {
|
249 |
throw new CreateException(params.getFileName(), e1); |
250 |
} |
251 |
resource.addConsumer(this);
|
252 |
|
253 |
|
254 |
try {
|
255 |
resource.begin(); |
256 |
Builder builder = new Builder().initialice(
|
257 |
resource.getFileName(), |
258 |
projection); |
259 |
resource.notifyOpen(); |
260 |
builder.begin(); |
261 |
builder.create(); |
262 |
builder.end(); |
263 |
resource.notifyClose(); |
264 |
|
265 |
resource.setData(null); // FIXME: Seguro que hay que ponerlo a null |
266 |
// ??
|
267 |
resource.notifyChanges(); |
268 |
} catch (Exception e) { |
269 |
throw new CreateException(params.getFileName(), e); |
270 |
} finally {
|
271 |
resource.end(); |
272 |
resource.removeConsumer(this);
|
273 |
} |
274 |
} |
275 |
|
276 |
private FeatureData getFeatureDataFormFeature(Object feature) { |
277 |
org.gvsig.fmap.dal.feature.impl.DefaultFeature f = (org.gvsig.fmap.dal.feature.impl.DefaultFeature) feature; |
278 |
return f.getData();
|
279 |
} |
280 |
|
281 |
public void performEditing(Iterator deleteds, Iterator inserteds, Iterator updateds) throws PerformEditingException { |
282 |
|
283 |
String fileName = ""; |
284 |
try {
|
285 |
resource.begin(); |
286 |
|
287 |
File file = (File) resource.get(); |
288 |
fileName = file.getAbsolutePath(); |
289 |
Writer writer = new Writer().initialice(file, projection); |
290 |
FeatureSet features = this.getStoreServices().getFeatureSet();
|
291 |
|
292 |
writer.begin(); |
293 |
Iterator it = features.fastIterator();
|
294 |
while (it.hasNext()) {
|
295 |
writer.add(getFeatureDataFormFeature(it.next())); |
296 |
} |
297 |
resource.notifyOpen(); |
298 |
writer.end(); |
299 |
resource.notifyClose(); |
300 |
features.dispose(); |
301 |
counterNewsOIDs = 0;
|
302 |
|
303 |
} catch (Exception e) { |
304 |
throw new PerformEditingException(fileName, e); |
305 |
} finally {
|
306 |
resource.end(); |
307 |
} |
308 |
} |
309 |
|
310 |
public class Builder { |
311 |
private String fileName; |
312 |
private IProjection projection;
|
313 |
|
314 |
public Builder initialice(String fileName, IProjection projection) { |
315 |
this.fileName = fileName;
|
316 |
this.projection = projection;
|
317 |
return this; |
318 |
} |
319 |
|
320 |
public void begin() { |
321 |
|
322 |
} |
323 |
|
324 |
public void end() { |
325 |
|
326 |
} |
327 |
|
328 |
public void create() throws IOException { |
329 |
DxfEntityMaker entityMaker = new DxfEntityMaker(projection);
|
330 |
DxfFile dxfFile = new DxfFile(null, fileName, entityMaker); |
331 |
dxfFile.setCadFlag(true);
|
332 |
dxfFile.setDxf3DFlag(false);
|
333 |
dxfFile.save(fileName); |
334 |
} |
335 |
} |
336 |
|
337 |
|
338 |
public class Reader { |
339 |
private File file; |
340 |
private String fileName; |
341 |
private IProjection projection;
|
342 |
private List types; |
343 |
private LegendBuilder leyendBuilder;
|
344 |
private AbstractMemoryStoreProvider store;
|
345 |
|
346 |
public Reader initialice(AbstractMemoryStoreProvider store, File file, |
347 |
IProjection projection, |
348 |
LegendBuilder leyendBuilder) { |
349 |
this.store = store;
|
350 |
this.file = file;
|
351 |
this.fileName = file.getAbsolutePath();
|
352 |
this.projection = projection;
|
353 |
this.leyendBuilder = leyendBuilder;
|
354 |
return this; |
355 |
} |
356 |
|
357 |
public void begin(FeatureStoreProviderServices store) { |
358 |
|
359 |
EditableFeatureType featureType = store.createFeatureType(); |
360 |
|
361 |
ID_FIELD_ID = featureType.add("ID", DataTypes.INT)
|
362 |
.setDefaultValue(Integer.valueOf(0)) |
363 |
.getIndex(); |
364 |
|
365 |
ID_FIELD_GEOMETRY = featureType.add("Geometry",
|
366 |
DataTypes.GEOMETRY) |
367 |
.getIndex(); |
368 |
featureType.setDefaultGeometryAttributeName("Geometry");
|
369 |
|
370 |
// FIXME: Cual es el size y el valor por defecto para Entity ?
|
371 |
ID_FIELD_ENTITY = featureType.add("Entity",
|
372 |
DataTypes.STRING, 100)
|
373 |
.setDefaultValue("")
|
374 |
.getIndex(); |
375 |
|
376 |
// FIXME: Cual es el size de Layer ?
|
377 |
ID_FIELD_LAYER = featureType.add("Layer",
|
378 |
DataTypes.STRING, 100)
|
379 |
.setDefaultValue( |
380 |
"default").getIndex();
|
381 |
|
382 |
ID_FIELD_COLOR = featureType.add("Color",
|
383 |
DataTypes.INT) |
384 |
.setDefaultValue( |
385 |
Integer.valueOf(0)).getIndex(); |
386 |
|
387 |
ID_FIELD_ELEVATION = featureType.add("Elevation",
|
388 |
DataTypes.DOUBLE) |
389 |
.setDefaultValue( |
390 |
Double.valueOf(0)).getIndex(); |
391 |
|
392 |
ID_FIELD_THICKNESS = featureType.add("Thickness",
|
393 |
DataTypes.DOUBLE) |
394 |
.setDefaultValue( |
395 |
Double.valueOf(0)).getIndex(); |
396 |
|
397 |
// FIXME: Cual es el size de Text ?
|
398 |
ID_FIELD_TEXT = featureType.add("Text",
|
399 |
DataTypes.STRING, 100)
|
400 |
.setDefaultValue("")
|
401 |
.getIndex(); |
402 |
|
403 |
ID_FIELD_HEIGHTTEXT = featureType.add("HeightText",
|
404 |
DataTypes.DOUBLE).setDefaultValue( |
405 |
Double.valueOf(10)).getIndex(); |
406 |
|
407 |
ID_FIELD_ROTATIONTEXT = featureType.add("Rotation",
|
408 |
DataTypes.DOUBLE).setDefaultValue( |
409 |
Double.valueOf(0)).getIndex(); |
410 |
|
411 |
|
412 |
|
413 |
// FIXME: Parece que el DXF puede tener mas atributos opcionales.
|
414 |
// Habria que ver de pillarlos ?
|
415 |
|
416 |
types = new ArrayList(); |
417 |
types.add(featureType); |
418 |
|
419 |
if (leyendBuilder != null) { |
420 |
leyendBuilder.begin(); |
421 |
} |
422 |
|
423 |
} |
424 |
|
425 |
public void end() { |
426 |
if (leyendBuilder != null) { |
427 |
leyendBuilder.end(); |
428 |
} |
429 |
} |
430 |
|
431 |
public List getTypes() { |
432 |
return types;
|
433 |
} |
434 |
|
435 |
public EditableFeatureType getDefaultType() {
|
436 |
return (EditableFeatureType) types.get(0); |
437 |
} |
438 |
|
439 |
private Double toDouble(String value) { |
440 |
if (value == null) { |
441 |
return Double.valueOf(0); |
442 |
} |
443 |
return Double.valueOf(value); |
444 |
} |
445 |
|
446 |
public void load() throws DataException { |
447 |
|
448 |
|
449 |
DxfFile.EntityFactory featureMaker = new DxfFeatureMaker(projection);
|
450 |
DxfFile.VarSettings headerManager = new DxfHeaderManager();
|
451 |
DxfFile dxfFeatureFile = new DxfFile(projection, file
|
452 |
.getAbsolutePath(), featureMaker, headerManager); |
453 |
|
454 |
try {
|
455 |
dxfFeatureFile.load(); |
456 |
} catch (Exception e1) { |
457 |
throw new LoadException(e1, fileName); |
458 |
} |
459 |
|
460 |
IObjList.vector features = (IObjList.vector) ((DxfFeatureMaker) featureMaker) |
461 |
.getObjects(); |
462 |
String acadVersion = ((DxfHeaderManager) headerManager)
|
463 |
.getAcadVersion(); |
464 |
|
465 |
|
466 |
getLogger().info("load: acadVersion = '" + acadVersion + "'"); |
467 |
|
468 |
GeometryFactory gFactory = GeometryManager.getInstance() |
469 |
.getGeometryFactory(); |
470 |
|
471 |
if (!featureMaker.isDxf3DFile() && !headerManager.isWritedDxf3D()) {
|
472 |
// y no est?n todos a 9999
|
473 |
Feature[] features2D = new Feature[features.size()]; |
474 |
for (int i = 0; i < features.size(); i++) { |
475 |
Feature fea = (Feature) features.get(i); |
476 |
if (fea.getGeometry() instanceof org.cresques.px.gml.Point3D) { |
477 |
Point point = (Point) fea.getGeometry(); |
478 |
Point point2 = new Point(); |
479 |
for (int j = 0; j < point.pointNr(); j++) { |
480 |
point2.add(point.get(j)); |
481 |
} |
482 |
point2.setTextPoint(point.isTextPoint()); |
483 |
fea.setGeometry(point2); |
484 |
features2D[i] = fea; |
485 |
|
486 |
} else if (fea.getGeometry() instanceof LineString3D) { |
487 |
LineString lineString = (LineString) fea.getGeometry(); |
488 |
LineString lineString2 = new LineString();
|
489 |
for (int j = 0; j < lineString.pointNr(); j++) { |
490 |
lineString2.add(lineString.get(j)); |
491 |
} |
492 |
fea.setGeometry(lineString2); |
493 |
features2D[i] = fea; |
494 |
} else if (fea.getGeometry() instanceof Polygon3D) { |
495 |
Polygon polygon = (Polygon) fea.getGeometry(); |
496 |
Polygon polygon2 = new Polygon(); |
497 |
for (int j = 0; j < polygon.pointNr(); j++) { |
498 |
polygon2.add(polygon.get(j)); |
499 |
} |
500 |
fea.setGeometry(polygon2); |
501 |
features2D[i] = fea; |
502 |
} |
503 |
} |
504 |
features.clear(); |
505 |
for (int i = 0; i < features2D.length; i++) { |
506 |
features.add(features2D[i]); |
507 |
} |
508 |
} |
509 |
|
510 |
for (int i = 0; i < features.size(); i++) { |
511 |
|
512 |
FeatureData feature = store.createFeatureData(store |
513 |
.getStoreServices().getDefaultFeatureType()); |
514 |
|
515 |
try {
|
516 |
Feature fea = (Feature) features.get(i); |
517 |
|
518 |
feature.set(ID_FIELD_ID, Integer.valueOf(i));
|
519 |
feature.set(ID_FIELD_ENTITY, fea.getProp("dxfEntity"));
|
520 |
feature.set(ID_FIELD_LAYER, fea.getProp("layer"));
|
521 |
feature.set(ID_FIELD_COLOR, Integer.valueOf(fea
|
522 |
.getProp("color")));
|
523 |
feature.set(ID_FIELD_TEXT, fea.getProp("text"));
|
524 |
feature.set(ID_FIELD_HEIGHTTEXT, toDouble(fea |
525 |
.getProp("textHeight")));
|
526 |
feature.set(ID_FIELD_ROTATIONTEXT, toDouble(fea |
527 |
.getProp("textRotation")));
|
528 |
feature.set(ID_FIELD_ELEVATION, toDouble(fea |
529 |
.getProp("elevation")));
|
530 |
feature.set(ID_FIELD_THICKNESS, toDouble(fea |
531 |
.getProp("thickness")));
|
532 |
// FIXME: Abria que pillar el resto de atributos del DXF.
|
533 |
|
534 |
store.addFeatureData(feature); |
535 |
|
536 |
|
537 |
// FIXME: Habia una incongruencia en el codigo ya que al
|
538 |
// campo
|
539 |
// ID_FIELD_GEOMETRY igual le asignaba una geometria que un
|
540 |
// valor de cadena como 'Point3D', 'Polyline2D' o
|
541 |
// 'Polyline3D'
|
542 |
// Faltaria un atributo ID_FIELD_FSHAPE ?
|
543 |
//
|
544 |
|
545 |
if (fea.getGeometry() instanceof Point |
546 |
&& !(fea.getGeometry() instanceof org.cresques.px.gml.Point3D)) {
|
547 |
Point point = (Point) fea.getGeometry(); |
548 |
Point2D pto = new Point2D.Double(); |
549 |
pto = point.get(0);
|
550 |
if (point.isTextPoint()) {
|
551 |
Geometry geom = gFactory |
552 |
.createPoint2D(new org.gvsig.fmap.geom.primitive.Point2D(
|
553 |
pto.getX(), pto.getY())); |
554 |
feature.set(ID_FIELD_GEOMETRY, geom); |
555 |
} else {
|
556 |
Geometry geom = gFactory |
557 |
.createPoint2D(new org.gvsig.fmap.geom.primitive.Point2D(
|
558 |
pto.getX(), pto.getY())); |
559 |
feature.set(ID_FIELD_GEOMETRY, geom); |
560 |
} |
561 |
} else if (fea.getGeometry() instanceof org.cresques.px.gml.Point3D) { |
562 |
org.cresques.px.gml.Point3D point = (org.cresques.px.gml.Point3D) fea |
563 |
.getGeometry(); |
564 |
Point3D pto = new Point3D();
|
565 |
pto = point.getPoint3D(0);
|
566 |
if (point.isTextPoint()) {
|
567 |
Geometry geom = gFactory.createPoint3D(pto.getX(), |
568 |
pto.getY(), pto.getZ()); |
569 |
feature.set(ID_FIELD_GEOMETRY, geom); |
570 |
} else {
|
571 |
Geometry geom = gFactory.createPoint3D(pto.getX(), |
572 |
pto.getY(), pto.getZ()); |
573 |
feature.set(ID_FIELD_GEOMETRY, geom); |
574 |
} |
575 |
} else if (fea.getGeometry() instanceof LineString |
576 |
&& !(fea.getGeometry() instanceof LineString3D)) {
|
577 |
GeneralPathX genPathX = new GeneralPathX();
|
578 |
Point2D[] pts = new Point2D[fea.getGeometry().pointNr()]; |
579 |
for (int j = 0; j < fea.getGeometry().pointNr(); j++) { |
580 |
pts[j] = fea.getGeometry().get(j); |
581 |
} |
582 |
genPathX.moveTo(pts[0].getX(), pts[0].getY()); |
583 |
for (int j = 1; j < pts.length; j++) { |
584 |
genPathX.lineTo(pts[j].getX(), pts[j].getY()); |
585 |
} |
586 |
Geometry geom = gFactory.createPolyline2D(genPathX); |
587 |
feature.set(ID_FIELD_GEOMETRY, geom); |
588 |
|
589 |
} else if (fea.getGeometry() instanceof LineString3D) { |
590 |
GeneralPathX genPathX = new GeneralPathX();
|
591 |
Point3D[] pts = new Point3D[fea.getGeometry().pointNr()]; |
592 |
for (int j = 0; j < fea.getGeometry().pointNr(); j++) { |
593 |
pts[j] = ((LineString3D) fea.getGeometry()) |
594 |
.getPoint3D(j); |
595 |
} |
596 |
genPathX.moveTo(pts[0].getX(), pts[0].getY()); |
597 |
for (int j = 1; j < pts.length; j++) { |
598 |
genPathX.lineTo(pts[j].getX(), pts[j].getY()); |
599 |
} |
600 |
double[] elevations = new double[pts.length]; |
601 |
for (int j = 0; j < pts.length; j++) { |
602 |
elevations[j] = pts[j].getZ(); |
603 |
} |
604 |
Geometry geom = gFactory.createPolyline3D(genPathX, |
605 |
elevations); |
606 |
feature.set(ID_FIELD_GEOMETRY, geom); |
607 |
|
608 |
} else if (fea.getGeometry() instanceof Polygon |
609 |
&& !(fea.getGeometry() instanceof Polygon3D)) {
|
610 |
GeneralPathX genPathX = new GeneralPathX();
|
611 |
Point2D firstPt = new Point2D.Double(); |
612 |
firstPt = fea.getGeometry().get(0);
|
613 |
Point2D[] pts = new Point2D[fea.getGeometry().pointNr() + 1]; |
614 |
for (int j = 0; j < fea.getGeometry().pointNr(); j++) { |
615 |
pts[j] = fea.getGeometry().get(j); |
616 |
} |
617 |
pts[fea.getGeometry().pointNr()] = firstPt; |
618 |
genPathX.moveTo(pts[0].getX(), pts[0].getY()); |
619 |
for (int j = 1; j < pts.length; j++) { |
620 |
genPathX.lineTo(pts[j].getX(), pts[j].getY()); |
621 |
} |
622 |
Geometry geom = gFactory.createPolygon2D(genPathX); |
623 |
feature.set(ID_FIELD_GEOMETRY, geom); |
624 |
|
625 |
} else if (fea.getGeometry() instanceof Polygon3D) { |
626 |
GeneralPathX genPathX = new GeneralPathX();
|
627 |
Point3D firstPt = new Point3D();
|
628 |
firstPt = ((Polygon3D) fea.getGeometry()).getPoint3D(0);
|
629 |
Point3D[] pts = new Point3D[fea.getGeometry().pointNr() + 1]; |
630 |
for (int j = 0; j < fea.getGeometry().pointNr(); j++) { |
631 |
pts[j] = ((Polygon3D) fea.getGeometry()) |
632 |
.getPoint3D(j); |
633 |
} |
634 |
pts[fea.getGeometry().pointNr()] = firstPt; |
635 |
genPathX.moveTo(pts[0].getX(), pts[0].getY()); |
636 |
for (int j = 1; j < pts.length; j++) { |
637 |
genPathX.lineTo(pts[j].getX(), pts[j].getY()); |
638 |
} |
639 |
double[] elevations = new double[pts.length]; |
640 |
for (int j = 0; j < pts.length; j++) { |
641 |
elevations[j] = pts[j].getZ(); |
642 |
} |
643 |
Geometry geom = gFactory.createPolygon3D(genPathX, |
644 |
elevations); |
645 |
feature.set(ID_FIELD_GEOMETRY, geom); |
646 |
} else {
|
647 |
getLogger().warn( |
648 |
MessageFormat.format(
|
649 |
"load: geometry type {1} not supported",
|
650 |
new Object[] { fea.getGeometry().getClass().getName() } |
651 |
) |
652 |
); |
653 |
} |
654 |
} catch (Exception e) { |
655 |
throw new LoadException(e, fileName); |
656 |
} |
657 |
if (leyendBuilder != null) { |
658 |
leyendBuilder.process(feature); |
659 |
} |
660 |
|
661 |
} |
662 |
} |
663 |
|
664 |
} |
665 |
|
666 |
public class Writer { |
667 |
private Double DEFAULT_ELEVATION = new Double(0); |
668 |
|
669 |
private DxfFile.EntityFactory entityMaker;
|
670 |
|
671 |
private IProjection proj = null; |
672 |
|
673 |
private int handle = 40; // Revisar porqu? es 40. |
674 |
|
675 |
private int k = 0; |
676 |
|
677 |
private boolean dxf3DFile = false; |
678 |
private String fileName; |
679 |
|
680 |
public Writer initialice(File file, IProjection projection) { |
681 |
this.proj = projection;
|
682 |
this.fileName = file.getAbsolutePath();
|
683 |
entityMaker = new DxfEntityMaker(proj);
|
684 |
|
685 |
return this; |
686 |
} |
687 |
|
688 |
public void begin() { |
689 |
entityMaker = new DxfEntityMaker(proj);
|
690 |
} |
691 |
|
692 |
public void end() throws WriteException { |
693 |
try {
|
694 |
DxfFile dxfFile = new DxfFile(null, fileName, entityMaker); |
695 |
dxfFile.setCadFlag(true);
|
696 |
if (dxf3DFile) {
|
697 |
dxfFile.setDxf3DFlag(true);
|
698 |
} |
699 |
dxfFile.save(fileName); |
700 |
dxfFile.close(); |
701 |
} catch (Exception e) { |
702 |
throw new WriteException(fileName, e); |
703 |
} |
704 |
} |
705 |
|
706 |
public void add(FeatureData feature) throws WriteException { |
707 |
try {
|
708 |
Geometry geom = feature.getDefaultGeometry(); |
709 |
|
710 |
if (Point2DZ.class.isAssignableFrom(geom.getClass())) {
|
711 |
dxf3DFile = true;
|
712 |
k = createPoint3D(handle, k, feature); |
713 |
|
714 |
} else if (org.gvsig.fmap.geom.primitive.Point2D.class |
715 |
.isAssignableFrom(geom.getClass())) { |
716 |
k = createPoint2D(handle, k, feature); |
717 |
|
718 |
} else if (Curve2DZ.class.isAssignableFrom(geom.getClass())) { |
719 |
dxf3DFile = true;
|
720 |
k = createPolyline3D(handle, k, feature); |
721 |
|
722 |
} else if (Arc2D.class.isAssignableFrom(geom.getClass())) { |
723 |
k = createArc2D(handle, k, feature); |
724 |
|
725 |
} else if (Curve2D.class.isAssignableFrom(geom.getClass())) { |
726 |
k = createLwPolyline2D(handle, k, feature, false);
|
727 |
|
728 |
} else if (Surface2DZ.class.isAssignableFrom(geom.getClass())) { |
729 |
dxf3DFile = true;
|
730 |
k = createPolyline3D(handle, k, feature); |
731 |
|
732 |
} else if (Circle2D.class.isAssignableFrom(geom.getClass())) { |
733 |
k = createCircle2D(handle, k, feature); |
734 |
|
735 |
} else if (Ellipse2D.class.isAssignableFrom(geom.getClass())) { |
736 |
k = createEllipse2D(handle, k, feature); |
737 |
|
738 |
} else if (Surface2D.class.isAssignableFrom(geom.getClass())) { |
739 |
k = createLwPolyline2D(handle, k, feature, true);
|
740 |
|
741 |
} else {
|
742 |
getLogger().warn( |
743 |
MessageFormat.format(
|
744 |
"Geometry '{1}' not yet supported",
|
745 |
new Object[] {geom.getClass().getName()} |
746 |
) |
747 |
); |
748 |
k++; |
749 |
} |
750 |
} catch (Exception e) { |
751 |
throw new WriteException(fileName, e); |
752 |
} |
753 |
|
754 |
} |
755 |
|
756 |
private boolean hasText(FeatureData feature) { |
757 |
if (feature.isNull(ID_FIELD_TEXT)) {
|
758 |
return false; |
759 |
} |
760 |
if (feature.get(ID_FIELD_TEXT).equals("")) { |
761 |
return false; |
762 |
} |
763 |
return true; |
764 |
} |
765 |
|
766 |
private DxfGroupVector updateProperties(FeatureData feature, int k) { |
767 |
DxfGroupVector polv = new DxfGroupVector();
|
768 |
|
769 |
String layer = (String) feature.get(ID_FIELD_LAYER); |
770 |
Integer color = (Integer) feature.get(ID_FIELD_COLOR); |
771 |
Double thickness = (Double) feature.get(ID_FIELD_THICKNESS); |
772 |
|
773 |
DxfGroup geometryLayer = new DxfGroup(8, layer); |
774 |
|
775 |
DxfGroup handleGroup = new DxfGroup();
|
776 |
handleGroup.setCode(5);
|
777 |
handleGroup.setData(new Integer(handle + k).toString()); |
778 |
|
779 |
DxfGroup handleColor = new DxfGroup();
|
780 |
handleColor.setCode(62);
|
781 |
handleColor.setData(color); |
782 |
|
783 |
DxfGroup handleThickness = new DxfGroup();
|
784 |
handleThickness.setCode(39);
|
785 |
handleThickness.setData(thickness); |
786 |
|
787 |
polv.add(geometryLayer); |
788 |
polv.add(handleGroup); |
789 |
polv.add(handleColor); |
790 |
return polv;
|
791 |
} |
792 |
|
793 |
private int createPoint2D(int handle, int k, FeatureData feature) |
794 |
throws Exception { |
795 |
|
796 |
if (hasText(feature)) {
|
797 |
return createText2D(handle, k, feature);
|
798 |
} |
799 |
org.gvsig.fmap.geom.primitive.Point2D point = new org.gvsig.fmap.geom.primitive.Point2D(
|
800 |
0, 0); |
801 |
double[] pointCoords = new double[6]; |
802 |
PathIterator pointIt = (feature.getDefaultGeometry())
|
803 |
.getPathIterator(null);
|
804 |
while (!pointIt.isDone()) {
|
805 |
pointIt.currentSegment(pointCoords); |
806 |
point = new org.gvsig.fmap.geom.primitive.Point2D(
|
807 |
pointCoords[0], pointCoords[1]); |
808 |
pointIt.next(); |
809 |
} |
810 |
Point2D pto = new Point2D.Double(point.getX(), point.getY()); |
811 |
|
812 |
DxfGroup px = new DxfGroup();
|
813 |
DxfGroup py = new DxfGroup();
|
814 |
DxfGroup pz = new DxfGroup();
|
815 |
px.setCode(10);
|
816 |
px.setData(new Double(pto.getX())); |
817 |
py.setCode(20);
|
818 |
py.setData(new Double(pto.getY())); |
819 |
pz.setCode(30);
|
820 |
// FIXME: POINT del DXF tiene cota. Le asigno cero arbitrariamente.
|
821 |
pz.setData(new Double(0.0)); |
822 |
DxfGroupVector pv = updateProperties(feature, k); |
823 |
pv.add(px); |
824 |
pv.add(py); |
825 |
pv.add(pz); |
826 |
entityMaker.createPoint(pv); |
827 |
k++; |
828 |
return k;
|
829 |
} |
830 |
|
831 |
private int createText2D(int handle, int k, FeatureData feature) |
832 |
throws Exception { |
833 |
|
834 |
String text = feature.get(ID_FIELD_TEXT).toString();
|
835 |
Double heightText = (Double) feature.get(ID_FIELD_HEIGHTTEXT); |
836 |
Double rotationText = (Double) feature.get(ID_FIELD_ROTATIONTEXT); |
837 |
|
838 |
DxfGroup handleText = new DxfGroup();
|
839 |
handleText.setCode(1);
|
840 |
handleText.setData(text); |
841 |
|
842 |
DxfGroup handleHeightText = new DxfGroup();
|
843 |
handleHeightText.setCode(40);
|
844 |
handleHeightText.setData(heightText); |
845 |
|
846 |
DxfGroup handleRotationText = new DxfGroup();
|
847 |
handleRotationText.setCode(50);
|
848 |
handleRotationText.setData(rotationText); |
849 |
|
850 |
org.gvsig.fmap.geom.primitive.Point2D point = new org.gvsig.fmap.geom.primitive.Point2D(
|
851 |
0, 0); |
852 |
double[] pointCoords = new double[6]; |
853 |
PathIterator pointIt = (feature.getDefaultGeometry())
|
854 |
.getPathIterator(null);
|
855 |
while (!pointIt.isDone()) {
|
856 |
pointIt.currentSegment(pointCoords); |
857 |
point = new org.gvsig.fmap.geom.primitive.Point2D(
|
858 |
pointCoords[0], pointCoords[1]); |
859 |
pointIt.next(); |
860 |
} |
861 |
Point2D pto = new Point2D.Double(point.getX(), point.getY()); |
862 |
DxfGroup handleGroup = new DxfGroup();
|
863 |
handleGroup.setCode(5);
|
864 |
handleGroup.setData(new Integer(handle + k).toString()); |
865 |
DxfGroup px = new DxfGroup();
|
866 |
DxfGroup py = new DxfGroup();
|
867 |
DxfGroup pz = new DxfGroup();
|
868 |
px.setCode(10);
|
869 |
px.setData(new Double(pto.getX())); |
870 |
py.setCode(20);
|
871 |
py.setData(new Double(pto.getY())); |
872 |
pz.setCode(30);
|
873 |
// FIXME: POINT del DXF tiene cota. Le asigno cero arbitrariamente.
|
874 |
pz.setData(new Double(0.0)); |
875 |
DxfGroupVector pv = updateProperties(feature, k); |
876 |
pv.add(handleText); |
877 |
pv.add(handleHeightText); |
878 |
pv.add(handleRotationText); |
879 |
pv.add(handleGroup); |
880 |
pv.add(px); |
881 |
pv.add(py); |
882 |
pv.add(pz); |
883 |
entityMaker.createText(pv); |
884 |
k++; |
885 |
return k;
|
886 |
} |
887 |
|
888 |
private int createPoint3D(int handle, int k, FeatureData feature) |
889 |
throws Exception { |
890 |
if (hasText(feature)) {
|
891 |
return createText3D(handle, k, feature);
|
892 |
} |
893 |
Point2DZ point = new Point2DZ(0, 0, 0); |
894 |
double[] pointCoords = new double[6]; |
895 |
PathIterator pointIt = (feature.getDefaultGeometry())
|
896 |
.getPathIterator(null);
|
897 |
while (!pointIt.isDone()) {
|
898 |
pointIt.currentSegment(pointCoords); |
899 |
point = new Point2DZ(pointCoords[0], pointCoords[1], |
900 |
pointCoords[2]);
|
901 |
pointIt.next(); |
902 |
} |
903 |
Point3D pto = new Point3D(point.getX(), point.getY(),
|
904 |
point.getZs()[0]);
|
905 |
DxfGroup px = new DxfGroup();
|
906 |
DxfGroup py = new DxfGroup();
|
907 |
DxfGroup pz = new DxfGroup();
|
908 |
px.setCode(10);
|
909 |
px.setData(new Double(pto.getX())); |
910 |
py.setCode(20);
|
911 |
py.setData(new Double(pto.getY())); |
912 |
pz.setCode(30);
|
913 |
pz.setData(new Double(pto.getZ())); |
914 |
double[] velev = ((AbstractPrimitive) feature.getDefaultGeometry()) |
915 |
.getZs(); |
916 |
Double elevation = DEFAULT_ELEVATION;
|
917 |
elevation = new Double(velev[0]); |
918 |
DxfGroup handleElevation = new DxfGroup();
|
919 |
handleElevation.setCode(38);
|
920 |
handleElevation.setData(elevation); |
921 |
|
922 |
DxfGroupVector pv = updateProperties(feature, k); |
923 |
pv.add(handleElevation); |
924 |
pv.add(px); |
925 |
pv.add(py); |
926 |
pv.add(pz); |
927 |
entityMaker.createPoint(pv); |
928 |
k++; |
929 |
return k;
|
930 |
} |
931 |
|
932 |
private int createText3D(int handle, int k, FeatureData feature) |
933 |
throws Exception { |
934 |
|
935 |
double[] velev = ((AbstractPrimitive) feature.getDefaultGeometry()) |
936 |
.getZs(); |
937 |
|
938 |
Double elevation = new Double(velev[0]); |
939 |
String text = feature.get(ID_FIELD_TEXT).toString();
|
940 |
Double heightText = (Double) feature.get(ID_FIELD_HEIGHTTEXT); |
941 |
Double rotationText = (Double) feature.get(ID_FIELD_ROTATIONTEXT); |
942 |
|
943 |
DxfGroup handleText = new DxfGroup();
|
944 |
handleText.setCode(1);
|
945 |
handleText.setData(text); |
946 |
|
947 |
DxfGroup handleHeightText = new DxfGroup();
|
948 |
handleHeightText.setCode(40);
|
949 |
handleHeightText.setData(heightText); |
950 |
|
951 |
DxfGroup handleRotationText = new DxfGroup();
|
952 |
handleRotationText.setCode(50);
|
953 |
handleRotationText.setData(rotationText); |
954 |
|
955 |
DxfGroup handleElevation = new DxfGroup();
|
956 |
handleElevation.setCode(38);
|
957 |
handleElevation.setData(elevation); |
958 |
|
959 |
Point2DZ point = (Point2DZ) (feature |
960 |
.getDefaultGeometry()).getInternalShape(); |
961 |
|
962 |
DxfGroup handleGroup = new DxfGroup();
|
963 |
handleGroup.setCode(5);
|
964 |
handleGroup.setData(new Integer(handle + k).toString()); |
965 |
DxfGroup px = new DxfGroup();
|
966 |
DxfGroup py = new DxfGroup();
|
967 |
DxfGroup pz = new DxfGroup();
|
968 |
px.setCode(10);
|
969 |
px.setData(new Double(point.getX())); |
970 |
py.setCode(20);
|
971 |
py.setData(new Double(point.getY())); |
972 |
pz.setCode(30);
|
973 |
pz.setData(new Double(point.getZs()[0])); |
974 |
DxfGroupVector pv = updateProperties(feature, k); |
975 |
pv.add(handleElevation); |
976 |
pv.add(handleText); |
977 |
pv.add(handleHeightText); |
978 |
pv.add(handleRotationText); |
979 |
pv.add(handleGroup); |
980 |
pv.add(px); |
981 |
pv.add(py); |
982 |
pv.add(pz); |
983 |
entityMaker.createText(pv); |
984 |
k++; |
985 |
return k;
|
986 |
} |
987 |
|
988 |
private int createLwPolyline2D(int handle, int k, FeatureData feature, |
989 |
boolean isPolygon) throws Exception { |
990 |
boolean first = true; |
991 |
DxfGroupVector polv = updateProperties(feature, k); |
992 |
Vector vpoints = new Vector(); |
993 |
|
994 |
DxfGroup polylineFlag = new DxfGroup();
|
995 |
polylineFlag.setCode(70);
|
996 |
if (isPolygon) {
|
997 |
polylineFlag.setData(new Integer(1)); // cerrada |
998 |
} else {
|
999 |
polylineFlag.setData(new Integer(0)); // abierta |
1000 |
} |
1001 |
|
1002 |
PathIterator theIterator = (feature.getDefaultGeometry())
|
1003 |
.getPathIterator(null, Converter.FLATNESS); // polyLine. |
1004 |
// getPathIterator
|
1005 |
// (null,
|
1006 |
// flatness);
|
1007 |
|
1008 |
double[] theData = new double[6]; |
1009 |
while (!theIterator.isDone()) {
|
1010 |
int theType = theIterator.currentSegment(theData);
|
1011 |
switch (theType) {
|
1012 |
case PathIterator.SEG_MOVETO: |
1013 |
if (!first) {
|
1014 |
for (int j = 0; j < vpoints.size(); j++) { |
1015 |
DxfGroup xvertex = new DxfGroup();
|
1016 |
xvertex.setCode(10);
|
1017 |
xvertex |
1018 |
.setData(new Double( |
1019 |
((org.gvsig.fmap.geom.primitive.Point2D) vpoints |
1020 |
.get(j)).getX())); |
1021 |
DxfGroup yvertex = new DxfGroup();
|
1022 |
yvertex.setCode(20);
|
1023 |
yvertex |
1024 |
.setData(new Double( |
1025 |
((org.gvsig.fmap.geom.primitive.Point2D) vpoints |
1026 |
.get(j)).getY())); |
1027 |
polv.add(xvertex); |
1028 |
polv.add(yvertex); |
1029 |
} |
1030 |
|
1031 |
entityMaker.createLwPolyline(polv); |
1032 |
k++; |
1033 |
polv = updateProperties(feature, k); |
1034 |
|
1035 |
} |
1036 |
first = false;
|
1037 |
polv.add(polylineFlag); |
1038 |
vpoints.clear(); |
1039 |
vpoints.add(new org.gvsig.fmap.geom.primitive.Point2D(
|
1040 |
theData[0], theData[1])); |
1041 |
break;
|
1042 |
case PathIterator.SEG_LINETO: |
1043 |
vpoints.add(new org.gvsig.fmap.geom.primitive.Point2D(
|
1044 |
theData[0], theData[1])); |
1045 |
break;
|
1046 |
case PathIterator.SEG_QUADTO: |
1047 |
break;
|
1048 |
case PathIterator.SEG_CUBICTO: |
1049 |
break;
|
1050 |
case PathIterator.SEG_CLOSE: |
1051 |
polylineFlag.setData(new Integer(1)); // cerrada |
1052 |
break;
|
1053 |
|
1054 |
} |
1055 |
theIterator.next(); |
1056 |
} |
1057 |
|
1058 |
for (int j = 0; j < vpoints.size(); j++) { |
1059 |
DxfGroup xvertex = new DxfGroup();
|
1060 |
xvertex.setCode(10);
|
1061 |
xvertex |
1062 |
.setData(new Double( |
1063 |
((org.gvsig.fmap.geom.primitive.Point2D) vpoints |
1064 |
.get(j)).getX())); |
1065 |
DxfGroup yvertex = new DxfGroup();
|
1066 |
yvertex.setCode(20);
|
1067 |
yvertex |
1068 |
.setData(new Double( |
1069 |
((org.gvsig.fmap.geom.primitive.Point2D) vpoints |
1070 |
.get(j)).getY())); |
1071 |
polv.add(xvertex); |
1072 |
polv.add(yvertex); |
1073 |
} |
1074 |
|
1075 |
entityMaker.createLwPolyline(polv); |
1076 |
k++; |
1077 |
return k;
|
1078 |
} |
1079 |
|
1080 |
private int createPolyline3D(int handle, int k, FeatureData feature) |
1081 |
throws Exception { |
1082 |
DxfGroupVector polv = updateProperties(feature, k); |
1083 |
Vector vpoints = new Vector(); |
1084 |
PathIterator theIterator = (feature.getDefaultGeometry())
|
1085 |
.getPathIterator(null, Converter.FLATNESS); // polyLine. |
1086 |
// getPathIterator
|
1087 |
// (null,
|
1088 |
// flatness);
|
1089 |
double[] theData = new double[6]; |
1090 |
double[] velev = ((AbstractPrimitive) feature.getDefaultGeometry()) |
1091 |
.getZs(); |
1092 |
while (!theIterator.isDone()) {
|
1093 |
int theType = theIterator.currentSegment(theData);
|
1094 |
switch (theType) {
|
1095 |
case PathIterator.SEG_MOVETO: |
1096 |
vpoints.add(new org.gvsig.fmap.geom.primitive.Point2D(
|
1097 |
theData[0], theData[1])); |
1098 |
break;
|
1099 |
case PathIterator.SEG_LINETO: |
1100 |
vpoints.add(new org.gvsig.fmap.geom.primitive.Point2D(
|
1101 |
theData[0], theData[1])); |
1102 |
break;
|
1103 |
} |
1104 |
theIterator.next(); |
1105 |
} |
1106 |
if (constantElevation(velev)) {
|
1107 |
DxfGroup polylineFlag = new DxfGroup();
|
1108 |
polylineFlag.setCode(70);
|
1109 |
polylineFlag.setData(new Integer(0)); |
1110 |
polv.add(polylineFlag); |
1111 |
DxfGroup elevation = new DxfGroup();
|
1112 |
elevation.setCode(38);
|
1113 |
elevation.setData(new Double(velev[0])); |
1114 |
polv.add(elevation); |
1115 |
for (int j = 0; j < vpoints.size(); j++) { |
1116 |
DxfGroup xvertex = new DxfGroup();
|
1117 |
xvertex.setCode(10);
|
1118 |
xvertex.setData(new Double( |
1119 |
((org.gvsig.fmap.geom.primitive.Point2D) vpoints |
1120 |
.get(j)).getX())); |
1121 |
DxfGroup yvertex = new DxfGroup();
|
1122 |
yvertex.setCode(20);
|
1123 |
yvertex.setData(new Double( |
1124 |
((org.gvsig.fmap.geom.primitive.Point2D) vpoints |
1125 |
.get(j)).getY())); |
1126 |
polv.add(xvertex); |
1127 |
polv.add(yvertex); |
1128 |
} |
1129 |
entityMaker.createLwPolyline(polv); |
1130 |
k++; |
1131 |
} else {
|
1132 |
DxfGroup polylineFlag = new DxfGroup();
|
1133 |
polylineFlag.setCode(70);
|
1134 |
polylineFlag.setData(new Integer(8)); |
1135 |
polv.add(polylineFlag); |
1136 |
DxfGroup xgroup = new DxfGroup();
|
1137 |
xgroup.setCode(10);
|
1138 |
xgroup.setData(new Double(0.0)); |
1139 |
polv.add(xgroup); |
1140 |
DxfGroup ygroup = new DxfGroup();
|
1141 |
ygroup.setCode(20);
|
1142 |
ygroup.setData(new Double(0.0)); |
1143 |
polv.add(ygroup); |
1144 |
DxfGroup elevation = new DxfGroup();
|
1145 |
elevation.setCode(30);
|
1146 |
elevation.setData(new Double(0.0)); |
1147 |
polv.add(elevation); |
1148 |
DxfGroup subclassMarker = new DxfGroup(100, "AcDb3dPolyline"); |
1149 |
polv.add(subclassMarker); |
1150 |
entityMaker.createPolyline(polv); |
1151 |
k++; |
1152 |
for (int j = 0; j < vpoints.size(); j++) { |
1153 |
DxfGroupVector verv = new DxfGroupVector();
|
1154 |
DxfGroup entityType = new DxfGroup(0, "VERTEX"); |
1155 |
verv.add(entityType); |
1156 |
DxfGroup generalSubclassMarker = new DxfGroup(100, |
1157 |
"AcDbEntity");
|
1158 |
verv.add(generalSubclassMarker); |
1159 |
DxfGroup layerName = new DxfGroup(8, "default"); |
1160 |
verv.add(layerName); |
1161 |
DxfGroup vertexSubclassMarker = new DxfGroup(100, |
1162 |
"AcDbVertex");
|
1163 |
verv.add(vertexSubclassMarker); |
1164 |
DxfGroup xvertex = new DxfGroup();
|
1165 |
xvertex.setCode(10);
|
1166 |
xvertex.setData(new Double( |
1167 |
((org.gvsig.fmap.geom.primitive.Point2D) vpoints |
1168 |
.get(j)).getX())); |
1169 |
DxfGroup yvertex = new DxfGroup();
|
1170 |
yvertex.setCode(20);
|
1171 |
yvertex.setData(new Double( |
1172 |
((org.gvsig.fmap.geom.primitive.Point2D) vpoints |
1173 |
.get(j)).getY())); |
1174 |
DxfGroup zvertex = new DxfGroup();
|
1175 |
zvertex.setCode(30);
|
1176 |
zvertex.setData(new Double(velev[j])); |
1177 |
verv.add(xvertex); |
1178 |
verv.add(yvertex); |
1179 |
verv.add(zvertex); |
1180 |
entityMaker.addVertex(verv); |
1181 |
k++; |
1182 |
} |
1183 |
DxfGroupVector seqv = new DxfGroupVector();
|
1184 |
DxfGroup entityType = new DxfGroup(0, "SEQEND"); |
1185 |
seqv.add(entityType); |
1186 |
DxfGroup generalSubclassMarker = new DxfGroup(100, "AcDbEntity"); |
1187 |
seqv.add(generalSubclassMarker); |
1188 |
DxfGroup layerName = new DxfGroup(8, "default"); |
1189 |
seqv.add(layerName); |
1190 |
DxfGroup handleSeqGroup = new DxfGroup();
|
1191 |
handleSeqGroup.setCode(5);
|
1192 |
handleSeqGroup.setData(new Integer(handle + k).toString()); |
1193 |
seqv.add(handleSeqGroup); |
1194 |
entityMaker.endSeq(); |
1195 |
k++; |
1196 |
} |
1197 |
return k;
|
1198 |
} |
1199 |
|
1200 |
private boolean constantElevation(double[] velev) { |
1201 |
boolean constant = true; |
1202 |
for (int i = 0; i < velev.length; i++) { |
1203 |
for (int j = 0; j < velev.length; j++) { |
1204 |
if (j > i) {
|
1205 |
if (velev[i] != velev[j]) {
|
1206 |
constant = false;
|
1207 |
break;
|
1208 |
} |
1209 |
} |
1210 |
} |
1211 |
break;
|
1212 |
} |
1213 |
return constant;
|
1214 |
} |
1215 |
|
1216 |
private int createCircle2D(int handle, int k, FeatureData feature) |
1217 |
throws Exception { |
1218 |
DxfGroupVector polv = updateProperties(feature, k); |
1219 |
DxfGroup circleFlag = new DxfGroup();
|
1220 |
circleFlag.setCode(100);
|
1221 |
polv.add(circleFlag); |
1222 |
|
1223 |
DxfGroup xvertex = new DxfGroup();
|
1224 |
xvertex.setCode(10);
|
1225 |
Circle2D circle = (Circle2D) (feature |
1226 |
.getDefaultGeometry()).getInternalShape(); |
1227 |
xvertex.setData(new Double(circle.getCenter().getX())); |
1228 |
DxfGroup yvertex = new DxfGroup();
|
1229 |
yvertex.setCode(20);
|
1230 |
yvertex.setData(new Double(circle.getCenter().getY())); |
1231 |
DxfGroup zvertex = new DxfGroup();
|
1232 |
zvertex.setCode(30);
|
1233 |
// TODO: COORDENADA Z. REVISAR ESTO PARA ENTIDADES 3D
|
1234 |
zvertex.setData(new Double(0)); |
1235 |
|
1236 |
DxfGroup radius = new DxfGroup();
|
1237 |
radius.setCode(40);
|
1238 |
radius.setData(new Double(circle.getRadio())); |
1239 |
|
1240 |
polv.add(xvertex); |
1241 |
polv.add(yvertex); |
1242 |
polv.add(zvertex); |
1243 |
polv.add(radius); |
1244 |
|
1245 |
entityMaker.createCircle(polv); |
1246 |
k++; |
1247 |
return k;
|
1248 |
} |
1249 |
|
1250 |
private int createArc2D(int handle, int k, FeatureData feature) |
1251 |
throws Exception { |
1252 |
Arc2D arc = (Arc2D) (feature.getDefaultGeometry()) |
1253 |
.getInternalShape(); |
1254 |
Point2D[] pts = new Point2D[3]; |
1255 |
pts[0] = arc.getInit();
|
1256 |
pts[1] = arc.getMid();
|
1257 |
pts[2] = arc.getEnd();
|
1258 |
Point2D center = arc.getCenter();
|
1259 |
double radius = center.distance(pts[0]); |
1260 |
double initAngle = UtilFunctions.getAngle(center, pts[0]); |
1261 |
initAngle = Math.toDegrees(initAngle);
|
1262 |
double midAngle = UtilFunctions.getAngle(center, pts[1]); |
1263 |
midAngle = Math.toDegrees(midAngle);
|
1264 |
double endAngle = UtilFunctions.getAngle(center, pts[2]); |
1265 |
endAngle = Math.toDegrees(endAngle);
|
1266 |
|
1267 |
DxfGroup ax = new DxfGroup();
|
1268 |
DxfGroup ay = new DxfGroup();
|
1269 |
DxfGroup ac = new DxfGroup();
|
1270 |
DxfGroup ai = new DxfGroup();
|
1271 |
DxfGroup ae = new DxfGroup();
|
1272 |
ax.setCode(10);
|
1273 |
ax.setData(new Double(center.getX())); |
1274 |
ay.setCode(20);
|
1275 |
ay.setData(new Double(center.getY())); |
1276 |
ac.setCode(40);
|
1277 |
ac.setData(new Double(radius)); |
1278 |
ai.setCode(50);
|
1279 |
ai.setData(new Double(initAngle)); |
1280 |
ae.setCode(51);
|
1281 |
ae.setData(new Double(endAngle)); |
1282 |
DxfGroupVector av = updateProperties(feature, k); |
1283 |
av.add(ax); |
1284 |
av.add(ay); |
1285 |
av.add(ac); |
1286 |
av.add(ai); |
1287 |
av.add(ae); |
1288 |
entityMaker.createArc(av); |
1289 |
k++; |
1290 |
return k;
|
1291 |
} |
1292 |
|
1293 |
private int createEllipse2D(int handle, int k, FeatureData feature) |
1294 |
throws Exception { |
1295 |
Ellipse2D ellipse = (Ellipse2D) (feature |
1296 |
.getDefaultGeometry()).getInternalShape(); |
1297 |
|
1298 |
Point2D center = new Point2D.Double( |
1299 |
(ellipse.getInit().getX() + ellipse.getEnd().getX()) / 2,
|
1300 |
(ellipse.getInit().getY() + ellipse.getEnd().getY()) / 2);
|
1301 |
double mAxisL = ellipse.getDist() * 2; |
1302 |
double maAxisL = ellipse.getInit().distance(ellipse.getEnd());
|
1303 |
|
1304 |
Point2D endPointOfMajorAxis = ellipse.getEnd();
|
1305 |
double azimut = Math |
1306 |
.atan2(endPointOfMajorAxis.getX() - center.getX(), |
1307 |
endPointOfMajorAxis.getY() - center.getY()); |
1308 |
double azimut2 = azimut + Math.PI / 2.0; |
1309 |
if (azimut2 >= Math.PI * 2) { |
1310 |
azimut2 = azimut2 - Math.PI * 2; |
1311 |
} |
1312 |
Point2D endPointOfMinorAxis = new Point2D.Double(center.getX() |
1313 |
+ (ellipse.getDist() * Math.sin(azimut2)), center.getY()
|
1314 |
+ (ellipse.getDist() * Math.cos(azimut2)));
|
1315 |
|
1316 |
if (mAxisL >= maAxisL) {
|
1317 |
// El menor debe ser menor que el mayor. Los cambiamos.
|
1318 |
double aux = mAxisL;
|
1319 |
mAxisL = maAxisL; |
1320 |
maAxisL = aux; |
1321 |
// Tambi?n cambiamos los puntos finales de los ejes.
|
1322 |
Point2D pAux = endPointOfMinorAxis;
|
1323 |
endPointOfMinorAxis = endPointOfMajorAxis; |
1324 |
endPointOfMajorAxis = pAux; |
1325 |
} |
1326 |
double mToMAR = mAxisL / maAxisL;
|
1327 |
DxfGroup x = new DxfGroup();
|
1328 |
DxfGroup y = new DxfGroup();
|
1329 |
DxfGroup xc = new DxfGroup();
|
1330 |
DxfGroup yc = new DxfGroup();
|
1331 |
DxfGroup minToMaj = new DxfGroup();
|
1332 |
x.setCode(10);
|
1333 |
x.setData(new Double(center.getX())); |
1334 |
y.setCode(20);
|
1335 |
y.setData(new Double(center.getY())); |
1336 |
xc.setCode(11);
|
1337 |
xc.setData(new Double(endPointOfMajorAxis.getX() - center.getX())); |
1338 |
yc.setCode(21);
|
1339 |
yc.setData(new Double(endPointOfMajorAxis.getY() - center.getY())); |
1340 |
minToMaj.setCode(40);
|
1341 |
minToMaj.setData(new Double(mToMAR)); |
1342 |
DxfGroupVector av = updateProperties(feature, k); |
1343 |
av.add(x); |
1344 |
av.add(y); |
1345 |
av.add(xc); |
1346 |
av.add(yc); |
1347 |
av.add(minToMaj); |
1348 |
entityMaker.createEllipse(av); |
1349 |
k++; |
1350 |
return k;
|
1351 |
} |
1352 |
|
1353 |
} |
1354 |
|
1355 |
public boolean closeResourceRequested(ResourceProvider resource) { |
1356 |
return true; |
1357 |
} |
1358 |
|
1359 |
public int getFeatureReferenceOIDType() { |
1360 |
return DataTypes.LONG;
|
1361 |
} |
1362 |
|
1363 |
public boolean supportsAppendMode() { |
1364 |
return false; |
1365 |
} |
1366 |
|
1367 |
public void append(org.gvsig.fmap.dal.feature.Feature feature) { |
1368 |
throw new UnsupportedOperationException(); |
1369 |
} |
1370 |
|
1371 |
public void beginAppend() { |
1372 |
throw new UnsupportedOperationException(); |
1373 |
} |
1374 |
|
1375 |
public void endAppend() { |
1376 |
throw new UnsupportedOperationException(); |
1377 |
} |
1378 |
|
1379 |
public PersistentState getState() throws PersistenceException { |
1380 |
return AbstractPersistenceManager.getState(this); |
1381 |
} |
1382 |
|
1383 |
public void loadState(PersistentState state) throws PersistenceException { |
1384 |
// TODO Auto-generated method stub
|
1385 |
throw new NotYetImplemented(); |
1386 |
} |
1387 |
|
1388 |
public void setState(PersistentState state) throws PersistenceException { |
1389 |
// TODO Auto-generated method stub
|
1390 |
throw new NotYetImplemented(); |
1391 |
} |
1392 |
|
1393 |
public Object createNewOID() { |
1394 |
return new Integer(++counterNewsOIDs); |
1395 |
} |
1396 |
|
1397 |
protected void initializeFeatureTypes() throws InitializeException { |
1398 |
try {
|
1399 |
this.open();
|
1400 |
} catch (OpenException e) {
|
1401 |
throw new InitializeException(this.getName(), e); |
1402 |
} |
1403 |
} |
1404 |
|
1405 |
public boolean canCreate(DataStoreParameters parameters) { |
1406 |
if (!(parameters instanceof DXFStoreParameters)) { |
1407 |
throw new IllegalArgumentException(); // FIXME ??? |
1408 |
} |
1409 |
DXFStoreParameters dxfParams =(DXFStoreParameters) parameters; |
1410 |
// TODO comporbar si el ftype es correcto (para este formato es fijo)
|
1411 |
File file = new File(dxfParams.getFileName()); |
1412 |
|
1413 |
if (dxfParams.getSRSID() == null) { |
1414 |
return false; |
1415 |
} |
1416 |
// TODO comprobamos extension del fichero ??
|
1417 |
if (file.exists()) {
|
1418 |
return file.canWrite();
|
1419 |
} else {
|
1420 |
return file.getParentFile().canWrite();
|
1421 |
} |
1422 |
} |
1423 |
|
1424 |
public void remove(DataStoreParameters parameters) throws RemoveException { |
1425 |
File file = new File(((DXFStoreParameters) parameters).getFileName()); |
1426 |
if (!file.exists()) {
|
1427 |
throw new RemoveException(this.getName(), |
1428 |
new FileNotFoundException(file)); |
1429 |
} |
1430 |
if (!file.delete()) {
|
1431 |
// FIXME throws ???
|
1432 |
} |
1433 |
|
1434 |
} |
1435 |
|
1436 |
public boolean canCreate(NewDataStoreParameters parameters) { |
1437 |
// TODO Auto-generated method stub
|
1438 |
return false; |
1439 |
} |
1440 |
|
1441 |
public NewDataStoreParameters getCreateParameters() throws DataException { |
1442 |
return (NewFeatureStoreParameters) DALLocator.getDataManager()
|
1443 |
.createStoreParameters(this.getName());
|
1444 |
} |
1445 |
|
1446 |
|
1447 |
} |