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 @ 47635
History | View | Annotate | Download (7.62 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 java.util.HashMap; |
18 |
import java.util.List; |
19 |
import org.apache.commons.lang3.StringUtils; |
20 |
import org.apache.commons.lang3.tuple.ImmutablePair; |
21 |
import org.apache.commons.lang3.tuple.Pair; |
22 |
import org.gvsig.fmap.geom.aggregate.MultiLine; |
23 |
import static org.gvsig.fmap.geom.jts.gml.GeometryStrategies.getSrid; |
24 |
import org.xml.sax.SAXException; |
25 |
|
26 |
/**
|
27 |
*
|
28 |
* @author fdiaz
|
29 |
*/
|
30 |
public class GeometryStrategiesGML3 { |
31 |
|
32 |
@SuppressWarnings("Convert2Lambda") |
33 |
public static HashMap loadStrategies(HashMap strats) { |
34 |
|
35 |
GeometryStrategies.ParseStrategy oneChild = new GeometryStrategies.ParseStrategy() {
|
36 |
|
37 |
public Object parse(GMLHandler.Handler arg, GeometryFactory gf) throws SAXException { |
38 |
if (arg.children.size() != 1) { |
39 |
throw new SAXException("Geometry Members may only contain one geometry."); |
40 |
} |
41 |
|
42 |
// type checking will occur in the parent geom collection.
|
43 |
// may wish to add this in the future
|
44 |
return arg.children.get(0); |
45 |
} |
46 |
}; |
47 |
|
48 |
GeometryStrategies.ParseStrategy manyChilds = new GeometryStrategies.ParseStrategy() {
|
49 |
|
50 |
public Object parse(GMLHandler.Handler arg, GeometryFactory gf) throws SAXException { |
51 |
return arg.children;
|
52 |
} |
53 |
}; |
54 |
|
55 |
strats.put("pos".toLowerCase(), new GeometryStrategies.ParseStrategy() { |
56 |
|
57 |
@Override
|
58 |
public Object parse(GMLHandler.Handler arg, GeometryFactory gf) throws SAXException { |
59 |
String[] ss = StringUtils.split(arg.text.toString(), ' '); |
60 |
Coordinate c = new Coordinate();
|
61 |
c.x = Double.parseDouble(ss[0]); |
62 |
if (ss.length > 1) { |
63 |
c.y = Double.parseDouble(ss[1]); |
64 |
} |
65 |
if (ss.length > 3) { |
66 |
c.z = Double.parseDouble(ss[2]); |
67 |
} |
68 |
return c;
|
69 |
} |
70 |
}); |
71 |
|
72 |
strats.put("posList".toLowerCase(), new GeometryStrategies.ParseStrategy() { |
73 |
|
74 |
public Object parse(GMLHandler.Handler arg, GeometryFactory gf) throws SAXException { |
75 |
if (arg.text == null || "".equals(arg.text)) { |
76 |
throw new SAXException("Cannot create a coordinate sequence without text to parse"); |
77 |
} |
78 |
|
79 |
String coordSeperator = " "; |
80 |
int srsDimension = 2; |
81 |
|
82 |
if (arg.attrs.getIndex("cs") >= 0) { |
83 |
coordSeperator = arg.attrs.getValue("cs");
|
84 |
} else if (arg.attrs.getIndex(GMLConstants.GML_NAMESPACE, "cs") >= 0) { |
85 |
coordSeperator = arg.attrs.getValue(GMLConstants.GML_NAMESPACE, "cs");
|
86 |
} |
87 |
|
88 |
if (arg.attrs.getIndex("srsDimension") >= 0) { |
89 |
srsDimension = Integer.parseInt(arg.attrs.getValue("srsDimension")); |
90 |
} else if (arg.attrs.getIndex(GMLConstants.GML_NAMESPACE, "srsDimension") >= 0) { |
91 |
srsDimension = Integer.parseInt(arg.attrs.getValue(GMLConstants.GML_NAMESPACE, "srsDimension")); |
92 |
} |
93 |
|
94 |
// now to start parse
|
95 |
String t = arg.text.toString();
|
96 |
t = t.replaceAll("\\s", " "); |
97 |
String[] ss = StringUtils.split(t,coordSeperator); |
98 |
int len = ss.length/srsDimension;
|
99 |
CoordinateSequence cs = gf.getCoordinateSequenceFactory().create(len, srsDimension); |
100 |
for (int i = 0; i < len; i++) { |
101 |
cs.setOrdinate(i, 0, Double.parseDouble(ss[i*2])); |
102 |
if(srsDimension > 1) { |
103 |
cs.setOrdinate(i, 1, Double.parseDouble(ss[(i*2)+1])); |
104 |
} |
105 |
if(srsDimension > 2) { |
106 |
cs.setOrdinate(i, 2, Double.parseDouble(ss[(i*2)+2])); |
107 |
} |
108 |
} |
109 |
|
110 |
return cs;
|
111 |
} |
112 |
}); |
113 |
|
114 |
strats.put("PolygonPatch".toLowerCase(), new GeometryStrategies.ParseStrategy() { |
115 |
|
116 |
public Object parse(GMLHandler.Handler arg, GeometryFactory gf) throws SAXException { |
117 |
Object exterior = arg.children.get(0); |
118 |
Object interior = null; |
119 |
if(arg.children.size() > 1){ |
120 |
interior = arg.children.get(1);
|
121 |
} |
122 |
return new ImmutablePair(exterior, interior); |
123 |
} |
124 |
}); |
125 |
|
126 |
strats.put("patches".toLowerCase(), new GeometryStrategies.ParseStrategy() { |
127 |
|
128 |
public Object parse(GMLHandler.Handler arg, GeometryFactory gf) throws SAXException { |
129 |
return arg.children;
|
130 |
} |
131 |
}); |
132 |
|
133 |
strats.put("Surface".toLowerCase(), new GeometryStrategies.ParseStrategy() { |
134 |
|
135 |
public Object parse(GMLHandler.Handler arg, GeometryFactory gf) throws SAXException { |
136 |
|
137 |
List<Pair<LinearRing,List<LinearRing>>> patches = (List<Pair<LinearRing,List<LinearRing>>>) arg.children.get(0); |
138 |
LinearRing shell = patches.get(0).getLeft();
|
139 |
List<LinearRing> holes = patches.get(0).getRight(); |
140 |
Polygon polygon;
|
141 |
if(holes == null){ |
142 |
polygon = gf.createPolygon(shell); |
143 |
} else {
|
144 |
polygon = gf.createPolygon(shell, holes.toArray(new LinearRing[holes.size()]));
|
145 |
} |
146 |
int srid = getSrid(arg.attrs,gf.getSRID());
|
147 |
if (polygon.getSRID() != srid) {
|
148 |
polygon.setSRID(srid); |
149 |
} |
150 |
|
151 |
return polygon;
|
152 |
} |
153 |
}); |
154 |
|
155 |
strats.put("exterior".toLowerCase(), oneChild);
|
156 |
strats.put("surfaceMember".toLowerCase(), oneChild);
|
157 |
|
158 |
strats.put("interior".toLowerCase(), manyChilds);
|
159 |
|
160 |
strats.put("MultiSurface".toLowerCase(), new GeometryStrategies.ParseStrategy() { |
161 |
|
162 |
public Object parse(GMLHandler.Handler arg, GeometryFactory gf) throws SAXException { |
163 |
|
164 |
List<Polygon> polygons = arg.children; |
165 |
MultiPolygon multiPolygon = gf.createMultiPolygon(polygons.toArray(new Polygon[polygons.size()])); |
166 |
int srid = getSrid(arg.attrs,gf.getSRID());
|
167 |
if (multiPolygon.getSRID() != srid) {
|
168 |
multiPolygon.setSRID(srid); |
169 |
} |
170 |
return multiPolygon;
|
171 |
|
172 |
} |
173 |
}); |
174 |
|
175 |
strats.put("curveMember".toLowerCase(), oneChild);
|
176 |
|
177 |
strats.put("MultiCurve".toLowerCase(), new GeometryStrategies.ParseStrategy() { |
178 |
|
179 |
public Object parse(GMLHandler.Handler arg, GeometryFactory gf) throws SAXException { |
180 |
|
181 |
List<LineString> curves = arg.children;
|
182 |
MultiLineString multicurve = gf.createMultiLineString(curves.toArray(new LineString[curves.size()]));
|
183 |
int srid = getSrid(arg.attrs,gf.getSRID());
|
184 |
if (multicurve.getSRID() != srid) {
|
185 |
multicurve.setSRID(srid); |
186 |
} |
187 |
return multicurve;
|
188 |
|
189 |
} |
190 |
}); |
191 |
|
192 |
|
193 |
return strats;
|
194 |
|
195 |
} |
196 |
|
197 |
} |