Search in sources :

Example 51 with FeatureCollection

use of org.geotoolkit.storage.feature.FeatureCollection in project geotoolkit by Geomatys.

the class UnionDemo method buildFeatureUnionList.

/**
 * Build the second FeatureCollection usind createSimpleType2() as FeatureType.
 * It define 4 features with basic geometry and property.
 * @return FeatureCollection
 */
private static FeatureCollection buildFeatureUnionList() {
    try {
        type = createSimpleType2();
    } catch (FactoryException ex) {
        Logger.getLogger("org.geotoolkit.pending.demo.processing").log(Level.SEVERE, null, ex);
    }
    final FeatureCollection featureList = FeatureStoreUtilities.collection("", type);
    Feature myFeature1 = type.newInstance();
    LinearRing ring = geometryFactory.createLinearRing(new Coordinate[] { new Coordinate(4.0, 4.0), new Coordinate(4.0, 8.0), new Coordinate(7.0, 8.0), new Coordinate(7.0, 4.0), new Coordinate(4.0, 4.0) });
    myFeature1.setPropertyValue("id", "id-11");
    myFeature1.setPropertyValue("name", "feature11");
    myFeature1.setPropertyValue("color", "red");
    myFeature1.setPropertyValue("geom3", geometryFactory.createPolygon(ring, null));
    myFeature1.setPropertyValue("att", 20);
    featureList.add(myFeature1);
    Feature myFeature2 = type.newInstance();
    ring = geometryFactory.createLinearRing(new Coordinate[] { new Coordinate(7.0, 4.0), new Coordinate(7.0, 8.0), new Coordinate(9.0, 8.0), new Coordinate(9.0, 4.0), new Coordinate(7.0, 4.0) });
    myFeature2.setPropertyValue("id", "id-12");
    myFeature2.setPropertyValue("name", "feature12");
    myFeature2.setPropertyValue("color", "blue");
    myFeature2.setPropertyValue("geom3", geometryFactory.createPolygon(ring, null));
    myFeature2.setPropertyValue("att", 20);
    featureList.add(myFeature2);
    Feature myFeature3 = type.newInstance();
    ring = geometryFactory.createLinearRing(new Coordinate[] { new Coordinate(6.0, 2.0), new Coordinate(6.0, 4.0), new Coordinate(9.0, 4.0), new Coordinate(9.0, 2.0), new Coordinate(6.0, 2.0) });
    myFeature3.setPropertyValue("id", "id-13");
    myFeature3.setPropertyValue("name", "feature13");
    myFeature3.setPropertyValue("color", "grey");
    myFeature3.setPropertyValue("geom3", geometryFactory.createPolygon(ring, null));
    myFeature3.setPropertyValue("att", 10);
    featureList.add(myFeature3);
    Feature myFeature4 = type.newInstance();
    ring = geometryFactory.createLinearRing(new Coordinate[] { new Coordinate(4.0, 2.0), new Coordinate(4.0, 3.0), new Coordinate(5.0, 3.0), new Coordinate(5.0, 2.0), new Coordinate(4.0, 2.0) });
    myFeature4.setPropertyValue("id", "id-14");
    myFeature4.setPropertyValue("name", "feature14");
    myFeature4.setPropertyValue("color", "grey");
    myFeature4.setPropertyValue("geom3", geometryFactory.createPolygon(ring, null));
    myFeature4.setPropertyValue("att", 12);
    featureList.add(myFeature4);
    Feature myFeature5 = type.newInstance();
    ring = geometryFactory.createLinearRing(new Coordinate[] { new Coordinate(2.0, 5.0), new Coordinate(2.0, 6.0), new Coordinate(3.0, 6.0), new Coordinate(3.0, 5.0), new Coordinate(2.0, 5.0) });
    myFeature5.setPropertyValue("id", "id-15");
    myFeature5.setPropertyValue("name", "feature15");
    myFeature5.setPropertyValue("color", "grey");
    myFeature5.setPropertyValue("geom3", geometryFactory.createPolygon(ring, null));
    myFeature5.setPropertyValue("att", 12);
    featureList.add(myFeature5);
    return featureList;
}
Also used : FactoryException(org.opengis.util.FactoryException) FeatureCollection(org.geotoolkit.storage.feature.FeatureCollection) Coordinate(org.locationtech.jts.geom.Coordinate) LinearRing(org.locationtech.jts.geom.LinearRing) Feature(org.opengis.feature.Feature)

