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