示例#1
0
  /**
   * This transforms all external graphics references that are relative to absolute. This is a
   * workaround to be able to visualize png and svg in relative mode, which doesn't work right now
   * in geotools. See: http://jira.codehaus.org/browse/GEOT-3235
   *
   * <p>This will not be necessary any more as soon as the geotools bug is fixed.
   *
   * @param relatedFile the related shapefile.
   * @param style the style to check.
   */
  private void makeGraphicsAbsolute(File relatedFile, Style style) {
    File parentFolder = relatedFile.getParentFile();
    Rule[] rules = SLDs.rules(style);
    for (Rule rule : rules) {
      Symbolizer[] onlineResource = rule.getSymbolizers();

      for (Symbolizer symbolizer : onlineResource) {
        if (symbolizer instanceof PointSymbolizer) {
          PointSymbolizer pointSymbolizer = (PointSymbolizer) symbolizer;
          Graphic graphic = SLDs.graphic(pointSymbolizer);
          List<GraphicalSymbol> graphicalSymbols = graphic.graphicalSymbols();
          for (GraphicalSymbol graphicalSymbol : graphicalSymbols) {
            if (graphicalSymbol instanceof ExternalGraphic) {
              ExternalGraphic externalGraphic = (ExternalGraphic) graphicalSymbol;
              try {
                URL location = externalGraphic.getLocation();
                File urlToFile = URLUtils.urlToFile(location);
                if (urlToFile != null && !urlToFile.exists()) {
                  File newFile = new File(parentFolder, urlToFile.getPath());
                  if (newFile.exists()) {
                    externalGraphic.setLocation(newFile.toURI().toURL());
                  }
                }
              } catch (MalformedURLException e) {
                e.printStackTrace();
              }
            }
          }
        }
      }
    }
  }
 @SuppressWarnings("nls")
 private void setExternalGraphicPath(String externalGraphicPath, ExternalGraphic extGraphic)
     throws MalformedURLException {
   URL url = null;
   File f = new File(externalGraphicPath);
   String format = Utilities.getFormat(externalGraphicPath);
   if (!f.exists()) {
     if (externalGraphicPath.startsWith("http://") || externalGraphicPath.startsWith("file:")) {
       url = new URL(externalGraphicPath);
     }
   }
   if (url == null) {
     url = f.toURI().toURL();
     if (externalGraphicPath.equals("")) {
       url = new URL("file:");
     }
   }
   extGraphic.setLocation(url);
   extGraphic.setFormat(format);
 }
 private String getExternalGraphicPath(ExternalGraphic extGraphic) throws MalformedURLException {
   if (extGraphic == null) {
     return "";
   }
   URL location = extGraphic.getLocation();
   String urlString = location.toExternalForm();
   if (urlString.startsWith("http://")) {
     return urlString;
   } else {
     File urlToFile = URLUtils.urlToFile(location);
     if (urlString.equals("file:")) {
       return "";
     } else {
       return urlToFile.getAbsolutePath();
     }
   }
 }