Beispiel #1
0
  public static void main(String[] args) {
    try {
      // create the plotting domain
      final int WIDTH = 1024;
      final int HEIGHT = 512;
      BoundingBox bbox = new BoundingBoxImpl(-180, -90, 180, 90, DefaultGeographicCRS.WGS84);
      PlottingDomainParams params =
          new PlottingDomainParams(WIDTH, HEIGHT, bbox, null, null, null, null, null);

      // load the datasets from a config file
      DatasetFactory.setDefaultDatasetFactoryClass(CdmGridDatasetFactory.class);
      NcwmsConfig config =
          NcwmsConfig.readFromFile(new File("C:\\Users\\Charles\\.ncWMS-edal\\config.xml"));

      // create the feature catalogue
      FeatureCatalogue catalogue = new NcwmsCatalogue(config);

      try {
        Thread.sleep(3000);
      } catch (InterruptedException ex) {
        Thread.currentThread().interrupt();
      }

      // process each XML file
      final File folder = new File("N:\\Documents\\SLDInput");
      for (final File fileEntry : folder.listFiles()) {
        String fileName = fileEntry.getName();
        if (fileName.endsWith("xml")) {
          System.out.println("Processing " + fileName);
          // create the image from the XML file
          MapImage mapImage = StyleSLDParser.createImage(fileEntry);
          BufferedImage image = mapImage.drawImage(params, catalogue);
          // write the image to a PNG file
          File outputfile = new File("N:\\Documents\\SLDOutput\\" + fileName.replace("xml", "png"));
          ImageIO.write(image, "png", outputfile);
          // create the legend
          BufferedImage legend = mapImage.getLegend(250);
          outputfile =
              new File("N:\\Documents\\SLDOutput\\" + fileName.replace(".xml", "_lgd.png"));
          ImageIO.write(legend, "png", outputfile);
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
    }

    NcwmsConfig.shutdown();
  }
  @Override
  public void init(ServletConfig servletConfig) throws ServletException {
    super.init(servletConfig);

    NcwmsConfig config;
    /*
     * Set the default dataset factory - will be used when a dataset factory
     * name is not specified
     */
    DatasetFactory.setDefaultDatasetFactoryClass(CdmGridDatasetFactory.class);

    /*
     * Load the XML config for ncWMS, or create it if it doesn't yet exist.
     */
    Properties appProperties = new Properties();

    String configDir = null;
    String homeDir = System.getProperty("user.home").replace("\\", "\\\\");
    try {
      /*
       * See if we have a properties file which defines a configDir,
       * replacing $HOME with the actual home directory
       */
      appProperties.load(getClass().getResourceAsStream("/config.properties"));
      configDir = appProperties.getProperty("configDir");
      if (configDir != null) {
        configDir = configDir.replaceAll("\\$HOME", homeDir);
      }
    } catch (Exception e) {
      configDir = null;
      e.printStackTrace();
    }

    /*
     * If we didn't define a config directory, use the user's home as a
     * default
     */
    if (configDir == null) {
      configDir = homeDir + File.separator + ".ncWMS-edal";
    }

    /*
     * If the config location doesn't exist, create it.
     */
    File configDirFile = new File(configDir);
    if (!configDirFile.exists()) {
      configDirFile.mkdirs();
    }

    DatasetFactory.setWorkingDirectory(configDirFile);

    /*
     * If necessary, create a directory for logs.
     */
    File logDirFile = new File(configDir + File.separator + "logs");
    if (!logDirFile.exists()) {
      logDirFile.mkdir();
    }

    /*
     * Get the file appending logger and set the log location
     */
    // Set up the log4j logging system
    Properties logProps = new Properties();
    InputStream log4jInputStream = getClass().getResourceAsStream("/log4j.properties");
    try {
      logProps.load(log4jInputStream);
      logProps.put("log4j.appender.file.File", logDirFile.getPath() + File.separator + "ncwms.log");
      PropertyConfigurator.configure(logProps);
    } catch (IOException e) {
      log.error("Problem setting logging properties", e);
      /*
       * This is a problem, but not a fatal one.  Logging will go to its default location.
       */
    }

    /*
     * Now either create or read the ncWMS config.xml
     */
    File configFile = new File(configDir + File.separator, "config.xml");
    try {
      if (configFile.exists()) {
        config = NcwmsConfig.readFromFile(configFile);
      } else {
        config = new NcwmsConfig(configFile);
      }

    } catch (JAXBException e) {
      log.error("Config file is invalid - creating new one", e);
      try {
        config = new NcwmsConfig(configFile);
      } catch (Exception e1) {
        throw new ServletException("Old config is invalid, and a new one cannot be created", e1);
      }
    } catch (FileNotFoundException e) {
      /*
       * We shouldn't get here. It means that we've checked that a config
       * file exists and then the FileReader has thrown a
       * FileNotFoundException
       */
      log.error(
          "Cannot find config file - has it been deleted during startup?  Creating new one", e);
      try {
        config = new NcwmsConfig(configFile);
      } catch (Exception e1) {
        throw new ServletException("Old config is missing, and a new one cannot be created", e1);
      }
    } catch (IOException e) {
      log.error("Problem writing new config file", e);
      throw new ServletException("Cannot create a new config file", e);
    }
    try {
      catalogue = new NcwmsCatalogue(config);
    } catch (IOException e) {
      log.error("Problem loading datasets", e);
    }

    /*
     * Store the config in the ServletContext, so that the other servlets
     * can access it. All other servlets are loaded after this one.
     */
    servletConfig.getServletContext().setAttribute("NcwmsCatalogue", catalogue);

    /*
     * Now create a VelocityEngine to load velocity templates, and make it
     * available to other servlets in the same way
     */
    Properties props = new Properties();
    props.put("resource.loader", "class");
    props.put(
        "class.resource.loader.class",
        "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
    velocityEngine = new VelocityEngine();
    velocityEngine.setProperty(
        RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS,
        "org.apache.velocity.runtime.log.Log4JLogChute");
    velocityEngine.setProperty("runtime.log.logsystem.log4j.logger", "velocity");
    velocityEngine.init(props);
    servletConfig.getServletContext().setAttribute("VelocityEngine", velocityEngine);
  }