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