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