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