gvsig-vectorediting / org.gvsig.vectorediting / trunk / org.gvsig.vectorediting / org.gvsig.vectorediting.lib / org.gvsig.vectorediting.lib.prov / org.gvsig.vectorediting.lib.prov.ellipse / src / main / java / org / gvsig / vectorediting / lib / prov / ellipse / FilledEllipseEditingProvider.java @ 575
History | View | Annotate | Download (15 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright ? 2007-2014 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 |
package org.gvsig.vectorediting.lib.prov.ellipse; |
26 |
|
27 |
import java.util.ArrayList; |
28 |
import java.util.HashMap; |
29 |
import java.util.List; |
30 |
import java.util.Map; |
31 |
|
32 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
33 |
import org.gvsig.fmap.geom.Geometry; |
34 |
import org.gvsig.fmap.geom.GeometryLocator; |
35 |
import org.gvsig.fmap.geom.aggregate.MultiSurface; |
36 |
import org.gvsig.fmap.geom.exception.CreateGeometryException; |
37 |
import org.gvsig.fmap.geom.primitive.Curve; |
38 |
import org.gvsig.fmap.geom.primitive.Ellipse; |
39 |
import org.gvsig.fmap.geom.primitive.Point; |
40 |
import org.gvsig.fmap.geom.type.GeometryType; |
41 |
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol; |
42 |
import org.gvsig.tools.dynobject.DynObject; |
43 |
import org.gvsig.tools.service.spi.ProviderServices; |
44 |
import org.gvsig.vectorediting.lib.api.DrawingStatus; |
45 |
import org.gvsig.vectorediting.lib.api.EditingServiceParameter; |
46 |
import org.gvsig.vectorediting.lib.api.exceptions.DrawServiceException; |
47 |
import org.gvsig.vectorediting.lib.api.exceptions.FinishServiceException; |
48 |
import org.gvsig.vectorediting.lib.api.exceptions.InvalidEntryException; |
49 |
import org.gvsig.vectorediting.lib.api.exceptions.StartServiceException; |
50 |
import org.gvsig.vectorediting.lib.api.exceptions.StopServiceException; |
51 |
import org.gvsig.vectorediting.lib.spi.AbstractEditingProvider; |
52 |
import org.gvsig.vectorediting.lib.spi.DefaultDrawingStatus; |
53 |
import org.gvsig.vectorediting.lib.spi.DefaultEditingServiceParameter; |
54 |
import org.gvsig.vectorediting.lib.spi.EditingProvider; |
55 |
import org.gvsig.vectorediting.lib.spi.EditingProviderFactory; |
56 |
import org.gvsig.vectorediting.lib.spi.EditingProviderLocator; |
57 |
import org.gvsig.vectorediting.lib.spi.EditingProviderManager; |
58 |
import org.gvsig.vectorediting.lib.spi.EditingProviderServices; |
59 |
|
60 |
public class FilledEllipseEditingProvider extends AbstractEditingProvider implements |
61 |
EditingProvider { |
62 |
|
63 |
protected EditingServiceParameter firstPointAxisA;
|
64 |
|
65 |
protected EditingServiceParameter secondPointAxisA;
|
66 |
|
67 |
protected EditingServiceParameter lengthAxisB;
|
68 |
|
69 |
protected Map<EditingServiceParameter, Object> values; |
70 |
|
71 |
protected FeatureStore featureStore;
|
72 |
|
73 |
public FilledEllipseEditingProvider(ProviderServices providerServices,
|
74 |
DynObject parameters) { |
75 |
super(providerServices);
|
76 |
|
77 |
this.featureStore =
|
78 |
(FeatureStore) parameters |
79 |
.getDynValue(EditingProviderFactory.FEATURE_STORE_FIELD); |
80 |
|
81 |
this.firstPointAxisA =
|
82 |
new DefaultEditingServiceParameter("first_point_A_axis", |
83 |
"first_point_A_axis", EditingServiceParameter.TYPE.POSITION);
|
84 |
|
85 |
this.secondPointAxisA =
|
86 |
new DefaultEditingServiceParameter("second_point_A_axis", |
87 |
"second_point_A_axis", EditingServiceParameter.TYPE.POSITION);
|
88 |
|
89 |
this.lengthAxisB =
|
90 |
new DefaultEditingServiceParameter("length_of_B_axis", |
91 |
"length_of_B_axis", EditingServiceParameter.TYPE.POSITION,
|
92 |
EditingServiceParameter.TYPE.VALUE); |
93 |
} |
94 |
|
95 |
public EditingServiceParameter next() {
|
96 |
if (values.get(firstPointAxisA) == null) { |
97 |
return firstPointAxisA;
|
98 |
} else if (values.get(secondPointAxisA) == null) { |
99 |
return secondPointAxisA;
|
100 |
} else if (values.get(lengthAxisB) == null) { |
101 |
return lengthAxisB;
|
102 |
} |
103 |
return null; |
104 |
} |
105 |
|
106 |
public DrawingStatus getDrawingStatus(Point mousePosition) |
107 |
throws DrawServiceException {
|
108 |
DefaultDrawingStatus drawingStatus = new DefaultDrawingStatus();
|
109 |
|
110 |
EditingProviderManager editingProviderManager = |
111 |
EditingProviderLocator.getProviderManager(); |
112 |
ISymbol auxiliaryLineSymbolEditing = editingProviderManager.getSymbol("auxiliary-line-symbol-editing");
|
113 |
ISymbol auxiliaryPointSymbolEditing = editingProviderManager.getSymbol("auxiliary-point-symbol-editing");
|
114 |
|
115 |
|
116 |
if (values != null) { |
117 |
EditingProviderServices editingServiceProvider = |
118 |
(EditingProviderServices) getProviderServices(); |
119 |
|
120 |
Point firstPointAxis1Value = (Point) values.get(firstPointAxisA); |
121 |
Point secondPointAxis1Value = (Point) values.get(secondPointAxisA); |
122 |
|
123 |
if (firstPointAxis1Value != null) { |
124 |
drawingStatus.addStatus(firstPointAxis1Value, auxiliaryPointSymbolEditing, "");
|
125 |
|
126 |
|
127 |
if (secondPointAxis1Value == null) { |
128 |
|
129 |
try {
|
130 |
int subtype =
|
131 |
editingServiceProvider.getSubType(featureStore); |
132 |
Curve line = |
133 |
editingServiceProvider.createLine( |
134 |
firstPointAxis1Value, mousePosition, subtype); |
135 |
drawingStatus.addStatus(mousePosition, auxiliaryPointSymbolEditing, "");
|
136 |
drawingStatus.addStatus(line, auxiliaryLineSymbolEditing, "");
|
137 |
return drawingStatus;
|
138 |
} catch (Exception e) { |
139 |
throw new DrawServiceException(e); |
140 |
} |
141 |
|
142 |
} else {
|
143 |
|
144 |
try {
|
145 |
int subtype =
|
146 |
editingServiceProvider.getSubType(featureStore); |
147 |
Point center =
|
148 |
editingServiceProvider.getMidPoint( |
149 |
firstPointAxis1Value, secondPointAxis1Value, |
150 |
subtype); |
151 |
double distance = center.distance(mousePosition);
|
152 |
drawingStatus.addStatus(secondPointAxis1Value, auxiliaryPointSymbolEditing, "");
|
153 |
drawingStatus.addStatus(center, auxiliaryPointSymbolEditing, "");
|
154 |
|
155 |
Curve aAxis = |
156 |
editingServiceProvider.createLine( |
157 |
firstPointAxis1Value, secondPointAxis1Value, |
158 |
subtype); |
159 |
drawingStatus.addStatus(aAxis, auxiliaryLineSymbolEditing, "");
|
160 |
|
161 |
addTemporaryGeometryToDrawingStatus(drawingStatus, firstPointAxis1Value, secondPointAxis1Value, |
162 |
subtype, distance); |
163 |
|
164 |
Double[] lineParams = |
165 |
editingServiceProvider.getLineParams( |
166 |
firstPointAxis1Value, secondPointAxis1Value); |
167 |
Point[] bisector = |
168 |
editingServiceProvider.getPerpendicular( |
169 |
lineParams[0], lineParams[1], center, subtype); |
170 |
Double[] bisectorParams = |
171 |
editingServiceProvider.getLineParams(bisector[0],
|
172 |
bisector[1]);
|
173 |
|
174 |
double m = bisectorParams[0]; // Line slope |
175 |
double y0 = bisectorParams[1]; // Y-Intercept line |
176 |
double mp2 = Math.pow(m, 2); // slope? |
177 |
|
178 |
Curve line; |
179 |
if (!Double.isInfinite(m)) { |
180 |
double a = 1 + mp2; |
181 |
double b = (-2 - (2 * mp2)) * center.getX(); |
182 |
double c =
|
183 |
((1 + mp2) * Math.pow(center.getX(), 2)) |
184 |
- Math.pow(distance, 2); |
185 |
|
186 |
double x1 =
|
187 |
(-b + Math.sqrt((Math.pow(b, 2) - (4 * a * c)))) |
188 |
/ (2 * a);
|
189 |
double y1 = (m * x1) + y0;
|
190 |
|
191 |
double x2 =
|
192 |
(-b - Math.sqrt((Math.pow(b, 2) - (4 * a * c)))) |
193 |
/ (2 * a);
|
194 |
double y2 = (m * x2) + y0;
|
195 |
|
196 |
line = |
197 |
editingServiceProvider.createLine(x1, y1, x2, |
198 |
y2, subtype); |
199 |
drawingStatus.addStatus(line, auxiliaryLineSymbolEditing, "");
|
200 |
|
201 |
|
202 |
} else {
|
203 |
double x1 = center.getX();
|
204 |
double y1 = center.getY() + distance;
|
205 |
double y2 = center.getY() - distance;
|
206 |
|
207 |
line = |
208 |
editingServiceProvider.createLine(x1, y1, x1, |
209 |
y2, subtype); |
210 |
drawingStatus.addStatus(line, auxiliaryLineSymbolEditing, "");
|
211 |
} |
212 |
|
213 |
drawingStatus.addStatus(aAxis, auxiliaryLineSymbolEditing, "");
|
214 |
|
215 |
} catch (Exception e) { |
216 |
throw new DrawServiceException(e); |
217 |
} |
218 |
} |
219 |
} |
220 |
} |
221 |
return drawingStatus;
|
222 |
} |
223 |
|
224 |
/**
|
225 |
* @param drawingStatus
|
226 |
* @param firstPointAxis1Value
|
227 |
* @param secondPointAxis1Value
|
228 |
* @param subtype
|
229 |
* @param distance
|
230 |
* @throws CreateGeometryException
|
231 |
*/
|
232 |
protected void addTemporaryGeometryToDrawingStatus(DefaultDrawingStatus drawingStatus, Point firstPointAxis1Value, |
233 |
Point secondPointAxis1Value, int subtype, double distance) throws CreateGeometryException { |
234 |
Ellipse ellipse = (Ellipse) GeometryLocator.getGeometryManager().create(Geometry.TYPES.ELLIPSE, subtype); |
235 |
ellipse.setPoints(firstPointAxis1Value, secondPointAxis1Value, distance); |
236 |
|
237 |
EditingProviderManager editingProviderManager = EditingProviderLocator.getProviderManager(); |
238 |
ISymbol polygonSymbolEditing = editingProviderManager.getSymbol("polygon-symbol-editing");
|
239 |
drawingStatus.addStatus(ellipse, polygonSymbolEditing, "");
|
240 |
} |
241 |
|
242 |
public void stop() throws StopServiceException { |
243 |
if (values != null) { |
244 |
values.clear(); |
245 |
} |
246 |
|
247 |
} |
248 |
|
249 |
public List<EditingServiceParameter> getParameters() { |
250 |
List<EditingServiceParameter> list =
|
251 |
new ArrayList<EditingServiceParameter>(); |
252 |
list.add(firstPointAxisA); |
253 |
list.add(secondPointAxisA); |
254 |
list.add(lengthAxisB); |
255 |
return list;
|
256 |
} |
257 |
|
258 |
public void setValue(Object value) throws InvalidEntryException { |
259 |
EditingServiceParameter parameter = this.next();
|
260 |
validateAndInsertValue(parameter, value); |
261 |
} |
262 |
|
263 |
private void validateAndInsertValue(EditingServiceParameter param, |
264 |
Object value) throws InvalidEntryException { |
265 |
|
266 |
if ((param == firstPointAxisA) || (param == secondPointAxisA)) {
|
267 |
if (value instanceof Point) { |
268 |
values.put(param, value); |
269 |
return;
|
270 |
} |
271 |
|
272 |
} else if (param == lengthAxisB) { |
273 |
if (value instanceof Point) { |
274 |
Double length = null; |
275 |
|
276 |
try {
|
277 |
EditingProviderServices editingProviderServices = |
278 |
(EditingProviderServices) getProviderServices(); |
279 |
|
280 |
Point firstPointAxis1Value =
|
281 |
(Point) values.get(firstPointAxisA);
|
282 |
Point firstPointAxis2Value =
|
283 |
(Point) values.get(secondPointAxisA);
|
284 |
int subtype =
|
285 |
editingProviderServices.getSubType(featureStore); |
286 |
Point center =
|
287 |
editingProviderServices |
288 |
.getMidPoint(firstPointAxis1Value, |
289 |
firstPointAxis2Value, subtype); |
290 |
length = ((Point) value).distance(center) * 2; |
291 |
|
292 |
} catch (Exception e) { |
293 |
throw new InvalidEntryException(e); |
294 |
} |
295 |
|
296 |
if (length != null) { |
297 |
values.put(param, length); |
298 |
return;
|
299 |
} |
300 |
|
301 |
} else if (value instanceof Double) { |
302 |
|
303 |
if ((((Double) value) - 0.01) > 0) { |
304 |
values.put(param, value); |
305 |
return;
|
306 |
} |
307 |
} |
308 |
} |
309 |
} |
310 |
|
311 |
public Geometry finish() throws FinishServiceException { |
312 |
if (values != null) { |
313 |
|
314 |
EditingProviderServices editingProviderServices = |
315 |
(EditingProviderServices) getProviderServices(); |
316 |
|
317 |
Point firstPointAxis1Value = (Point) values.get(firstPointAxisA); |
318 |
Point secondPointAxis1Value = (Point) values.get(secondPointAxisA); |
319 |
double lengthAxis2Value = (Double) values.get(lengthAxisB); |
320 |
|
321 |
if ((firstPointAxis1Value != null) |
322 |
&& (secondPointAxis1Value != null) && (lengthAxis2Value > 0)) { |
323 |
|
324 |
|
325 |
try {
|
326 |
int subtype = editingProviderServices.getSubType(featureStore);
|
327 |
GeometryType storeGeomType = editingProviderServices.getGeomType(featureStore); |
328 |
|
329 |
Ellipse ellipse = |
330 |
(Ellipse) GeometryLocator.getGeometryManager().create(Geometry.TYPES.ELLIPSE, subtype); |
331 |
ellipse.setPoints(firstPointAxis1Value, secondPointAxis1Value, lengthAxis2Value / 2);
|
332 |
|
333 |
// if (storeGeomType.isTypeOf(MULTISURFACE)) {
|
334 |
// return ellipse.toPolygons();
|
335 |
// }
|
336 |
|
337 |
if (storeGeomType.isTypeOf(MULTISURFACE)) {
|
338 |
MultiSurface multisurface; |
339 |
multisurface = |
340 |
GeometryLocator.getGeometryManager().createMultiSurface( |
341 |
storeGeomType.getSubType()); |
342 |
multisurface.addSurface((Ellipse) ellipse); |
343 |
return multisurface;
|
344 |
} |
345 |
|
346 |
return ellipse;
|
347 |
} catch (Exception e) { |
348 |
throw new FinishServiceException(e); |
349 |
} |
350 |
} |
351 |
} |
352 |
return null; |
353 |
} |
354 |
|
355 |
public void finishAndStore() throws FinishServiceException { |
356 |
EditingProviderServices editingProviderServices = |
357 |
(EditingProviderServices) getProviderServices(); |
358 |
Geometry geometry = finish(); |
359 |
editingProviderServices.insertGeometryIntoFeatureStore(geometry, |
360 |
featureStore); |
361 |
} |
362 |
|
363 |
public void start() throws StartServiceException { |
364 |
values = new HashMap<EditingServiceParameter, Object>(); |
365 |
} |
366 |
|
367 |
public String getName() { |
368 |
return FilledEllipseEditingProviderFactory.PROVIDER_NAME;
|
369 |
} |
370 |
|
371 |
} |