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 @ 237
History | View | Annotate | Download (6.59 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 |
/*
|
22 |
|
23 |
* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
24 |
*
|
25 |
* Copyright (C) 2010 Generalitat Valenciana.
|
26 |
*
|
27 |
* This program is free software; you can redistribute it and/or
|
28 |
* modify it under the terms of the GNU General Public License
|
29 |
* as published by the Free Software Foundation; either version 2
|
30 |
* of the License, or (at your option) any later version.
|
31 |
*
|
32 |
* This program is distributed in the hope that it will be useful,
|
33 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
34 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
35 |
* GNU General Public License for more details.
|
36 |
*
|
37 |
* You should have received a copy of the GNU General Public License
|
38 |
* along with this program; if not, write to the Free Software
|
39 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
40 |
*/
|
41 |
|
42 |
package org.gvsig.geoprocess.algorithm.xyshift; |
43 |
|
44 |
import java.util.Iterator; |
45 |
import java.util.List; |
46 |
|
47 |
import es.unex.sextante.core.Sextante; |
48 |
|
49 |
import org.gvsig.fmap.dal.exception.DataException; |
50 |
import org.gvsig.fmap.dal.feature.EditableFeature; |
51 |
import org.gvsig.fmap.dal.feature.Feature; |
52 |
import org.gvsig.fmap.geom.Geometry; |
53 |
import org.gvsig.fmap.geom.GeometryLocator; |
54 |
import org.gvsig.fmap.geom.GeometryManager; |
55 |
import org.gvsig.fmap.geom.exception.CreateGeometryException; |
56 |
import org.gvsig.fmap.geom.primitive.Curve; |
57 |
import org.gvsig.fmap.geom.primitive.GeneralPathX; |
58 |
import org.gvsig.fmap.geom.primitive.Point; |
59 |
import org.gvsig.fmap.geom.primitive.Surface; |
60 |
import org.gvsig.geoprocess.algorithm.base.core.GeometryOperation; |
61 |
/**
|
62 |
* Translates all geometries
|
63 |
* @author Nacho Brodin (nachobrodin@gmail.com)
|
64 |
*/
|
65 |
public class XYShiftOperation extends GeometryOperation { |
66 |
private double x = 0D; |
67 |
private double y = 0D; |
68 |
private GeometryManager geometryManager = null; |
69 |
|
70 |
public XYShiftOperation(double x, double y) { |
71 |
this.x = x;
|
72 |
this.y = y;
|
73 |
this.geometryManager = GeometryLocator.getGeometryManager();
|
74 |
} |
75 |
|
76 |
/*
|
77 |
* (non-Javadoc)
|
78 |
* @see org.gvsig.geoprocess.algorithm.base.core.GeometryOperation#invoke(org.gvsig.fmap.geom.Geometry, org.gvsig.fmap.dal.feature.Feature)
|
79 |
*/
|
80 |
@SuppressWarnings("unchecked") |
81 |
public EditableFeature invoke(org.gvsig.fmap.geom.Geometry g, Feature feature) {
|
82 |
List geomList = feature.getGeometries();
|
83 |
try {
|
84 |
if(geomList == null) { |
85 |
org.gvsig.fmap.geom.Geometry oldGeom = feature.getDefaultGeometry(); |
86 |
Geometry newGeom = shiftGeometry(oldGeom); |
87 |
EditableFeature editFeat = feature.getEditable(); |
88 |
editFeat.setDefaultGeometry(newGeom); |
89 |
persister.addFeature(feature, newGeom); |
90 |
} else {
|
91 |
Iterator<org.gvsig.fmap.geom.Geometry> itGeom = geomList.iterator();
|
92 |
EditableFeature editFeat = null;
|
93 |
boolean first = true; |
94 |
int nGeom = 0; |
95 |
while(itGeom.hasNext()) {
|
96 |
org.gvsig.fmap.geom.Geometry oldGeom = itGeom.next(); |
97 |
Geometry newGeom = shiftGeometry(oldGeom); |
98 |
editFeat = feature.getEditable(); |
99 |
editFeat.setGeometry(nGeom, newGeom); |
100 |
nGeom ++; |
101 |
if(first)
|
102 |
persister.addFeature(editFeat, newGeom); |
103 |
else
|
104 |
persister.addGeometryToExistingFeature(editFeat, newGeom); |
105 |
} |
106 |
} |
107 |
} catch (DataException e) {
|
108 |
Sextante.addErrorToLog(e); |
109 |
} catch (CreateGeometryException e1) {
|
110 |
Sextante.addErrorToLog(e1); |
111 |
} |
112 |
|
113 |
return lastEditFeature;
|
114 |
} |
115 |
|
116 |
/**
|
117 |
* Shifts all coordinates in a geometry
|
118 |
* @param oldGeom
|
119 |
* @return
|
120 |
* @throws CreateGeometryException
|
121 |
*/
|
122 |
private Geometry shiftGeometry(Geometry oldGeom) throws CreateGeometryException { |
123 |
Geometry newGeom = null;
|
124 |
if(oldGeom.getType() == Geometry.TYPES.SURFACE || oldGeom.getType() == Geometry.TYPES.MULTISURFACE)
|
125 |
newGeom = shiftSurface((Surface)oldGeom); |
126 |
if(oldGeom.getType() == Geometry.TYPES.CURVE || oldGeom.getType() == Geometry.TYPES.MULTICURVE)
|
127 |
newGeom = shiftCurve((Curve)oldGeom); |
128 |
if(oldGeom.getType() == Geometry.TYPES.POINT || oldGeom.getType() == Geometry.TYPES.MULTIPOINT)
|
129 |
newGeom = shiftPoint((Point)oldGeom);
|
130 |
return newGeom;
|
131 |
} |
132 |
|
133 |
/**
|
134 |
* Shifts all coordinates in a surface
|
135 |
* @param geom
|
136 |
* @return
|
137 |
* @throws CreateGeometryException
|
138 |
*/
|
139 |
private Surface shiftSurface(Surface geom) throws CreateGeometryException { |
140 |
Surface surface = (Surface)geometryManager.create(Geometry.TYPES.SURFACE, geom.getGeometryType().getSubType()); |
141 |
GeneralPathX path = geom.getGeneralPath(); |
142 |
double[] coords = path.getPointCoords(); |
143 |
for (int i = 0; i < coords.length; i++) { |
144 |
if((coords[i] % 2) == 0) |
145 |
coords[i] = coords[i] + x; |
146 |
else
|
147 |
coords[i] = coords[i] + y; |
148 |
} |
149 |
path.setPointCoords(coords); |
150 |
surface.setGeneralPath(path); |
151 |
return surface;
|
152 |
} |
153 |
|
154 |
/**
|
155 |
* Shifts all coordinates in a curve
|
156 |
* @param geom
|
157 |
* @return
|
158 |
* @throws CreateGeometryException
|
159 |
*/
|
160 |
private Curve shiftCurve(Curve geom) throws CreateGeometryException { |
161 |
Curve curve = (Curve)geometryManager.create(Geometry.TYPES.CURVE, geom.getGeometryType().getSubType()); |
162 |
GeneralPathX path = geom.getGeneralPath(); |
163 |
double[] coords = path.getPointCoords(); |
164 |
for (int i = 0; i < coords.length; i++) { |
165 |
if((coords[i] % 2) == 0) |
166 |
coords[i] = coords[i] + x; |
167 |
else
|
168 |
coords[i] = coords[i] + y; |
169 |
} |
170 |
path.setPointCoords(coords); |
171 |
curve.setGeneralPath(path); |
172 |
return curve;
|
173 |
} |
174 |
|
175 |
/**
|
176 |
* Shifts a point
|
177 |
* @param geom
|
178 |
* @return
|
179 |
* @throws CreateGeometryException
|
180 |
*/
|
181 |
private Point shiftPoint(Point geom) throws CreateGeometryException { |
182 |
Point point = (Point)geometryManager.create(Geometry.TYPES.POINT, geom.getGeometryType().getSubType()); |
183 |
point.setX(geom.getX() + x); |
184 |
point.setY(geom.getY() + y); |
185 |
return point;
|
186 |
} |
187 |
|
188 |
/*
|
189 |
* (non-Javadoc)
|
190 |
* @see org.gvsig.geoprocess.algorithm.base.core.GeometryOperation#invoke(org.gvsig.fmap.geom.Geometry, org.gvsig.fmap.dal.feature.EditableFeature)
|
191 |
*/
|
192 |
public void invoke(org.gvsig.fmap.geom.Geometry g, EditableFeature feature) { |
193 |
} |
194 |
} |
195 |
|