gvsig-vectorediting / org.gvsig.vectorediting / trunk / org.gvsig.vectorediting / org.gvsig.vectorediting.lib / org.gvsig.vectorediting.lib.prov / org.gvsig.vectorediting.lib.prov.regularpolygon / src / main / java / org / gvsig / vectorediting / lib / prov / regularpolygon / FilledRegularPolygonEditingProvider.java @ 2101
History | View | Annotate | Download (6.29 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 |
/* gvSIG. Desktop Geographic Information System.
|
26 |
*
|
27 |
* Copyright ? 2007-2014 gvSIG Association
|
28 |
*
|
29 |
* This program is free software; you can redistribute it and/or
|
30 |
* modify it under the terms of the GNU General Public License
|
31 |
* as published by the Free Software Foundation; either version 2
|
32 |
* of the License, or (at your option) any later version.
|
33 |
*
|
34 |
* This program is distributed in the hope that it will be useful,
|
35 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
36 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
37 |
* GNU General Public License for more details.
|
38 |
*
|
39 |
* You should have received a copy of the GNU General Public License
|
40 |
* along with this program; if not, write to the Free Software
|
41 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
42 |
* MA 02110-1301, USA.
|
43 |
*
|
44 |
* For any additional information, do not hesitate to contact us
|
45 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
46 |
*/
|
47 |
package org.gvsig.vectorediting.lib.prov.regularpolygon; |
48 |
|
49 |
import org.gvsig.fmap.geom.Geometry; |
50 |
import org.gvsig.fmap.geom.GeometryLocator; |
51 |
import org.gvsig.fmap.geom.GeometryManager; |
52 |
import org.gvsig.fmap.geom.aggregate.MultiSurface; |
53 |
import org.gvsig.fmap.geom.primitive.Curve; |
54 |
import org.gvsig.fmap.geom.primitive.Point; |
55 |
import org.gvsig.fmap.geom.primitive.Polygon; |
56 |
import org.gvsig.fmap.geom.type.GeometryType; |
57 |
import org.gvsig.tools.dynobject.DynObject; |
58 |
import org.gvsig.tools.exception.BaseException; |
59 |
import org.gvsig.tools.service.spi.ProviderServices; |
60 |
import org.gvsig.vectorediting.lib.api.exceptions.FinishServiceException; |
61 |
import org.gvsig.vectorediting.lib.spi.EditingProvider; |
62 |
import org.gvsig.vectorediting.lib.spi.EditingProviderServices; |
63 |
|
64 |
/**
|
65 |
* @author llmarques
|
66 |
*
|
67 |
*/
|
68 |
public class FilledRegularPolygonEditingProvider extends |
69 |
RegularPolygonEditingProvider implements EditingProvider {
|
70 |
|
71 |
/**
|
72 |
* Default constructor.
|
73 |
*
|
74 |
* @param services
|
75 |
* Available services to this provider.
|
76 |
* @param parameters
|
77 |
* Needed parameters to create and instance of this provider
|
78 |
*/
|
79 |
public FilledRegularPolygonEditingProvider(DynObject services,
|
80 |
ProviderServices parameters) { |
81 |
super(parameters, services);
|
82 |
} |
83 |
|
84 |
@Override
|
85 |
public Geometry finish() throws FinishServiceException { |
86 |
|
87 |
if (values != null) { |
88 |
|
89 |
Integer sidesValue = (Integer) values.get(polygonSides); |
90 |
String modeValue = (String) values.get(mode); |
91 |
Point centerValue = (Point) values.get(center); |
92 |
Point pointOfCircleValue = (Point) values.get(pointOfCircle); |
93 |
|
94 |
try {
|
95 |
|
96 |
Polygon polygon =
|
97 |
calculateFilledPolygon(modeValue, centerValue, |
98 |
pointOfCircleValue, sidesValue); |
99 |
if (polygon != null) { |
100 |
EditingProviderServices editingProviderServices |
101 |
= (EditingProviderServices) getProviderServices(); |
102 |
GeometryType geomType |
103 |
= editingProviderServices.getGeomType(featureStore); |
104 |
|
105 |
if (geomType.isTypeOf(Geometry.TYPES.MULTISURFACE)) {
|
106 |
GeometryManager geometryManager |
107 |
= GeometryLocator.getGeometryManager(); |
108 |
MultiSurface multiSurface |
109 |
= geometryManager.createMultiSurface(geomType |
110 |
.getSubType()); |
111 |
multiSurface.addSurface(polygon); |
112 |
return multiSurface;
|
113 |
} |
114 |
} |
115 |
|
116 |
return polygon;
|
117 |
} catch (Exception e) { |
118 |
throw new FinishServiceException(e); |
119 |
} |
120 |
} |
121 |
return null; |
122 |
} |
123 |
|
124 |
/**
|
125 |
* Calculates filled polygon as of mode (inscribed or circumscribed), center
|
126 |
* of polygon, point of circle of polygon and number of sides.
|
127 |
*
|
128 |
* @param modeValue
|
129 |
* inscribed or circumscribed
|
130 |
* @param centerValue
|
131 |
* of filled polygon
|
132 |
* @param pointOfCircleValue
|
133 |
* of circle of filled polygon
|
134 |
* @param sides
|
135 |
* of filled polygon
|
136 |
* @return Surface with calculated filled polygon.
|
137 |
*/
|
138 |
private Polygon calculateFilledPolygon(String modeValue, Point centerValue, |
139 |
Point pointOfCircleValue, int sides) throws BaseException { |
140 |
|
141 |
EditingProviderServices editingProviderServices = |
142 |
(EditingProviderServices) getProviderServices(); |
143 |
int subtype = editingProviderServices.getSubType(featureStore);
|
144 |
Polygon filledPolygon =
|
145 |
GeometryLocator.getGeometryManager().createPolygon(subtype); |
146 |
|
147 |
Curve polygon = |
148 |
calculateRegularPolygon(modeValue, centerValue, pointOfCircleValue, |
149 |
sides); |
150 |
if(polygon==null){ |
151 |
return null; |
152 |
} |
153 |
|
154 |
Point vertexAnt = null; |
155 |
for (int i = 0; i < polygon.getNumVertices(); i++) { |
156 |
if ((vertexAnt == null) || !vertexAnt.equals(polygon.getVertex(i))) { |
157 |
filledPolygon.addVertex(polygon.getVertex(i)); |
158 |
vertexAnt = filledPolygon.getVertex(i); |
159 |
} |
160 |
} |
161 |
|
162 |
filledPolygon = (Polygon) closeGeometry(filledPolygon);
|
163 |
return filledPolygon;
|
164 |
} |
165 |
|
166 |
@Override
|
167 |
public String getName() { |
168 |
return FilledRegularPolygonEditingProviderFactory.PROVIDER_NAME;
|
169 |
} |
170 |
|
171 |
} |