gvsig-vectorediting / org.gvsig.vectorediting / trunk / org.gvsig.vectorediting / org.gvsig.vectorediting.lib / org.gvsig.vectorediting.lib.prov / org.gvsig.vectorediting.lib.prov.editvertex / src / main / java / org / gvsig / vectorediting / lib / prov / editvertex / operation / PolygonEditVertexOperation.java @ 424
History | View | Annotate | Download (6.2 KB)
1 |
package org.gvsig.vectorediting.lib.prov.editvertex.operation; |
---|---|
2 |
|
3 |
import java.util.ArrayList; |
4 |
import java.util.List; |
5 |
|
6 |
import org.gvsig.fmap.geom.Geometry; |
7 |
import org.gvsig.fmap.geom.GeometryLocator; |
8 |
import org.gvsig.fmap.geom.GeometryManager; |
9 |
import org.gvsig.fmap.geom.exception.CreateGeometryException; |
10 |
import org.gvsig.fmap.geom.primitive.OrientablePrimitive; |
11 |
import org.gvsig.fmap.geom.primitive.Point; |
12 |
import org.gvsig.fmap.geom.primitive.Polygon; |
13 |
import org.gvsig.fmap.geom.primitive.Primitive; |
14 |
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol; |
15 |
import org.gvsig.vectorediting.lib.spi.DefaultDrawingStatus; |
16 |
import org.gvsig.vectorediting.lib.spi.EditingProviderLocator; |
17 |
import org.gvsig.vectorediting.lib.spi.EditingProviderManager; |
18 |
|
19 |
public class PolygonEditVertexOperation implements EditVertexOperation { |
20 |
|
21 |
public PolygonEditVertexOperation() {
|
22 |
// TODO Auto-generated constructor stub
|
23 |
} |
24 |
|
25 |
public Geometry insertVertex(Primitive geom, int index) |
26 |
throws CreateGeometryException {
|
27 |
GeometryManager geometryManager = GeometryLocator.getGeometryManager(); |
28 |
Polygon result =
|
29 |
geometryManager.createPolygon(geom.getGeometryType().getSubType()); |
30 |
|
31 |
Polygon polygon = (Polygon) geom; |
32 |
int numVertices = polygon.getNumVertices();
|
33 |
boolean added = false; |
34 |
Point lastVertex = polygon.getVertex(numVertices - 1); |
35 |
if (index < 0 || index >= numVertices) { |
36 |
return geom;
|
37 |
} |
38 |
int indexToInsert = index;
|
39 |
if (index == 0 && polygon.getVertex(0).equals(lastVertex)) { |
40 |
indexToInsert = numVertices - 1;
|
41 |
} |
42 |
|
43 |
if (indexToInsert == 0) { |
44 |
Point point = polygon.getVertex(0); |
45 |
if (!point.equals(lastVertex)) {
|
46 |
result.addVertex(geometryManager.createPoint( |
47 |
(point.getX() + lastVertex.getX()) / 2,
|
48 |
(point.getY() + lastVertex.getY()) / 2, geom
|
49 |
.getGeometryType().getSubType())); |
50 |
added = true;
|
51 |
} |
52 |
} |
53 |
Point antPoint = null; |
54 |
for (int i = 0; i < numVertices; i++) { |
55 |
Point point = polygon.getVertex(i);
|
56 |
if (i == indexToInsert && !added) {
|
57 |
result.addVertex(geometryManager.createPoint( |
58 |
(point.getX() + antPoint.getX()) / 2,
|
59 |
(point.getY() + antPoint.getY()) / 2, geom
|
60 |
.getGeometryType().getSubType())); |
61 |
|
62 |
} |
63 |
result.addVertex(point); |
64 |
antPoint = point; |
65 |
} |
66 |
return result;
|
67 |
} |
68 |
|
69 |
public Geometry removeVertex(Primitive geom, int index) |
70 |
throws CreateGeometryException {
|
71 |
Polygon polygon = (Polygon) geom; |
72 |
int numVertices = polygon.getNumVertices();
|
73 |
if (index < 0 || index >= numVertices) { |
74 |
return geom;
|
75 |
} |
76 |
GeometryManager geometryManager = GeometryLocator.getGeometryManager(); |
77 |
Polygon result =
|
78 |
geometryManager.createPolygon(geom.getGeometryType().getSubType()); |
79 |
|
80 |
if (polygon.getVertex(0).equals(polygon.getVertex(numVertices - 1))) { |
81 |
if (numVertices <= 4) { |
82 |
return geom;
|
83 |
} |
84 |
numVertices--; |
85 |
} else {
|
86 |
if (numVertices <= 3) { |
87 |
return geom;
|
88 |
} |
89 |
} |
90 |
|
91 |
int j = 0; |
92 |
for (int i = 0; i < numVertices; i++) { |
93 |
Point point = polygon.getVertex(i);
|
94 |
if (i == index) {
|
95 |
continue;
|
96 |
} |
97 |
result.addVertex(point); |
98 |
j++; |
99 |
} |
100 |
if (!result.getVertex(0).equals(result.getVertex(j - 1))) { |
101 |
result.addVertex(result.getVertex(0));
|
102 |
} |
103 |
|
104 |
return result;
|
105 |
} |
106 |
|
107 |
public Geometry moveVertex(Primitive geom, int index, Point point) { |
108 |
Polygon result = (Polygon) geom.cloneGeometry(); |
109 |
int numVertices = result.getNumVertices();
|
110 |
|
111 |
if (result.getVertex(0).equals(result.getVertex(numVertices - 1))) { |
112 |
if (index == 0 || index == (result.getNumVertices() - 1)) { |
113 |
EditVertexOperationUtils.setVertex(result, 0, point);
|
114 |
EditVertexOperationUtils.setVertex(result, |
115 |
result.getNumVertices() - 1, (Point)point.cloneGeometry()); |
116 |
return result;
|
117 |
} |
118 |
} |
119 |
EditVertexOperationUtils.setVertex(result, index, point); |
120 |
return result;
|
121 |
} |
122 |
|
123 |
public List<Point> getVertexesList(Primitive geom) { |
124 |
ArrayList<Point> vertexList = new ArrayList<Point>(); |
125 |
if (geom instanceof OrientablePrimitive) { |
126 |
OrientablePrimitive orientable = (OrientablePrimitive) geom; |
127 |
orientable.getNumVertices(); |
128 |
for (int i = 0; i < orientable.getNumVertices(); i++) { |
129 |
vertexList.add(orientable.getVertex(i)); |
130 |
} |
131 |
} |
132 |
return vertexList;
|
133 |
} |
134 |
|
135 |
public int getNumVertex(Primitive geom) { |
136 |
if (geom instanceof OrientablePrimitive) { |
137 |
OrientablePrimitive orientable = (OrientablePrimitive) geom; |
138 |
return orientable.getNumVertices();
|
139 |
} |
140 |
return 0; |
141 |
} |
142 |
|
143 |
public boolean canInsertVertex(Primitive geom, int index) { |
144 |
return true; |
145 |
} |
146 |
|
147 |
public boolean canRemoveVertex(Primitive geom, int index) { |
148 |
|
149 |
Polygon polygon = (Polygon) geom; |
150 |
int numVertices = polygon.getNumVertices();
|
151 |
if (index < 0) { |
152 |
return false; |
153 |
} else if (index >= numVertices) { |
154 |
return false; |
155 |
} |
156 |
if (polygon.getVertex(0).equals(polygon.getVertex(numVertices - 1))) { |
157 |
if (numVertices <= 4) { |
158 |
return false; |
159 |
} |
160 |
} else {
|
161 |
if (numVertices <= 3) { |
162 |
return false; |
163 |
} |
164 |
} |
165 |
return true; |
166 |
} |
167 |
|
168 |
public boolean canMoveVertex(int index, Point point) { |
169 |
return true; |
170 |
} |
171 |
|
172 |
public void addToDrawingStatus(DefaultDrawingStatus drawingStatus, |
173 |
Primitive primitive) { |
174 |
EditingProviderManager editingProviderManager = |
175 |
EditingProviderLocator.getProviderManager(); |
176 |
ISymbol polygonSymbolEditing = |
177 |
editingProviderManager.getSymbol("polygon-symbol-editing");
|
178 |
drawingStatus.addStatus(primitive, polygonSymbolEditing, "");
|
179 |
} |
180 |
|
181 |
} |