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.action;
006:
007: import org.apache.struts.action.Action;
008: import org.apache.struts.action.ActionForm;
009: import org.apache.struts.action.ActionForward;
010: import org.apache.struts.action.ActionMapping;
011: import org.apache.struts.action.DynaActionForm;
012: import org.geotools.factory.Hints;
013: import org.geotools.referencing.CRS;
014: import org.geotools.referencing.ReferencingFactoryFinder;
015: import org.opengis.referencing.FactoryException;
016: import org.opengis.referencing.crs.CRSAuthorityFactory;
017: import org.opengis.referencing.crs.CoordinateReferenceSystem;
018: import org.vfny.geoserver.crs.GeoserverCustomWKTFactory;
019: import java.io.IOException;
020: import java.util.ArrayList;
021: import java.util.Collections;
022: import java.util.HashSet;
023: import java.util.Iterator;
024: import java.util.List;
025: import java.util.Set;
026: import java.util.logging.Level;
027: import java.util.logging.Logger;
028: import javax.servlet.ServletException;
029: import javax.servlet.http.HttpServletRequest;
030: import javax.servlet.http.HttpServletResponse;
031:
032: /**
033: *
034: * @author Simone Giannecchini, GeoSolutions
035: *
036: */
037: public class SrsHelpAction extends Action {
038: private final static Logger LOGGER = org.geotools.util.logging.Logging
039: .getLogger(SrsHelpAction.class.toString());
040:
041: /**
042: * This is a simple action - it reads in the GT2 supported EPSG codes.
043: *
044: * DONE: once geosever support EPSG thats not in the properties file, this should
045: * be a bit more abstract and get a list of all EPSG defs from the
046: * GDSFactory (if possible). Use toWKT() as its nicer to read.
047: *
048: * Form has two properies - ids (list of String - the epsg #)
049: * defs (list of String - the epsg WKT definitions)
050: */
051: public ActionForward execute(ActionMapping mapping,
052: ActionForm form, HttpServletRequest request,
053: HttpServletResponse response) throws IOException,
054: ServletException {
055: ArrayList defs = new ArrayList();
056: ArrayList ids_string = new ArrayList();
057:
058: Set codes = CRS.getSupportedCodes("EPSG");
059:
060: CRSAuthorityFactory customFactory = ReferencingFactoryFinder
061: .getCRSAuthorityFactory("EPSG", new Hints(
062: Hints.CRS_AUTHORITY_FACTORY,
063: GeoserverCustomWKTFactory.class));
064: Set customCodes = new HashSet();
065:
066: try {
067: customCodes = customFactory
068: .getAuthorityCodes(CoordinateReferenceSystem.class);
069: } catch (FactoryException e) {
070: LOGGER
071: .log(
072: Level.WARNING,
073: "Error occurred while trying to gather custom CRS codes",
074: e);
075: }
076:
077: // make an array of each code (as an int)
078: HashSet idSet = new HashSet();
079: Iterator codeIt = codes.iterator();
080:
081: while (codeIt.hasNext()) {
082: String code = (String) codeIt.next();
083: String id = code.substring(code.indexOf(':') + 1); //just the number
084:
085: idSet.add(Integer.valueOf(id));
086: }
087:
088: List ids = new ArrayList(idSet);
089: Collections.sort(ids); //sort to get them in order
090:
091: CoordinateReferenceSystem crs;
092: String def;
093: codeIt = ids.iterator();
094:
095: Integer id;
096:
097: while (codeIt.hasNext()) //for each id (in sorted order)
098: {
099: id = (Integer) codeIt.next();
100:
101: try { //get its definition
102: crs = CRS.decode("EPSG:" + id);
103: def = crs.toString();
104: defs.add(def);
105: ids_string.add(id.toString());
106: } catch (Exception e) {
107: if (customCodes.contains(id.toString())) {
108: if (LOGGER.isLoggable(Level.FINE)) {
109: LOGGER
110: .log(Level.FINE,
111: "Issues converting EPSG:" + id
112: + ".", e);
113: } else {
114: LOGGER
115: .log(
116: Level.WARNING,
117: "Issues converting EPSG:"
118: + id
119: + ". "
120: + e
121: .getLocalizedMessage()
122: + " Stack trace included at FINE logging level");
123: }
124: } else if (LOGGER.isLoggable(Level.FINER)) {
125: LOGGER.log(Level.FINER, "Issues converting EPSG:"
126: + id, e);
127: }
128: }
129: }
130:
131: //send off to the .jsp
132: DynaActionForm myForm = (DynaActionForm) form;
133:
134: myForm.set("srsDefinitionList", defs.toArray(new String[1]));
135: myForm.set("srsIDList", ids_string.toArray(new String[1]));
136:
137: // return back to the admin demo
138: //
139: return mapping.findForward("success");
140: }
141: }
|