Example 52 with FeatureCollection

use of org.geotoolkit.storage.feature.FeatureCollection in project geotoolkit by Geomatys.

the class ShapefileDataStoreTest method testLoadAndVerify.

@Test
public void testLoadAndVerify() throws Exception {
    FeatureCollection features = loadFeatures(STATE_POP, new Query(NamesExt.create("statepop")));
    // FeatureCollection<SimpleFeatureType, SimpleFeature> features = loadFeaturesM2();
    int count = features.size();
    assertTrue("Have features", count > 0);
    // assertEquals("Number of Features loaded",49,features.size()); // FILE
    // (correct value)
    // assertEquals("Number of Features loaded",3, count); // JAR
    Feature firstFeature = firstFeature(features);
    FeatureType schema = firstFeature.getType();
    assertNotNull(FeatureExt.getDefaultGeometry(schema));
    assertEquals("Number of Attributes", 256, schema.getProperties(true).size());
    assertEquals("Value of statename is wrong", "Illinois", firstFeature.getPropertyValue("STATE_NAME"));
    assertEquals("Value of land area is wrong", 143986.61, ((Double) firstFeature.getPropertyValue("LAND_KM")).doubleValue(), 0.001);
}
Also used : FeatureType(org.opengis.feature.FeatureType) Query(org.geotoolkit.storage.feature.query.Query) FeatureCollection(org.geotoolkit.storage.feature.FeatureCollection) Feature(org.opengis.feature.Feature) Point(org.locationtech.jts.geom.Point) Test(org.junit.Test)

Example 53 with FeatureCollection

use of org.geotoolkit.storage.feature.FeatureCollection in project geotoolkit by Geomatys.

the class ShapefileDataStoreTest method testUpdating.

/**
 * Create a set of features, then remove every other one, updating the
 * remaining. Test for removal and proper update after reloading...
 */
@Test
public void testUpdating() throws Throwable {
    ShapefileFeatureStore sds = createDataStore();
    loadFeatures(sds);
    FeatureWriter writer = null;
    try {
        writer = sds.getFeatureWriter(new Query(sds.getNames().iterator().next()));
        while (writer.hasNext()) {
            Feature feat = writer.next();
            Byte b = (Byte) feat.getPropertyValue("b");
            if (b.byteValue() % 2 == 0) {
                writer.remove();
            } else {
                feat.setPropertyValue("b", new Byte((byte) -1));
            }
        }
    } finally {
        if (writer != null) {
            writer.close();
        }
    }
    FeatureCollection fc = loadFeatures(sds);
    assertEquals(10, fc.size());
    FeatureIterator i = fc.iterator();
    for (; i.hasNext(); ) {
        assertEquals(-1, ((Byte) (i.next()).getPropertyValue("b")).byteValue());
    }
    i.close();
}
Also used : FeatureIterator(org.geotoolkit.storage.feature.FeatureIterator) FeatureWriter(org.geotoolkit.storage.feature.FeatureWriter) Query(org.geotoolkit.storage.feature.query.Query) FeatureCollection(org.geotoolkit.storage.feature.FeatureCollection) Feature(org.opengis.feature.Feature) Test(org.junit.Test)

Example 54 with FeatureCollection

use of org.geotoolkit.storage.feature.FeatureCollection in project geotoolkit by Geomatys.

the class ShapefileReadWriteTest method test.

