svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.exportto / org.gvsig.exportto.lib / org.gvsig.exportto.lib.impl / src / main / java / org / gvsig / export / impl / service / DefaultExportGeometryHelper.java @ 43939
History | View | Annotate | Download (6.63 KB)
1 |
package org.gvsig.export.impl.service; |
---|---|
2 |
|
3 |
import org.apache.commons.lang3.StringUtils; |
4 |
import org.cresques.cts.ICoordTrans; |
5 |
import org.cresques.cts.IProjection; |
6 |
import org.gvsig.export.ExportLocator; |
7 |
import org.gvsig.export.ExportParametersGeometry; |
8 |
import org.gvsig.export.spi.ExportGeometryHelper; |
9 |
import org.gvsig.export.spi.ExportServiceManager; |
10 |
import org.gvsig.fmap.dal.DataTypes; |
11 |
import org.gvsig.fmap.dal.feature.EditableFeature; |
12 |
import org.gvsig.fmap.dal.feature.Feature; |
13 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
14 |
import org.gvsig.fmap.dal.feature.FeatureType; |
15 |
import org.gvsig.fmap.geom.Geometry; |
16 |
|
17 |
/**
|
18 |
*
|
19 |
* @author jjdelcerro
|
20 |
*/
|
21 |
public class DefaultExportGeometryHelper implements ExportGeometryHelper { |
22 |
private final int targetGeometryIndex; |
23 |
private final int sourceGeometryIndex; |
24 |
private final ICoordTrans coord_trans; |
25 |
|
26 |
private final ExportServiceManager serviceManager; |
27 |
private final ExportParametersGeometry parameters; |
28 |
private String lastErrorMessage; |
29 |
|
30 |
public DefaultExportGeometryHelper(
|
31 |
ExportParametersGeometry parameters, |
32 |
FeatureType theTargetFeatureType, |
33 |
FeatureType theSourceFeatureType |
34 |
) { |
35 |
this.serviceManager = ExportLocator.getServiceManager();
|
36 |
this.parameters = parameters;
|
37 |
|
38 |
FeatureAttributeDescriptor sourceGeomAtt = theSourceFeatureType.getDefaultGeometryAttribute(); |
39 |
FeatureAttributeDescriptor targetGeomAtt; |
40 |
|
41 |
String sourceGeometryFieldName = this.parameters.getSourceGeometryFieldName(); |
42 |
if( !StringUtils.isEmpty(sourceGeometryFieldName) ) {
|
43 |
sourceGeomAtt = theSourceFeatureType.getAttributeDescriptor(sourceGeometryFieldName); |
44 |
} |
45 |
if (sourceGeomAtt == null) { |
46 |
// Si el origen no tiene geometria, no haremos nada con las
|
47 |
// geometrias.
|
48 |
this.targetGeometryIndex = -1; |
49 |
this.sourceGeometryIndex = -1; |
50 |
this.coord_trans = null; |
51 |
return;
|
52 |
} |
53 |
switch( this.getGeometryColumnCount(theTargetFeatureType) ) { |
54 |
case 0: |
55 |
// Si el destino no tiene campo geometry no haremos nada con
|
56 |
// las geometrias.
|
57 |
this.targetGeometryIndex = -1; |
58 |
this.sourceGeometryIndex = -1; |
59 |
this.coord_trans = null; |
60 |
return;
|
61 |
case 1: |
62 |
// Si solo hay una columna de geometria asignaremos las geometrias
|
63 |
// independientemente de como se llamen los campos.
|
64 |
targetGeomAtt = theTargetFeatureType.getDefaultGeometryAttribute(); |
65 |
break;
|
66 |
default:
|
67 |
// Si hay mas de una geometria en el target y hay un campo de tipo
|
68 |
// geometria que coincida en nombre con el del source, usamos ese.
|
69 |
targetGeomAtt = theTargetFeatureType.getAttributeDescriptor(sourceGeomAtt.getName()); |
70 |
if( targetGeomAtt == null || targetGeomAtt.getType()!=DataTypes.GEOMETRY ) { |
71 |
// Si no coinciden por nombre y tipo, pillaremos el primer campo
|
72 |
// geometry por defecto del target.
|
73 |
targetGeomAtt = theTargetFeatureType.getDefaultGeometryAttribute(); |
74 |
if( targetGeomAtt==null ) { |
75 |
targetGeomAtt = this.getFirstGeometryColumn(theTargetFeatureType);
|
76 |
if( targetGeomAtt == null ) { |
77 |
this.targetGeometryIndex = -1; |
78 |
this.sourceGeometryIndex = -1; |
79 |
this.coord_trans = null; |
80 |
return;
|
81 |
} |
82 |
} |
83 |
} |
84 |
} |
85 |
|
86 |
IProjection targetProjection = this.parameters.getTargetProjection();
|
87 |
IProjection sourceProjection = sourceGeomAtt.getSRS(); |
88 |
if( targetProjection == null ) { |
89 |
targetProjection = sourceProjection; |
90 |
} |
91 |
// this comparison is perhaps too preventive
|
92 |
// we could have two instances of same projection
|
93 |
// so we would do more computations than needed
|
94 |
if (sourceProjection != null && targetProjection != null && sourceProjection != targetProjection) { |
95 |
this.coord_trans = sourceProjection.getCT(targetProjection);
|
96 |
} else {
|
97 |
this.coord_trans = null; |
98 |
} |
99 |
this.sourceGeometryIndex = sourceGeomAtt.getIndex();
|
100 |
this.targetGeometryIndex = targetGeomAtt.getIndex();
|
101 |
} |
102 |
|
103 |
private int getGeometryColumnCount(FeatureType featureType) { |
104 |
int count = 0; |
105 |
for( int i=0; i<featureType.size(); i++ ) { |
106 |
if( featureType.getAttributeDescriptor(i).getType()==DataTypes.GEOMETRY ) {
|
107 |
count++; |
108 |
} |
109 |
} |
110 |
return count;
|
111 |
} |
112 |
|
113 |
@Override
|
114 |
public String getLastErrorMessage() { |
115 |
return this.lastErrorMessage; |
116 |
} |
117 |
|
118 |
@Override
|
119 |
public int copyGeometry( |
120 |
Feature sourceFeature, |
121 |
EditableFeature targetFeature |
122 |
) { |
123 |
if( this.sourceGeometryIndex<0 || this.targetGeometryIndex<0 ) { |
124 |
return ExportServiceManager.FixGeometryStatus.STATE_OK;
|
125 |
} |
126 |
Geometry geometry = sourceFeature.getGeometry(this.sourceGeometryIndex);
|
127 |
ExportServiceManager.FixGeometryStatus check = serviceManager.fixGeometry( |
128 |
this.parameters,
|
129 |
this.coord_trans,
|
130 |
geometry |
131 |
); |
132 |
if( check.getState() == ExportServiceManager.FixGeometryStatus.STATE_OK ) {
|
133 |
targetFeature.setGeometry(this.targetGeometryIndex, check.getGeometry());
|
134 |
} |
135 |
this.lastErrorMessage = check.getMessage();
|
136 |
return check.getState();
|
137 |
} |
138 |
|
139 |
@Override
|
140 |
public boolean canProcessGeometry() { |
141 |
return this.sourceGeometryIndex>=0 && this.targetGeometryIndex>=0 ; |
142 |
} |
143 |
|
144 |
private FeatureAttributeDescriptor getFirstGeometryColumn(FeatureType featureType) {
|
145 |
for( int i=0; i<featureType.size(); i++ ) { |
146 |
FeatureAttributeDescriptor descriptor = featureType.getAttributeDescriptor(i); |
147 |
if( descriptor.getType()==DataTypes.GEOMETRY ) {
|
148 |
return descriptor;
|
149 |
} |
150 |
} |
151 |
return null; |
152 |
} |
153 |
|
154 |
|
155 |
} |