예제 #1
0
  public void testImportIntoDatabase() throws Exception {
    Catalog cat = getCatalog();

    DataStoreInfo ds = createH2DataStore(cat.getDefaultWorkspace().getName(), "spearfish");

    File dir = tmpDir();
    unpack("shape/archsites_epsg_prj.zip", dir);
    unpack("shape/bugsites_esri_prj.tar.gz", dir);

    ImportContext context = importer.createContext(new Directory(dir), ds);
    assertEquals(2, context.getTasks().size());

    ImportTask task1 = context.getTasks().get(0);
    ImportTask task2 = context.getTasks().get(1);

    assertEquals(ImportTask.State.READY, task1.getState());
    assertEquals(ImportTask.State.READY, task2.getState());
    // assertEquals(ImportTask.State.READY, context.getTasks().get(1).getState());

    // cannot ensure ordering of items
    HashSet resources = new HashSet();
    resources.add(task1.getLayer().getResource().getName());
    resources.add(task2.getLayer().getResource().getName());
    assertTrue(resources.contains("bugsites"));
    assertTrue(resources.contains("archsites"));

    importer.run(context);

    assertEquals(ImportTask.State.COMPLETE, task1.getState());
    assertEquals(ImportTask.State.COMPLETE, task2.getState());

    assertNotNull(cat.getLayerByName("archsites"));
    assertNotNull(cat.getLayerByName("bugsites"));

    assertNotNull(cat.getFeatureTypeByDataStore(ds, "archsites"));
    assertNotNull(cat.getFeatureTypeByDataStore(ds, "bugsites"));

    runChecks("archsites");
    runChecks("bugsites");
  }
예제 #2
0
  public void testImportDatabase() throws Exception {
    File dir = unpack("h2/cookbook.zip");

    Map params = new HashMap();
    params.put(H2DataStoreFactory.DBTYPE.key, "h2");
    params.put(H2DataStoreFactory.DATABASE.key, new File(dir, "cookbook").getAbsolutePath());

    ImportContext context = importer.createContext(new Database(params));
    assertEquals(3, context.getTasks().size());

    assertEquals(ImportTask.State.READY, context.getTasks().get(0).getState());
    assertEquals(ImportTask.State.READY, context.getTasks().get(1).getState());
    assertEquals(ImportTask.State.READY, context.getTasks().get(2).getState());

    Catalog cat = getCatalog();
    assertNull(cat.getDataStoreByName(cat.getDefaultWorkspace(), "cookbook"));
    assertNull(cat.getLayerByName("point"));
    assertNull(cat.getLayerByName("line"));
    assertNull(cat.getLayerByName("polygon"));

    importer.run(context);
    assertEquals(ImportTask.State.COMPLETE, context.getTasks().get(0).getState());
    assertEquals(ImportTask.State.COMPLETE, context.getTasks().get(1).getState());
    assertEquals(ImportTask.State.COMPLETE, context.getTasks().get(2).getState());

    assertNotNull(cat.getDataStoreByName(cat.getDefaultWorkspace(), "cookbook"));

    DataStoreInfo ds = cat.getDataStoreByName(cat.getDefaultWorkspace(), "cookbook");
    assertNotNull(cat.getFeatureTypeByDataStore(ds, "point"));
    assertNotNull(cat.getFeatureTypeByDataStore(ds, "line"));
    assertNotNull(cat.getFeatureTypeByDataStore(ds, "polygon"));
    assertNotNull(cat.getLayerByName("point"));
    assertNotNull(cat.getLayerByName("line"));
    assertNotNull(cat.getLayerByName("polygon"));

    runChecks("point");
    runChecks("line");
    runChecks("polygon");
  }
