Beispiel #1
0
 /**
  * Recursive function to parse a layer tree
  *
  * @param lt
  * @param parentLayer
  */
 private void parseJaxbLayer(LayerType lt, ILayer parentLayer) throws LayerException {
   // Test if lt is a group
   if (!lt.getLayer().isEmpty() || (lt.getDataURL() == null)) {
     // it will create a LayerCollection
     parseJaxbLayerCollection(lt, parentLayer);
   } else {
     // it corresponding to a leaf layer
     // We need to read the data declaration and create the layer
     URLType dataUrl = lt.getDataURL();
     if (dataUrl != null) {
       OnlineResourceType resType = dataUrl.getOnlineResource();
       try {
         URI layerURI = new URI(resType.getHref());
         // The resource is given as relative to MapContext location
         if (!layerURI.isAbsolute() && getLocation() != null) {
           try {
             // Resolve the relative resource ex: new Uri("myFile.shp")
             layerURI = getLocation().resolve(layerURI);
           } catch (IllegalArgumentException ex) {
             LOGGER.warn(
                 "Error while trying to find an absolute path for an external resource", ex);
           }
         }
         // Get table name
         ILayer leafLayer = createLayer(layerURI);
         leafLayer.setDescription(new Description(lt));
         leafLayer.setVisible(!lt.isHidden());
         // Parse styles
         if (lt.isSetStyleList()) {
           for (StyleType st : lt.getStyleList().getStyle()) {
             if (st.isSetSLD()) {
               if (st.getSLD().isSetAbstractStyle()) {
                 leafLayer.addStyle(
                     new Style(
                         (JAXBElement<net.opengis.se._2_0.core.StyleType>)
                             st.getSLD().getAbstractStyle(),
                         leafLayer));
               }
             }
           }
         }
         parentLayer.addLayer(leafLayer);
       } catch (URISyntaxException ex) {
         throw new LayerException(
             I18N.tr("Unable to parse the href URI {0}.", resType.getHref()), ex);
       } catch (InvalidStyle ex) {
         throw new LayerException(
             I18N.tr("Unable to load the description of the layer {0}", lt.getTitle().toString()),
             ex);
       }
     }
   }
 }
Beispiel #2
0
  private static LayerType createJAXBFromLayer(ILayer layer, MapContext mapContext) {
    ObjectFactory ows_context_factory = new ObjectFactory();
    LayerType layerType = ows_context_factory.createLayerType();
    Description description = layer.getDescription();
    description.initJAXBType(layerType);
    layerType.setHidden(!layer.isVisible());
    ILayer[] childrens = layer.getChildren();
    for (ILayer child : childrens) {
      if (child.isSerializable()) {
        layerType.getLayer().add(createJAXBFromLayer(child, mapContext));
      }
    }
    // If not a Layer Collection
    if (!(layer instanceof LayerCollection) && layer.getStyles() != null) {
      StyleListType slt = ows_context_factory.createStyleListType();
      layerType.setStyleList(slt);
      for (Style style : layer.getStyles()) {
        StyleType st = ows_context_factory.createStyleType();
        slt.getStyle().add(st);
        SLDType sltType = ows_context_factory.createSLDType();
        st.setSLD(sltType);
        sltType.setAbstractStyle(style.getJAXBElement());
      }
    }

    // Serialisation of dataSource as a DataUrl string
    // Create jaxb instances
    URLType dataURL = ows_context_factory.createURLType();
    if (!(layer instanceof LayerCollection)) {
      OnlineResourceType resource = ows_context_factory.createOnlineResourceType();
      dataURL.setOnlineResource(resource);

      String resourceSerialisation = "";
      URI srcUri = layer.getDataUri();
      if (srcUri != null) {
        // If file, use MapContext relative path
        if (srcUri.getScheme().equalsIgnoreCase("file") && mapContext.getLocation() != null) {
          srcUri = URIUtility.relativize(mapContext.getLocation(), srcUri);
        }
        resourceSerialisation = srcUri.toString();
      }
      resource.setHref(resourceSerialisation);
      if (resource.isSetHref()) {
        layerType.setDataURL(dataURL);
      }
    }
    return layerType;
  }