@Test
  public void testReprojectLayerGroup()
      throws NoSuchAuthorityCodeException, FactoryException, Exception {

    Catalog catalog = getCatalog();

    CatalogBuilder cb = new CatalogBuilder(catalog);
    LayerGroupInfo lg = catalog.getFactory().createLayerGroup();
    LayerInfo l = catalog.getLayerByName(getLayerId(MockData.ROAD_SEGMENTS));
    lg.getLayers().add(l);
    lg.setName("test-reproject");

    // Give our layer a CRS without the EPSG code defined
    CoordinateReferenceSystem lCrs = DefaultGeographicCRS.WGS84;
    ((FeatureTypeInfo) l.getResource()).setSRS(null);
    ((FeatureTypeInfo) l.getResource()).setNativeCRS(lCrs);
    assertNull(CRS.lookupEpsgCode(lCrs, false));

    // EPSG:4326 should have an EPSG code
    CoordinateReferenceSystem lgCrs = CRS.decode("EPSG:4326");
    assertNotNull(CRS.lookupEpsgCode(lgCrs, false));

    // Reproject our layer group to EPSG:4326. We expect it to have an EPSG code.
    cb.calculateLayerGroupBounds(lg, lgCrs);
    assertNotNull(CRS.lookupEpsgCode(lg.getBounds().getCoordinateReferenceSystem(), false));
  }
Exemplo n.º 2
0
  /**
   * This method returns LayerInfo for specified grid coordinates
   *
   * @param x
   * @param y
   * @return
   */
  public LayerInfo getLayerInfoByCoords(int x, int y) {
    for (LayerInfo layerInfo : layers) {
      if (layerInfo.getX() == x && layerInfo.getY() == y) return layerInfo;
    }

    return null;
  }
Exemplo n.º 3
0
  private void removeStyleInLayerGroup(LayerGroupInfo group, StyleInfo style) {
    boolean dirty = false;

    // root layer style
    if (style.equals(group.getRootLayerStyle())) {
      group.setRootLayerStyle(getResourceDefaultStyle(group.getRootLayer().getResource(), style));
      dirty = true;
    }

    // layer styles
    List<StyleInfo> styles = group.getStyles();
    for (int i = 0; i < styles.size(); i++) {
      StyleInfo publishedStyle = styles.get(i);
      if (publishedStyle != null && publishedStyle.equals(style)) {
        // if publishedStyle is not null, we have a layer
        LayerInfo layer = (LayerInfo) group.getLayers().get(i);

        if (!layer.getDefaultStyle().equals(style)) {
          // use default style
          styles.set(i, layer.getDefaultStyle());
        } else {
          styles.set(i, getResourceDefaultStyle(layer.getResource(), style));
        }

        dirty = true;
      }
    }

    if (dirty) {
      catalog.save(group);
    }
  }
Exemplo n.º 4
0
  public MockCatalogBuilder layerGroup(
      String name, List<String> layerNames, List<String> styleNames) {

    final LayerGroupInfo lg = createMock(LayerGroupInfo.class);
    layerGroups.add(lg);

    expect(lg.getId()).andReturn(newId()).anyTimes();
    expect(lg.getName()).andReturn(name).anyTimes();

    List<PublishedInfo> grpLayers = new ArrayList<PublishedInfo>();
    List<StyleInfo> grpStyles = new ArrayList<StyleInfo>();
    for (int i = 0; i < layerNames.size(); i++) {
      String layerName = layerNames.get(i);
      LayerInfo l = null;
      for (LayerInfo layer : layers) {
        if (layerName.equals(layer.getName())) {
          l = layer;
          break;
        }
      }

      if (l == null) {
        throw new RuntimeException("No such layer: " + layerName);
      }

      grpLayers.add(l);

      StyleInfo s = null;
      if (styleNames != null) {
        String styleName = styleNames.get(i);
        for (StyleInfo style : styles) {
          if (styleName.equals(style.getName())) {
            s = style;
            break;
          }
        }
      }

      grpStyles.add(s);
    }
    expect(lg.getLayers()).andReturn(grpLayers).anyTimes();
    expect(lg.getStyles()).andReturn(grpStyles).anyTimes();

    lg.accept((CatalogVisitor) anyObject());
    expectLastCall()
        .andAnswer(
            new VisitAnswer() {
              @Override
              protected void doVisit(CatalogVisitor visitor) {
                visitor.visit(lg);
              }
            })
        .anyTimes();

    expect(catalog.getLayerGroupByName(name)).andReturn(lg).anyTimes();

    callback.onLayerGroup(name, lg, this);
    replay(lg);
    return this;
  }
