01: /* Copyright (c) 2001 - 2007 TOPP - www.openplans.org. All rights reserved.
02: * This code is licensed under the GPL 2.0 license, availible at the root
03: * application directory.
04: */
05: package org.geoserver.wfs.xml.v1_0_0;
06:
07: import com.vividsolutions.jts.geom.Coordinate;
08: import com.vividsolutions.jts.geom.Envelope;
09: import com.vividsolutions.jts.geom.GeometryFactory;
10: import com.vividsolutions.jts.geom.MultiPolygon;
11: import com.vividsolutions.jts.geom.Polygon;
12: import org.geotools.feature.AttributeType;
13: import org.geotools.feature.FeatureType;
14: import org.geotools.gml2.FeatureTypeCache;
15: import org.geotools.xml.BindingWalkerFactory;
16: import org.geotools.xml.ElementInstance;
17: import org.geotools.xml.Node;
18: import org.vfny.geoserver.global.Data;
19: import org.vfny.geoserver.global.FeatureTypeInfo;
20:
21: public final class GMLAbstractFeatureTypeBinding extends
22: org.geotools.gml2.bindings.GMLAbstractFeatureTypeBinding {
23: GeometryFactory geometryFactory;
24: Data catalog;
25:
26: public GMLAbstractFeatureTypeBinding(
27: FeatureTypeCache featureTypeCache,
28: BindingWalkerFactory bwFactory,
29: GeometryFactory geometryFactory, Data catalog) {
30: super (featureTypeCache, bwFactory);
31: this .geometryFactory = geometryFactory;
32: this .catalog = catalog;
33: }
34:
35: public Object parse(ElementInstance instance, Node node,
36: Object value) throws Exception {
37: //pre process parsee tree to make sure types match up
38: FeatureTypeInfo meta = catalog.getFeatureTypeInfo(instance
39: .getName(), instance.getNamespace());
40:
41: if (meta != null) {
42: FeatureType featureType = meta.getFeatureType();
43:
44: //go through each attribute, performing various hacks to make make sure things
45: // cocher
46: for (int i = 0; i < featureType.getAttributeCount(); i++) {
47: AttributeType attributeType = featureType
48: .getAttributeType(i);
49: String name = attributeType.getName();
50: Class type = attributeType.getType();
51:
52: if ("boundedBy".equals(name)) {
53: Node boundedByNode = node.getChild("boundedBy");
54:
55: //hack 1: if boundedBy is in the parse tree has a bounding box and the attribute
56: // needs a polygon, convert
57: if (boundedByNode.getValue() instanceof Envelope) {
58: Envelope bounds = (Envelope) boundedByNode
59: .getValue();
60:
61: if (type.isAssignableFrom(Polygon.class)) {
62: Polygon polygon = polygon(bounds);
63: boundedByNode.setValue(polygon);
64: } else if (type
65: .isAssignableFrom(MultiPolygon.class)) {
66: MultiPolygon multiPolygon = geometryFactory
67: .createMultiPolygon(new Polygon[] { polygon(bounds) });
68: boundedByNode.setValue(multiPolygon);
69: }
70: }
71: }
72: }
73: }
74:
75: return super .parse(instance, node, value);
76: }
77:
78: Polygon polygon(Envelope bounds) {
79: return geometryFactory.createPolygon(geometryFactory
80: .createLinearRing(new Coordinate[] {
81: new Coordinate(bounds.getMinX(), bounds
82: .getMinY()),
83: new Coordinate(bounds.getMinX(), bounds
84: .getMaxY()),
85: new Coordinate(bounds.getMaxX(), bounds
86: .getMaxY()),
87: new Coordinate(bounds.getMaxX(), bounds
88: .getMinY()),
89: new Coordinate(bounds.getMinX(), bounds
90: .getMinY()) }), null);
91: }
92: }
|