gvsig-vectorediting / org.gvsig.vectorediting / trunk / org.gvsig.vectorediting / org.gvsig.vectorediting.lib / org.gvsig.vectorediting.lib.prov / org.gvsig.vectorediting.lib.prov.split / src / main / java / org / gvsig / vectorediting / lib / prov / split / operation / CurveSplitOperation.java @ 2616
History | View | Annotate | Download (5.1 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 |
package org.gvsig.vectorediting.lib.prov.split.operation; |
26 |
|
27 |
import org.gvsig.fmap.geom.Geometry; |
28 |
import org.gvsig.fmap.geom.GeometryException; |
29 |
import org.gvsig.fmap.geom.GeometryLocator; |
30 |
import org.gvsig.fmap.geom.aggregate.MultiCurve; |
31 |
import org.gvsig.fmap.geom.aggregate.MultiPoint; |
32 |
import org.gvsig.fmap.geom.operation.GeometryOperationException; |
33 |
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException; |
34 |
import org.gvsig.fmap.geom.primitive.Curve; |
35 |
import org.gvsig.fmap.geom.primitive.Line; |
36 |
import org.gvsig.fmap.geom.primitive.Point; |
37 |
|
38 |
/**
|
39 |
* @author llmarques
|
40 |
*
|
41 |
*/
|
42 |
public class CurveSplitOperation implements SplitOperation { |
43 |
|
44 |
/*
|
45 |
* Strategy:
|
46 |
*
|
47 |
* 1. Get intersection points.
|
48 |
* 2. Get difference between geometry and splitter. If splitter does not
|
49 |
* touch geometry return original geometry.
|
50 |
* 3. If any intersection points are not equal to first or last vertex of
|
51 |
* original geometry, join last and first curve splitted. It's necessary do
|
52 |
* it because the difference between geometry and splitter returns a
|
53 |
* multicurve that its first curve starts at first vertex of ORGINAL and
|
54 |
* ends at first intersection point and its last curve starts at
|
55 |
* last intersection point and ends at last vertex of ORIGINAL geometry, so
|
56 |
* we have to join them. If any intersection points are equal to first or
|
57 |
* last vertex do nothing.
|
58 |
*/
|
59 |
@Override
|
60 |
public Geometry split(Geometry geometryToBeSplitted, Geometry splitter)
|
61 |
throws GeometryOperationNotSupportedException,
|
62 |
GeometryOperationException, GeometryException { |
63 |
|
64 |
Curve curveToBeSplitted = (Curve) geometryToBeSplitted.cloneGeometry(); |
65 |
|
66 |
Geometry intersection = geometryToBeSplitted.intersection(splitter); |
67 |
MultiCurve multicurveSplitted = null;
|
68 |
|
69 |
if (intersection instanceof MultiPoint) { |
70 |
MultiPoint intersectionMultiPoint = (MultiPoint) intersection; |
71 |
Point firstVertex = intersectionMultiPoint.getPointAt(0); |
72 |
Point lastVertex =
|
73 |
intersectionMultiPoint.getPointAt(intersectionMultiPoint |
74 |
.getPrimitivesNumber() - 1);
|
75 |
|
76 |
MultiCurve difference = |
77 |
(MultiCurve) geometryToBeSplitted.difference(splitter); |
78 |
|
79 |
if (difference == null) { |
80 |
return geometryToBeSplitted;
|
81 |
} |
82 |
if(!(curveToBeSplitted instanceof Line)){ |
83 |
curveToBeSplitted = (Curve) curveToBeSplitted.toLines().getPrimitiveAt(0);
|
84 |
} |
85 |
|
86 |
if ((!firstVertex.equals(curveToBeSplitted.getVertex(0)) && !lastVertex |
87 |
.equals(curveToBeSplitted.getVertex(0)))
|
88 |
&& isClosed(curveToBeSplitted)) { |
89 |
|
90 |
Curve firstCurve = difference.getCurveAt(0);
|
91 |
Curve lastCurve = |
92 |
difference.getCurveAt(difference.getPrimitivesNumber() - 1);
|
93 |
|
94 |
Curve union = |
95 |
GeometryLocator.getGeometryManager().createLine( |
96 |
geometryToBeSplitted.getGeometryType().getSubType()); |
97 |
for (int i = 0; i < lastCurve.getNumVertices(); i++) { |
98 |
union.addVertex(lastCurve.getVertex(i)); |
99 |
} |
100 |
|
101 |
for (int i = 0; i < firstCurve.getNumVertices(); i++) { |
102 |
union.addVertex(firstCurve.getVertex(i)); |
103 |
} |
104 |
|
105 |
multicurveSplitted = |
106 |
GeometryLocator.getGeometryManager().createMultiCurve( |
107 |
geometryToBeSplitted.getGeometryType().getSubType()); |
108 |
|
109 |
multicurveSplitted.addCurve(union); |
110 |
for (int i = 1; i < difference.getPrimitivesNumber() - 1; i++) { |
111 |
multicurveSplitted.addCurve(difference.getCurveAt(i)); |
112 |
} |
113 |
} |
114 |
} |
115 |
|
116 |
if(multicurveSplitted == null){ |
117 |
return geometryToBeSplitted.difference(splitter);
|
118 |
} |
119 |
|
120 |
return multicurveSplitted;
|
121 |
} |
122 |
|
123 |
private boolean isClosed(Curve curveToBeSplitted) { |
124 |
|
125 |
Point firstPoint = curveToBeSplitted.getVertex(0); |
126 |
Point lastPoint =
|
127 |
curveToBeSplitted.getVertex(curveToBeSplitted.getNumVertices()-1);
|
128 |
|
129 |
return firstPoint.equals(lastPoint);
|
130 |
} |
131 |
} |