Exemplo n.º 5
0
  /**
   * This method returns all LayerTypes used in this model
   *
   * @return
   */
  public Set<String> getLayerTypes() {
    Set<String> set = new LinkedHashSet<>();

    for (LayerInfo layerInfo : layers) {
      set.add(layerInfo.getLayerType());
    }
    return set;
  }
Exemplo n.º 6
0
  public Set<LayerInfo> getLayersByType(String layerType) {
    Set<LayerInfo> set = new LinkedHashSet<>();

    for (LayerInfo layerInfo : layers) {
      if (layerInfo.getLayerType().equals(layerType)) set.add(layerInfo);
    }

    return set;
  }
Exemplo n.º 7
0
  LayerInfo createLayer(ResourceInfo r, String name, NamespaceInfo ns) {
    String lId = newId();
    StyleInfo s = styles.peekLast();

    final LayerInfo l = createNiceMock(LayerInfo.class);
    layers.add(l);

    expect(l.getId()).andReturn(lId).anyTimes();
    expect(l.getName()).andReturn(name).anyTimes();
    expect(l.getType()).andReturn(LayerInfo.Type.VECTOR).anyTimes();
    expect(l.getResource()).andReturn(r).anyTimes();
    expect(l.getDefaultStyle()).andReturn(s).anyTimes();
    expect(l.isEnabled()).andReturn(true).anyTimes();
    expect(l.isAdvertised()).andReturn(true).anyTimes();

    expect(catalog.getLayer(lId)).andReturn(l).anyTimes();
    expect(catalog.getLayerByName(name)).andReturn(l).anyTimes();
    expect(catalog.getLayerByName(ns.getPrefix() + ":" + name)).andReturn(l).anyTimes();
    expect(catalog.getLayerByName(new NameImpl(ns.getPrefix(), name))).andReturn(l).anyTimes();
    expect(catalog.getLayerByName(new NameImpl(ns.getURI(), name))).andReturn(l).anyTimes();
    expect(catalog.getLayers(r)).andReturn(Arrays.asList(l)).anyTimes();
    l.accept((CatalogVisitor) anyObject());
    expectLastCall()
        .andAnswer(
            new VisitAnswer() {
              @Override
              protected void doVisit(CatalogVisitor visitor) {
                visitor.visit(l);
              }
            })
        .anyTimes();

    callback.onLayer(name, l, this);
    return l;
  }
  void visitStore(StoreInfo store) {
    // drill down into layers (into resources since we cannot scan layers)
    List<ResourceInfo> resources = catalog.getResourcesByStore(store, ResourceInfo.class);
    for (ResourceInfo ri : resources) {
      List<LayerInfo> layers = catalog.getLayers(ri);
      for (LayerInfo li : layers) {
        li.accept(this);
      }
    }

    catalog.remove(store);
  }
  public void visit(LayerInfo layer) {
    // first update the groups, remove the layer, and if no
    // other layers remained, remove the group as well
    for (LayerGroupInfo group : catalog.getLayerGroups()) {
      if (group.getLayers().contains(layer)) {
        // parallel remove of layer and styles
        int index = group.getLayers().indexOf(layer);
        group.getLayers().remove(index);
        group.getStyles().remove(index);

        // either update or remove the group
        if (group.getLayers().size() == 0) {
          catalog.remove(group);
        } else {
          catalog.save(group);
        }
      }
    }

    // remove the layer and (for the moment) its resource as well
    // TODO: change this to just remove the resource once the
    // resource/publish split is done
    ResourceInfo resource = layer.getResource();
    catalog.remove(layer);
    catalog.remove(resource);
  }
