svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.geometry / org.gvsig.fmap.geometry.operation / src / main / java / org / gvsig / fmap / geom / operation / tojts / Surface2DToJTSLineString.java @ 40767
History | View | Annotate | Download (3.63 KB)
1 | 40559 | jjdelcerro | /**
|
---|---|---|---|
2 | * gvSIG. Desktop Geographic Information System.
|
||
3 | *
|
||
4 | * Copyright (C) 2007-2013 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 3
|
||
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 | 40435 | jjdelcerro | |
25 | package org.gvsig.fmap.geom.operation.tojts; |
||
26 | |||
27 | import java.awt.geom.PathIterator; |
||
28 | import java.util.ArrayList; |
||
29 | |||
30 | import org.gvsig.fmap.geom.Geometry; |
||
31 | import org.gvsig.fmap.geom.operation.GeometryOperationContext; |
||
32 | import org.gvsig.fmap.geom.operation.GeometryOperationException; |
||
33 | |||
34 | import com.vividsolutions.jts.geom.Coordinate; |
||
35 | import com.vividsolutions.jts.geom.CoordinateArrays; |
||
36 | import com.vividsolutions.jts.geom.LinearRing; |
||
37 | import com.vividsolutions.jts.geom.MultiLineString; |
||
38 | |||
39 | /**
|
||
40 | * Converts a Surface2D into a MultiLineString object
|
||
41 | * @author <a href="mailto:nachobrodin@gmail.com">Nacho Brodin</a>
|
||
42 | */
|
||
43 | public class Surface2DToJTSLineString extends ToJTS { |
||
44 | public static final String NAME = "toJTSLineString"; |
||
45 | public static final int CODE = geomManager.getGeometryOperationCode(NAME); |
||
46 | |||
47 | /*
|
||
48 | * (non-Javadoc)
|
||
49 | * @see org.gvsig.fmap.geom.operation.tojts.ToJTS#invoke(org.gvsig.fmap.geom.Geometry, org.gvsig.fmap.geom.operation.GeometryOperationContext)
|
||
50 | */
|
||
51 | public Object invoke(Geometry geom, GeometryOperationContext ctx) throws GeometryOperationException { |
||
52 | int srid = -1; |
||
53 | if (ctx != null){ |
||
54 | srid = ((JTSGeometryOperationContext)ctx).getSrid(); |
||
55 | } |
||
56 | PathIterator theIterator = geom.getPathIterator(null, geomManager.getFlatness()); |
||
57 | int theType;
|
||
58 | double[] theData = new double[6]; |
||
59 | ArrayList arrayCoords = null; |
||
60 | Coordinate[] points = null; |
||
61 | ArrayList listRings = new ArrayList(); |
||
62 | theIterator = geom.getPathIterator(null, geomManager.getFlatness());
|
||
63 | |||
64 | while (!theIterator.isDone()) {
|
||
65 | theType = theIterator.currentSegment(theData); |
||
66 | |||
67 | switch (theType) {
|
||
68 | case PathIterator.SEG_MOVETO: |
||
69 | if (arrayCoords == null) { |
||
70 | arrayCoords = new ArrayList(); |
||
71 | } else
|
||
72 | arrayCoords.clear(); |
||
73 | arrayCoords.add(new Coordinate(theData[0], theData[1])); |
||
74 | break;
|
||
75 | |||
76 | case PathIterator.SEG_LINETO: |
||
77 | arrayCoords.add(new Coordinate(theData[0],theData[1])); |
||
78 | break;
|
||
79 | case PathIterator.SEG_QUADTO: |
||
80 | System.out.println("SEG_QUADTO Not supported here"); |
||
81 | break;
|
||
82 | case PathIterator.SEG_CUBICTO: |
||
83 | System.out.println("SEG_CUBICTO Not supported here"); |
||
84 | break;
|
||
85 | case PathIterator.SEG_CLOSE: |
||
86 | break;
|
||
87 | } |
||
88 | theIterator.next(); |
||
89 | } |
||
90 | |||
91 | Coordinate firstCoord = (Coordinate) arrayCoords.get(0);
|
||
92 | Coordinate lastCoord = (Coordinate) arrayCoords.get(arrayCoords.size() - 1);
|
||
93 | if (!isClosed(firstCoord, lastCoord)) {
|
||
94 | arrayCoords.add(firstCoord); |
||
95 | } |
||
96 | points = CoordinateArrays.toCoordinateArray(arrayCoords); |
||
97 | listRings.add(geomFactory.createLinearRing(points)); |
||
98 | |||
99 | LinearRing[] list = (LinearRing[])listRings.toArray(new LinearRing[listRings.size()]); |
||
100 | MultiLineString lineStr = new com.vividsolutions.jts.geom.GeometryFactory().createMultiLineString(list);
|
||
101 | lineStr.setSRID(srid); |
||
102 | return lineStr;
|
||
103 | } |
||
104 | } |