001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2007, Geotools Project Managment Committee (PMC)
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: */
016: package org.geotools.styling;
017:
018: import java.io.ByteArrayInputStream;
019: import java.io.InputStream;
020: import java.net.URI;
021: import java.net.URISyntaxException;
022: import java.net.URL;
023: import java.util.HashMap;
024: import java.util.Map;
025:
026: import junit.framework.TestCase;
027:
028: import org.geotools.data.DataStore;
029: import org.geotools.data.memory.MemoryDataStore;
030: import org.geotools.data.memory.MemoryFeatureCollection;
031: import org.geotools.factory.CommonFactoryFinder;
032: import org.geotools.factory.GeoTools;
033: import org.geotools.factory.Hints;
034: import org.geotools.feature.AttributeType;
035: import org.geotools.feature.AttributeTypeFactory;
036: import org.geotools.feature.FeatureCollection;
037: import org.geotools.feature.FeatureType;
038: import org.geotools.feature.FeatureTypes;
039: import org.geotools.feature.GeometryAttributeType;
040: import org.geotools.feature.type.GeometricAttributeType;
041: import org.geotools.referencing.CRS;
042: import org.geotools.test.TestData;
043: import org.opengis.filter.PropertyIsLessThan;
044: import org.opengis.referencing.crs.CoordinateReferenceSystem;
045:
046: import com.vividsolutions.jts.geom.Coordinate;
047: import com.vividsolutions.jts.geom.GeometryFactory;
048: import com.vividsolutions.jts.geom.Point;
049: import com.vividsolutions.jts.geom.PrecisionModel;
050:
051: public class UserLayerTest extends TestCase {
052:
053: private static final String CRS_WKT = "GEOGCS[\"WGS 84\", "
054: + " DATUM[\"WGS_1984\","
055: + " SPHEROID[\"WGS 84\", 6378137.0, 298.257223563, AUTHORITY[\"EPSG\",\"7030\"]],"
056: + " AUTHORITY[\"EPSG\",\"6326\"]],"
057: + " PRIMEM[\"Greenwich\", 0.0, AUTHORITY[\"EPSG\",\"8901\"]],"
058: + " UNIT[\"degree\", 0.017453292519943295],"
059: + " AXIS[\"Lon\", EAST]," + " AXIS[\"Lat\", NORTH],"
060: + " AUTHORITY[\"EPSG\",\"4326\"]]";
061:
062: private static final int SRID = 4326;
063:
064: private static final String LAYER_NAME = "user-layer-1";
065:
066: private static final String ID_COLUMN = "id";
067:
068: private static final String GEOMETRY_COLUMN = "geometry";
069:
070: private static final String LABEL_COLUMN = "label";
071:
072: private static final String ID_1 = "point-1";
073:
074: private static final double X_1 = -180.0;
075:
076: private static final double Y_1 = -90.0;
077:
078: private static final String LABEL_1 = "point location #1";
079:
080: private static final String ID_2 = "point-2";
081:
082: private static final double X_2 = +180.0;
083:
084: private static final double Y_2 = +90.0;
085:
086: private static final String LABEL_2 = "point location #2";
087:
088: private static final double OPACITY = 0.75;
089:
090: private static final String IMAGE_EXT = "jpg";
091:
092: private static final String IMAGE_URL = "file:/somewhere/image."
093: + IMAGE_EXT;
094:
095: private static final String MY_NAMESPACE = "integeo";
096:
097: private static final URI MY_URI;
098:
099: private static final String MY_FEATURE = "myFeature";
100: static {
101: try {
102: MY_URI = new URI("http://geotools.org");
103: } catch (URISyntaxException x) {
104: throw new ExceptionInInitializerError(x);
105: }
106: }
107:
108: public void testUserLayerWithInlineFeatures() throws Exception {
109: // create the feature's schema ----------------------------------------
110: final CoordinateReferenceSystem crs = CRS.parseWKT(CRS_WKT);
111: final AttributeType id = AttributeTypeFactory.newAttributeType(
112: ID_COLUMN, Integer.class);
113: final GeometryAttributeType geom = new GeometricAttributeType(
114: GEOMETRY_COLUMN, Point.class, false, null, crs, null);
115: final AttributeType label = AttributeTypeFactory
116: .newAttributeType(LABEL_COLUMN, String.class);
117: final AttributeType[] attributes = new AttributeType[] { id,
118: geom, label };
119: final FeatureType schema = FeatureTypes.newFeatureType(
120: attributes, MY_FEATURE, MY_URI, false, null, geom);
121:
122: // create a feature collection ----------------------------------------
123: final FeatureCollection fc = new MemoryFeatureCollection(schema);
124:
125: // populate the collection --------------------------------------------
126: final PrecisionModel pm = new PrecisionModel(
127: PrecisionModel.FLOATING);
128: final GeometryFactory jtsFactory = new GeometryFactory(pm, SRID);
129:
130: // create 1st point
131: final Point g1 = jtsFactory
132: .createPoint(new Coordinate(X_1, Y_1));
133: fc.add(schema.create(
134: new Object[] { new Integer(1), g1, LABEL_1 }, ID_1));
135:
136: // create 2nd point
137: final Point g2 = jtsFactory
138: .createPoint(new Coordinate(X_2, Y_2));
139: fc.add(schema.create(
140: new Object[] { new Integer(2), g2, LABEL_2 }, ID_2));
141:
142: final DataStore ds = new MemoryDataStore(fc);
143:
144: // create and populate the layer --------------------------------------
145: final StyleFactory sf = CommonFactoryFinder
146: .getStyleFactory(GeoTools.getDefaultHints());
147:
148: final UserLayer layer = sf.createUserLayer();
149: layer.setName(LAYER_NAME);
150: layer.setInlineFeatureType(schema);
151: layer.setInlineFeatureDatastore(ds);
152:
153: // create a user style and add it to that layer -----------------------
154: final Style style = sf.createStyle();
155:
156: final StyleBuilder sb = new StyleBuilder(sf);
157: final ExternalGraphic overlay = sb.createExternalGraphic(
158: IMAGE_URL, "image/" + IMAGE_EXT);
159: final Graphic g = sb.createGraphic(overlay, null, null,
160: OPACITY, Double.NaN, 0.0);
161: final PointSymbolizer ps = sb.createPointSymbolizer(g);
162: final FeatureTypeStyle fts = sb.createFeatureTypeStyle(ps);
163: fts.setFeatureTypeName(MY_NAMESPACE + ":" + MY_FEATURE);
164:
165: style.addFeatureTypeStyle(fts);
166: layer.addUserStyle(style);
167:
168: // create an SLD and populate it with that styled layer ---------------
169: final StyledLayerDescriptor sld1 = sf
170: .createStyledLayerDescriptor();
171: sld1.addStyledLayer(layer);
172:
173: // marshal the SLD to XML ---------------------------------------------
174: final Map nsMap = new HashMap();
175: nsMap.put(MY_URI, MY_NAMESPACE);
176: final SLDTransformer sldTransformer = new SLDTransformer(nsMap);
177: sldTransformer.setIndentation(2);
178: String xml = sldTransformer.transform(sld1);
179:
180: // unmarshal it back to an SLD instance -------------------------------
181: final InputStream is = new ByteArrayInputStream(xml
182: .getBytes("UTF-8"));
183: final SLDParser parser = new SLDParser(sf);
184: parser.setInput(is);
185: final StyledLayerDescriptor sld2 = parser.parseSLD();
186: xml = sldTransformer.transform(sld2);
187:
188: // check both SLDs ----------------------------------------------------
189: final StyledLayer[] layers = sld2.getStyledLayers();
190: assertNotNull("Styled layers array MUST NOT be null", layers);
191: assertEquals("Styled layers array MUST be 1-element long", 1,
192: layers.length);
193: final StyledLayer sLayer = layers[0];
194: assertNotNull("Single styled layer MUST NOT be null", sLayer);
195: assertTrue("Single layer MUST be a UserLayer", UserLayer.class
196: .isAssignableFrom(sLayer.getClass()));
197: final UserLayer uLayer = (UserLayer) sLayer;
198: final String lName = uLayer.getName();
199: assertEquals("Read layer name MUST match", LAYER_NAME, lName);
200: final FeatureType ft = uLayer.getInlineFeatureType();
201: assertNotNull("Unmarshalled feature type MUST NOT be null", ft);
202: final String fName = ft.getTypeName();
203: assertEquals("Read feature type name MUST match", MY_FEATURE,
204: fName);
205: assertEquals(CRS.decode("EPSG:4326"), ft.getDefaultGeometry()
206: .getCoordinateSystem());
207: }
208:
209: public void testUserLayerWithRemoteOWS() throws Exception {
210: URL sldUrl = TestData.getResource(this , "remoteOws.sld");
211: StyleFactory factory = CommonFactoryFinder
212: .getStyleFactory(null);
213: SLDParser stylereader = new SLDParser(factory, sldUrl);
214: StyledLayerDescriptor sld = stylereader.parseSLD();
215: assertEquals(1, sld.getStyledLayers().length);
216: assertTrue(sld.getStyledLayers()[0] instanceof UserLayer);
217: UserLayer layer = (UserLayer) sld.getStyledLayers()[0];
218: assertEquals("LayerWithRemoteOWS", layer.getName());
219: assertNotNull(layer.getRemoteOWS());
220: assertEquals("WFS", layer.getRemoteOWS().getService());
221: assertEquals("http://sigma.openplans.org:8080/geoserver/wfs?",
222: layer.getRemoteOWS().getOnlineResource());
223: assertEquals(1, layer.getLayerFeatureConstraints().length);
224: FeatureTypeConstraint ftc = layer.getLayerFeatureConstraints()[0];
225: assertEquals("topp:states", ftc.getFeatureTypeName());
226: assertNotNull(ftc.getFilter());
227: assertTrue(ftc.getFilter() instanceof PropertyIsLessThan);
228: }
229: }
|