svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.mapcontext / org.gvsig.fmap.mapcontext.api / src / main / java / org / gvsig / fmap / mapcontext / layers / vectorial / ReprojectDefaultGeometry.java @ 44831
History | View | Annotate | Download (8.8 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 3
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
package org.gvsig.fmap.mapcontext.layers.vectorial; |
25 |
|
26 |
import java.util.ArrayList; |
27 |
import java.util.Iterator; |
28 |
import java.util.List; |
29 |
|
30 |
import org.cresques.cts.ICoordTrans; |
31 |
import org.cresques.cts.IProjection; |
32 |
import org.gvsig.fmap.dal.exception.DataException; |
33 |
import org.gvsig.fmap.dal.feature.AbstractFeatureStoreTransform; |
34 |
import org.gvsig.fmap.dal.feature.EditableFeature; |
35 |
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor; |
36 |
import org.gvsig.fmap.dal.feature.EditableFeatureType; |
37 |
import org.gvsig.fmap.dal.feature.Feature; |
38 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
39 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
40 |
import org.gvsig.fmap.dal.feature.FeatureType; |
41 |
import org.gvsig.fmap.geom.Geometry; |
42 |
import org.gvsig.tools.ToolsLocator; |
43 |
import org.gvsig.tools.dynobject.DynStruct; |
44 |
import org.gvsig.tools.persistence.PersistenceManager; |
45 |
import org.gvsig.tools.persistence.PersistentState; |
46 |
import org.gvsig.tools.persistence.exception.PersistenceException; |
47 |
import org.gvsig.tools.util.Callable; |
48 |
|
49 |
/**
|
50 |
* @author jmvivo
|
51 |
*
|
52 |
*/
|
53 |
public class ReprojectDefaultGeometry |
54 |
extends AbstractFeatureStoreTransform {
|
55 |
|
56 |
private IProjection targetSRS;
|
57 |
private ICoordTrans ct;
|
58 |
private IProjection sourceSRS;
|
59 |
private FeatureType orgDefFeatureType;
|
60 |
private List orgFeatureTypes; |
61 |
|
62 |
public ReprojectDefaultGeometry() {
|
63 |
|
64 |
} |
65 |
|
66 |
// public ReprojectDefaultGeometry(IProjection targetSRS){
|
67 |
// super("ReprojectDefaultGeometry", "ReprojectDefaultGeometry");
|
68 |
// this.targetSRS = targetSRS;
|
69 |
// this.ct = null;
|
70 |
// }
|
71 |
//
|
72 |
// public ReprojectDefaultGeometry(ICoordTrans ct){
|
73 |
// super("ReprojectDefaultGeometry", "ReprojectDefaultGeometry");
|
74 |
// this.ct = ct;
|
75 |
// }
|
76 |
public IProjection getTargetSRS() {
|
77 |
return targetSRS;
|
78 |
} |
79 |
|
80 |
public void setTargetSRS(IProjection targetSRS) { |
81 |
this.targetSRS = targetSRS;
|
82 |
} |
83 |
|
84 |
// public void setCoordTrans(ICoordTrans ct){
|
85 |
// this.ct = ct;
|
86 |
// if ( getFeatureStore() != null ){
|
87 |
// setFeatureStore(getFeatureStore());
|
88 |
// }
|
89 |
// }
|
90 |
|
91 |
/* (non-Javadoc)
|
92 |
* @see org.gvsig.fmap.dal.feature.FeatureStoreTransform#applyTransform(org.gvsig.fmap.dal.feature.Feature, org.gvsig.fmap.dal.feature.EditableFeature)
|
93 |
*/
|
94 |
public void applyTransform(Feature source, EditableFeature target) |
95 |
throws DataException {
|
96 |
FeatureAttributeDescriptor attr; |
97 |
Iterator iter = target.getType().iterator();
|
98 |
int defGeomIndex = target.getType().getDefaultGeometryAttributeIndex();
|
99 |
while( iter.hasNext() ) {
|
100 |
attr = (FeatureAttributeDescriptor) iter.next(); |
101 |
if( attr.getIndex() == defGeomIndex ) {
|
102 |
Geometry geom = source.getDefaultGeometry(); |
103 |
geom.reProject(ct); |
104 |
target.setDefaultGeometry(geom); |
105 |
} else {
|
106 |
target.set(attr.getIndex(), source.get(attr.getName())); |
107 |
} |
108 |
|
109 |
} |
110 |
|
111 |
} |
112 |
|
113 |
@Override
|
114 |
public void setUp() { |
115 |
try {
|
116 |
FeatureStore store = this.getFeatureStore();
|
117 |
orgDefFeatureType = store.getDefaultFeatureType(); |
118 |
orgFeatureTypes = store.getFeatureTypes(); |
119 |
EditableFeatureType defFType = orgDefFeatureType |
120 |
.getEditable(); |
121 |
EditableFeatureAttributeDescriptor attr = (EditableFeatureAttributeDescriptor) defFType |
122 |
.getAttributeDescriptor(defFType |
123 |
.getDefaultGeometryAttributeName()); |
124 |
// if (ct!=null){
|
125 |
// targetSRS = ct.getPDest();
|
126 |
// sourceSRS = ct.getPOrig();
|
127 |
// } else {
|
128 |
sourceSRS = attr.getSRS(); |
129 |
ct = sourceSRS.getCT(targetSRS); |
130 |
// }
|
131 |
attr.setSRS(this.targetSRS);
|
132 |
FeatureType defaultType = defFType.getNotEditableCopy(); |
133 |
List types = new ArrayList(); |
134 |
Iterator iter = orgFeatureTypes.iterator();
|
135 |
FeatureType tmp; |
136 |
while( iter.hasNext() ) {
|
137 |
tmp = (FeatureType) iter.next(); |
138 |
if( tmp.getId().equals(defaultType.getId()) ) {
|
139 |
types.add(defaultType); |
140 |
} else {
|
141 |
types.add(tmp); |
142 |
} |
143 |
} |
144 |
|
145 |
this.setFeatureTypes(types, defaultType);
|
146 |
} catch (DataException e) {
|
147 |
// FIXME
|
148 |
throw new RuntimeException(e); |
149 |
} |
150 |
} |
151 |
|
152 |
/* (non-Javadoc)
|
153 |
* @see org.gvsig.fmap.dal.feature.FeatureStoreTransform#getSourceFeatureTypeFrom(org.gvsig.fmap.dal.feature.FeatureType)
|
154 |
*/
|
155 |
public FeatureType getSourceFeatureTypeFrom(FeatureType targetFeatureType) {
|
156 |
EditableFeatureType result = null;
|
157 |
FeatureType tmp; |
158 |
EditableFeatureAttributeDescriptor attr; |
159 |
Iterator iter = orgFeatureTypes.iterator();
|
160 |
Iterator iterAttr;
|
161 |
while( iter.hasNext() ) {
|
162 |
tmp = (FeatureType) iter.next(); |
163 |
if( tmp.getId().equals(targetFeatureType.getId()) ) {
|
164 |
result = tmp.getEditable(); |
165 |
iterAttr = result.iterator(); |
166 |
while( iterAttr.hasNext() ) {
|
167 |
attr = (EditableFeatureAttributeDescriptor) iterAttr.next(); |
168 |
if( targetFeatureType.getIndex(attr.getName()) < 0 ) { |
169 |
iterAttr.remove(); |
170 |
} |
171 |
} |
172 |
break;
|
173 |
} |
174 |
} |
175 |
return result.getNotEditableCopy();
|
176 |
} |
177 |
|
178 |
/* (non-Javadoc)
|
179 |
* @see org.gvsig.fmap.dal.feature.FeatureStoreTransform#isTransformsOriginalValues()
|
180 |
*/
|
181 |
public boolean isTransformsOriginalValues() { |
182 |
return true; |
183 |
} |
184 |
|
185 |
/* (non-Javadoc)
|
186 |
* @see org.gvsig.tools.persistence.Persistent#loadState(org.gvsig.tools.persistence.PersistentState)
|
187 |
*/
|
188 |
public void saveToState(PersistentState state) throws PersistenceException { |
189 |
super.saveToState(state);
|
190 |
|
191 |
state.set("source_crs", this.sourceSRS); |
192 |
state.set("source_defaultFeatureType", this.orgDefFeatureType); |
193 |
state.set("source_featureTypes", this.orgFeatureTypes); |
194 |
state.set("target_crs", this.targetSRS); |
195 |
state.set("coordtrans", this.ct); |
196 |
} |
197 |
|
198 |
/* (non-Javadoc)
|
199 |
* @see org.gvsig.tools.persistence.Persistent#loadFromState(org.gvsig.tools.persistence.PersistentState)
|
200 |
*/
|
201 |
public void loadFromState(PersistentState state) throws PersistenceException { |
202 |
super.loadFromState(state);
|
203 |
this.sourceSRS = (IProjection) state.get("source_crs"); |
204 |
this.orgDefFeatureType = (FeatureType) state.get("source_defaultFeatureType"); |
205 |
this.orgFeatureTypes = new ArrayList((List) state.get("source_featureTypes")); |
206 |
this.targetSRS = (IProjection) state.get("target_crs"); |
207 |
this.ct = (ICoordTrans) state.get("coordtrans"); |
208 |
} |
209 |
|
210 |
public static class RegisterPersistence |
211 |
implements Callable { |
212 |
|
213 |
public Object call() { |
214 |
PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
215 |
DynStruct definition = manager.addDefinition( |
216 |
ReprojectDefaultGeometry.class, |
217 |
"ReprojectDefaultGeometry",
|
218 |
"ReprojectDefaultGeometry Persistence definition",
|
219 |
null,
|
220 |
null
|
221 |
); |
222 |
definition.extend(manager.getDefinition(AbstractFeatureStoreTransform.class)); |
223 |
|
224 |
definition.addDynFieldObject("source_crs")
|
225 |
.setClassOfValue(IProjection.class) |
226 |
.setMandatory(true);
|
227 |
definition.addDynFieldObject("source_defaultFeatureType")
|
228 |
.setClassOfValue(FeatureType.class) |
229 |
.setMandatory(true);
|
230 |
definition.addDynFieldList("source_featureTypes")
|
231 |
.setClassOfItems(FeatureType.class) |
232 |
.setMandatory(true);
|
233 |
definition.addDynFieldObject("target_crs")
|
234 |
.setClassOfValue(IProjection.class) |
235 |
.setMandatory(true);
|
236 |
definition.addDynFieldObject("coordtrans")
|
237 |
.setClassOfValue(ICoordTrans.class) |
238 |
.setMandatory(true);
|
239 |
|
240 |
return Boolean.TRUE; |
241 |
} |
242 |
} |
243 |
} |