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