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