org.geoserver.catalog.util.LegacyCoverageInfoReader Maven / Gradle / Ivy
The newest version!
/* Copyright (c) 2001 - 2008 TOPP - www.openplans.org. All rights reserved.
* This code is licensed under the GPL 2.0 license, available at the root
* application directory.
*/
package org.geoserver.catalog.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
import org.geoserver.ows.util.XmlCharsetDetector;
import org.w3c.dom.Element;
/**
* Reads a legacy coverage info.xml file.
*
* @author Justin Deoliveira, The Open Planning Project
*
*/
public class LegacyCoverageInfoReader {
/**
* Root catalog element.
*/
Element coverage;
/**
* The directory containing the feature type info.xml file
*/
File parentDirectory;
/**
* Parses the info.xml file into a DOM.
*
* This method *must* be called before any other methods.
*
*
* @param file The info.xml file.
*
* @throws IOException In event of a parser error.
*/
public void read(File file) throws IOException {
parentDirectory = file.getParentFile();
Reader reader = XmlCharsetDetector.getCharsetAwareReader(new FileInputStream(file));
try {
coverage = ReaderUtils.parse(reader);
} finally {
reader.close();
}
}
public String format() {
return coverage.getAttribute( "format" );
}
public String name() {
return ReaderUtils.getChildText(coverage, "name");
}
public String description() {
return ReaderUtils.getChildText(coverage, "description");
}
public String label() {
return ReaderUtils.getChildText(coverage, "label");
}
public Map metadataLink() {
HashMap ml = new HashMap();
ml.put( "about", ReaderUtils.getChildAttribute(coverage, "metadataLink", "about" ) );
ml.put( "metadataType", ReaderUtils.getChildAttribute(coverage, "metadataLink", ",metadataType" ) );
return ml;
}
public List keywords() {
String raw = ReaderUtils.getChildText( coverage, "keywords" );
StringTokenizer st = new StringTokenizer( raw, ", " );
ArrayList keywords = new ArrayList();
while( st.hasMoreTokens() ) {
keywords.add( st.nextToken() );
}
return keywords;
}
public String defaultStyle() throws Exception {
Element styles = ReaderUtils.getChildElement(coverage, "styles" );
return ReaderUtils.getAttribute( styles, "default", true );
}
public Map envelope() throws Exception {
Element envelopeElement = ReaderUtils.getChildElement(coverage, "envelope");
HashMap e = new HashMap();
String nativeCrsWkt = ReaderUtils.getAttribute(envelopeElement, "crs", false);
nativeCrsWkt = nativeCrsWkt.replaceAll("'", "\"");
e.put( "crs", nativeCrsWkt);
e.put( "srsName", ReaderUtils.getAttribute(envelopeElement, "srsName", false));
Element[] posElements = ReaderUtils.getChildElements(envelopeElement, "pos" );
String[] pos1 = posElements[0].getFirstChild().getTextContent().split( " " );
String[] pos2 = posElements[1].getFirstChild().getTextContent().split( " " );
e.put( "x1", Double.parseDouble(pos1[0]) );
e.put( "y1", Double.parseDouble(pos1[1]) );
e.put( "x2", Double.parseDouble(pos2[0]) );
e.put( "y2", Double.parseDouble(pos2[1]) );
return e;
}
public Map grid() throws Exception {
Element gridElement = ReaderUtils.getChildElement(coverage, "grid");
HashMap grid = new HashMap();
grid.put( "dimension",
Integer.parseInt( ReaderUtils.getAttribute(gridElement, "dimension", true ) ) );
Element lowElement = ReaderUtils.getChildElement(gridElement, "low");
String[] lows = lowElement.getFirstChild().getTextContent().trim().split( " ");
int[] low = new int[lows.length];
for ( int i = 0; i < low.length; i++ ) {
low[i] = Integer.parseInt( lows[i] );
}
grid.put( "low", low );
Element highElement = ReaderUtils.getChildElement(gridElement, "high" );
String[] highs = highElement.getFirstChild().getTextContent().trim().split( " ");
int[] high = new int[highs.length];
for ( int i = 0; i < high.length; i++ ) {
high[i] = Integer.parseInt( highs[i] );
}
grid.put( "high", high );
Element[] axisNameElements = ReaderUtils.getChildElements( gridElement, "axisName" );
String[] axisName = new String[ axisNameElements.length ];
for ( int i = 0; i < axisName.length; i++ ) {
axisName[i] = axisNameElements[i].getFirstChild().getTextContent();
}
grid.put( "axisName", axisName );
Element geoTransformElement = ReaderUtils.getChildElement( gridElement, "geoTransform" );
if ( geoTransformElement != null) {
Map geoTransform = new HashMap();
String scaleX = ReaderUtils.getChildText( geoTransformElement, "scaleX" );
String scaleY = ReaderUtils.getChildText( geoTransformElement, "scaleY" );
String shearX = ReaderUtils.getChildText( geoTransformElement, "shearX" );
String shearY = ReaderUtils.getChildText( geoTransformElement, "shearY" );
String translateX = ReaderUtils.getChildText( geoTransformElement, "translateX" );
String translateY = ReaderUtils.getChildText( geoTransformElement, "translateY" );
geoTransform.put( "scaleX", scaleX != null ? new Double( scaleX ) : null );
geoTransform.put( "scaleY", scaleY != null ? new Double( scaleY ) : null );
geoTransform.put( "shearX", shearX != null ? new Double( shearX ) : null );
geoTransform.put( "shearY", shearY != null ? new Double( shearY ) : null );
geoTransform.put( "translateX", translateX != null ? new Double( translateX ) : null );
geoTransform.put( "translateY", translateY != null ? new Double( translateY ) : null );
grid.put( "geoTransform", geoTransform );
}
return grid;
}
public List