001: /* Copyright (c) 2001 - 2007 TOPP - www.openplans.org. All rights reserved.
002: * This code is licensed under the GPL 2.0 license, availible at the root
003: * application directory.
004: */
005: package org.vfny.geoserver.wms.responses.featureInfo;
006:
007: import java.io.OutputStream;
008: import java.io.OutputStreamWriter;
009: import java.io.PrintWriter;
010: import java.nio.charset.Charset;
011: import java.util.Collections;
012: import java.util.HashMap;
013: import java.util.logging.Level;
014:
015: import org.geotools.feature.AttributeType;
016: import org.geotools.feature.Feature;
017: import org.geotools.feature.FeatureCollection;
018: import org.geotools.feature.FeatureIterator;
019: import org.geotools.feature.FeatureType;
020:
021: import com.vividsolutions.jts.geom.Geometry;
022:
023: /**
024: * Generates a FeatureInfoResponse of type text. This simply reports the
025: * attributes of the feature requested as a text string. This class just
026: * performs the writeTo, the GetFeatureInfoDelegate and abstract feature info
027: * class handle the rest.
028: *
029: * @author James Macgill, PSU
030: * @version $Id: TextFeatureInfoResponse.java,v 1.3 2004/07/19 22:31:40 jmacgill
031: * Exp $
032: */
033: public class TextFeatureInfoResponse extends
034: AbstractFeatureInfoResponse {
035: /**
036: *
037: */
038: public TextFeatureInfoResponse() {
039: format = "text/plain";
040: supportedFormats = Collections.singletonList("text/plain");
041: }
042:
043: /**
044: * Returns any extra headers that this service might want to set in the HTTP
045: * response object.
046: *
047: * @see org.vfny.geoserver.Response#getResponseHeaders()
048: */
049: public HashMap getResponseHeaders() {
050: return new HashMap();
051: }
052:
053: /**
054: * Writes the feature information to the client in text/plain format.
055: *
056: * @param out
057: * The output stream to write to.
058: *
059: * @throws org.vfny.geoserver.ServiceException
060: * DOCUMENT ME!
061: * @throws java.io.IOException
062: * DOCUMENT ME!
063: */
064: public void writeTo(OutputStream out)
065: throws org.vfny.geoserver.ServiceException,
066: java.io.IOException {
067: Charset charSet = getRequest().getGeoServer().getCharSet();
068: OutputStreamWriter osw = new OutputStreamWriter(out, charSet);
069:
070: // getRequest().getGeoServer().getCharSet());
071: PrintWriter writer = new PrintWriter(osw);
072:
073: // DJB: this is to limit the number of features read - as per the spec
074: // 7.3.3.7 FEATURE_COUNT
075: int featuresPrinted = 0; // how many features we've actually printed
076: // so far!
077:
078: int maxfeatures = getRequest().getFeatureCount(); // will default to 1
079: // if not specified
080: // in the request
081:
082: FeatureIterator reader = null;
083:
084: try {
085: final int size = results.size();
086: FeatureCollection fr;
087: Feature f;
088:
089: FeatureType schema;
090: AttributeType[] types;
091:
092: for (int i = 0; i < size; i++) // for each layer queried
093: {
094: fr = (FeatureCollection) results.get(i);
095: reader = fr.features();
096:
097: if (reader.hasNext() && (featuresPrinted < maxfeatures)) // if this layer has a hit and we're going to print it
098: {
099: writer.println("Results for FeatureType '"
100: + fr.getFeatureType().getTypeName() + "':");
101: }
102:
103: while (reader.hasNext()) {
104: f = reader.next();
105: schema = f.getFeatureType();
106: types = schema.getAttributeTypes();
107:
108: if (featuresPrinted < maxfeatures) {
109: writer
110: .println("--------------------------------------------");
111:
112: for (int j = 0; j < types.length; j++) // for each
113: // column in the
114: // featuretype
115: {
116: if (Geometry.class
117: .isAssignableFrom(types[j]
118: .getType())) {
119: // writer.println(types[j].getName() + " =
120: // [GEOMETRY]");
121:
122: // DJB: changed this to print out WKT - its very
123: // nice for users
124: // Geometry g = (Geometry)
125: // f.getAttribute(types[j].getName());
126: // writer.println(types[j].getName() + " =
127: // [GEOMETRY] = "+g.toText() );
128:
129: // DJB: decided that all the geometry info was
130: // too much - they should use GML version if
131: // they want those details
132: Geometry g = (Geometry) f
133: .getAttribute(types[j]
134: .getName());
135: writer.println(types[j].getName()
136: + " = [GEOMETRY ("
137: + g.getGeometryType()
138: + ") with " + g.getNumPoints()
139: + " points]");
140: } else {
141: writer.println(types[j].getName()
142: + " = "
143: + f.getAttribute(types[j]
144: .getName()));
145: }
146: }
147:
148: writer
149: .println("--------------------------------------------");
150: featuresPrinted++;
151: }
152: }
153: }
154: } catch (Exception ife) {
155: LOGGER.log(Level.WARNING,
156: "Error generating getFeaturInfo, HTML format", ife);
157: writer.println("Unable to generate information " + ife);
158: } finally {
159: if (reader != null) {
160: reader.close();
161: }
162: }
163:
164: if (featuresPrinted == 0) {
165: writer.println("no features were found");
166: }
167:
168: writer.flush();
169: }
170:
171: public String getContentDisposition() {
172: // TODO Auto-generated method stub
173: return null;
174: }
175: }
|