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 @ 245
History | View | Annotate | Download (6.72 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 es.unex.sextante.core.Sextante; |
51 |
|
52 |
import org.gvsig.fmap.dal.exception.DataException; |
53 |
import org.gvsig.fmap.dal.feature.EditableFeature; |
54 |
import org.gvsig.fmap.dal.feature.Feature; |
55 |
import org.gvsig.fmap.geom.Geometry; |
56 |
import org.gvsig.fmap.geom.GeometryLocator; |
57 |
import org.gvsig.fmap.geom.GeometryManager; |
58 |
import org.gvsig.fmap.geom.exception.CreateGeometryException; |
59 |
import org.gvsig.fmap.geom.primitive.Curve; |
60 |
import org.gvsig.fmap.geom.primitive.GeneralPathX; |
61 |
import org.gvsig.fmap.geom.primitive.Point; |
62 |
import org.gvsig.fmap.geom.primitive.Surface; |
63 |
import org.gvsig.geoprocess.algorithm.base.core.GeometryOperation; |
64 |
/**
|
65 |
* Translates all geometries
|
66 |
* @author Nacho Brodin (nachobrodin@gmail.com)
|
67 |
*/
|
68 |
public class XYShiftOperation extends GeometryOperation { |
69 |
private double x = 0D; |
70 |
private double y = 0D; |
71 |
private GeometryManager geometryManager = null; |
72 |
|
73 |
public XYShiftOperation(double x, double y) { |
74 |
this.x = x;
|
75 |
this.y = y;
|
76 |
this.geometryManager = GeometryLocator.getGeometryManager();
|
77 |
} |
78 |
|
79 |
/*
|
80 |
* (non-Javadoc)
|
81 |
* @see org.gvsig.geoprocess.algorithm.base.core.GeometryOperation#invoke(org.gvsig.fmap.geom.Geometry, org.gvsig.fmap.dal.feature.Feature)
|
82 |
*/
|
83 |
@SuppressWarnings("unchecked") |
84 |
public EditableFeature invoke(org.gvsig.fmap.geom.Geometry g, Feature feature) {
|
85 |
List geomList = feature.getGeometries();
|
86 |
try {
|
87 |
if(geomList == null) { |
88 |
org.gvsig.fmap.geom.Geometry oldGeom = feature.getDefaultGeometry(); |
89 |
Geometry newGeom = shiftGeometry(oldGeom); |
90 |
EditableFeature editFeat = feature.getEditable(); |
91 |
editFeat.setDefaultGeometry(newGeom); |
92 |
persister.addFeature(feature, newGeom); |
93 |
} else {
|
94 |
Iterator<org.gvsig.fmap.geom.Geometry> itGeom = geomList.iterator();
|
95 |
EditableFeature editFeat = null;
|
96 |
boolean first = true; |
97 |
int nGeom = 0; |
98 |
while(itGeom.hasNext()) {
|
99 |
org.gvsig.fmap.geom.Geometry oldGeom = itGeom.next(); |
100 |
Geometry newGeom = shiftGeometry(oldGeom); |
101 |
editFeat = feature.getEditable(); |
102 |
editFeat.setGeometry(nGeom, newGeom); |
103 |
nGeom ++; |
104 |
if(first)
|
105 |
persister.addFeature(editFeat, newGeom); |
106 |
else
|
107 |
persister.addGeometryToExistingFeature(editFeat, newGeom); |
108 |
} |
109 |
} |
110 |
} catch (DataException e) {
|
111 |
Sextante.addErrorToLog(e); |
112 |
} catch (CreateGeometryException e1) {
|
113 |
Sextante.addErrorToLog(e1); |
114 |
} |
115 |
|
116 |
return lastEditFeature;
|
117 |
} |
118 |
|
119 |
/**
|
120 |
* Shifts all coordinates in a geometry
|
121 |
* @param oldGeom
|
122 |
* @return
|
123 |
* @throws CreateGeometryException
|
124 |
*/
|
125 |
private Geometry shiftGeometry(Geometry oldGeom) throws CreateGeometryException { |
126 |
Geometry newGeom = null;
|
127 |
if(oldGeom.getType() == Geometry.TYPES.SURFACE || oldGeom.getType() == Geometry.TYPES.MULTISURFACE)
|
128 |
newGeom = shiftSurface((Surface)oldGeom); |
129 |
if(oldGeom.getType() == Geometry.TYPES.CURVE || oldGeom.getType() == Geometry.TYPES.MULTICURVE)
|
130 |
newGeom = shiftCurve((Curve)oldGeom); |
131 |
if(oldGeom.getType() == Geometry.TYPES.POINT || oldGeom.getType() == Geometry.TYPES.MULTIPOINT)
|
132 |
newGeom = shiftPoint((Point)oldGeom);
|
133 |
return newGeom;
|
134 |
} |
135 |
|
136 |
/**
|
137 |
* Shifts all coordinates in a surface
|
138 |
* @param geom
|
139 |
* @return
|
140 |
* @throws CreateGeometryException
|
141 |
*/
|
142 |
private Surface shiftSurface(Surface geom) throws CreateGeometryException { |
143 |
Surface surface = (Surface)geometryManager.create(Geometry.TYPES.SURFACE, geom.getGeometryType().getSubType()); |
144 |
GeneralPathX path = geom.getGeneralPath(); |
145 |
double[] coords = path.getPointCoords(); |
146 |
for (int i = 0; i < coords.length; i++) { |
147 |
if((coords[i] % 2) == 0) |
148 |
coords[i] = coords[i] + x; |
149 |
else
|
150 |
coords[i] = coords[i] + y; |
151 |
} |
152 |
path.setPointCoords(coords); |
153 |
surface.setGeneralPath(path); |
154 |
return surface;
|
155 |
} |
156 |
|
157 |
/**
|
158 |
* Shifts all coordinates in a curve
|
159 |
* @param geom
|
160 |
* @return
|
161 |
* @throws CreateGeometryException
|
162 |
*/
|
163 |
private Curve shiftCurve(Curve geom) throws CreateGeometryException { |
164 |
Curve curve = (Curve)geometryManager.create(Geometry.TYPES.CURVE, geom.getGeometryType().getSubType()); |
165 |
GeneralPathX path = geom.getGeneralPath(); |
166 |
double[] coords = path.getPointCoords(); |
167 |
for (int i = 0; i < coords.length; i++) { |
168 |
if((coords[i] % 2) == 0) |
169 |
coords[i] = coords[i] + x; |
170 |
else
|
171 |
coords[i] = coords[i] + y; |
172 |
} |
173 |
path.setPointCoords(coords); |
174 |
curve.setGeneralPath(path); |
175 |
return curve;
|
176 |
} |
177 |
|
178 |
/**
|
179 |
* Shifts a point
|
180 |
* @param geom
|
181 |
* @return
|
182 |
* @throws CreateGeometryException
|
183 |
*/
|
184 |
private Point shiftPoint(Point geom) throws CreateGeometryException { |
185 |
Point point = (Point)geometryManager.create(Geometry.TYPES.POINT, geom.getGeometryType().getSubType()); |
186 |
point.setX(geom.getX() + x); |
187 |
point.setY(geom.getY() + y); |
188 |
return point;
|
189 |
} |
190 |
|
191 |
/*
|
192 |
* (non-Javadoc)
|
193 |
* @see org.gvsig.geoprocess.algorithm.base.core.GeometryOperation#invoke(org.gvsig.fmap.geom.Geometry, org.gvsig.fmap.dal.feature.EditableFeature)
|
194 |
*/
|
195 |
public void invoke(org.gvsig.fmap.geom.Geometry g, EditableFeature feature) { |
196 |
} |
197 |
} |
198 |
|