Exemplo n.º 1
0
  public void testImportKMLIndirect() throws Exception {
    File dir = unpack("kml/sample.zip");
    String wsName = getCatalog().getDefaultWorkspace().getName();
    DataStoreInfo h2DataStore = createH2DataStore(wsName, "kmltest");
    SpatialFile importData = new SpatialFile(new File(dir, "sample.kml"));
    ImportContext context = importer.createContext(importData, h2DataStore);
    assertEquals(1, context.getTasks().size());
    ImportTask task = context.getTasks().get(0);

    LayerInfo layer = task.getLayer();
    ResourceInfo resource = layer.getResource();
    assertEquals("Invalid srs", "EPSG:4326", resource.getSRS());
    ReferencedEnvelope emptyBounds = new ReferencedEnvelope();
    emptyBounds.setToNull();
    assertTrue("Unexpected bounding box", emptyBounds.equals(resource.getNativeBoundingBox()));
    // transform chain to limit characters
    // otherwise we get a sql exception thrown
    TransformChain transformChain = task.getTransform();
    transformChain.add(new DescriptionLimitingTransform());
    importer.run(context);
    Exception error = task.getError();
    if (error != null) {
      error.printStackTrace();
      fail(error.getMessage());
    }
    assertFalse("Bounding box not updated", emptyBounds.equals(resource.getNativeBoundingBox()));
    FeatureTypeInfo fti = (FeatureTypeInfo) resource;
    assertEquals("Invalid type name", "sample", fti.getName());
    FeatureSource<? extends FeatureType, ? extends Feature> featureSource =
        fti.getFeatureSource(null, null);
    assertEquals("Unexpected feature count", 20, featureSource.getCount(Query.ALL));
  }
Exemplo 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();
  }
Exemplo n.º 3
0
 TransformChain transformChain(JSONObject json) throws IOException {
   String type = json.getString("type");
   TransformChain chain = null;
   if ("vector".equalsIgnoreCase(type) || "VectorTransformChain".equalsIgnoreCase(type)) {
     chain = new VectorTransformChain();
   } else if ("raster".equalsIgnoreCase(type) || "RasterTransformChain".equalsIgnoreCase(type)) {
     chain = new RasterTransformChain();
   } else {
     throw new IOException("Unable to parse transformChain of type " + type);
   }
   JSONArray transforms = json.getJSONArray("transforms");
   for (int i = 0; i < transforms.size(); i++) {
     chain.add(transform(transforms.getJSONObject(i)));
   }
   return chain;
 }