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 @ 40596
History | View | Annotate | Download (7.61 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 |
|
48 |
/**
|
49 |
* @author jmvivo
|
50 |
*
|
51 |
*/
|
52 |
public class ReprojectDefaultGeometry extends AbstractFeatureStoreTransform { |
53 |
|
54 |
private IProjection targetSRS;
|
55 |
private ICoordTrans ct;
|
56 |
private IProjection sourceSRS;
|
57 |
private FeatureType orgDefFeatureType;
|
58 |
private List orgFeatureTypes; |
59 |
|
60 |
public ReprojectDefaultGeometry(){
|
61 |
|
62 |
} |
63 |
|
64 |
// public ReprojectDefaultGeometry(IProjection targetSRS){
|
65 |
// super("ReprojectDefaultGeometry", "ReprojectDefaultGeometry");
|
66 |
// this.targetSRS = targetSRS;
|
67 |
// this.ct = null;
|
68 |
// }
|
69 |
//
|
70 |
// public ReprojectDefaultGeometry(ICoordTrans ct){
|
71 |
// super("ReprojectDefaultGeometry", "ReprojectDefaultGeometry");
|
72 |
// this.ct = ct;
|
73 |
// }
|
74 |
|
75 |
public IProjection getTargetSRS() {
|
76 |
return targetSRS;
|
77 |
} |
78 |
|
79 |
public void setTargetSRS(IProjection targetSRS) { |
80 |
this.targetSRS = targetSRS;
|
81 |
} |
82 |
|
83 |
// public void setCoordTrans(ICoordTrans ct){
|
84 |
// this.ct = ct;
|
85 |
// if ( getFeatureStore() != null ){
|
86 |
// setFeatureStore(getFeatureStore());
|
87 |
// }
|
88 |
// }
|
89 |
|
90 |
/* (non-Javadoc)
|
91 |
* @see org.gvsig.fmap.dal.feature.FeatureStoreTransform#applyTransform(org.gvsig.fmap.dal.feature.Feature, org.gvsig.fmap.dal.feature.EditableFeature)
|
92 |
*/
|
93 |
public void applyTransform(Feature source, EditableFeature target) |
94 |
throws DataException {
|
95 |
FeatureAttributeDescriptor attr; |
96 |
Iterator iter = target.getType().iterator();
|
97 |
int defGeomIndex = target.getType().getDefaultGeometryAttributeIndex();
|
98 |
while (iter.hasNext()) {
|
99 |
attr = (FeatureAttributeDescriptor)iter.next(); |
100 |
if (attr.getIndex() == defGeomIndex) {
|
101 |
Geometry geom =source.getDefaultGeometry(); |
102 |
geom.reProject(ct); |
103 |
target.setDefaultGeometry(geom); |
104 |
} else {
|
105 |
target.set(attr.getIndex(), source.get(attr.getName())); |
106 |
} |
107 |
|
108 |
} |
109 |
|
110 |
} |
111 |
|
112 |
public void setFeatureStore(FeatureStore store) { |
113 |
try {
|
114 |
orgDefFeatureType = store.getDefaultFeatureType(); |
115 |
orgFeatureTypes = store.getFeatureTypes(); |
116 |
EditableFeatureType defFType = orgDefFeatureType |
117 |
.getEditable(); |
118 |
EditableFeatureAttributeDescriptor attr = (EditableFeatureAttributeDescriptor) defFType |
119 |
.getAttributeDescriptor(defFType |
120 |
.getDefaultGeometryAttributeName()); |
121 |
// if (ct!=null){
|
122 |
// targetSRS = ct.getPDest();
|
123 |
// sourceSRS = ct.getPOrig();
|
124 |
// } else {
|
125 |
sourceSRS = attr.getSRS(); |
126 |
ct = sourceSRS.getCT(targetSRS); |
127 |
// }
|
128 |
attr.setSRS(this.targetSRS);
|
129 |
FeatureType defaultType = defFType.getNotEditableCopy(); |
130 |
List types = new ArrayList(); |
131 |
Iterator iter = orgFeatureTypes.iterator();
|
132 |
FeatureType tmp; |
133 |
while (iter.hasNext()) {
|
134 |
tmp = (FeatureType) iter.next(); |
135 |
if (tmp.getId().equals(defaultType.getId())) {
|
136 |
types.add(defaultType); |
137 |
} else {
|
138 |
types.add(tmp); |
139 |
} |
140 |
} |
141 |
|
142 |
this.setFeatureTypes(types, defaultType);
|
143 |
} catch (DataException e) {
|
144 |
// FIXME
|
145 |
throw new RuntimeException(e); |
146 |
} |
147 |
|
148 |
super.setFeatureStore(store);
|
149 |
} |
150 |
|
151 |
/* (non-Javadoc)
|
152 |
* @see org.gvsig.fmap.dal.feature.FeatureStoreTransform#getSourceFeatureTypeFrom(org.gvsig.fmap.dal.feature.FeatureType)
|
153 |
*/
|
154 |
public FeatureType getSourceFeatureTypeFrom(FeatureType targetFeatureType) {
|
155 |
EditableFeatureType result = null;
|
156 |
FeatureType tmp; |
157 |
EditableFeatureAttributeDescriptor attr; |
158 |
Iterator iter = orgFeatureTypes.iterator();
|
159 |
Iterator iterAttr;
|
160 |
while (iter.hasNext()) {
|
161 |
tmp = (FeatureType) iter.next(); |
162 |
if (tmp.getId().equals(targetFeatureType.getId())) {
|
163 |
result = tmp.getEditable(); |
164 |
iterAttr = result.iterator(); |
165 |
while (iterAttr.hasNext()) {
|
166 |
attr = (EditableFeatureAttributeDescriptor) iterAttr.next(); |
167 |
if (targetFeatureType.getIndex(attr.getName()) < 0) { |
168 |
iterAttr.remove(); |
169 |
} |
170 |
} |
171 |
break;
|
172 |
} |
173 |
} |
174 |
return result.getNotEditableCopy();
|
175 |
} |
176 |
|
177 |
/* (non-Javadoc)
|
178 |
* @see org.gvsig.fmap.dal.feature.FeatureStoreTransform#isTransformsOriginalValues()
|
179 |
*/
|
180 |
public boolean isTransformsOriginalValues() { |
181 |
return true; |
182 |
} |
183 |
|
184 |
/* (non-Javadoc)
|
185 |
* @see org.gvsig.tools.persistence.Persistent#loadState(org.gvsig.tools.persistence.PersistentState)
|
186 |
*/
|
187 |
public void saveToState(PersistentState state) throws PersistenceException { |
188 |
super.saveToState(state);
|
189 |
|
190 |
state.set("source_crs", this.sourceSRS); |
191 |
state.set("source_defaultFeatureType", this.orgDefFeatureType); |
192 |
state.set("source_featureTypes", this.orgFeatureTypes); |
193 |
state.set("target_crs", this.targetSRS); |
194 |
state.set("coordtrans", this.ct); |
195 |
} |
196 |
|
197 |
/* (non-Javadoc)
|
198 |
* @see org.gvsig.tools.persistence.Persistent#loadFromState(org.gvsig.tools.persistence.PersistentState)
|
199 |
*/
|
200 |
public void loadFromState(PersistentState state) throws PersistenceException { |
201 |
super.loadFromState(state);
|
202 |
this.sourceSRS = (IProjection) state.get("source_crs"); |
203 |
this.orgDefFeatureType = (FeatureType) state.get("source_defaultFeatureType"); |
204 |
this.orgFeatureTypes = new ArrayList( (List) state.get("source_featureTypes")); |
205 |
this.targetSRS = (IProjection) state.get("target_crs"); |
206 |
this.ct = (ICoordTrans) state.get("coordtrans"); |
207 |
} |
208 |
|
209 |
public static void registerPersistent() { |
210 |
PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
211 |
DynStruct definition = manager.addDefinition( |
212 |
ReprojectDefaultGeometry.class, |
213 |
"ReprojectDefaultGeometry",
|
214 |
"ReprojectDefaultGeometry Persistence definition",
|
215 |
null,
|
216 |
null
|
217 |
); |
218 |
definition.extend( manager.getDefinition(AbstractFeatureStoreTransform.class)); |
219 |
|
220 |
definition.addDynFieldObject("source_crs")
|
221 |
.setClassOfValue(IProjection.class) |
222 |
.setMandatory(true);
|
223 |
definition.addDynFieldObject("source_defaultFeatureType")
|
224 |
.setClassOfValue(FeatureType.class) |
225 |
.setMandatory(true);
|
226 |
definition.addDynFieldList("source_featureTypes")
|
227 |
.setClassOfItems(FeatureType.class) |
228 |
.setMandatory(true);
|
229 |
definition.addDynFieldObject("target_crs")
|
230 |
.setClassOfValue(IProjection.class) |
231 |
.setMandatory(true);
|
232 |
definition.addDynFieldObject("coordtrans")
|
233 |
.setClassOfValue(ICoordTrans.class) |
234 |
.setMandatory(true);
|
235 |
|
236 |
} |
237 |
} |