예제 #3
0
  public void testIntegerToDateTransform() throws Exception {
    Catalog cat = getCatalog();

    File dir = unpack("shape/archsites_epsg_prj.zip");

    SpatialFile file = new SpatialFile(new File(dir, "archsites.shp"));
    file.prepare();

    ImportContext context = importer.createContext(file, store);
    assertEquals(1, context.getTasks().size());

    context.setTargetStore(store);

    ImportTask task = context.getTasks().get(0);
    // this is a silly test - CAT_ID ranges from 1-25 and is not supposed to be a date
    // java date handling doesn't like dates in year 1
    task.getTransform().add(new IntegerFieldToDateTransform("CAT_ID"));
    importer.run(context);

    assertEquals(ImportContext.State.COMPLETE, context.getState());

    FeatureTypeInfo ft = cat.getFeatureTypeByDataStore(store, "archsites");
    assertNotNull(ft);

    SimpleFeatureType schema = (SimpleFeatureType) ft.getFeatureType();
    assertEquals(Timestamp.class, schema.getDescriptor("CAT_ID").getType().getBinding());

    FeatureIterator it = ft.getFeatureSource(null, null).getFeatures().features();
    int year = 2;
    Calendar cal = Calendar.getInstance();
    cal.setTimeZone(TimeZone.getTimeZone("UTC"));
    try {
      // make sure we have something
      assertTrue(it.hasNext());
      // the first date will be bogus due to java date limitation
      it.next();
      while (it.hasNext()) {
        SimpleFeature f = (SimpleFeature) it.next();
        // class will be timestamp
        cal.setTime((Date) f.getAttribute("CAT_ID"));
        assertEquals(year++, cal.get(Calendar.YEAR));
      }
    } finally {
      it.close();
    }
  }
예제 #4
0
  public void testDateFormatTransform() throws Exception {
    Catalog cat = getCatalog();

    File dir = unpack("shape/ivan.zip");

    SpatialFile file = new SpatialFile(new File(dir, "ivan.shp"));
    file.prepare();

    ImportContext context = importer.createContext(file, store);
    assertEquals(1, context.getTasks().size());

    context.setTargetStore(store);

    ImportTask task = context.getTasks().get(0);
    task.getTransform().add(new DateFormatTransform("timestamp", "yyyy-MM-dd HH:mm:ss.S"));

    importer.run(context);

    assertEquals(ImportContext.State.COMPLETE, context.getState());

    FeatureTypeInfo ft = cat.getFeatureTypeByDataStore(store, "ivan");
    assertNotNull(ft);

    SimpleFeatureType schema = (SimpleFeatureType) ft.getFeatureType();
    assertTrue(
        Date.class.isAssignableFrom(schema.getDescriptor("timestamp").getType().getBinding()));

    FeatureIterator it = ft.getFeatureSource(null, null).getFeatures().features();
    try {
      assertTrue(it.hasNext());
      while (it.hasNext()) {
        SimpleFeature f = (SimpleFeature) it.next();
        assertTrue(f.getAttribute("timestamp") instanceof Date);
      }
    } finally {
      it.close();
    }
  }
예제 #5
0
  public void testNumberFormatTransform() throws Exception {
    Catalog cat = getCatalog();

    File dir = unpack("shape/restricted.zip");

    SpatialFile file = new SpatialFile(new File(dir, "restricted.shp"));
    file.prepare();

    ImportContext context = importer.createContext(file, store);
    assertEquals(1, context.getTasks().size());

    context.setTargetStore(store);

    ImportTask task = context.getTasks().get(0);
    task.getTransform().add(new NumberFormatTransform("cat", Integer.class));
    importer.run(context);

    assertEquals(ImportContext.State.COMPLETE, context.getState());

    FeatureTypeInfo ft = cat.getFeatureTypeByDataStore(store, "restricted");
    assertNotNull(ft);

    SimpleFeatureType schema = (SimpleFeatureType) ft.getFeatureType();
    assertEquals(Integer.class, schema.getDescriptor("cat").getType().getBinding());

    FeatureIterator it = ft.getFeatureSource(null, null).getFeatures().features();
    try {
      assertTrue(it.hasNext());
      while (it.hasNext()) {
        SimpleFeature f = (SimpleFeature) it.next();
        assertTrue(f.getAttribute("cat") instanceof Integer);
      }
    } finally {
      it.close();
    }
  }
