gvsig-geoprocess / org.gvsig.sextante / trunk / org.gvsig.sextante.app / org.gvsig.sextante.app.algorithm / org.gvsig.sextante.app.algorithm.base / src / main / java / org / gvsig / sextante / app / algorithm / base / util / GeometryUtil.java @ 37
History | View | Annotate | Download (5 KB)
1 |
/*
|
---|---|
2 |
* gvSIG. Sistema de Informaci�n Geogr�fica de la Generalitat Valenciana
|
3 |
*
|
4 |
* Copyright (C) 2010 Generalitat Valenciana.
|
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
19 |
*/
|
20 |
package org.gvsig.sextante.app.algorithm.base.util; |
21 |
|
22 |
import java.util.ArrayList; |
23 |
import java.util.List; |
24 |
|
25 |
import org.gvsig.fmap.geom.GeometryLocator; |
26 |
import org.gvsig.fmap.geom.operation.GeometryOperationContext; |
27 |
import org.gvsig.fmap.geom.operation.GeometryOperationException; |
28 |
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException; |
29 |
import org.gvsig.fmap.geom.operation.fromjts.FromJTS; |
30 |
import org.gvsig.fmap.geom.operation.tojts.ToJTS; |
31 |
|
32 |
import com.vividsolutions.jts.geom.Coordinate; |
33 |
import com.vividsolutions.jts.geom.Geometry; |
34 |
import com.vividsolutions.jts.geom.GeometryFactory; |
35 |
import com.vividsolutions.jts.geom.LineString; |
36 |
import com.vividsolutions.jts.geom.LinearRing; |
37 |
import com.vividsolutions.jts.geom.MultiPolygon; |
38 |
import com.vividsolutions.jts.geom.Polygon; |
39 |
|
40 |
import es.unex.sextante.core.Sextante; |
41 |
|
42 |
public class GeometryUtil { |
43 |
|
44 |
private static GeometryFactory fact = new GeometryFactory(); |
45 |
|
46 |
/**
|
47 |
* Gets a JTS geometry from a DAL geometry
|
48 |
* @param g
|
49 |
* @return
|
50 |
*/
|
51 |
public static Geometry geomToJTS(org.gvsig.fmap.geom.Geometry g) { |
52 |
try {
|
53 |
return (Geometry)g.invokeOperation(ToJTS.CODE, null); |
54 |
} catch (GeometryOperationNotSupportedException e1) {
|
55 |
Sextante.addErrorToLog(e1); |
56 |
} catch (GeometryOperationException e1) {
|
57 |
Sextante.addErrorToLog(e1); |
58 |
} |
59 |
return null; |
60 |
} |
61 |
|
62 |
/**
|
63 |
* Gets a DAL geometry from a JTS geometry
|
64 |
* @param g
|
65 |
* @return
|
66 |
*/
|
67 |
public static org.gvsig.fmap.geom.Geometry jtsToGeom(Geometry g) { |
68 |
GeometryOperationContext ctx = new GeometryOperationContext();
|
69 |
ctx.setAttribute(FromJTS.PARAM, g); |
70 |
try {
|
71 |
return (org.gvsig.fmap.geom.Geometry)GeometryLocator.getGeometryManager().invokeOperation(FromJTS.NAME, ctx);
|
72 |
} catch (GeometryOperationNotSupportedException e) {
|
73 |
Sextante.addErrorToLog(e); |
74 |
return null; |
75 |
} catch (GeometryOperationException e) {
|
76 |
Sextante.addErrorToLog(e); |
77 |
return null; |
78 |
} |
79 |
} |
80 |
|
81 |
/**
|
82 |
* Joins the list of geometries
|
83 |
* @param geometries
|
84 |
* @param geometryType
|
85 |
* @return
|
86 |
*/
|
87 |
@SuppressWarnings("unchecked") |
88 |
public static Geometry geometryUnion(List geometries, int geometryType){ |
89 |
Geometry[] geom = new Geometry[geometries.size()]; |
90 |
geometries.toArray(geom); |
91 |
Geometry union = JTSFacade.union(geom, geometryType); |
92 |
return union;
|
93 |
} |
94 |
|
95 |
public static Polygon removeDuplicates(Polygon polygon){ |
96 |
LineString shell = polygon.getExteriorRing(); |
97 |
LineString newShell = removeDuplicates(shell); |
98 |
LinearRing[] holes = new LinearRing[polygon.getNumInteriorRing()]; |
99 |
for(int i = 0; i < holes.length; i++){ |
100 |
holes[i] = (LinearRing) polygon.getInteriorRingN(i); |
101 |
} |
102 |
Polygon newPolygon = fact.createPolygon((LinearRing) newShell, holes);
|
103 |
return newPolygon;
|
104 |
} |
105 |
|
106 |
|
107 |
public static MultiPolygon removeDuplicates(MultiPolygon multiPolygon){ |
108 |
Polygon[] pols = new Polygon[multiPolygon.getNumGeometries()]; |
109 |
for(int i = 0; i < pols.length; i++){ |
110 |
Polygon pol = (Polygon) multiPolygon.getGeometryN(i); |
111 |
Polygon newPol = removeDuplicates(pol);
|
112 |
pols[i] = newPol; |
113 |
} |
114 |
return fact.createMultiPolygon(pols);
|
115 |
} |
116 |
|
117 |
public static Geometry removeDuplicatesFrom(Geometry geometry){ |
118 |
Geometry solution = null;
|
119 |
if(geometry instanceof LineString) |
120 |
solution = removeDuplicates((LineString)geometry); |
121 |
else if(geometry instanceof Polygon) |
122 |
solution = removeDuplicates((Polygon) geometry);
|
123 |
else if(geometry instanceof MultiPolygon) |
124 |
solution = removeDuplicates((MultiPolygon) geometry); |
125 |
else
|
126 |
solution = geometry; |
127 |
return solution;
|
128 |
} |
129 |
|
130 |
@SuppressWarnings("unchecked") |
131 |
public static LineString removeDuplicates(LineString line){ |
132 |
ArrayList coordinates = new ArrayList(); |
133 |
Coordinate prevCoord = null;
|
134 |
Coordinate actualCoord = null;
|
135 |
int numPoints = line.getNumPoints();
|
136 |
for(int i = 0; i < numPoints; i++){ |
137 |
actualCoord = line.getCoordinateN(i); |
138 |
if(prevCoord != null){ |
139 |
if(!prevCoord.equals2D(actualCoord)){
|
140 |
coordinates.add(actualCoord); |
141 |
} |
142 |
}else{
|
143 |
coordinates.add(actualCoord); |
144 |
} |
145 |
prevCoord = actualCoord; |
146 |
}//for
|
147 |
//llegados a este punto, construimos la nueva geometria
|
148 |
Coordinate[] newCoord = new Coordinate[coordinates.size()]; |
149 |
coordinates.toArray(newCoord); |
150 |
if(line instanceof LineString){ |
151 |
return fact.createLineString(newCoord);
|
152 |
}else{
|
153 |
return fact.createLinearRing(newCoord);
|
154 |
} |
155 |
|
156 |
} |
157 |
|
158 |
} |
159 |
|