private void test(final FeatureType type, final FeatureCollection original, final File tmp, final ShapefileProvider maker, final boolean memorymapped, final Charset charset) throws IOException, MalformedURLException, Exception {
    ShapefileFeatureStore shapefile;
    GenericName typeName = type.getName();
    final ParameterValueGroup params = maker.getOpenParameters().createValue();
    params.parameter(ShapefileProvider.LOCATION).setValue(tmp.toURI());
    params.parameter(ShapefileProvider.MEMORY_MAPPED.getName().toString()).setValue(memorymapped);
    params.parameter(ShapefileProvider.DBFCHARSET.getName().toString()).setValue(charset);
    shapefile = (ShapefileFeatureStore) maker.open(params);
    shapefile.createFeatureType(type);
    Session session = shapefile.createSession(true);
    session.addFeatures(typeName.toString(), original);
    session.commit();
    assertFalse(session.hasPendingChanges());
    FeatureCollection copy = session.getFeatureCollection(new Query(typeName));
    compare(original, copy);
    // review open
    ShapefileFeatureStore review = new ShapefileFeatureStore(tmp.toURI(), memorymapped, charset);
    typeName = review.getNames().iterator().next();
    FeatureCollection again = review.createSession(true).getFeatureCollection(new Query(typeName));
    compare(copy, again);
    compare(original, again);
}
Also used : GenericName(org.opengis.util.GenericName) Query(org.geotoolkit.storage.feature.query.Query) ParameterValueGroup(org.opengis.parameter.ParameterValueGroup) FeatureCollection(org.geotoolkit.storage.feature.FeatureCollection) Session(org.geotoolkit.storage.feature.session.Session)

Example 55 with FeatureCollection

use of org.geotoolkit.storage.feature.FeatureCollection in project geotoolkit by Geomatys.

the class ShapefileTest method testHolyPolygons.

@Test
public void testHolyPolygons() throws Exception {
    final FeatureTypeBuilder ftb = new FeatureTypeBuilder();
    ftb.setName("test");
    ftb.addAttribute(MultiPolygon.class).setName("a").addRole(AttributeRole.DEFAULT_GEOMETRY);
    final FeatureType type = ftb.build();
    Collection<Feature> features = new ArrayList<>();
    File tmpFile = getTempFile();
    tmpFile.delete();
    // write features
    ShapefileProvider make = new ShapefileProvider();
    String pathId = ShapefileProvider.PATH.getName().getCode();
    FeatureStore s = (FeatureStore) make.create(Parameters.toParameter(Collections.singletonMap(pathId, tmpFile.toURI().toURL()), make.getOpenParameters()));
    s.createFeatureType(type);
    GenericName typeName = type.getName();
    Session session = s.createSession(true);
    session.addFeatures(typeName.toString(), features);
    session.commit();
    s = new ShapefileFeatureStore(tmpFile.toURI());
    typeName = s.getNames().iterator().next();
    FeatureCollection fc = s.createSession(true).getFeatureCollection(new Query(typeName));
    ShapefileReadWriteTest.compare(features, fc);
}
Also used : FeatureTypeBuilder(org.apache.sis.feature.builder.FeatureTypeBuilder) FeatureType(org.opengis.feature.FeatureType) Query(org.geotoolkit.storage.feature.query.Query) ArrayList(java.util.ArrayList) Feature(org.opengis.feature.Feature) GenericName(org.opengis.util.GenericName) FeatureCollection(org.geotoolkit.storage.feature.FeatureCollection) File(java.io.File) FeatureStore(org.geotoolkit.storage.feature.FeatureStore) Session(org.geotoolkit.storage.feature.session.Session) Test(org.junit.Test)

Aggregations

FeatureCollection (org.geotoolkit.storage.feature.FeatureCollection)131 Feature (org.opengis.feature.Feature)75 Test (org.junit.Test)49 Coordinate (org.locationtech.jts.geom.Coordinate)49 LinearRing (org.locationtech.jts.geom.LinearRing)39 Query (org.geotoolkit.storage.feature.query.Query)31 FeatureType (org.opengis.feature.FeatureType)27 FeatureIterator (org.geotoolkit.storage.feature.FeatureIterator)24 LineString (org.locationtech.jts.geom.LineString)23 MultiPoint (org.locationtech.jts.geom.MultiPoint)16 ProcessDescriptor (org.geotoolkit.process.ProcessDescriptor)15 ParameterValueGroup (org.opengis.parameter.ParameterValueGroup)15 Geometry (org.locationtech.jts.geom.Geometry)14 AbstractProcessTest (org.geotoolkit.processing.vector.AbstractProcessTest)13 Session (org.geotoolkit.storage.feature.session.Session)12 CheckCloseFeatureIterator (org.geotoolkit.storage.feature.CheckCloseFeatureIterator)10 Point (org.locationtech.jts.geom.Point)10 GenericName (org.opengis.util.GenericName)10 NoSuchElementException (java.util.NoSuchElementException)8 File (java.io.File)7