예제 #6
0
  public MockCatalogBuilder featureType(
      final String name,
      String srs,
      ProjectionPolicy projPolicy,
      ReferencedEnvelope envelope,
      ReferencedEnvelope latLonEnvelope) {

    String ftId = newId();
    final DataStoreInfo ds = dataStores.peekLast();
    NamespaceInfo ns = namespaces.peekLast();

    final FeatureTypeInfo ft = createNiceMock(FeatureTypeInfo.class);
    featureTypes.add(ft);

    initResource(
        ft, FeatureTypeInfo.class, ftId, name, ds, ns, srs, projPolicy, envelope, latLonEnvelope);

    expect(ft.getNumDecimals()).andReturn(8);

    // setup the property file data
    File propDir = new File(dataDirRoot, ds.getName());
    propDir.mkdirs();

    String fileName = name + ".properties";
    try {
      IOUtils.copy(getClass().getResourceAsStream(fileName), new File(propDir, fileName));
    } catch (IOException e) {
      throw new RuntimeException(e);
    }

    try {
      expect(ft.getFeatureType())
          .andAnswer(
              new IAnswer<FeatureType>() {
                @Override
                public FeatureType answer() throws Throwable {
                  return ((DataStore) ds.getDataStore(null)).getSchema(name);
                }
              })
          .anyTimes();
      expect(ft.getFeatureSource(null, null))
          .andAnswer(
              (IAnswer)
                  new IAnswer<FeatureSource>() {
                    @Override
                    public FeatureSource answer() throws Throwable {
                      return ((DataStore) ds.getDataStore(null)).getFeatureSource(name);
                    }
                  })
          .anyTimes();
    } catch (IOException e) {
    }

    expect(catalog.getFeatureTypeByName(or(eq(name), eq(ns.getPrefix() + ":" + name))))
        .andReturn(ft)
        .anyTimes();

    expect(
            catalog.getFeatureTypeByName(
                or(eq(new NameImpl(ns.getPrefix(), name)), eq(new NameImpl(ns.getURI(), name)))))
        .andReturn(ft)
        .anyTimes();
    expect(catalog.getFeatureTypeByName(ns, name)).andReturn(ft).anyTimes();

    expect(catalog.getFeatureTypeByName(ns.getPrefix(), name)).andReturn(ft).anyTimes();
    // expect(catalog.getFeatureTypeByName(or(eq(ns.getPrefix()), eq(ns.getURI())), name))
    //    .andReturn(ft).anyTimes();

    expect(catalog.getFeatureTypeByStore(ds, name)).andReturn(ft).anyTimes();
    expect(catalog.getFeatureTypeByDataStore(ds, name)).andReturn(ft).anyTimes();

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

    callback.onResource(name, ft, ds, this);
    replay(ft, createLayer(ft, name, ns));
    return this;
  }
