/**
   * Create a {@link TiledImageLayer} layer described by an XML layer description.
   *
   * @param domElement the XML element describing the layer to create. The element must inculde a
   *     service name identifying the type of service to use to retrieve layer data. Recognized
   *     service types are "Offline", "WWTileService" and "OGC:WMS".
   * @param params any parameters to apply when creating the layer.
   * @return a new layer
   * @throws WWUnrecognizedException if the service type given in the describing element is
   *     unrecognized.
   */
  protected Layer createTiledImageLayer(Element domElement, AVList params) {
    Layer layer;

    String serviceName = WWXML.getText(domElement, "Service/@serviceName");

    if ("Offline".equals(serviceName)) {
      layer = new BasicTiledImageLayer(domElement, params);
    } else if ("WWTileService".equals(serviceName)) {
      layer = new BasicTiledImageLayer(domElement, params);
    } else if (OGCConstants.WMS_SERVICE_NAME.equals(serviceName)) {
      layer = new WMSTiledImageLayer(domElement, params);
    } else if (AVKey.SERVICE_NAME_LOCAL_RASTER_SERVER.equals(serviceName)) {
      layer = new LocalRasterServerLayer(domElement, params);
    } else {
      String msg = Logging.getMessage("generic.UnrecognizedServiceName", serviceName);
      throw new WWUnrecognizedException(msg);
    }
    //
    //        String name = layer.getStringValue(AVKey.DISPLAY_NAME);
    //        System.out.println(name);

    String actuate = WWXML.getText(domElement, "@actuate");
    layer.setEnabled(actuate != null && actuate.equals("onLoad"));

    return layer;
  }
  /**
   * Appends elevation model parameters as elements to a specified context. If a parameter key
   * exists, that parameter is appended to the context. Supported key and element paths are:
   *
   * <table> <th><td>Key</td><td>Name</td><td>Type</td></th>
   * <tr><td>{@link AVKey#DISPLAY_NAME}</td><td>DisplayName</td><td>String</td></tr> <tr><td>{@link
   * AVKey#NETWORK_RETRIEVAL_ENABLED}</td><td>NetworkRetrievalEnabled</td><td>Boolean</td></tr> <tr><td>{@link
   * AVKey#MISSING_DATA_SIGNAL}</td><td>MissingData/@signal</td><td>Double</td></tr> <tr><td>{@link
   * AVKey#MISSING_DATA_REPLACEMENT}</td><td>MissingData/@replacement</td><td>Double</td></tr> <tr><td>{@link
   * AVKey#DETAIL_HINT}</td><td>DataDetailHint</td><td>Double</td></tr> </table>
   *
   * @param params the key-value pairs which define the elevation model parameters.
   * @param context the XML document root on which to append parameter elements.
   * @return a reference to context.
   * @throws IllegalArgumentException if either the parameters or the context are null.
   */
  public static Element createElevationModelElements(AVList params, Element context) {
    if (params == null) {
      String message = Logging.getMessage("nullValue.ParametersIsNull");
      Logging.logger().severe(message);
      throw new IllegalArgumentException(message);
    }

    if (context == null) {
      String message = Logging.getMessage("nullValue.ContextIsNull");
      Logging.logger().severe(message);
      throw new IllegalArgumentException(message);
    }

    WWXML.checkAndAppendTextElement(params, AVKey.DISPLAY_NAME, context, "DisplayName");
    WWXML.checkAndAppendBooleanElement(
        params, AVKey.NETWORK_RETRIEVAL_ENABLED, context, "NetworkRetrievalEnabled");

    if (params.getValue(AVKey.MISSING_DATA_SIGNAL) != null
        || params.getValue(AVKey.MISSING_DATA_REPLACEMENT) != null) {
      Element el = WWXML.getElement(context, "MissingData", null);
      if (el == null) el = WWXML.appendElementPath(context, "MissingData");

      Double d = AVListImpl.getDoubleValue(params, AVKey.MISSING_DATA_SIGNAL);
      if (d != null) el.setAttribute("signal", Double.toString(d));

      d = AVListImpl.getDoubleValue(params, AVKey.MISSING_DATA_REPLACEMENT);
      if (d != null) el.setAttribute("replacement", Double.toString(d));
    }

    WWXML.checkAndAppendDoubleElement(params, AVKey.DETAIL_HINT, context, "DataDetailHint");

    return context;
  }
  /**
   * Returns true if a specified DOM document should be accepted as an ElevationModel configuration
   * document, and false otherwise.
   *
   * @param domElement the DOM document in question.
   * @return true if the document is an ElevationModel configuration document; false otherwise.
   * @throws IllegalArgumentException if document is null.
   */
  public static boolean isElevationModelDocument(Element domElement) {
    if (domElement == null) {
      String message = Logging.getMessage("nullValue.DocumentIsNull");
      Logging.logger().severe(message);
      throw new IllegalArgumentException(message);
    }

    XPath xpath = WWXML.makeXPath();
    Element[] elements = WWXML.getElements(domElement, "//ElevationModel", xpath);

    return elements != null && elements.length > 0;
  }
  /**
   * Parses basic elevation model parameters from a specified DOM document. This also parses
   * LevelSet parameters by invoking {@link
   * gov.nasa.worldwind.util.DataConfigurationUtils#getLevelSetParams(org.w3c.dom.Element,
   * gov.nasa.worldwind.avlist.AVList)}. This writes output as key-value pairs to params. If a
   * parameter from the XML document already exists in params, that parameter is ignored. Supported
   * key and parameter names are:
   *
   * <table>
   * <th><td>Key</td><td>Name</td><td>Type</td></th> <tr><td>{@link AVKey#SERVICE_NAME}</td><td>Service/@serviceName</td><td>String</td></tr>
   * <tr><td>{@link AVKey#PIXEL_TYPE}</td><td>DataType</td><td>String</td></tr> <tr><td>{@link
   * AVKey#BYTE_ORDER}</td><td>DataType/@byteOrder</td><td>String</td></tr> <tr><td>{@link
   * AVKey#ELEVATION_EXTREMES_FILE}</td><td>ExtremeElevations/FileName</td><td>String</td></tr> <tr><td>{@link
   * AVKey#ELEVATION_MAX}</td><td>ExtremeElevations/@max</td><td>Double</td></tr> <tr><td>{@link
   * AVKey#ELEVATION_MIN}</td><td>ExtremeElevations/@min</td><td>Double</td></tr> </table>
   *
   * @param domElement the XML document root to parse for basic elevation model parameters.
   * @param params the output key-value pairs which recieve the basic elevation model parameters. A
   *     null reference is permitted.
   * @return a reference to params, or a new AVList if params is null.
   * @throws IllegalArgumentException if the document is null.
   */
  public static AVList getBasicElevationModelParams(Element domElement, AVList params) {
    if (domElement == null) {
      String message = Logging.getMessage("nullValue.DocumentIsNull");
      Logging.logger().severe(message);
      throw new IllegalArgumentException(message);
    }

    if (params == null) params = new AVListImpl();

    XPath xpath = WWXML.makeXPath();

    // LevelSet properties.
    DataConfigurationUtils.getLevelSetParams(domElement, params);

    // Service properties.
    WWXML.checkAndSetStringParam(
        domElement, params, AVKey.SERVICE_NAME, "Service/@serviceName", xpath);
    WWXML.checkAndSetBooleanParam(
        domElement,
        params,
        AVKey.RETRIEVE_PROPERTIES_FROM_SERVICE,
        "RetrievePropertiesFromService",
        xpath);

    // Image format properties.
    if (params.getValue(AVKey.PIXEL_TYPE) == null) {
      String s = WWXML.getText(domElement, "DataType/@type", xpath);
      if (s != null && s.length() > 0) {
        s = WWXML.parseDataType(s);
        if (s != null && s.length() > 0) params.setValue(AVKey.PIXEL_TYPE, s);
      }
    }

    if (params.getValue(AVKey.BYTE_ORDER) == null) {
      String s = WWXML.getText(domElement, "DataType/@byteOrder", xpath);
      if (s != null && s.length() > 0) {
        s = WWXML.parseByteOrder(s);
        if (s != null && s.length() > 0) params.setValue(AVKey.BYTE_ORDER, s);
      }
    }

    // Elevation data properties.
    WWXML.checkAndSetStringParam(
        domElement, params, AVKey.ELEVATION_EXTREMES_FILE, "ExtremeElevations/FileName", xpath);
    WWXML.checkAndSetDoubleParam(
        domElement, params, AVKey.ELEVATION_MAX, "ExtremeElevations/@max", xpath);
    WWXML.checkAndSetDoubleParam(
        domElement, params, AVKey.ELEVATION_MIN, "ExtremeElevations/@min", xpath);

    return params;
  }
  /**
   * Create a collection of layer lists and their included layers described by an array of XML
   * layer-list description elements.
   *
   * <p>Any exceptions occurring during creation of the layer lists or their included layers are
   * logged and not re-thrown. The layers associated with the exceptions are not included in the
   * returned layer list.
   *
   * @param elements the XML elements describing the layer lists to create.
   * @param params any parameters to apply when creating the included layers.
   * @return an array containing the specified layer lists.
   */
  protected LayerList[] createLayerLists(Element[] elements, AVList params) {
    ArrayList<LayerList> layerLists = new ArrayList<LayerList>();

    for (Element element : elements) {
      try {
        String href = WWXML.getText(element, "@href");
        if (href != null && href.length() > 0) {
          Object o = this.createFromConfigSource(href, params);
          if (o == null) continue;

          if (o instanceof Layer) {
            LayerList ll = new LayerList();
            ll.add((Layer) o);
            o = ll;
          }

          if (o instanceof LayerList) {
            LayerList list = (LayerList) o;
            if (list != null && list.size() > 0) layerLists.add(list);
          } else if (o instanceof LayerList[]) {
            LayerList[] lists = (LayerList[]) o;
            if (lists != null && lists.length > 0) layerLists.addAll(Arrays.asList(lists));
          } else {
            String msg =
                Logging.getMessage("LayerFactory.UnexpectedTypeForLayer", o.getClass().getName());
            Logging.logger().log(java.util.logging.Level.WARNING, msg);
          }

          continue;
        }

        String title = WWXML.getText(element, "@title");
        Element[] children = WWXML.getElements(element, "./Layer", null);
        if (children != null && children.length > 0) {
          LayerList list = this.createLayerList(children, params);
          if (list != null && list.size() > 0) {
            layerLists.add(list);
            if (title != null && title.length() > 0) list.setValue(AVKey.DISPLAY_NAME, title);
          }
        }
      } catch (Exception e) {
        Logging.logger().log(java.util.logging.Level.WARNING, e.getMessage(), e);
        // keep going to create other layers
      }
    }

    return layerLists.toArray(new LayerList[layerLists.size()]);
  }
  /**
   * Create the objects described in an XML element containing layer and/or layer-list descriptions.
   * Nested layer lists and included layers are created recursively.
   *
   * @param domElement an XML element describing the layers and/or layer lists.
   * @param params any properties to apply when creating the included layers.
   * @return a <code>Layer</code>, <code>LayerList</code> or array of <code>LayerList</code>s, as
   *     described by the specified description.
   * @throws Exception if an exception occurs during creation. Exceptions occurring during creation
   *     of internal layers or layer lists are not re-thrown but are logged. The layer or layer list
   *     associated with the exception is not contained in the returned object.
   */
  @Override
  protected Object doCreateFromElement(Element domElement, AVList params) throws Exception {
    Element[] elements = WWXML.getElements(domElement, "//LayerList", null);
    if (elements != null && elements.length > 0) return createLayerLists(elements, params);

    elements = WWXML.getElements(domElement, "./Layer", null);
    if (elements != null && elements.length > 1) return createLayerList(elements, params);

    if (elements != null && elements.length == 1)
      return this.createFromLayerDocument(elements[0], params);

    String localName = WWXML.getUnqualifiedName(domElement);
    if (localName != null && localName.equals("Layer"))
      return this.createFromLayerDocument(domElement, params);

    return null;
  }
  /**
   * Creates a configuration document for the basic elevation model described by the specified
   * params. This document's root element may be passed to a ElevationModelConfiguration
   * constructor, and it may be passed to the constructor of a {@link
   * gov.nasa.worldwind.terrain.BasicElevationModel}.
   *
   * @param params parameters describing the basic elevation model.
   * @return a configuration document for the basic elevation model.
   */
  public static Document createBasicElevationModelDocument(AVList params) {
    Document doc = WWXML.createDocumentBuilder(true).newDocument();

    Element root = doc.createElement("ElevationModel");
    root.setAttribute("version", Integer.toString(1));
    // No type attribute denotes the default elevation model, which currently is
    // BasicElevationModel.
    doc.appendChild(root);

    createElevationModelElements(params, root);
    createBasicElevationModelElements(params, root);

    return doc;
  }
  /**
   * Create a layer described by an XML layer description.
   *
   * @param domElement the XML element describing the layer to create.
   * @param params any parameters to apply when creating the layer.
   * @return a new layer
   * @throws WWUnrecognizedException if the layer type or service type given in the describing
   *     element is unrecognized.
   * @see #createTiledImageLayer(org.w3c.dom.Element, gov.nasa.worldwind.avlist.AVList).
   */
  protected Layer createFromLayerDocument(Element domElement, AVList params) {
    String className = WWXML.getText(domElement, "@className");
    if (className != null && className.length() > 0) {
      Layer layer = (Layer) WorldWind.createComponent(className);
      String actuate = WWXML.getText(domElement, "@actuate");
      layer.setEnabled(WWUtil.isEmpty(actuate) || actuate.equals("onLoad"));
      WWXML.invokePropertySetters(layer, domElement);
      return layer;
    }

    AVList props = WWXML.copyProperties(domElement, null);
    if (props != null) { // Copy params and add any properties for this layer to the copy
      if (params != null) props.setValues(params);
      params = props;
    }

    Layer layer;
    String href = WWXML.getText(domElement, "@href");
    if (href != null && href.length() > 0) {
      Object o = this.createFromConfigSource(href, params);
      if (o == null) return null;

      if (!(o instanceof Layer)) {
        String msg =
            Logging.getMessage("LayerFactory.UnexpectedTypeForLayer", o.getClass().getName());
        throw new WWRuntimeException(msg);
      }

      layer = (Layer) o;
    } else {
      String layerType = WWXML.getText(domElement, "@layerType");
      if (layerType != null && layerType.equals("TiledImageLayer")) {
        layer = this.createTiledImageLayer(domElement, params);
      } else {
        String msg = Logging.getMessage("generic.UnrecognizedLayerType", layerType);
        throw new WWUnrecognizedException(msg);
      }
    }

    if (layer != null) {
      String actuate = WWXML.getText(domElement, "@actuate");
      layer.setEnabled(actuate != null && actuate.equals("onLoad"));
      WWXML.invokePropertySetters(layer, domElement);
    }

    return layer;
  }
    public URL getURL(Tile tile, String altImageFormat) throws MalformedURLException {
      StringBuffer sb;
      if (this.URLTemplate == null) {
        sb = new StringBuffer(WWXML.fixGetMapString(tile.getLevel().getService()));

        if (!sb.toString().toLowerCase().contains("service=wms")) sb.append("service=WMS");
        sb.append("&request=GetMap");
        sb.append("&version=").append(this.wmsVersion);
        sb.append(this.crs);
        sb.append("&layers=").append(this.layerNames);
        sb.append("&styles=").append(this.styleNames != null ? this.styleNames : "");
        sb.append("&transparent=TRUE");
        if (this.backgroundColor != null) sb.append("&bgcolor=").append(this.backgroundColor);

        this.URLTemplate = sb.toString();
      } else {
        sb = new StringBuffer(this.URLTemplate);
      }

      String format = (altImageFormat != null) ? altImageFormat : this.imageFormat;
      if (null != format) sb.append("&format=").append(format);

      sb.append("&width=").append(tile.getWidth());
      sb.append("&height=").append(tile.getHeight());

      Sector s = tile.getSector();
      sb.append("&bbox=");
      sb.append(s.getMinLongitude().getDegrees());
      sb.append(",");
      sb.append(s.getMinLatitude().getDegrees());
      sb.append(",");
      sb.append(s.getMaxLongitude().getDegrees());
      sb.append(",");
      sb.append(s.getMaxLatitude().getDegrees());
      //            sb.append("&"); // terminate the query string

      return new java.net.URL(sb.toString().replace(" ", "%20"));
    }
  /**
   * Parses elevation model parameters from a specified DOM document. This writes output as
   * key-value pairs to params. If a parameter from the XML document already exists in params, that
   * parameter is ignored. Supported key and parameter names are:
   *
   * <table> <th><td>Key</td><td>Name</td><td>Type</td></th> <tr><td>{@link
   * gov.nasa.worldwind.avlist.AVKey#DISPLAY_NAME}</td><td>DisplayName</td><td>String</td></tr> <tr><td>{@link
   * gov.nasa.worldwind.avlist.AVKey#NETWORK_RETRIEVAL_ENABLED}</td><td>NetworkRetrievalEnabled</td><td>Boolean</td></tr>
   * <tr><td>{@link gov.nasa.worldwind.avlist.AVKey#MISSING_DATA_SIGNAL}</td><td>MissingData/@signal</td><td>Double</td></tr>
   * <tr><td>{@link gov.nasa.worldwind.avlist.AVKey#MISSING_DATA_REPLACEMENT}</td><td>MissingData/@replacement</td><td>Double</td></tr>
   * <tr><td>{@link gov.nasa.worldwind.avlist.AVKey#DETAIL_HINT}</td><td>DataDetailHint</td><td>Double</td></tr>
   * </table>
   *
   * @param domElement the XML document root to parse for elevaiton model parameters.
   * @param params the output key-value pairs which recieve the elevation model parameters. A null
   *     reference is permitted.
   * @return a reference to params, or a new AVList if params is null.
   * @throws IllegalArgumentException if the document is null.
   */
  public static AVList getElevationModelParams(Element domElement, AVList params) {
    if (domElement == null) {
      String message = Logging.getMessage("nullValue.DocumentIsNull");
      Logging.logger().severe(message);
      throw new IllegalArgumentException(message);
    }

    if (params == null) params = new AVListImpl();

    XPath xpath = WWXML.makeXPath();

    WWXML.checkAndSetStringParam(domElement, params, AVKey.DISPLAY_NAME, "DisplayName", xpath);
    WWXML.checkAndSetBooleanParam(
        domElement, params, AVKey.NETWORK_RETRIEVAL_ENABLED, "NetworkRetrievalEnabled", xpath);
    WWXML.checkAndSetDoubleParam(
        domElement, params, AVKey.MISSING_DATA_SIGNAL, "MissingData/@signal", xpath);
    WWXML.checkAndSetDoubleParam(
        domElement, params, AVKey.MISSING_DATA_REPLACEMENT, "MissingData/@replacement", xpath);
    WWXML.checkAndSetDoubleParam(domElement, params, AVKey.DETAIL_HINT, "DataDetailHint", xpath);

    return params;
  }
  /**
   * Appends basic elevation model parameters as elements to a specified context. If a parameter key
   * exists, that parameter is appended to the context. This also writes LevelSet parameters by
   * invoking {@link DataConfigurationUtils#createLevelSetElements(gov.nasa.worldwind.avlist.AVList,
   * org.w3c.dom.Element)}. Supported key and element paths are:
   *
   * <table> <th><td>Key</td><td>Name</td><td>Type</td></th> <tr><td>{@link
   * AVKey#SERVICE_NAME}</td><td>Service/@serviceName</td><td>String</td></tr> <tr><td>{@link
   * AVKey#PIXEL_TYPE}</td><td>PixelType</td><td>String</td></tr> <tr><td>{@link AVKey#BYTE_ORDER}</td><td>ByteOrder</td><td>String</td></tr>
   * <tr><td>{@link AVKey#ELEVATION_EXTREMES_FILE}</td><td>ExtremeElevations/FileName</td><td>String</td></tr>
   * <tr><td>{@link AVKey#ELEVATION_MAX}</td><td>ExtremeElevations/@max</td><td>Double</td></tr> <tr><td>{@link
   * AVKey#ELEVATION_MIN}</td><td>ExtremeElevations/@min</td><td>Double</td></tr> </table>
   *
   * @param params the key-value pairs which define the basic elevation model parameters.
   * @param context the XML document root on which to append parameter elements.
   * @return a reference to context.
   * @throws IllegalArgumentException if either the parameters or the context are null.
   */
  public static Element createBasicElevationModelElements(AVList params, Element context) {
    if (params == null) {
      String message = Logging.getMessage("nullValue.ParametersIsNull");
      Logging.logger().severe(message);
      throw new IllegalArgumentException(message);
    }

    if (context == null) {
      String message = Logging.getMessage("nullValue.ContextIsNull");
      Logging.logger().severe(message);
      throw new IllegalArgumentException(message);
    }

    XPath xpath = WWXML.makeXPath();

    // LevelSet properties.
    DataConfigurationUtils.createLevelSetElements(params, context);

    // Service properties.
    // Try to get the SERVICE_NAME property, but default to "WWTileService".
    String s = AVListImpl.getStringValue(params, AVKey.SERVICE_NAME, "WWTileService");
    if (s != null && s.length() > 0) {
      // The service element may already exist, in which case we want to append to it.
      Element el = WWXML.getElement(context, "Service", xpath);
      if (el == null) el = WWXML.appendElementPath(context, "Service");
      el.setAttribute("serviceName", s);
    }

    WWXML.checkAndAppendBooleanElement(
        params, AVKey.RETRIEVE_PROPERTIES_FROM_SERVICE, context, "RetrievePropertiesFromService");

    // Image format properties.
    if (params.getValue(AVKey.PIXEL_TYPE) != null || params.getValue(AVKey.BYTE_ORDER) != null) {
      Element el = WWXML.getElement(context, "DataType", null);
      if (el == null) el = WWXML.appendElementPath(context, "DataType");

      s = params.getStringValue(AVKey.PIXEL_TYPE);
      if (s != null && s.length() > 0) {
        s = WWXML.dataTypeAsText(s);
        if (s != null && s.length() > 0) el.setAttribute("type", s);
      }

      s = params.getStringValue(AVKey.BYTE_ORDER);
      if (s != null && s.length() > 0) {
        s = WWXML.byteOrderAsText(s);
        if (s != null && s.length() > 0) el.setAttribute("byteOrder", s);
      }
    }

    // Elevation data properties.
    Element el = WWXML.appendElementPath(context, "ExtremeElevations");
    WWXML.checkAndAppendTextElement(params, AVKey.ELEVATION_EXTREMES_FILE, el, "FileName");

    Double d = AVListImpl.getDoubleValue(params, AVKey.ELEVATION_MAX);
    if (d != null) el.setAttribute("max", Double.toString(d));

    d = AVListImpl.getDoubleValue(params, AVKey.ELEVATION_MIN);
    if (d != null) el.setAttribute("min", Double.toString(d));

    return context;
  }