svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.geometry / org.gvsig.fmap.geometry.jts / src / main / java / org / gvsig / fmap / geom / jts / gml / GeometryStrategiesGML3.java @ 47646
History | View | Annotate | Download (9.49 KB)
1 |
/*
|
---|---|
2 |
* To change this license header, choose License Headers in Project Properties.
|
3 |
* To change this template file, choose Tools | Templates
|
4 |
* and open the template in the editor.
|
5 |
*/
|
6 |
package org.gvsig.fmap.geom.jts.gml; |
7 |
|
8 |
import com.vividsolutions.jts.geom.Coordinate; |
9 |
import com.vividsolutions.jts.geom.CoordinateSequence; |
10 |
import com.vividsolutions.jts.geom.GeometryFactory; |
11 |
import com.vividsolutions.jts.geom.LineString; |
12 |
import com.vividsolutions.jts.geom.LinearRing; |
13 |
import com.vividsolutions.jts.geom.MultiLineString; |
14 |
import com.vividsolutions.jts.geom.MultiPolygon; |
15 |
import com.vividsolutions.jts.geom.Polygon; |
16 |
import com.vividsolutions.jts.io.gml2.GMLConstants; |
17 |
import com.vividsolutions.jts.operation.linemerge.LineMerger; |
18 |
import java.util.HashMap; |
19 |
import java.util.List; |
20 |
import org.apache.commons.collections4.CollectionUtils; |
21 |
import org.apache.commons.lang3.StringUtils; |
22 |
import org.apache.commons.lang3.tuple.ImmutablePair; |
23 |
import org.apache.commons.lang3.tuple.Pair; |
24 |
import static org.gvsig.fmap.geom.jts.gml.GeometryStrategies.getSrid; |
25 |
import org.xml.sax.SAXException; |
26 |
|
27 |
/**
|
28 |
*
|
29 |
* @author fdiaz
|
30 |
*/
|
31 |
public class GeometryStrategiesGML3 { |
32 |
|
33 |
@SuppressWarnings("Convert2Lambda") |
34 |
public static HashMap loadStrategies(HashMap strats) { |
35 |
|
36 |
GeometryStrategies.ParseStrategy oneChild = new GeometryStrategies.ParseStrategy() {
|
37 |
|
38 |
@Override
|
39 |
public Object parse(GMLHandler.Handler arg, GeometryFactory gf) throws SAXException { |
40 |
if (arg.children.size() != 1) { |
41 |
throw new SAXException("Geometry Members may only contain one geometry."); |
42 |
} |
43 |
|
44 |
// type checking will occur in the parent geom collection.
|
45 |
// may wish to add this in the future
|
46 |
return arg.children.get(0); |
47 |
} |
48 |
}; |
49 |
|
50 |
GeometryStrategies.ParseStrategy manyChilds = new GeometryStrategies.ParseStrategy() {
|
51 |
|
52 |
@Override
|
53 |
public Object parse(GMLHandler.Handler arg, GeometryFactory gf) throws SAXException { |
54 |
return arg.children;
|
55 |
} |
56 |
}; |
57 |
|
58 |
strats.put("pos".toLowerCase(), new GeometryStrategies.ParseStrategy() { |
59 |
|
60 |
@Override
|
61 |
public Object parse(GMLHandler.Handler arg, GeometryFactory gf) throws SAXException { |
62 |
String[] ss = StringUtils.split(arg.text.toString(), ' '); |
63 |
Coordinate c = new Coordinate();
|
64 |
c.x = Double.parseDouble(ss[0]); |
65 |
if (ss.length > 1) { |
66 |
c.y = Double.parseDouble(ss[1]); |
67 |
} |
68 |
if (ss.length > 3) { |
69 |
c.z = Double.parseDouble(ss[2]); |
70 |
} |
71 |
return c;
|
72 |
} |
73 |
}); |
74 |
|
75 |
strats.put("posList".toLowerCase(), new GeometryStrategies.ParseStrategy() { |
76 |
|
77 |
@Override
|
78 |
public Object parse(GMLHandler.Handler arg, GeometryFactory gf) throws SAXException { |
79 |
if (arg.text == null || "".equals(arg.text)) { |
80 |
throw new SAXException("Cannot create a coordinate sequence without text to parse"); |
81 |
} |
82 |
|
83 |
String coordSeperator = " "; |
84 |
int srsDimension = 2; |
85 |
|
86 |
if (arg.attrs.getIndex("cs") >= 0) { |
87 |
coordSeperator = arg.attrs.getValue("cs");
|
88 |
} else if (arg.attrs.getIndex(GMLConstants.GML_NAMESPACE, "cs") >= 0) { |
89 |
coordSeperator = arg.attrs.getValue(GMLConstants.GML_NAMESPACE, "cs");
|
90 |
} |
91 |
|
92 |
if (arg.attrs.getIndex("srsDimension") >= 0) { |
93 |
srsDimension = Integer.parseInt(arg.attrs.getValue("srsDimension")); |
94 |
} else if (arg.attrs.getIndex(GMLConstants.GML_NAMESPACE, "srsDimension") >= 0) { |
95 |
srsDimension = Integer.parseInt(arg.attrs.getValue(GMLConstants.GML_NAMESPACE, "srsDimension")); |
96 |
} |
97 |
|
98 |
// now to start parse
|
99 |
String t = arg.text.toString();
|
100 |
t = t.replaceAll("\\s", " "); |
101 |
String[] ss = StringUtils.split(t,coordSeperator); |
102 |
int len = ss.length/srsDimension;
|
103 |
CoordinateSequence cs = gf.getCoordinateSequenceFactory().create(len, srsDimension); |
104 |
for (int i = 0; i < len; i++) { |
105 |
cs.setOrdinate(i, 0, Double.parseDouble(ss[i*2])); |
106 |
if(srsDimension > 1) { |
107 |
cs.setOrdinate(i, 1, Double.parseDouble(ss[(i*2)+1])); |
108 |
} |
109 |
if(srsDimension > 2) { |
110 |
cs.setOrdinate(i, 2, Double.parseDouble(ss[(i*2)+2])); |
111 |
} |
112 |
} |
113 |
|
114 |
return cs;
|
115 |
} |
116 |
}); |
117 |
|
118 |
strats.put("PolygonPatch".toLowerCase(), new GeometryStrategies.ParseStrategy() { |
119 |
|
120 |
@Override
|
121 |
public Object parse(GMLHandler.Handler arg, GeometryFactory gf) throws SAXException { |
122 |
Object exterior = arg.children.get(0); |
123 |
Object interior = null; |
124 |
if(arg.children.size() > 1){ //FIXME: Ver si se est? cogiendo solo un agujero |
125 |
interior = arg.children.get(1);
|
126 |
} |
127 |
return new ImmutablePair(exterior, interior); |
128 |
} |
129 |
}); |
130 |
|
131 |
strats.put("patches".toLowerCase(), new GeometryStrategies.ParseStrategy() { |
132 |
|
133 |
@Override
|
134 |
public Object parse(GMLHandler.Handler arg, GeometryFactory gf) throws SAXException { |
135 |
return arg.children;
|
136 |
} |
137 |
}); |
138 |
|
139 |
strats.put("Surface".toLowerCase(), new GeometryStrategies.ParseStrategy() { |
140 |
|
141 |
@Override
|
142 |
public Object parse(GMLHandler.Handler arg, GeometryFactory gf) throws SAXException { |
143 |
|
144 |
List<Pair<LinearRing,List<LinearRing>>> patches = (List<Pair<LinearRing,List<LinearRing>>>) arg.children.get(0); |
145 |
LinearRing shell = patches.get(0).getLeft();
|
146 |
List<LinearRing> holes = patches.get(0).getRight(); |
147 |
Polygon polygon;
|
148 |
if(holes == null){ |
149 |
polygon = gf.createPolygon(shell); |
150 |
} else {
|
151 |
polygon = gf.createPolygon(shell, holes.toArray(new LinearRing[holes.size()]));
|
152 |
} |
153 |
int srid = getSrid(arg.attrs,gf.getSRID());
|
154 |
if (polygon.getSRID() != srid) {
|
155 |
polygon.setSRID(srid); |
156 |
} |
157 |
|
158 |
return polygon;
|
159 |
} |
160 |
}); |
161 |
|
162 |
strats.put("exterior".toLowerCase(), oneChild);
|
163 |
strats.put("surfaceMember".toLowerCase(), oneChild);
|
164 |
|
165 |
strats.put("interior".toLowerCase(), manyChilds);
|
166 |
|
167 |
strats.put("MultiSurface".toLowerCase(), new GeometryStrategies.ParseStrategy() { |
168 |
|
169 |
@Override
|
170 |
public Object parse(GMLHandler.Handler arg, GeometryFactory gf) throws SAXException { |
171 |
|
172 |
List<Polygon> polygons = arg.children; |
173 |
MultiPolygon multiPolygon = gf.createMultiPolygon(polygons.toArray(new Polygon[polygons.size()])); |
174 |
int srid = getSrid(arg.attrs,gf.getSRID());
|
175 |
if (multiPolygon.getSRID() != srid) {
|
176 |
multiPolygon.setSRID(srid); |
177 |
} |
178 |
return multiPolygon;
|
179 |
|
180 |
} |
181 |
}); |
182 |
|
183 |
strats.put("curveMember".toLowerCase(), oneChild);
|
184 |
|
185 |
strats.put("MultiCurve".toLowerCase(), new GeometryStrategies.ParseStrategy() { |
186 |
|
187 |
@Override
|
188 |
public Object parse(GMLHandler.Handler arg, GeometryFactory gf) throws SAXException { |
189 |
|
190 |
List<LineString> curves = arg.children;
|
191 |
MultiLineString multicurve = gf.createMultiLineString(curves.toArray(new LineString[curves.size()]));
|
192 |
int srid = getSrid(arg.attrs,gf.getSRID());
|
193 |
if (multicurve.getSRID() != srid) {
|
194 |
multicurve.setSRID(srid); |
195 |
} |
196 |
return multicurve;
|
197 |
|
198 |
} |
199 |
}); |
200 |
|
201 |
|
202 |
strats.put("segments".toLowerCase(), manyChilds);
|
203 |
strats.put("LineStringSegment".toLowerCase(), oneChild);
|
204 |
|
205 |
strats.put("Curve".toLowerCase(), new GeometryStrategies.ParseStrategy() { |
206 |
|
207 |
@Override
|
208 |
public Object parse(GMLHandler.Handler arg, GeometryFactory gf) throws SAXException { |
209 |
|
210 |
//Curve/segments/LineStringSegment/posList
|
211 |
|
212 |
LineString curve = null;
|
213 |
List<CoordinateSequence> segments = (List<CoordinateSequence>) arg.children.get(0); |
214 |
if (CollectionUtils.isNotEmpty(segments)) {
|
215 |
CoordinateSequence segment = segments.get(0);
|
216 |
if (segment != null) { |
217 |
curve = gf.createLineString(segment); |
218 |
} |
219 |
} |
220 |
return curve;
|
221 |
} |
222 |
}); |
223 |
|
224 |
strats.put("Ring".toLowerCase(), new GeometryStrategies.ParseStrategy() { |
225 |
|
226 |
@Override
|
227 |
public Object parse(GMLHandler.Handler arg, GeometryFactory gf) throws SAXException { |
228 |
|
229 |
//Ring/curveMember/LineString/posList
|
230 |
|
231 |
LineMerger merger = new LineMerger();
|
232 |
List<LineString> segments = (List<LineString>) arg.children;//.get(0); |
233 |
for (LineString segment : segments) {
|
234 |
merger.add(segment); |
235 |
} |
236 |
List<LineString> merged = (List<LineString>) merger.getMergedLineStrings(); |
237 |
return gf.createLinearRing(merged.get(0).getCoordinateSequence()); |
238 |
} |
239 |
}); |
240 |
|
241 |
return strats;
|
242 |
|
243 |
} |
244 |
|
245 |
} |