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