예제 #7
0
  /**
   * Adds a vector layer to the catalog setup.
   *
   * <p>The layer is created within a store named <code>qName.getPrefix()</code>, creating it if it
   * does not exist. The resulting store is a {@link PropertyDataStore} that points at the directory
   * <code>getDataDirectoryRoot()/qName.getPrefix()</code>. Similarily the layer and store are
   * created within a workspace named <code>qName.getPrefix()</code>, which is created if it does
   * not already exist.
   *
   * <p>The properties data for the layer is copied from the classpath, with a file name of "<code>
   * filename</code>.properties". The <tt>scope</tt> parameter is used as the class from which to
   * load the properties file relative to.
   *
   * <p>The <tt>props</tt> parameter is used to define custom properties for the layer. See the
   * {@link LayerProperty} class for supported properties.
   */
  public void addVectorLayer(
      QName qName, Map<LayerProperty, Object> props, String filename, Class scope, Catalog catalog)
      throws IOException {
    String prefix = qName.getPrefix();
    String name = qName.getLocalPart();
    String uri = qName.getNamespaceURI();

    // configure workspace if it doesn;t already exist
    if (catalog.getWorkspaceByName(prefix) == null) {
      addWorkspace(prefix, uri, catalog);
    }

    // configure store if it doesn't already exist

    File storeDir = catalog.getResourceLoader().findOrCreateDirectory(prefix);

    DataStoreInfo store = catalog.getDataStoreByName(prefix);
    if (store == null) {
      store = catalog.getFactory().createDataStore();
      store.setName(prefix);
      store.setWorkspace(catalog.getWorkspaceByName(prefix));
      store.setEnabled(true);

      store.getConnectionParameters().put(PropertyDataStoreFactory.DIRECTORY.key, storeDir);
      store.getConnectionParameters().put(PropertyDataStoreFactory.NAMESPACE.key, uri);
      catalog.add(store);
    }

    // copy the properties file over

    catalog.getResourceLoader().copyFromClassPath(filename, new File(storeDir, filename), scope);

    // configure feature type
    FeatureTypeInfo featureType = catalog.getFactory().createFeatureType();
    featureType.setStore(store);
    featureType.setNamespace(catalog.getNamespaceByPrefix(prefix));
    featureType.setName(LayerProperty.NAME.get(props, name));
    featureType.setNativeName(FilenameUtils.getBaseName(filename));
    featureType.setTitle(name);
    featureType.setAbstract("abstract about " + name);

    Integer srs = LayerProperty.SRS.get(props, SRS.get(qName));
    if (srs == null) {
      srs = 4326;
    }
    featureType.setSRS("EPSG:" + srs);
    try {
      featureType.setNativeCRS(CRS.decode("EPSG:" + srs));
    } catch (Exception e) {
      LOGGER.warning("Failed to decode EPSG:" + srs + ", setting the native SRS to null");
    }
    featureType.setNumDecimals(8);
    featureType.getKeywords().add(new Keyword(name));
    featureType.setEnabled(true);
    featureType.setProjectionPolicy(
        LayerProperty.PROJECTION_POLICY.get(props, ProjectionPolicy.NONE));
    featureType.setLatLonBoundingBox(
        LayerProperty.LATLON_ENVELOPE.get(props, DEFAULT_LATLON_ENVELOPE));
    featureType.setNativeBoundingBox(LayerProperty.ENVELOPE.get(props, null));

    FeatureTypeInfo ft = catalog.getFeatureTypeByDataStore(store, name);
    LayerInfo layer = catalog.getLayerByName(new NameImpl(prefix, name));
    if (ft == null) {
      ft = featureType;
      catalog.add(featureType);
    } else {
      if (layer == null) {
        // handles the case of layer removed, but feature type not
        catalog.remove(ft);
        ft = featureType;
        catalog.add(featureType);
      } else {
        new CatalogBuilder(catalog).updateFeatureType(ft, featureType);
        catalog.save(ft);
      }
    }

    if (layer == null
        || !layer.getResource().getNamespace().equals(catalog.getNamespaceByPrefix(prefix))) {
      layer = catalog.getFactory().createLayer();
    }

    layer.setResource(ft);

    StyleInfo defaultStyle = null;
    if (LayerProperty.STYLE.get(props, null) != null) {
      defaultStyle = catalog.getStyleByName(LayerProperty.STYLE.get(props, null));
    } else {
      // look for a style matching the layer name
      defaultStyle = catalog.getStyleByName(name);
      if (defaultStyle == null) {
        // see if the resource exists and we just need to create it
        if (getClass().getResource(name + ".sld") != null) {
          addStyle(name, catalog);
          defaultStyle = catalog.getStyleByName(name);
        }
      }
    }

    if (defaultStyle == null) {
      defaultStyle = catalog.getStyleByName(DEFAULT_VECTOR_STYLE);
    }

    layer.getStyles().clear();
    layer.setDefaultStyle(defaultStyle);
    layer.setType(LayerInfo.Type.VECTOR);
    layer.setEnabled(true);

    if (layer.getId() == null) {
      catalog.add(layer);
    } else {
      catalog.save(layer);
    }
  }