gvsig-geoprocess / org.gvsig.geoprocess / trunk / org.gvsig.geoprocess / org.gvsig.geoprocess.algorithm / org.gvsig.geoprocess.algorithm.xyshift / src / main / java / org / gvsig / geoprocess / algorithm / xyshift / XYShiftOperation.java @ 330
History | View | Annotate | Download (8.56 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2012 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 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 |
* 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 |
/*
|
25 |
|
26 |
* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
27 |
*
|
28 |
* Copyright (C) 2010 Generalitat Valenciana.
|
29 |
*
|
30 |
* This program is free software; you can redistribute it and/or
|
31 |
* modify it under the terms of the GNU General Public License
|
32 |
* as published by the Free Software Foundation; either version 2
|
33 |
* of the License, or (at your option) any later version.
|
34 |
*
|
35 |
* This program is distributed in the hope that it will be useful,
|
36 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
37 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
38 |
* GNU General Public License for more details.
|
39 |
*
|
40 |
* You should have received a copy of the GNU General Public License
|
41 |
* along with this program; if not, write to the Free Software
|
42 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
43 |
*/
|
44 |
|
45 |
package org.gvsig.geoprocess.algorithm.xyshift; |
46 |
|
47 |
import java.util.Iterator; |
48 |
import java.util.List; |
49 |
|
50 |
import org.gvsig.fmap.dal.exception.DataException; |
51 |
import org.gvsig.fmap.dal.feature.EditableFeature; |
52 |
import org.gvsig.fmap.dal.feature.Feature; |
53 |
import org.gvsig.fmap.geom.Geometry; |
54 |
import org.gvsig.fmap.geom.GeometryLocator; |
55 |
import org.gvsig.fmap.geom.GeometryManager; |
56 |
import org.gvsig.fmap.geom.aggregate.MultiCurve; |
57 |
import org.gvsig.fmap.geom.aggregate.MultiPoint; |
58 |
import org.gvsig.fmap.geom.aggregate.MultiSurface; |
59 |
import org.gvsig.fmap.geom.exception.CreateGeometryException; |
60 |
import org.gvsig.fmap.geom.primitive.Curve; |
61 |
import org.gvsig.fmap.geom.primitive.GeneralPathX; |
62 |
import org.gvsig.fmap.geom.primitive.OrientablePrimitive; |
63 |
import org.gvsig.fmap.geom.primitive.Point; |
64 |
import org.gvsig.fmap.geom.primitive.Surface; |
65 |
import org.gvsig.geoprocess.algorithm.base.core.GeometryOperation; |
66 |
import org.gvsig.geoprocess.lib.sextante.AbstractSextanteGeoProcess; |
67 |
|
68 |
import es.unex.sextante.core.Sextante; |
69 |
/**
|
70 |
* Translates all geometries
|
71 |
* @author Nacho Brodin (nachobrodin@gmail.com)
|
72 |
*/
|
73 |
@SuppressWarnings("deprecation") |
74 |
public class XYShiftOperation extends GeometryOperation { |
75 |
private double x = 0D; |
76 |
private double y = 0D; |
77 |
private GeometryManager geometryManager = null; |
78 |
|
79 |
public XYShiftOperation(double x, double y, AbstractSextanteGeoProcess p) { |
80 |
super(p);
|
81 |
this.x = x;
|
82 |
this.y = y;
|
83 |
this.geometryManager = GeometryLocator.getGeometryManager();
|
84 |
} |
85 |
/*
|
86 |
* (non-Javadoc)
|
87 |
* @see org.gvsig.geoprocess.algorithm.base.core.GeometryOperation#invoke(org.gvsig.fmap.geom.Geometry, org.gvsig.fmap.dal.feature.Feature)
|
88 |
*/
|
89 |
@SuppressWarnings("unchecked") |
90 |
public EditableFeature invoke(org.gvsig.fmap.geom.Geometry g, Feature feature) {
|
91 |
List geomList = feature.getGeometries();
|
92 |
try {
|
93 |
if(!persister.isCompatibleType(feature.getDefaultGeometry()))
|
94 |
return lastEditFeature;
|
95 |
|
96 |
if(geomList == null) { |
97 |
org.gvsig.fmap.geom.Geometry oldGeom = feature.getDefaultGeometry(); |
98 |
Geometry newGeom = shiftGeometry(oldGeom); |
99 |
lastEditFeature = persister.addFeature(feature, newGeom); |
100 |
} else {
|
101 |
Iterator<org.gvsig.fmap.geom.Geometry> itGeom = geomList.iterator();
|
102 |
EditableFeature editFeat = null;
|
103 |
int nGeom = 0; |
104 |
while(itGeom.hasNext()) {
|
105 |
org.gvsig.fmap.geom.Geometry oldGeom = itGeom.next(); |
106 |
Geometry newGeom = shiftGeometry(oldGeom); |
107 |
nGeom ++; |
108 |
if(editFeat == null) |
109 |
editFeat = persister.addFeature(feature, newGeom); |
110 |
else
|
111 |
persister.addGeometryToExistingFeature(editFeat, newGeom); |
112 |
lastEditFeature = editFeat; |
113 |
} |
114 |
} |
115 |
} catch (DataException e) {
|
116 |
Sextante.addErrorToLog(e); |
117 |
} catch (CreateGeometryException e1) {
|
118 |
Sextante.addErrorToLog(e1); |
119 |
} |
120 |
|
121 |
return lastEditFeature;
|
122 |
} |
123 |
|
124 |
/**
|
125 |
* Shifts all coordinates in a geometry
|
126 |
* @param oldGeom
|
127 |
* @return
|
128 |
* @throws CreateGeometryException
|
129 |
*/
|
130 |
private Geometry shiftGeometry(Geometry oldGeom) throws CreateGeometryException { |
131 |
if(oldGeom.getType() == Geometry.TYPES.SURFACE)
|
132 |
return shiftSurface((Surface)oldGeom);
|
133 |
else if(oldGeom.getType() == Geometry.TYPES.MULTISURFACE) |
134 |
return shiftMultiSurface((MultiSurface)oldGeom);
|
135 |
else if(oldGeom.getType() == Geometry.TYPES.CURVE) |
136 |
return shiftCurve((Curve)oldGeom);
|
137 |
else if(oldGeom.getType() == Geometry.TYPES.MULTICURVE) |
138 |
return shiftMultiCurve((MultiCurve)oldGeom);
|
139 |
else if(oldGeom.getType() == Geometry.TYPES.POINT) |
140 |
return shiftPoint((Point)oldGeom); |
141 |
else if(oldGeom.getType() == Geometry.TYPES.MULTIPOINT) |
142 |
return shiftMultiPoint((MultiPoint)oldGeom);
|
143 |
return oldGeom;
|
144 |
} |
145 |
|
146 |
/**
|
147 |
* Shifts all coordinates in a surface
|
148 |
* @param geom
|
149 |
* @return
|
150 |
* @throws CreateGeometryException
|
151 |
*/
|
152 |
private Surface shiftSurface(Surface geom) throws CreateGeometryException { |
153 |
Surface surface = (Surface)geometryManager.create(Geometry.TYPES.SURFACE, Geometry.SUBTYPES.GEOM2D); |
154 |
return (Surface)shiftGeom(geom, surface);
|
155 |
} |
156 |
|
157 |
/**
|
158 |
* Shifts all coordinates in a multisurface
|
159 |
* @param geom
|
160 |
* @return
|
161 |
* @throws CreateGeometryException
|
162 |
*/
|
163 |
private MultiSurface shiftMultiSurface(MultiSurface geom) throws CreateGeometryException { |
164 |
MultiSurface surface = (MultiSurface)geometryManager.create(Geometry.TYPES.MULTISURFACE, geom.getGeometryType().getSubType()); |
165 |
for (int i = 0; i < geom.getPrimitivesNumber(); i++) { |
166 |
Surface s = geom.getSurfaceAt(i); |
167 |
Surface newSurface = shiftSurface(s); |
168 |
surface.addSurface(newSurface); |
169 |
} |
170 |
return surface;
|
171 |
} |
172 |
|
173 |
/**
|
174 |
* Shifts all coordinates in a curve
|
175 |
* @param geom
|
176 |
* @return
|
177 |
* @throws CreateGeometryException
|
178 |
*/
|
179 |
private Curve shiftCurve(Curve geom) throws CreateGeometryException { |
180 |
Curve curve = (Curve)geometryManager.create(Geometry.TYPES.CURVE, Geometry.SUBTYPES.GEOM2D); |
181 |
return (Curve)shiftGeom(geom, curve);
|
182 |
} |
183 |
|
184 |
private OrientablePrimitive shiftGeom(OrientablePrimitive src, OrientablePrimitive dst) throws CreateGeometryException { |
185 |
GeneralPathX gp = (GeneralPathX)src.getGeneralPath().clone(); |
186 |
double[] pcoords = gp.getPointCoords(); |
187 |
|
188 |
for (int i = 0; i < pcoords.length; i++) { |
189 |
if((i % 2) == 0) |
190 |
pcoords[i] = pcoords[i] + x; |
191 |
else
|
192 |
pcoords[i] = pcoords[i] + y; |
193 |
} |
194 |
gp.setPointCoords(pcoords); |
195 |
dst.setGeneralPath(gp); |
196 |
return dst;
|
197 |
} |
198 |
|
199 |
/**
|
200 |
* Shifts all coordinates in a multicurve
|
201 |
* @param geom
|
202 |
* @return
|
203 |
* @throws CreateGeometryException
|
204 |
*/
|
205 |
private MultiCurve shiftMultiCurve(MultiCurve geom) throws CreateGeometryException { |
206 |
MultiCurve curve = (MultiCurve)geometryManager.create(Geometry.TYPES.MULTICURVE, geom.getGeometryType().getSubType()); |
207 |
for (int i = 0; i < geom.getPrimitivesNumber(); i++) { |
208 |
Curve c = geom.getCurveAt(i); |
209 |
Curve newCurve = shiftCurve(c); |
210 |
curve.addCurve(newCurve); |
211 |
} |
212 |
return curve;
|
213 |
} |
214 |
|
215 |
/**
|
216 |
* Shifts a point
|
217 |
* @param geom
|
218 |
* @return
|
219 |
* @throws CreateGeometryException
|
220 |
*/
|
221 |
private Point shiftPoint(Point geom) throws CreateGeometryException { |
222 |
Point point = (Point)geometryManager.create(Geometry.TYPES.POINT, geom.getGeometryType().getSubType()); |
223 |
point.setX(geom.getX() + x); |
224 |
point.setY(geom.getY() + y); |
225 |
return point;
|
226 |
} |
227 |
|
228 |
/**
|
229 |
* Shifts all coordinates in a multisurface
|
230 |
* @param geom
|
231 |
* @return
|
232 |
* @throws CreateGeometryException
|
233 |
*/
|
234 |
private MultiPoint shiftMultiPoint(MultiPoint geom) throws CreateGeometryException { |
235 |
MultiPoint point = (MultiPoint)geometryManager.create(Geometry.TYPES.MULTIPOINT, geom.getGeometryType().getSubType()); |
236 |
for (int i = 0; i < geom.getPrimitivesNumber(); i++) { |
237 |
Point p = geom.getPointAt(i);
|
238 |
Point newPoint = shiftPoint(p);
|
239 |
point.addPoint(newPoint); |
240 |
} |
241 |
return point;
|
242 |
} |
243 |
|
244 |
/*
|
245 |
* (non-Javadoc)
|
246 |
* @see org.gvsig.geoprocess.algorithm.base.core.GeometryOperation#invoke(org.gvsig.fmap.geom.Geometry, org.gvsig.fmap.dal.feature.EditableFeature)
|
247 |
*/
|
248 |
public void invoke(org.gvsig.fmap.geom.Geometry g, EditableFeature feature) { |
249 |
} |
250 |
} |
251 |
|