private void adjustTileMatrixSetInfo(LayerDefinition layerDefinition, Map<String, ?> layerObj) {

    if (layerObj == null) {
      return;
    }

    Map<String, ?> contents = (Map<String, ?>) layerObj.get("contents");
    if (contents == null) {
      return;
    }

    /* find REST url information for selected layer */
    /* tileMatrixSetData may contain other layer specs as well */

    Object rawLayers = contents.get("layers");
    if (rawLayers == null) {
      return;
    }

    Map<String, ?> wmtsLayerSpec = null;
    if (rawLayers instanceof List) {
      String layerName = layerDefinition.getWmsname();
      List<Map<String, ?>> wmtsLayers = (List<Map<String, ?>>) rawLayers;

      for (Map<String, ?> wmtsLayerListEl : wmtsLayers) {
        if (!layerName.equals(wmtsLayerListEl.get("identifier"))) {
          continue;
        }

        wmtsLayerSpec = wmtsLayerListEl;
        break;
      }

    } else {
      wmtsLayerSpec = (Map<String, ?>) rawLayers;
    }

    if (wmtsLayerSpec == null) {
      return;
    }

    /* */
    {
      Map<String, ?> resourceUrl = (Map<String, ?>) wmtsLayerSpec.get("resourceUrl");
      if (resourceUrl == null) {
        return;
      }
      Map<String, ?> resourceTileInfo = (Map<String, ?>) wmtsLayerSpec.get("tile");
      if (resourceTileInfo != null) {

        String format = (String) resourceTileInfo.get("format");
        if (format != null) {
          layerDefinition.setFormat(format);
          String template = (String) resourceTileInfo.get("template");
          if (template != null) {
            layerDefinition.setUrlTemplate(format, template);
          }
        }
      }
    }

    List<Map<String, ?>> resourceUrls = (List<Map<String, ?>>) wmtsLayerSpec.get("resourceUrls");
    if (resourceUrls == null) {
      return;
    }

    for (Map<String, ?> resourceTileInfo : resourceUrls) {
      String format = (String) resourceTileInfo.get("format");
      if (format != null) {
        if (layerDefinition.getFormat() == null) {
          layerDefinition.setFormat(format);
        }
        String template = (String) resourceTileInfo.get("template");
        if (template != null) {
          layerDefinition.setUrlTemplate(format, template);
        }
      }
    }

    /*
     * "resourceUrl": { "tile": { "format": "image/png", "template":
     * "http://karttamoottori.maanmittauslaitos.fi/maasto/wmts/1.0.0/taustakartta/default/{TileMatrixSet}/{TileMatrix}/{TileRow}/{TileCol}.png"
     * , "resourceType": "tile" } }, "resourceUrls": [ { "format":
     * "image/png", "template":
     * "http://karttamoottori.maanmittauslaitos.fi/maasto/wmts/1.0.0/taustakartta/default/{TileMatrixSet}/{TileMatrix}/{TileRow}/{TileCol}.png"
     * , "resourceType": "tile" } ]
     */

  }
  @SuppressWarnings({"rawtypes", "unchecked"})
  protected Map<String, LayerDefinition> parseLayersFromJSON(Map<String, ?> obj)
      throws IOException, ParseException {
    Map<String, LayerDefinition> layerDefs = new HashMap<String, LayerDefinition>();

    final Set<String> cacheExclusions = new HashSet<String>();
    final String exclusions = ConfigValue.LAYER_CACHE_EXCLUDE.getConfigProperty(props);
    if (exclusions != null) {
      String[] parts = exclusions.split(",");
      if (parts != null) {
        for (String p : parts) {
          cacheExclusions.add(p);
        }
      }
    }

    final List<Map<String, ?>> layerList = (List<Map<String, ?>>) obj.get("layers");

    for (Map<String, ?> layerObj : layerList) {

      String type = (String) layerObj.get("type");

      if (!("wfslayer".equals(type)
          || "wmslayer".equals(type)
          || "statslayer".equals(type)
          || "base".equals(type)
          || "groupMap".equals(type)
          || "myplaces".equals(type)
          || "geojson".equals(type)
          || "wmtslayer".equals(type))) {
        continue;
      }

      Object xolayerid = layerObj.get("id");
      String layerid = xolayerid.toString();
      String wmsname = (String) layerObj.get("wmsName");
      Number minScale = (Number) layerObj.get("minScale");
      Number maxScale = (Number) layerObj.get("maxScale");
      String wmsurl = (String) layerObj.get("wmsUrl");
      List<Map<String, ?>> tiles = (List<Map<String, ?>>) layerObj.get("tiles");

      LayerDefinition layerDefinition = new LayerDefinition();

      if (maxScale != null) {
        layerDefinition.setMaxScale(maxScale.doubleValue());
      }
      if (minScale != null) {
        layerDefinition.setMinScale(minScale.doubleValue());
      }
      layerDefinition.setWmsname(wmsname);
      layerDefinition.setWmsurl(fixWmsUrl(wmsurl));
      layerDefinition.setLayerid(layerid);
      layerDefinition.setLayerType(type);
      layerDefinition.setTiles(tiles);

      if (isMyPlacesType(layerDefinition)) {
        layerDefinition.setCacheable(false);
      } else if (tiles != null) {
        layerDefinition.setCacheable(false);
      } else {
        if (cacheExclusions.contains(layerid)) {
          layerDefinition.setCacheable(false);
        }
      }

      layerDefinition.setTileMatrixSetId((String) layerObj.get("tileMatrixSetId"));

      adjustLayerWmsNameAndUrl(layerDefinition);

      if (isMyPlacesType(layerDefinition)) {
        layerDefinition.setCredentials(
            ConfigValue.LAYERDEFINITION_CREDENTIALS_MYPLACES.getConfigProperty(props));
      }

      List<Map<String, ?>> subLayer = (List) layerObj.get("subLayer");
      if (subLayer != null) {
        for (Map<String, ?> subLayerObj : subLayer) {
          Object slolayerid = subLayerObj.get("id");
          String sllayerid = slolayerid.toString();
          String slwmsname = (String) subLayerObj.get("wmsName");
          Number slminScale = (Number) subLayerObj.get("minScale");
          Number slmaxScale = (Number) subLayerObj.get("maxScale");
          String slwmsurl = (String) subLayerObj.get("wmsUrl");
          String sltype = (String) subLayerObj.get("type");

          LayerDefinition subLayerDefinition = new LayerDefinition();

          if (slmaxScale != null) {
            subLayerDefinition.setMaxScale(slmaxScale.doubleValue());
          }
          if (slminScale != null) {
            subLayerDefinition.setMinScale(slminScale.doubleValue());
          }
          subLayerDefinition.setWmsname(slwmsname);
          subLayerDefinition.setWmsurl(fixWmsUrl(slwmsurl));
          subLayerDefinition.setLayerid(sllayerid);
          subLayerDefinition.setLayerType(sltype != null ? sltype : type);
          subLayerDefinition.setFormat(layerDefinition.getFormat());
          adjustLayerWmsNameAndUrl(subLayerDefinition);
          adjustStyles(subLayerDefinition, subLayerObj);
          adjustGeom(subLayerDefinition, subLayerObj);
          adjustData(subLayerDefinition, subLayerObj);
          /* format selection by default or by spec for wmts */
          if ("wmtslayer".equals(type)) {
            adjustTileMatrixSetInfo(
                subLayerDefinition, (Map<String, ?>) subLayerObj.get("tileMatrixSetData"));
          } else {
            subLayerDefinition.setFormat("image/png");
          }

          layerDefinition.getSubLayers().add(subLayerDefinition);
        }
      }

      adjustStyles(layerDefinition, layerObj);
      adjustGeom(layerDefinition, layerObj);

      adjustData(layerDefinition, layerObj);

      /* format selection by default or by spec for wmts */
      if ("wmtslayer".equals(type)) {
        adjustTileMatrixSetInfo(
            layerDefinition, (Map<String, ?>) layerObj.get("tileMatrixSetData"));
      } else {
        layerDefinition.setFormat("image/png");
      }

      layerDefs.put(layerDefinition.getLayerid(), layerDefinition);
    }

    return layerDefs;
  }