Esempio n. 1
0
  public void testImportCSV() throws Exception {
    File dir = unpack("csv/locations.zip");
    ImportContext context = importer.createContext(new SpatialFile(new File(dir, "locations.csv")));
    assertEquals(1, context.getTasks().size());

    ImportTask task = context.getTasks().get(0);
    assertEquals(ImportTask.State.NO_CRS, task.getState());

    LayerInfo layer = task.getLayer();
    ResourceInfo resource = layer.getResource();
    resource.setSRS("EPSG:4326");

    assertTrue("Item not ready", importer.prep(task));
    assertEquals(ImportTask.State.READY, task.getState());

    context.updated();
    assertEquals(ImportContext.State.PENDING, context.getState());
    importer.run(context);
    assertEquals(ImportContext.State.COMPLETE, context.getState());
    FeatureTypeInfo fti = (FeatureTypeInfo) resource;
    SimpleFeatureType featureType = (SimpleFeatureType) fti.getFeatureType();
    GeometryDescriptor geometryDescriptor = featureType.getGeometryDescriptor();
    assertNull("Expecting no geometry", geometryDescriptor);
    assertEquals(4, featureType.getAttributeCount());
  }
Esempio n. 2
0
  public void testImportCSVIndirect() throws Exception {
    File dir = unpack("csv/locations.zip");
    String wsName = getCatalog().getDefaultWorkspace().getName();

    DataStoreInfo h2DataStore = createH2DataStore(wsName, "csvindirecttest");
    SpatialFile importData = new SpatialFile(new File(dir, "locations.csv"));

    ImportContext context = importer.createContext(importData, h2DataStore);
    assertEquals(1, context.getTasks().size());
    ImportTask task = context.getTasks().get(0);

    TransformChain transformChain = task.getTransform();
    transformChain.add(new AttributesToPointGeometryTransform("LAT", "LON"));
    assertEquals(ImportTask.State.NO_CRS, task.getState());

    LayerInfo layer = task.getLayer();
    ResourceInfo resource = layer.getResource();
    resource.setSRS("EPSG:4326");

    assertTrue("Item not ready", importer.prep(task));
    assertEquals(ImportTask.State.READY, task.getState());

    context.updated();
    assertEquals(ImportContext.State.PENDING, context.getState());
    importer.run(context);

    assertEquals(ImportContext.State.COMPLETE, context.getState());
    FeatureTypeInfo fti = (FeatureTypeInfo) resource;
    SimpleFeatureType featureType = (SimpleFeatureType) fti.getFeatureType();
    GeometryDescriptor geometryDescriptor = featureType.getGeometryDescriptor();
    assertNotNull("Expecting geometry", geometryDescriptor);
    assertEquals("Invalid geometry name", "location", geometryDescriptor.getLocalName());
    assertEquals(3, featureType.getAttributeCount());
    FeatureSource<? extends FeatureType, ? extends Feature> featureSource =
        fti.getFeatureSource(null, null);
    FeatureCollection<? extends FeatureType, ? extends Feature> features =
        featureSource.getFeatures();
    assertEquals(9, features.size());
    FeatureIterator<? extends Feature> featureIterator = features.features();
    assertTrue("Expected features", featureIterator.hasNext());
    SimpleFeature feature = (SimpleFeature) featureIterator.next();
    assertNotNull(feature);
    assertEquals("Invalid city attribute", "Trento", feature.getAttribute("CITY"));
    assertEquals("Invalid number attribute", 140, feature.getAttribute("NUMBER"));
    Object geomAttribute = feature.getAttribute("location");
    assertNotNull("Expected geometry", geomAttribute);
    Point point = (Point) geomAttribute;
    Coordinate coordinate = point.getCoordinate();
    assertEquals("Invalid x coordinate", 11.12, coordinate.x, 0.1);
    assertEquals("Invalid y coordinate", 46.07, coordinate.y, 0.1);
    featureIterator.close();
  }
 private JSONObj resource(JSONObj json, ResourceInfo info, boolean details) {
   json.put("name", info.getName()).put("workspace", info.getStore().getWorkspace().getName());
   if (details) {
     if (info instanceof FeatureTypeInfo) {
       FeatureTypeInfo data = (FeatureTypeInfo) info;
       try {
         IO.schema(json.putObject("schema"), data.getFeatureType(), false);
       } catch (IOException e) {
       }
     } else if (info instanceof CoverageInfo) {
       CoverageInfo data = (CoverageInfo) info;
       IO.schemaGrid(json.putObject("schema"), data, false);
     }
   }
   return json;
 }
