gvsig-vectorediting / org.gvsig.vectorediting / trunk / org.gvsig.vectorediting / org.gvsig.vectorediting.lib / org.gvsig.vectorediting.lib.prov / org.gvsig.vectorediting.lib.prov.autopolygon / src / main / java / org / gvsig / vectorediting / lib / prov / autopolygon / AutopolygonEditingProvider.java @ 1494
History | View | Annotate | Download (7.45 KB)
1 | 301 | fdiaz | /**
|
---|---|---|---|
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 | 1494 | jjdelcerro | package org.gvsig.vectorediting.lib.prov.autopolygon; |
26 | 301 | fdiaz | |
27 | import java.util.ArrayList; |
||
28 | import java.util.List; |
||
29 | |||
30 | import org.cresques.cts.IProjection; |
||
31 | |||
32 | import org.gvsig.fmap.dal.exception.DataException; |
||
33 | import org.gvsig.fmap.dal.feature.Feature; |
||
34 | import org.gvsig.fmap.dal.feature.FeatureQuery; |
||
35 | import org.gvsig.fmap.dal.feature.FeatureSet; |
||
36 | import org.gvsig.fmap.dal.feature.FeatureStore; |
||
37 | import org.gvsig.fmap.dal.feature.FeatureType; |
||
38 | import org.gvsig.fmap.geom.Geometry; |
||
39 | import org.gvsig.fmap.geom.GeometryLocator; |
||
40 | import org.gvsig.fmap.geom.aggregate.MultiSurface; |
||
41 | import org.gvsig.fmap.geom.primitive.Point; |
||
42 | import org.gvsig.fmap.geom.primitive.Surface; |
||
43 | import org.gvsig.fmap.geom.type.GeometryType; |
||
44 | 1494 | jjdelcerro | import org.gvsig.fmap.mapcontext.layers.vectorial.SpatialEvaluatorsFactory; |
45 | 301 | fdiaz | import org.gvsig.tools.dispose.DisposableIterator; |
46 | import org.gvsig.tools.dynobject.DynObject; |
||
47 | 1494 | jjdelcerro | import org.gvsig.tools.evaluator.Evaluator; |
48 | 301 | fdiaz | import org.gvsig.tools.service.spi.ProviderServices; |
49 | import org.gvsig.vectorediting.lib.api.DrawingStatus; |
||
50 | import org.gvsig.vectorediting.lib.api.EditingServiceParameter; |
||
51 | import org.gvsig.vectorediting.lib.api.EditingServiceParameter.TYPE; |
||
52 | import org.gvsig.vectorediting.lib.api.exceptions.DrawServiceException; |
||
53 | import org.gvsig.vectorediting.lib.api.exceptions.FinishServiceException; |
||
54 | import org.gvsig.vectorediting.lib.api.exceptions.InvalidEntryException; |
||
55 | import org.gvsig.vectorediting.lib.api.exceptions.StartServiceException; |
||
56 | import org.gvsig.vectorediting.lib.api.exceptions.StopServiceException; |
||
57 | import org.gvsig.vectorediting.lib.spi.AbstractEditingProvider; |
||
58 | import org.gvsig.vectorediting.lib.spi.DefaultEditingServiceParameter; |
||
59 | import org.gvsig.vectorediting.lib.spi.EditingProvider; |
||
60 | import org.gvsig.vectorediting.lib.spi.EditingProviderFactory; |
||
61 | import org.gvsig.vectorediting.lib.spi.EditingProviderServices; |
||
62 | |||
63 | 1494 | jjdelcerro | @SuppressWarnings("UseSpecificCatch") |
64 | 301 | fdiaz | public class AutopolygonEditingProvider extends AbstractEditingProvider |
65 | implements EditingProvider {
|
||
66 | |||
67 | 1494 | jjdelcerro | private final EditingServiceParameter geometryParameter; |
68 | 301 | fdiaz | |
69 | 1494 | jjdelcerro | private final FeatureStore featureStore; |
70 | 301 | fdiaz | |
71 | private Geometry geometryValue;
|
||
72 | |||
73 | public AutopolygonEditingProvider(ProviderServices services,
|
||
74 | DynObject parameters) { |
||
75 | super(services);
|
||
76 | this.featureStore =
|
||
77 | (FeatureStore) parameters |
||
78 | .getDynValue(EditingProviderFactory.FEATURE_STORE_FIELD); |
||
79 | |||
80 | this.geometryParameter =
|
||
81 | new DefaultEditingServiceParameter("Draw geometry", |
||
82 | "draw_geometry_to_autopolygon", Geometry.TYPES.MULTISURFACE, TYPE.GEOMETRY);
|
||
83 | } |
||
84 | |||
85 | public EditingServiceParameter next() {
|
||
86 | if (geometryValue == null) { |
||
87 | return geometryParameter;
|
||
88 | } |
||
89 | |||
90 | return null; |
||
91 | } |
||
92 | |||
93 | public DrawingStatus getDrawingStatus(Point mousePosition) |
||
94 | throws DrawServiceException {
|
||
95 | return null; |
||
96 | } |
||
97 | |||
98 | public void stop() throws StopServiceException { |
||
99 | geometryValue = null;
|
||
100 | } |
||
101 | |||
102 | public List<EditingServiceParameter> getParameters() { |
||
103 | 1494 | jjdelcerro | List<EditingServiceParameter> list = new ArrayList<>(); |
104 | 301 | fdiaz | list.add(geometryParameter); |
105 | return list;
|
||
106 | } |
||
107 | |||
108 | public void setValue(Object value) throws InvalidEntryException { |
||
109 | EditingServiceParameter param = next(); |
||
110 | validateAndInsertValue(param, value); |
||
111 | } |
||
112 | |||
113 | private void validateAndInsertValue(EditingServiceParameter param, |
||
114 | Object value) throws InvalidEntryException { |
||
115 | |||
116 | if (param == geometryParameter) {
|
||
117 | 1494 | jjdelcerro | if (value instanceof Geometry ) { |
118 | 684 | fdiaz | Geometry geometry = (Geometry)value; |
119 | GeometryType geometryType = geometry.getGeometryType(); |
||
120 | if (geometryType.isTypeOf(Geometry.TYPES.MULTISURFACE) ||
|
||
121 | geometryType.isTypeOf(Geometry.TYPES.SURFACE)) { |
||
122 | geometryValue = geometry; |
||
123 | return;
|
||
124 | } |
||
125 | 301 | fdiaz | } |
126 | } |
||
127 | throw new InvalidEntryException(null); |
||
128 | } |
||
129 | |||
130 | public void finishAndStore() throws FinishServiceException { |
||
131 | EditingProviderServices editingProviderServices = |
||
132 | (EditingProviderServices) getProviderServices(); |
||
133 | Geometry geometry = finish(); |
||
134 | editingProviderServices.insertGeometryIntoFeatureStore(geometry, |
||
135 | featureStore); |
||
136 | } |
||
137 | |||
138 | public Geometry finish() throws FinishServiceException { |
||
139 | EditingProviderServices editingProviderServices = |
||
140 | (EditingProviderServices) getProviderServices(); |
||
141 | |||
142 | Geometry geometry = geometryValue.cloneGeometry(); |
||
143 | 1494 | jjdelcerro | FeatureQuery query; |
144 | 301 | fdiaz | try {
|
145 | query = queryByGeometry(geometry); |
||
146 | if (query != null) { |
||
147 | FeatureSet features = featureStore.getFeatureSet(query); |
||
148 | DisposableIterator featuresIterator = features.fastIterator(); |
||
149 | while (featuresIterator.hasNext()) {
|
||
150 | Feature feature = (Feature) featuresIterator.next(); |
||
151 | geometry = |
||
152 | geometry.difference(feature.getDefaultGeometry()); |
||
153 | } |
||
154 | featuresIterator.dispose(); |
||
155 | features.dispose(); |
||
156 | } |
||
157 | GeometryType storeGeomType = |
||
158 | editingProviderServices.getGeomType(featureStore); |
||
159 | |||
160 | if (storeGeomType.isTypeOf(MULTISURFACE)) {
|
||
161 | MultiSurface multisurface; |
||
162 | multisurface = |
||
163 | GeometryLocator.getGeometryManager().createMultiSurface( |
||
164 | storeGeomType.getSubType()); |
||
165 | multisurface.addSurface((Surface)geometry); |
||
166 | return multisurface;
|
||
167 | } |
||
168 | |||
169 | } catch (Exception e) { |
||
170 | 1494 | jjdelcerro | throw new FinishServiceException(e); |
171 | 301 | fdiaz | } |
172 | return geometry;
|
||
173 | } |
||
174 | |||
175 | public void start() throws StartServiceException, InvalidEntryException { |
||
176 | geometryValue = null;
|
||
177 | } |
||
178 | |||
179 | public String getName() { |
||
180 | return AutopolygonEditingProviderFactory.PROVIDER_NAME;
|
||
181 | } |
||
182 | |||
183 | private FeatureQuery queryByGeometry(Geometry geometry) throws DataException { |
||
184 | FeatureQuery featureQuery = featureStore.createFeatureQuery(); |
||
185 | FeatureType featureType = featureStore.getDefaultFeatureType(); |
||
186 | String geomName = featureType.getDefaultGeometryAttributeName();
|
||
187 | featureQuery.setFeatureType(featureType); |
||
188 | |||
189 | IProjection query_proj = featureType.getDefaultSRS(); |
||
190 | 1494 | jjdelcerro | Evaluator iee = SpatialEvaluatorsFactory.getInstance().intersects( |
191 | geometry, |
||
192 | query_proj, |
||
193 | featureStore |
||
194 | ); |
||
195 | 301 | fdiaz | featureQuery.setFilter(iee); |
196 | featureQuery.setAttributeNames(null);
|
||
197 | return featureQuery;
|
||
198 | } |
||
199 | |||
200 | |||
201 | |||
202 | } |