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 @ 47524
History | View | Annotate | Download (7.43 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 |
private boolean isExtraColumn; |
26 |
private String sourceGeometryName; |
27 |
|
28 |
private final ExportServiceManager serviceManager; |
29 |
private final ExportParametersGeometry parameters; |
30 |
private String lastErrorMessage; |
31 |
|
32 |
public DefaultExportGeometryHelper(
|
33 |
ExportParametersGeometry parameters, |
34 |
FeatureType theTargetFeatureType, |
35 |
FeatureType theSourceFeatureType |
36 |
) { |
37 |
this.serviceManager = ExportLocator.getServiceManager();
|
38 |
this.parameters = parameters;
|
39 |
|
40 |
FeatureAttributeDescriptor sourceGeomAtt = theSourceFeatureType.getDefaultGeometryAttribute(); |
41 |
FeatureAttributeDescriptor targetGeomAtt; |
42 |
|
43 |
this.isExtraColumn = false; |
44 |
String sourceGeometryFieldName = this.parameters.getSourceGeometryFieldName(); |
45 |
if( !StringUtils.isEmpty(sourceGeometryFieldName) ) {
|
46 |
sourceGeomAtt = theSourceFeatureType.getAttributeDescriptor(sourceGeometryFieldName); |
47 |
if (sourceGeomAtt == null) { |
48 |
sourceGeomAtt = theSourceFeatureType.getExtraColumns().get(sourceGeometryFieldName); |
49 |
this.isExtraColumn = (sourceGeomAtt!=null); |
50 |
} |
51 |
} |
52 |
if (sourceGeomAtt == null) { |
53 |
// Si el origen no tiene geometria, no haremos nada con las
|
54 |
// geometrias.
|
55 |
this.targetGeometryIndex = -1; |
56 |
this.sourceGeometryIndex = -1; |
57 |
this.coord_trans = null; |
58 |
return;
|
59 |
} |
60 |
switch( this.getGeometryColumnCount(theTargetFeatureType) ) { |
61 |
case 0: |
62 |
// Si el destino no tiene campo geometry no haremos nada con
|
63 |
// las geometrias.
|
64 |
this.targetGeometryIndex = -1; |
65 |
this.sourceGeometryIndex = -1; |
66 |
this.coord_trans = null; |
67 |
return;
|
68 |
case 1: |
69 |
// Si solo hay una columna de geometria asignaremos las geometrias
|
70 |
// independientemente de como se llamen los campos.
|
71 |
targetGeomAtt = theTargetFeatureType.getDefaultGeometryAttribute(); |
72 |
break;
|
73 |
default:
|
74 |
// Si hay mas de una geometria en el target y hay un campo de tipo
|
75 |
// geometria que coincida en nombre con el del source, usamos ese.
|
76 |
targetGeomAtt = theTargetFeatureType.getAttributeDescriptor(sourceGeomAtt.getName()); |
77 |
if( targetGeomAtt == null || targetGeomAtt.getType()!=DataTypes.GEOMETRY ) { |
78 |
// Si no coinciden por nombre y tipo, pillaremos el primer campo
|
79 |
// geometry por defecto del target.
|
80 |
targetGeomAtt = theTargetFeatureType.getDefaultGeometryAttribute(); |
81 |
if( targetGeomAtt==null ) { |
82 |
targetGeomAtt = this.getFirstGeometryColumn(theTargetFeatureType);
|
83 |
if( targetGeomAtt == null ) { |
84 |
this.targetGeometryIndex = -1; |
85 |
this.sourceGeometryIndex = -1; |
86 |
this.coord_trans = null; |
87 |
return;
|
88 |
} |
89 |
} |
90 |
} |
91 |
} |
92 |
|
93 |
IProjection targetProjection = this.parameters.getTargetProjection();
|
94 |
IProjection sourceProjection = sourceGeomAtt.getSRS(); |
95 |
if( targetProjection == null ) { |
96 |
targetProjection = sourceProjection; |
97 |
} |
98 |
// this comparison is perhaps too preventive
|
99 |
// we could have two instances of same projection
|
100 |
// so we would do more computations than needed
|
101 |
if (sourceProjection != null && targetProjection != null && sourceProjection != targetProjection) { |
102 |
this.coord_trans = sourceProjection.getCT(targetProjection);
|
103 |
} else {
|
104 |
this.coord_trans = null; |
105 |
} |
106 |
if( this.isExtraColumn ) { |
107 |
this.sourceGeometryIndex = theSourceFeatureType.getExtraColumns().getIndexOf(sourceGeomAtt.getName());
|
108 |
} else {
|
109 |
this.sourceGeometryIndex = sourceGeomAtt.getIndex();
|
110 |
} |
111 |
this.targetGeometryIndex = targetGeomAtt.getIndex();
|
112 |
this.sourceGeometryName = sourceGeomAtt.getName();
|
113 |
} |
114 |
|
115 |
private int getGeometryColumnCount(FeatureType featureType) { |
116 |
int count = 0; |
117 |
for( int i=0; i<featureType.size(); i++ ) { |
118 |
if( featureType.getAttributeDescriptor(i).getType()==DataTypes.GEOMETRY ) {
|
119 |
count++; |
120 |
} |
121 |
} |
122 |
return count;
|
123 |
} |
124 |
|
125 |
@Override
|
126 |
public String getLastErrorMessage() { |
127 |
return this.lastErrorMessage; |
128 |
} |
129 |
|
130 |
@Override
|
131 |
public int copyGeometry( |
132 |
Feature sourceFeature, |
133 |
EditableFeature targetFeature |
134 |
) { |
135 |
if( this.sourceGeometryIndex<0 || this.targetGeometryIndex<0 ) { |
136 |
return ExportServiceManager.FixGeometryStatus.STATE_OK;
|
137 |
} |
138 |
Geometry geometry; |
139 |
if( this.isExtraColumn ) { |
140 |
geometry = sourceFeature.getGeometry(this.sourceGeometryName);
|
141 |
} else {
|
142 |
geometry = sourceFeature.getGeometry(this.sourceGeometryIndex);
|
143 |
} |
144 |
ExportServiceManager.FixGeometryStatus check = serviceManager.fixGeometry( |
145 |
this.parameters,
|
146 |
this.coord_trans,
|
147 |
geometry |
148 |
); |
149 |
if( check.getState() == ExportServiceManager.FixGeometryStatus.STATE_OK ) {
|
150 |
targetFeature.setGeometry(this.targetGeometryIndex, check.getGeometry());
|
151 |
} |
152 |
this.lastErrorMessage = check.getMessage();
|
153 |
return check.getState();
|
154 |
} |
155 |
|
156 |
@Override
|
157 |
public boolean canProcessGeometry() { |
158 |
return this.sourceGeometryIndex>=0 && this.targetGeometryIndex>=0 ; |
159 |
} |
160 |
|
161 |
private FeatureAttributeDescriptor getFirstGeometryColumn(FeatureType featureType) {
|
162 |
for( int i=0; i<featureType.size(); i++ ) { |
163 |
FeatureAttributeDescriptor descriptor = featureType.getAttributeDescriptor(i); |
164 |
if( descriptor.getType()==DataTypes.GEOMETRY ) {
|
165 |
return descriptor;
|
166 |
} |
167 |
} |
168 |
return null; |
169 |
} |
170 |
|
171 |
|
172 |
} |