gvsig-vectorediting / org.gvsig.vectorediting / trunk / org.gvsig.vectorediting / org.gvsig.vectorediting.lib / org.gvsig.vectorediting.lib.prov / org.gvsig.vectorediting.lib.prov.circlecr / src / main / java / org / gvsig / vectorediting / lib / prov / circlecr / CircleTanTanRadiusEditingProvider.java @ 2109
History | View | Annotate | Download (12.6 KB)
1 | 159 | llmarques | /**
|
---|---|---|---|
2 | * gvSIG. Desktop Geographic Information System.
|
||
3 | 41 | fdiaz | *
|
4 | 159 | llmarques | * 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 | 28 | llmarques | */
|
24 | 47 | fdiaz | package org.gvsig.vectorediting.lib.prov.circlecr; |
25 | 28 | llmarques | |
26 | 38 | llmarques | import java.util.ArrayList; |
27 | 28 | llmarques | import java.util.HashMap; |
28 | import java.util.List; |
||
29 | import java.util.Map; |
||
30 | 2102 | fdiaz | import org.gvsig.euclidean.EuclideanManager; |
31 | 2109 | fdiaz | import org.gvsig.fmap.dal.exception.DataException; |
32 | 28 | llmarques | |
33 | 45 | llmarques | import org.gvsig.fmap.dal.feature.FeatureStore; |
34 | 28 | llmarques | import org.gvsig.fmap.geom.Geometry; |
35 | 55 | fdiaz | import org.gvsig.fmap.geom.GeometryLocator; |
36 | 2102 | fdiaz | import org.gvsig.fmap.geom.GeometryManager; |
37 | 46 | fdiaz | import org.gvsig.fmap.geom.aggregate.MultiSurface; |
38 | 2109 | fdiaz | import org.gvsig.fmap.geom.exception.CreateGeometryException; |
39 | 28 | llmarques | import org.gvsig.fmap.geom.primitive.Circle; |
40 | 37 | llmarques | import org.gvsig.fmap.geom.primitive.Point; |
41 | 46 | fdiaz | import org.gvsig.fmap.geom.type.GeometryType; |
42 | 2109 | fdiaz | import org.gvsig.fmap.mapcontext.MapContext; |
43 | import org.gvsig.fmap.mapcontext.layers.FLayer; |
||
44 | import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
||
45 | 333 | fdiaz | import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol; |
46 | 28 | llmarques | import org.gvsig.tools.dynobject.DynObject; |
47 | import org.gvsig.tools.service.spi.ProviderServices; |
||
48 | 2102 | fdiaz | import org.gvsig.tools.util.ToolsUtilLocator; |
49 | 28 | llmarques | import org.gvsig.vectorediting.lib.api.EditingServiceParameter; |
50 | 44 | llmarques | import org.gvsig.vectorediting.lib.api.EditingServiceParameter.TYPE; |
51 | 61 | llmarques | import org.gvsig.vectorediting.lib.api.exceptions.DrawServiceException; |
52 | import org.gvsig.vectorediting.lib.api.exceptions.FinishServiceException; |
||
53 | 62 | llmarques | import org.gvsig.vectorediting.lib.api.exceptions.InvalidEntryException; |
54 | 63 | fdiaz | import org.gvsig.vectorediting.lib.api.exceptions.StartServiceException; |
55 | 28 | llmarques | import org.gvsig.vectorediting.lib.spi.AbstractEditingProvider; |
56 | 55 | fdiaz | import org.gvsig.vectorediting.lib.spi.DefaultDrawingStatus; |
57 | 28 | llmarques | import org.gvsig.vectorediting.lib.spi.DefaultEditingServiceParameter; |
58 | import org.gvsig.vectorediting.lib.spi.EditingProvider; |
||
59 | import org.gvsig.vectorediting.lib.spi.EditingProviderFactory; |
||
60 | 333 | fdiaz | import org.gvsig.vectorediting.lib.spi.EditingProviderLocator; |
61 | import org.gvsig.vectorediting.lib.spi.EditingProviderManager; |
||
62 | 28 | llmarques | import org.gvsig.vectorediting.lib.spi.EditingProviderServices; |
63 | 2109 | fdiaz | import org.slf4j.Logger; |
64 | import org.slf4j.LoggerFactory; |
||
65 | 28 | llmarques | |
66 | 2109 | fdiaz | public class CircleTanTanRadiusEditingProvider extends AbstractEditingProvider implements |
67 | 2102 | fdiaz | EditingProvider { |
68 | 28 | llmarques | |
69 | 2102 | fdiaz | protected EditingServiceParameter firstPoint;
|
70 | 65 | fdiaz | |
71 | 2102 | fdiaz | protected EditingServiceParameter secondPoint;
|
72 | 28 | llmarques | |
73 | 2109 | fdiaz | protected EditingServiceParameter radius;
|
74 | |||
75 | protected Geometry firstGeometry;
|
||
76 | 2102 | fdiaz | |
77 | 2109 | fdiaz | protected Geometry secondGeometry;
|
78 | 2102 | fdiaz | |
79 | 136 | llmarques | protected Map<EditingServiceParameter, Object> values; |
80 | 28 | llmarques | |
81 | 136 | llmarques | protected FeatureStore featureStore;
|
82 | 2109 | fdiaz | |
83 | private final DynObject parameters; |
||
84 | |||
85 | private static final Logger LOGGER = LoggerFactory |
||
86 | .getLogger(CircleTanTanRadiusEditingProvider.class); |
||
87 | 28 | llmarques | |
88 | 2109 | fdiaz | |
89 | public CircleTanTanRadiusEditingProvider(ProviderServices providerServices,
|
||
90 | 2102 | fdiaz | DynObject parameters) { |
91 | 136 | llmarques | super(providerServices);
|
92 | 193 | llmarques | |
93 | 2109 | fdiaz | this.parameters = parameters;
|
94 | |||
95 | 2102 | fdiaz | this.featureStore
|
96 | = (FeatureStore) parameters |
||
97 | .getDynValue(EditingProviderFactory.FEATURE_STORE_FIELD); |
||
98 | 193 | llmarques | |
99 | 2102 | fdiaz | this.firstPoint
|
100 | 2109 | fdiaz | = new DefaultEditingServiceParameter("point_near_tangent_in_first_geometry", "point_near_tangent_in_first_geometry", |
101 | 2102 | fdiaz | TYPE.POSITION); |
102 | 193 | llmarques | |
103 | 2102 | fdiaz | this.secondPoint
|
104 | 2109 | fdiaz | = new DefaultEditingServiceParameter("point_near_tangent_in_second_geometry", "point_near_tangent_in_second_geometry", |
105 | 2102 | fdiaz | TYPE.POSITION); |
106 | |||
107 | 2109 | fdiaz | this.radius
|
108 | = new DefaultEditingServiceParameter("radius", "radius", |
||
109 | TYPE.VALUE); |
||
110 | 136 | llmarques | } |
111 | 28 | llmarques | |
112 | 136 | llmarques | public DefaultDrawingStatus getDrawingStatus(Point mousePosition) |
113 | 2102 | fdiaz | throws DrawServiceException {
|
114 | DefaultDrawingStatus drawingStatus = new DefaultDrawingStatus();
|
||
115 | EditingProviderManager editingProviderManager |
||
116 | = EditingProviderLocator.getProviderManager(); |
||
117 | 333 | fdiaz | ISymbol polygonSymbolEditing = editingProviderManager.getSymbol("polygon-symbol-editing");
|
118 | ISymbol auxiliaryLineSymbolEditing = editingProviderManager.getSymbol("auxiliary-line-symbol-editing");
|
||
119 | ISymbol auxiliaryPointSymbolEditing = editingProviderManager.getSymbol("auxiliary-point-symbol-editing");
|
||
120 | |||
121 | 2102 | fdiaz | EuclideanManager euclideanManager = ToolsUtilLocator.getEuclideanManager(); |
122 | GeometryManager geomManager = GeometryLocator.getGeometryManager(); |
||
123 | 178 | llmarques | |
124 | 2102 | fdiaz | EditingProviderServices editingProviderServices |
125 | = (EditingProviderServices) getProviderServices(); |
||
126 | |||
127 | if (values != null) { |
||
128 | 55 | fdiaz | |
129 | 2102 | fdiaz | Point firstPointValue = (Point) values.get(firstPoint); |
130 | Point secondPointValue = (Point) values.get(secondPoint); |
||
131 | 2109 | fdiaz | Double radiusValue = (Double) values.get(radius); |
132 | 61 | llmarques | |
133 | 28 | llmarques | |
134 | 2102 | fdiaz | if (firstPointValue != null && secondPointValue == null) { |
135 | 28 | llmarques | |
136 | 2102 | fdiaz | try {
|
137 | |||
138 | int subtype = editingProviderServices.getSubType(featureStore);
|
||
139 | |||
140 | 2109 | fdiaz | drawingStatus.addStatus(firstGeometry.toLines(), auxiliaryLineSymbolEditing, "");
|
141 | 2102 | fdiaz | drawingStatus.addStatus(firstPointValue, auxiliaryPointSymbolEditing, "");
|
142 | |||
143 | } catch (Exception e) { |
||
144 | throw new DrawServiceException(e); |
||
145 | } |
||
146 | |||
147 | 2109 | fdiaz | } else if (firstPointValue != null && secondPointValue != null && radiusValue == null) { |
148 | 2102 | fdiaz | |
149 | try {
|
||
150 | |||
151 | int subtype = editingProviderServices.getSubType(featureStore);
|
||
152 | |||
153 | 2109 | fdiaz | drawingStatus.addStatus(firstGeometry.toLines(), auxiliaryLineSymbolEditing, "");
|
154 | 2102 | fdiaz | drawingStatus.addStatus(firstPointValue, auxiliaryPointSymbolEditing, "");
|
155 | 2109 | fdiaz | drawingStatus.addStatus(secondGeometry.toLines(), auxiliaryLineSymbolEditing, "");
|
156 | 2102 | fdiaz | drawingStatus.addStatus(secondPointValue, auxiliaryPointSymbolEditing, "");
|
157 | |||
158 | } catch (Exception e) { |
||
159 | throw new DrawServiceException(e); |
||
160 | } |
||
161 | } |
||
162 | } |
||
163 | return drawingStatus;
|
||
164 | 136 | llmarques | } |
165 | 28 | llmarques | |
166 | 136 | llmarques | public EditingServiceParameter next() {
|
167 | 2102 | fdiaz | if (values != null) { |
168 | if (values.get(firstPoint) == null) { |
||
169 | return firstPoint;
|
||
170 | } else if (values.get(secondPoint) == null) { |
||
171 | return secondPoint;
|
||
172 | 2109 | fdiaz | } else if (values.get(radius) == null) { |
173 | return radius;
|
||
174 | 2102 | fdiaz | } |
175 | 136 | llmarques | } |
176 | return null; |
||
177 | 28 | llmarques | } |
178 | 136 | llmarques | |
179 | public void stop() { |
||
180 | values.clear(); |
||
181 | 28 | llmarques | } |
182 | |||
183 | 136 | llmarques | private void validateAndInsertValue(EditingServiceParameter param, |
184 | 2102 | fdiaz | Object value) throws InvalidEntryException { |
185 | if (param == firstPoint) {
|
||
186 | 136 | llmarques | if (value instanceof Point) { |
187 | 2109 | fdiaz | firstGeometry = getGeometry((Point)value);
|
188 | if(firstGeometry != null) { |
||
189 | values.put(param, value); |
||
190 | return;
|
||
191 | } |
||
192 | 136 | llmarques | } |
193 | 2102 | fdiaz | } else if (param == secondPoint) { |
194 | 136 | llmarques | if (value instanceof Point) { |
195 | 2109 | fdiaz | secondGeometry = getGeometry((Point)value);
|
196 | if(secondGeometry != null) { |
||
197 | values.put(param, value); |
||
198 | } |
||
199 | 2102 | fdiaz | return;
|
200 | 193 | llmarques | } |
201 | 2109 | fdiaz | } else if (value instanceof Double) { |
202 | double radius = (Double) value; |
||
203 | if ((radius - 0.01) > 0 && validateRadius(radius)) { |
||
204 | 2102 | fdiaz | values.put(param, value); |
205 | return;
|
||
206 | } |
||
207 | 56 | llmarques | } |
208 | 136 | llmarques | throw new InvalidEntryException(null); |
209 | 56 | llmarques | } |
210 | 2109 | fdiaz | |
211 | protected Geometry getGeometry(Point point){ |
||
212 | |||
213 | EditingProviderServices editingProviderServices = (EditingProviderServices) getProviderServices(); |
||
214 | 47 | fdiaz | |
215 | 2109 | fdiaz | MapContext mapContext = (MapContext) this.parameters.getDynValue(EditingProviderFactory.MAPCONTEXT_FIELD);
|
216 | Geometry geometry = editingProviderServices.getGeometryOfVisibleLayers(point, featureStore, mapContext); |
||
217 | // Geometry geometry = editingProviderServices.getGeometry(point, this.featureStore, mapContext);
|
||
218 | // if(geometry == null){
|
||
219 | // FLayer[] visibleLayers = mapContext.getLayers().getVisibles();//.getActives();
|
||
220 | // for (FLayer visibleLayer : visibleLayers) {
|
||
221 | // if(visibleLayer instanceof FLyrVect){
|
||
222 | // if(((FLyrVect) visibleLayer).getFeatureStore()!=this.featureStore){
|
||
223 | // geometry = editingProviderServices.getGeometry(point, ((FLyrVect)visibleLayer).getFeatureStore(), mapContext);
|
||
224 | // if(geometry != null){
|
||
225 | // break;
|
||
226 | // }
|
||
227 | // }
|
||
228 | // }
|
||
229 | // }
|
||
230 | // }
|
||
231 | return geometry;
|
||
232 | } |
||
233 | |||
234 | private boolean validateRadius(double radius) { |
||
235 | try {
|
||
236 | Point firstPointValue = (Point) values.get(firstPoint); |
||
237 | Point secondPointValue = (Point) values.get(secondPoint); |
||
238 | |||
239 | EditingProviderServices editingProviderServices |
||
240 | = (EditingProviderServices) getProviderServices(); |
||
241 | |||
242 | int subtype = editingProviderServices.getSubType(featureStore);
|
||
243 | |||
244 | Geometry geometry |
||
245 | = editingProviderServices.createCircle(firstGeometry, secondGeometry, radius, firstPointValue, secondPointValue, subtype); |
||
246 | return geometry != null; |
||
247 | } catch (DataException | CreateGeometryException ex) {
|
||
248 | LOGGER.warn("Can't create circle with radius "+radius, ex);
|
||
249 | } |
||
250 | return false; |
||
251 | } |
||
252 | |||
253 | 2102 | fdiaz | @Override
|
254 | 136 | llmarques | public List<EditingServiceParameter> getParameters() { |
255 | 2102 | fdiaz | List<EditingServiceParameter> list
|
256 | = new ArrayList<>(); |
||
257 | list.add(firstPoint); |
||
258 | list.add(secondPoint); |
||
259 | 2109 | fdiaz | list.add(radius); |
260 | 136 | llmarques | return list;
|
261 | } |
||
262 | 28 | llmarques | |
263 | 2102 | fdiaz | @Override
|
264 | 136 | llmarques | public void setValue(Object value) throws InvalidEntryException { |
265 | EditingServiceParameter param = next(); |
||
266 | validateAndInsertValue(param, value); |
||
267 | } |
||
268 | 28 | llmarques | |
269 | 2102 | fdiaz | @Override
|
270 | 136 | llmarques | public void finishAndStore() throws FinishServiceException { |
271 | 2102 | fdiaz | EditingProviderServices editingProviderServices |
272 | = (EditingProviderServices) getProviderServices(); |
||
273 | 136 | llmarques | Geometry geometry = finish(); |
274 | 2109 | fdiaz | if(geometry!=null){ |
275 | editingProviderServices.insertGeometryIntoFeatureStore(geometry, |
||
276 | 2102 | fdiaz | featureStore); |
277 | 2109 | fdiaz | } |
278 | 136 | llmarques | } |
279 | 80 | llmarques | |
280 | 136 | llmarques | public Geometry finish() throws FinishServiceException { |
281 | 174 | llmarques | |
282 | 2102 | fdiaz | final GeometryManager geomManager = GeometryLocator.getGeometryManager();
|
283 | |||
284 | Point firstPointValue = (Point) values.get(firstPoint); |
||
285 | Point secondPointValue = (Point) values.get(secondPoint); |
||
286 | 2109 | fdiaz | double radiusValue = (double) values.get(radius); |
287 | 2102 | fdiaz | |
288 | EditingProviderServices editingProviderServices |
||
289 | = (EditingProviderServices) getProviderServices(); |
||
290 | |||
291 | 136 | llmarques | try {
|
292 | 178 | llmarques | int subtype = editingProviderServices.getSubType(featureStore);
|
293 | 2102 | fdiaz | GeometryType storeGeomType |
294 | = editingProviderServices.getGeomType(featureStore); |
||
295 | Geometry geometry |
||
296 | 2109 | fdiaz | = editingProviderServices.createCircle(firstGeometry, secondGeometry, radiusValue, firstPointValue, secondPointValue, subtype); |
297 | 174 | llmarques | |
298 | 2109 | fdiaz | |
299 | 136 | llmarques | if (storeGeomType.isTypeOf(MULTISURFACE)) {
|
300 | 2109 | fdiaz | MultiSurface multisurface = null;
|
301 | if(geometry != null) { |
||
302 | multisurface |
||
303 | 2102 | fdiaz | = geomManager.createMultiSurface( |
304 | storeGeomType.getSubType()); |
||
305 | 2109 | fdiaz | multisurface.addSurface((Circle) geometry); |
306 | } |
||
307 | 136 | llmarques | return multisurface;
|
308 | } |
||
309 | 174 | llmarques | |
310 | 136 | llmarques | return geometry;
|
311 | } catch (Exception e) { |
||
312 | throw new FinishServiceException(e); |
||
313 | } |
||
314 | 56 | llmarques | } |
315 | 2109 | fdiaz | |
316 | 2102 | fdiaz | @Override
|
317 | 136 | llmarques | public void start() throws StartServiceException { |
318 | 2102 | fdiaz | this.values = new HashMap<>(); |
319 | 56 | llmarques | } |
320 | 28 | llmarques | |
321 | 2102 | fdiaz | @Override
|
322 | 136 | llmarques | public String getName() { |
323 | 2109 | fdiaz | return CircleTanTanRadiusEditingProviderFactory.PROVIDER_NAME;
|
324 | 136 | llmarques | } |
325 | 28 | llmarques | |
326 | } |