Revision 43936 trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.exportto/org.gvsig.exportto.lib/org.gvsig.exportto.lib.api/src/main/java/org/gvsig/export/spi/AbstractExportService.java
AbstractExportService.java | ||
---|---|---|
5 | 5 |
import java.util.HashSet; |
6 | 6 |
import java.util.List; |
7 | 7 |
import java.util.Set; |
8 |
import org.cresques.cts.ICoordTrans; |
|
9 |
import org.cresques.cts.IProjection; |
|
10 | 8 |
import org.gvsig.export.ExportException; |
11 | 9 |
import org.gvsig.export.ExportLocator; |
12 | 10 |
import org.gvsig.export.ExportParameters; |
11 |
import org.gvsig.export.ExportParametersGeometry; |
|
12 |
import org.gvsig.export.spi.ExportServiceManager.FixGeometryStatus; |
|
13 | 13 |
import org.gvsig.fmap.dal.DALLocator; |
14 | 14 |
import org.gvsig.fmap.dal.DataManager; |
15 | 15 |
import org.gvsig.fmap.dal.DataServerExplorer; |
... | ... | |
21 | 21 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
22 | 22 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
23 | 23 |
import org.gvsig.fmap.dal.feature.FeatureType; |
24 |
import org.gvsig.fmap.geom.Geometry; |
|
25 | 24 |
import org.gvsig.tools.ToolsLocator; |
26 | 25 |
import org.gvsig.tools.dispose.DisposableIterator; |
27 | 26 |
import org.gvsig.tools.dispose.DisposeUtils; |
... | ... | |
124 | 123 |
|
125 | 124 |
abstract protected OpenDataStoreParameters createTargetOpenStoreParameters() throws ExportException; |
126 | 125 |
|
126 |
|
|
127 |
protected static class InvalidGeometryException extends ExportException { |
|
128 |
|
|
129 |
public InvalidGeometryException(Feature feature, String checkMessage) { |
|
130 |
super(checkMessage, feature); |
|
131 |
} |
|
132 |
} |
|
133 |
|
|
127 | 134 |
@Override |
128 | 135 |
public void export(FeatureSet featureSet) throws ExportException { |
129 | 136 |
DataServerExplorer explorer = createServerExplorer(); |
... | ... | |
135 | 142 |
|
136 | 143 |
DisposableIterator it = null; |
137 | 144 |
FeatureStore target = null; |
138 |
EditableFeature newfeature = null;
|
|
145 |
EditableFeature targetFeature = null;
|
|
139 | 146 |
try { |
140 | 147 |
this.getTaskStatus().setRangeOfValues(0, featureSet.getSize()); |
141 | 148 |
|
... | ... | |
144 | 151 |
dataManager.newStore(explorerName, providerName, newStoreParameters, true); |
145 | 152 |
target = (FeatureStore) dataManager.openStore(providerName, openStoreParameters); |
146 | 153 |
|
147 |
FeatureType targetType = target.getDefaultFeatureType(); |
|
154 |
FeatureType theTargetFeatureType = target.getDefaultFeatureType(); |
|
155 |
FeatureType theSourceFeatureType = featureSet.getDefaultFeatureType(); |
|
148 | 156 |
|
157 |
ExportGeometryHelper geomHelper = null; |
|
158 |
if( this.getParameters() instanceof ExportParametersGeometry ) { |
|
159 |
geomHelper = ExportLocator.getServiceManager().createGeometryHelper( |
|
160 |
(ExportParametersGeometry) this.getParameters(), |
|
161 |
theTargetFeatureType, |
|
162 |
theSourceFeatureType |
|
163 |
); |
|
164 |
if( !geomHelper.canProcessGeometry() ) { |
|
165 |
geomHelper = null; |
|
166 |
} |
|
167 |
} |
|
168 |
// ============================================ |
|
169 |
|
|
149 | 170 |
target.edit(FeatureStore.MODE_APPEND); |
150 | 171 |
it = featureSet.fastIterator(); |
151 | 172 |
|
152 |
IProjection targetProjection; |
|
153 |
FeatureAttributeDescriptor defaultGeom = target.getDefaultFeatureType().getDefaultGeometryAttribute(); |
|
154 |
if (defaultGeom!=null) { |
|
155 |
targetProjection = defaultGeom.getSRS(); |
|
156 |
} else { |
|
157 |
targetProjection = null; |
|
158 |
} |
|
159 |
|
|
160 | 173 |
long featureCount = 0; |
161 | 174 |
while (it.hasNext()) { |
162 |
Feature feature = (Feature) it.next(); |
|
163 |
newfeature = target.createNewFeature(targetType, true); |
|
164 |
copyValues(feature, newfeature); |
|
165 |
// ================================================ |
|
166 |
// Reprojection stuff |
|
167 |
Geometry reproj_geom; |
|
168 |
IProjection sourceProjection = feature.getDefaultSRS(); |
|
175 |
this.getTaskStatus().setCurValue(featureCount); |
|
169 | 176 |
|
170 |
ICoordTrans coord_trans;
|
|
171 |
// this comparison is perhaps too preventive
|
|
172 |
// we could have two instances of same projection
|
|
173 |
// so we would do more computations than needed |
|
174 |
if (sourceProjection != null
|
|
175 |
&& targetProjection != null
|
|
176 |
&& sourceProjection != targetProjection) {
|
|
177 |
|
|
178 |
coord_trans = sourceProjection.getCT(targetProjection);
|
|
179 |
reproj_geom = feature.getDefaultGeometry();
|
|
180 |
reproj_geom = reproj_geom.cloneGeometry();
|
|
181 |
reproj_geom.reProject(coord_trans);
|
|
182 |
newfeature.setDefaultGeometry(reproj_geom);
|
|
177 |
Feature sourceFeature = (Feature) it.next();
|
|
178 |
targetFeature = target.createNewFeature(theTargetFeatureType, true);
|
|
179 |
copyValues(sourceFeature, targetFeature);
|
|
180 |
|
|
181 |
if( geomHelper!=null ) {
|
|
182 |
switch(geomHelper.copyGeometry(sourceFeature,targetFeature)) {
|
|
183 |
case FixGeometryStatus.STATE_OK:
|
|
184 |
break; |
|
185 |
case FixGeometryStatus.STATE_SKIP:
|
|
186 |
continue;
|
|
187 |
case FixGeometryStatus.STATE_ABORT:
|
|
188 |
throw new InvalidGeometryException(targetFeature, geomHelper.getLastErrorMessage());
|
|
189 |
}
|
|
183 | 190 |
} |
184 |
// ================================================ |
|
191 |
|
|
192 |
target.insert(targetFeature); |
|
185 | 193 |
|
186 |
target.insert(newfeature); |
|
187 |
|
|
188 | 194 |
featureCount++; |
189 |
this.getTaskStatus().setCurValue(featureCount); |
|
190 | 195 |
|
191 | 196 |
if (this.getTaskStatus().isCancellationRequested()) { |
192 | 197 |
return; |
... | ... | |
198 | 203 |
fireFinishedListener(); |
199 | 204 |
} catch (Exception e) { |
200 | 205 |
fireCancelledListeners(); |
201 |
throw new ExportException(e, newfeature);
|
|
206 |
throw new ExportException(e, targetFeature);
|
|
202 | 207 |
} finally { |
203 | 208 |
DisposeUtils.dispose(it); |
204 | 209 |
DisposeUtils.dispose(featureSet); |
205 | 210 |
DisposeUtils.dispose(target); |
206 |
} |
|
207 | 211 |
} |
212 |
} |
|
208 | 213 |
|
209 | 214 |
protected void copyValues(Feature source, EditableFeature target) { |
210 | 215 |
FeatureType sourceType= source.getType(); |
211 | 216 |
FeatureType targetType = target.getType(); |
212 |
for( FeatureAttributeDescriptor targetAttr : targetType) {
|
|
217 |
for( FeatureAttributeDescriptor targetAttr: targetType) { |
|
213 | 218 |
String targetAttrName = targetAttr.getName(); |
214 | 219 |
String sourceAttrName = getAttributeNamesTranslator().getSourceName(targetAttrName); |
215 | 220 |
FeatureAttributeDescriptor sourceAttr = sourceType.getAttributeDescriptor(sourceAttrName); |
Also available in: Unified diff