Esempio n. 4
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();
    }
  }
Esempio n. 5
0
 /**
  * Returns the appropriate icon for the specified layer. This one distinguishes the geometry type
  * inside vector layers.
  *
  * @param info
  * @return
  */
 public ResourceReference getSpecificLayerIcon(LayerInfo info) {
   if (info.getType() == Type.RASTER) {
     return RASTER_ICON;
   } else if (info.getType() == Type.VECTOR) {
     try {
       FeatureTypeInfo fti = (FeatureTypeInfo) info.getResource();
       GeometryDescriptor gd = fti.getFeatureType().getGeometryDescriptor();
       return getVectoryIcon(gd);
     } catch (Exception e) {
       return GEOMETRY_ICON;
     }
   } else if (info.getType() == Type.WMS) {
     return MAP_ICON;
   } else {
     return UNKNOWN_ICON;
   }
 }
Esempio n. 6
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();
    }
  }
Esempio n. 7
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();
    }
  }
Esempio n. 8
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;
  }
Esempio n. 9
0
  public ValueCollectionType run(GetPropertyValueType request) throws WFSException {
    // check the request resolve
    if (request.isSetResolve() && !ResolveValueType.NONE.equals(request.getResolve())) {
      throw new WFSException(request, "Only resolve = none is supported", "InvalidParameterValue")
          .locator("resolve");
    }

    if (request.getValueReference() == null) {
      throw new WFSException(request, "No valueReference specified", "MissingParameterValue")
          .locator("valueReference");
    }

    // do a getFeature request
    GetFeatureType getFeature = Wfs20Factory.eINSTANCE.createGetFeatureType();
    getFeature.getAbstractQueryExpression().add(request.getAbstractQueryExpression());

    FeatureCollectionType fc =
        (FeatureCollectionType) delegate.run(GetFeatureRequest.adapt(getFeature)).getAdaptee();

    QueryType query = (QueryType) request.getAbstractQueryExpression();
    QName typeName = (QName) query.getTypeNames().iterator().next();
    FeatureTypeInfo featureType =
        catalog.getFeatureTypeByName(typeName.getNamespaceURI(), typeName.getLocalPart());

    try {
      // look for the attribute type
      AttributeTypeInfo attribute = null;
      for (AttributeTypeInfo at : featureType.attributes()) {
        if (at.getName().equals(request.getValueReference())) {
          attribute = at;
          break;
        }
      }
      if (attribute == null) {
        throw new WFSException(request, "No such attribute: " + request.getValueReference());
      }

      AttributeDescriptor descriptor = attribute.getAttribute();
      if (descriptor == null) {
        PropertyDescriptor pd = featureType.getFeatureType().getDescriptor(attribute.getName());
        if (pd instanceof AttributeDescriptor) {
          descriptor = (AttributeDescriptor) pd;
        }
      }

      if (descriptor == null) {
        throw new WFSException(request, "Unable to obtain descriptor for " + attribute.getName());
      }

      // create value collection type from feature collection
      ValueCollectionType vc = Wfs20Factory.eINSTANCE.createValueCollectionType();
      vc.setTimeStamp(fc.getTimeStamp());
      vc.setNumberMatched(fc.getNumberMatched());
      vc.setNumberReturned(fc.getNumberReturned());
      vc.getMember().add(new PropertyValueCollection(fc.getMember().iterator().next(), descriptor));
      // TODO: next/previous but point back at GetPropertyValue
      // vc.setNext(fc.getNext());
      // vc.setPrevious(fc.getPrevious());
      return vc;
    } catch (IOException e) {
      throw new WFSException(request, e);
    }
  }