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