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