Exemplo n.º 10
0
 /**
  * This method maps given layer into model coordinate space
  *
  * @param layer
  */
 public synchronized void addLayer(@NonNull LayerInfo layer) {
   if (!layers.contains(layer)) {
     layer.setId(counter);
     this.layers.add(layer);
     counter++;
   }
 }
Exemplo n.º 11
0
  void visitStore(StoreInfo store) {
    // drill down into layers (into resources since we cannot scan layers)
    List<ResourceInfo> resources = catalog.getResourcesByStore(store, ResourceInfo.class);
    for (ResourceInfo ri : resources) {
      List<LayerInfo> layers = catalog.getLayers(ri);
      if (!layers.isEmpty()) {
        for (LayerInfo li : layers) {
          li.accept(this);
        }
      } else {
        // no layers for the resource, delete directly
        ri.accept(this);
      }
    }

    catalog.remove(store);
  }
Exemplo n.º 12
0
  private void removeStyleInLayer(LayerInfo layer, StyleInfo style) {
    boolean dirty = false;

    // remove it from the associated styles
    if (layer.getStyles().remove(style)) {
      dirty = true;
    }

    // if it's the default style, choose an associated style or reset it to the default one
    StyleInfo ds = layer.getDefaultStyle();
    if (ds != null && ds.equals(style)) {
      dirty = true;

      StyleInfo newDefaultStyle;
      if (layer.getStyles().size() > 0) {
        newDefaultStyle = layer.getStyles().iterator().next();
        layer.getStyles().remove(newDefaultStyle);
      } else {
        newDefaultStyle = getResourceDefaultStyle(layer.getResource(), style);
      }

      layer.setDefaultStyle(newDefaultStyle);
    }

    if (dirty) {
      catalog.save(layer);
    }
  }
  public void visit(StyleInfo style) {
    // add users of this style among the related objects: layers
    List<LayerInfo> layer = catalog.getLayers();
    for (LayerInfo li : layer) {
      // if it's the default style, reset it to the default one
      if (li.getDefaultStyle().equals(style)) {
        try {
          li.setDefaultStyle(new CatalogBuilder(catalog).getDefaultStyle(li.getResource()));
          catalog.save(li);
        } catch (IOException e) {
          // we fall back on the default style (since we cannot roll back the
          // entire operation, no transactions in the catalog)
          LOGGER.log(
              Level.WARNING,
              "Could not find default style for resource "
                  + li.getResource()
                  + " resetting the default to null");
          li.setDefaultStyle(null);
        }
      }
      // remove it also from the associated styles
      if (li.getStyles().remove(style)) catalog.save(li);
    }

    // groups can also refer styles, reset each reference to the
    // associated layer default style
    List<LayerGroupInfo> groups = catalog.getLayerGroups();
    for (LayerGroupInfo group : groups) {
      List<StyleInfo> styles = group.getStyles();
      boolean dirty = false;
      for (int i = 0; i < styles.size(); i++) {
        StyleInfo si = styles.get(i);
        if (si != null && si.equals(style)) {
          styles.set(i, group.getLayers().get(i).getDefaultStyle());
          dirty = true;
        }
      }
      if (dirty) catalog.save(group);
    }

    // finally remove the style
    catalog.remove(style);
  }
Exemplo n.º 14
0
  /** Add model to TileMap */
  public final void addModel(final TileMapModel model) {
    int mapTileWidth = (int) model.getTileSize().x;
    int mapTileHeight = (int) model.getTileSize().y;

    for (int i = 0; i < model.getLayersCount(); i++) {

      final TileMapLayer layer = model.getLayer(i);

      int width = (int) Math.min(layer.mWidth, model.getMapSize().x);
      int height = (int) Math.min(layer.mHeight, model.getMapSize().y);

      LayerInfo layerInfo = addLayerInfo(new Vector2(width, height));

      for (int x = 0; x < width; x++) {
        for (int y = 0; y < height; y++) {
          final int tileId = layer.mData[x + y * width];
          if (tileId == 0) continue;

          final TileInfo tileInfo = model.getTileInfo(tileId);

          // Prepare Buffer
          layerInfo.prepareBuffer(tileInfo);

          // Get Buffer
          LayerInfo.Buffer buff = layerInfo.getBuffer(tileInfo);

          // Get Rect Area
          RectF textureArea = tileInfo.getTextureArea();
          RectF textureAreaU =
              GeneralUtils.divideRect(textureArea, tileInfo.getTexture().getSize());

          Vector2 tileSize = tileInfo.getSize();
          final int tileWidth = (int) tileSize.x;
          final int tileHeight = (int) tileSize.y;

          // Put Element buffer
          buff.mElementsVertex.put(x * mapTileWidth);
          buff.mElementsVertex.put(y * mapTileHeight);
          buff.mElementsVertex.put(x * mapTileWidth + tileWidth);
          buff.mElementsVertex.put(y * mapTileHeight);
          buff.mElementsVertex.put(x * mapTileWidth + tileWidth);
          buff.mElementsVertex.put(y * mapTileHeight + tileHeight);
          buff.mElementsVertex.put(x * mapTileWidth);
          buff.mElementsVertex.put(y * mapTileHeight);
          buff.mElementsVertex.put(x * mapTileWidth);
          buff.mElementsVertex.put(y * mapTileHeight + tileHeight);
          buff.mElementsVertex.put(x * mapTileWidth + tileWidth);
          buff.mElementsVertex.put(y * mapTileHeight + tileHeight);

          // Put Texture Buffer
          buff.mTextureVertex.put(textureAreaU.left);
          buff.mTextureVertex.put(textureAreaU.top);
          buff.mTextureVertex.put(textureAreaU.right);
          buff.mTextureVertex.put(textureAreaU.top);
          buff.mTextureVertex.put(textureAreaU.right);
          buff.mTextureVertex.put(textureAreaU.bottom);
          buff.mTextureVertex.put(textureAreaU.left);
          buff.mTextureVertex.put(textureAreaU.top);
          buff.mTextureVertex.put(textureAreaU.left);
          buff.mTextureVertex.put(textureAreaU.bottom);
          buff.mTextureVertex.put(textureAreaU.right);
          buff.mTextureVertex.put(textureAreaU.bottom);
        }
      }

      layerInfo.pack();
    }
  }
  /**
   * Reads a REST service endpoint, determines what type of service it is if possible, and creates
   * and returns an array of corresponding LayerInfo objects if applicable.
   *
   * @param url the REST service endpoint URL.
   * @param useAsBasemap
   * @return an array of LayerInfo objects. The length of the array will be one unless it's a
   *     multi-layer service such as a feature service.
   */
  public static LayerInfo[] readService(URL url, boolean useAsBasemap) throws Exception {
    final String urlString = url.toString();
    StringBuilder urlSb = new StringBuilder(urlString);
    if (0 > urlString.indexOf('?')) {
      urlSb.append("?");
    } else {
      urlSb.append("&");
    }
    urlSb.append("f=json");
    url = new URL(urlSb.toString());
    BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
    StringBuilder contentSb = new StringBuilder();
    String line;
    while (null != (line = reader.readLine())) {
      contentSb.append(line);
    }

    JSONObject jsonObject = new JSONObject(contentSb.toString());
    LayerInfo layerInfo = useAsBasemap ? new BasemapLayerInfo((String) null) : new LayerInfo();
    layerInfo.setDatasetPath(urlString);
    layerInfo.setVisible(true);
    ArrayList<LayerInfo> layerInfos = new ArrayList<LayerInfo>();
    if (jsonObject.has("singleFusedMapCache")) {
      // It's a map service
      if ("true".equals(jsonObject.getString("singleFusedMapCache"))) {
        layerInfo.setLayerType(LayerType.TILED_MAP_SERVICE);
      } else {
        layerInfo.setLayerType(LayerType.DYNAMIC_MAP_SERVICE);
      }
    } else if (jsonObject.has("layers")) {
      // It's a feature service; get all the layers
      layerInfo = null;
      JSONArray layersArray = jsonObject.getJSONArray("layers");
      for (int i = 0; i < layersArray.length(); i++) {
        try {
          JSONObject layerJson = layersArray.getJSONObject(i);
          LayerInfo thisLayerInfo =
              useAsBasemap ? new BasemapLayerInfo((String) null) : new LayerInfo();
          StringBuilder datasetPath = new StringBuilder(urlString);
          if (!urlString.endsWith("/")) {
            datasetPath.append("/");
          }
          datasetPath.append(layerJson.getInt("id"));
          thisLayerInfo.setDatasetPath(datasetPath.toString());
          thisLayerInfo.setName(layerJson.getString("name"));
          thisLayerInfo.setVisible(true);
          thisLayerInfo.setLayerType(LayerType.FEATURE_SERVICE);
          layerInfos.add(thisLayerInfo);
        } catch (JSONException ex) {
          Logger.getLogger(RestServiceReader.class.getName()).log(Level.SEVERE, null, ex);
        }
      }
    } else if (jsonObject.has("drawingInfo")) {
      // It's a single feature service layer
      layerInfo.setDatasetPath(urlString);
      layerInfo.setName(jsonObject.getString("name"));
      layerInfo.setVisible(true);
      layerInfo.setLayerType(LayerType.FEATURE_SERVICE);
    } else if (jsonObject.has("pixelSizeX")) {
      // It's an image service
      layerInfo.setLayerType(LayerType.IMAGE_SERVICE);
    } else {
      throw new Exception("Unsupported service type: " + urlString);
    }
    if (null != layerInfo) {
      try {
        if (jsonObject.has("documentInfo")
            && jsonObject.getJSONObject("documentInfo").has("Title")) {
          layerInfo.setName(jsonObject.getJSONObject("documentInfo").getString("Title"));
        }
      } catch (JSONException ex) {
        Logger.getLogger(RestServiceReader.class.getName()).log(Level.SEVERE, null, ex);
      }
      if (null == layerInfo.getName() && jsonObject.has("mapName")) {
        try {
          layerInfo.setName(jsonObject.getString("mapName"));
        } catch (JSONException ex) {
          Logger.getLogger(RestServiceReader.class.getName()).log(Level.SEVERE, null, ex);
        }
      }
      if (null == layerInfo.getName() && jsonObject.has("name")) {
        try {
          layerInfo.setName(jsonObject.getString("name"));
        } catch (JSONException ex) {
          Logger.getLogger(RestServiceReader.class.getName()).log(Level.SEVERE, null, ex);
        }
      }
      layerInfos.add(layerInfo);
    }
    return layerInfos.toArray(new LayerInfo[layerInfos.size()]);
  }
  @Test
  public void modificationProxySerializeTest() throws Exception {
    Catalog catalog = getCatalog();

    // workspace
    WorkspaceInfo ws = catalog.getWorkspaceByName(MockData.CITE_PREFIX);
    WorkspaceInfo ws2 = serialize(ws);
    assertSame(ModificationProxy.unwrap(ws), ModificationProxy.unwrap(ws2));

    // namespace
    NamespaceInfo ns = catalog.getNamespaceByPrefix(MockData.CITE_PREFIX);
    NamespaceInfo ns2 = serialize(ns);
    assertSame(ModificationProxy.unwrap(ns), ModificationProxy.unwrap(ns2));

    // data store and related objects
    DataStoreInfo ds = catalog.getDataStoreByName(MockData.CITE_PREFIX);
    DataStoreInfo ds2 = serialize(ds);
    assertSame(ModificationProxy.unwrap(ds), ModificationProxy.unwrap(ds2));
    assertSame(
        ModificationProxy.unwrap(ds.getWorkspace()), ModificationProxy.unwrap(ds2.getWorkspace()));

    // coverage store and related objects
    CoverageStoreInfo cs = catalog.getCoverageStoreByName(MockData.TASMANIA_DEM.getLocalPart());
    CoverageStoreInfo cs2 = serialize(cs);
    assertSame(ModificationProxy.unwrap(cs), ModificationProxy.unwrap(cs2));
    assertSame(
        ModificationProxy.unwrap(cs.getWorkspace()), ModificationProxy.unwrap(cs2.getWorkspace()));

    // feature type and related objects
    FeatureTypeInfo ft = catalog.getFeatureTypeByName(getLayerId(MockData.BRIDGES));
    FeatureTypeInfo ft2 = serialize(ft);
    assertSame(ModificationProxy.unwrap(ft), ModificationProxy.unwrap(ft2));
    assertSame(ModificationProxy.unwrap(ft.getStore()), ModificationProxy.unwrap(ft2.getStore()));

    // coverage and related objects
    CoverageInfo ci = catalog.getCoverageByName(getLayerId(MockData.TASMANIA_DEM));
    CoverageInfo ci2 = serialize(ci);
    assertSame(ModificationProxy.unwrap(ci), ModificationProxy.unwrap(ci2));
    assertSame(ModificationProxy.unwrap(ci.getStore()), ModificationProxy.unwrap(ci.getStore()));

    // style
    StyleInfo streamsStyle = catalog.getStyleByName("Streams");
    StyleInfo si2 = serialize(streamsStyle);
    assertSame(ModificationProxy.unwrap(streamsStyle), ModificationProxy.unwrap(si2));

    // layer and related objects
    LayerInfo li = catalog.getLayerByName(getLayerId(MockData.BRIDGES));
    // ... let's add an extra style

    li.getStyles().add(streamsStyle);
    catalog.save(li);
    LayerInfo li2 = serialize(li);
    assertSame(ModificationProxy.unwrap(li), ModificationProxy.unwrap(li2));
    assertSame(
        ModificationProxy.unwrap(li.getResource()), ModificationProxy.unwrap(li2.getResource()));
    assertSame(
        ModificationProxy.unwrap(li.getDefaultStyle()),
        ModificationProxy.unwrap(li2.getDefaultStyle()));
    assertSame(
        ModificationProxy.unwrap(li.getStyles().iterator().next()),
        ModificationProxy.unwrap(li2.getStyles().iterator().next()));

    // try a group layer
    CatalogBuilder cb = new CatalogBuilder(catalog);
    LayerGroupInfo lg = catalog.getFactory().createLayerGroup();
    lg.getLayers().add(catalog.getLayerByName(getLayerId(MockData.ROAD_SEGMENTS)));
    lg.getLayers().add(catalog.getLayerByName(getLayerId(MockData.PONDS)));
    cb.calculateLayerGroupBounds(lg);
    lg.setName("test-lg");
    catalog.add(lg);
    // ... make sure we get a proxy
    lg = catalog.getLayerGroupByName("test-lg");
    if (lg instanceof SecuredLayerGroupInfo) {
      lg = ((SecuredLayerGroupInfo) lg).unwrap(LayerGroupInfo.class);
    }
    LayerGroupInfo lg2 = serialize(lg);
    assertSame(ModificationProxy.unwrap(lg), ModificationProxy.unwrap(lg2));
    assertSame(
        ModificationProxy.unwrap(lg.getLayers().get(0)),
        ModificationProxy.unwrap(lg2.getLayers().get(0)));
    assertSame(
        ModificationProxy.unwrap(lg.getLayers().get(1)),
        ModificationProxy.unwrap(lg2.getLayers().get(1)));

    // now check a half modified proxy
    LayerInfo lim = catalog.getLayerByName(getLayerId(MockData.BRIDGES));
    // ... let's add an extra style
    lim.setDefaultStyle(streamsStyle);
    lim.getStyles().add(streamsStyle);
    // clone and check
    LayerInfo lim2 = serialize(lim);
    assertSame(
        ModificationProxy.unwrap(lim.getDefaultStyle()),
        ModificationProxy.unwrap(lim2.getDefaultStyle()));
    assertSame(
        ModificationProxy.unwrap(lim.getStyles().iterator().next()),
        ModificationProxy.unwrap(lim2.getStyles().iterator().next()));

    // mess a bit with the metadata map too
    String key = "workspaceKey";
    lim.getMetadata().put(key, ws);
    LayerInfo lim3 = serialize(lim);
    assertSame(ModificationProxy.unwrap(lim), ModificationProxy.unwrap(lim3));
    assertSame(
        ModificationProxy.unwrap(lim.getMetadata().get(key)),
        ModificationProxy.unwrap(lim3.getMetadata().get(key)));
  }
Exemplo n.º 17
0
 /**
  * This method returns LayerInfo for specified layer name
  *
  * @param name
  * @return
  */
 public LayerInfo getLayerInfoByName(String name) {
   for (LayerInfo layerInfo : layers) {
     if (layerInfo.getName().equalsIgnoreCase(name)) return layerInfo;
   }
   return null;
 }