Search in sources :

Example 26 with SimpleFeatureCollection

use of org.geotools.data.simple.SimpleFeatureCollection in project GeoGig by boundlessgeo.

the class ExportOpTest method testExportFromHEAD.

@Test
public void testExportFromHEAD() throws Exception {
    Feature[] points = new Feature[] { points1, points2, points3 };
    for (Feature feature : points) {
        insert(feature);
    }
    geogig.command(AddOp.class).call();
    geogig.command(CommitOp.class).setAll(true).call();
    MemoryDataStore dataStore = new MemoryDataStore(pointsType);
    final String typeName = dataStore.getTypeNames()[0];
    SimpleFeatureSource featureSource = dataStore.getFeatureSource(typeName);
    SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;
    geogig.command(ExportOp.class).setFeatureStore(featureStore).setPath("HEAD:" + pointsName).call();
    featureSource = dataStore.getFeatureSource(typeName);
    featureStore = (SimpleFeatureStore) featureSource;
    SimpleFeatureCollection featureCollection = featureStore.getFeatures();
    assertEquals(featureCollection.size(), points.length);
    SimpleFeatureIterator features = featureCollection.features();
    assertTrue(collectionsAreEqual(features, points));
}
Also used : AddOp(org.locationtech.geogig.api.porcelain.AddOp) SimpleFeatureIterator(org.geotools.data.simple.SimpleFeatureIterator) SimpleFeatureSource(org.geotools.data.simple.SimpleFeatureSource) SimpleFeatureStore(org.geotools.data.simple.SimpleFeatureStore) MemoryDataStore(org.geotools.data.memory.MemoryDataStore) Feature(org.opengis.feature.Feature) SimpleFeature(org.opengis.feature.simple.SimpleFeature) SimpleFeatureCollection(org.geotools.data.simple.SimpleFeatureCollection) Test(org.junit.Test)

Example 27 with SimpleFeatureCollection

use of org.geotools.data.simple.SimpleFeatureCollection in project GeoGig by boundlessgeo.

the class ExportOpTest method testExportWithAlterUsingDefaultFeatureType.

@Test
public void testExportWithAlterUsingDefaultFeatureType() throws Exception {
    Feature[] points = new Feature[] { points2, points1B, points3 };
    for (Feature feature : points) {
        insert(feature);
    }
    MemoryDataStore dataStore = new MemoryDataStore(pointsType);
    final String typeName = dataStore.getTypeNames()[0];
    SimpleFeatureSource featureSource = dataStore.getFeatureSource(typeName);
    SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;
    geogig.command(ExportOp.class).setFeatureStore(featureStore).setPath(pointsName).setAlter(true).call();
    featureSource = dataStore.getFeatureSource(typeName);
    featureStore = (SimpleFeatureStore) featureSource;
    SimpleFeatureCollection featureCollection = featureStore.getFeatures();
    assertEquals(featureCollection.size(), points.length);
    SimpleFeatureIterator features = featureCollection.features();
    assertTrue(collectionsAreEqual(features, points));
}
Also used : SimpleFeatureIterator(org.geotools.data.simple.SimpleFeatureIterator) SimpleFeatureSource(org.geotools.data.simple.SimpleFeatureSource) SimpleFeatureStore(org.geotools.data.simple.SimpleFeatureStore) MemoryDataStore(org.geotools.data.memory.MemoryDataStore) Feature(org.opengis.feature.Feature) SimpleFeature(org.opengis.feature.simple.SimpleFeature) SimpleFeatureCollection(org.geotools.data.simple.SimpleFeatureCollection) Test(org.junit.Test)

Example 28 with SimpleFeatureCollection

use of org.geotools.data.simple.SimpleFeatureCollection in project GeoGig by boundlessgeo.

the class ExportOpTest method testExportFromTreeWithSeveralFeatureTypesUsingDefaultFeatureType.

@Test
public void testExportFromTreeWithSeveralFeatureTypesUsingDefaultFeatureType() throws Exception {
    Feature[] points = new Feature[] { points2, points1B, points3 };
    for (Feature feature : points) {
        insert(feature);
    }
    Feature[] expectedPoints = new Feature[] { points2, points3 };
    MemoryDataStore dataStore = new MemoryDataStore(pointsType);
    final String typeName = dataStore.getTypeNames()[0];
    SimpleFeatureSource featureSource = dataStore.getFeatureSource(typeName);
    SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;
    geogig.command(ExportOp.class).setFeatureStore(featureStore).setPath(pointsName).exportDefaultFeatureType().call();
    featureSource = dataStore.getFeatureSource(typeName);
    featureStore = (SimpleFeatureStore) featureSource;
    SimpleFeatureCollection featureCollection = featureStore.getFeatures();
    assertEquals(featureCollection.size(), expectedPoints.length);
    SimpleFeatureIterator features = featureCollection.features();
    assertTrue(collectionsAreEqual(features, expectedPoints));
}
Also used : SimpleFeatureIterator(org.geotools.data.simple.SimpleFeatureIterator) SimpleFeatureSource(org.geotools.data.simple.SimpleFeatureSource) SimpleFeatureStore(org.geotools.data.simple.SimpleFeatureStore) MemoryDataStore(org.geotools.data.memory.MemoryDataStore) Feature(org.opengis.feature.Feature) SimpleFeature(org.opengis.feature.simple.SimpleFeature) SimpleFeatureCollection(org.geotools.data.simple.SimpleFeatureCollection) Test(org.junit.Test)

Example 29 with SimpleFeatureCollection

use of org.geotools.data.simple.SimpleFeatureCollection in project GeoGig by boundlessgeo.

the class GeoGigFeatureSourceTest method testGetFeatures.

@Test
public void testGetFeatures() throws Exception {
    SimpleFeatureCollection collection;
    Set<List<Object>> actual;
    Set<List<Object>> expected;
    collection = pointsSource.getFeatures();
    assertEquals(pointsType, collection.getSchema());
    actual = Sets.newHashSet();
    for (Feature f : toList(collection)) {
        SimpleFeature sf = (SimpleFeature) f;
        actual.add(sf.getAttributes());
    }
    expected = ImmutableSet.of(((SimpleFeature) points1).getAttributes(), ((SimpleFeature) points2).getAttributes(), ((SimpleFeature) points3).getAttributes());
    assertEquals(expected, actual);
    collection = linesSource.getFeatures();
    assertEquals(linesType, collection.getSchema());
    actual = Sets.newHashSet();
    for (Feature f : toList(collection)) {
        actual.add(((SimpleFeature) f).getAttributes());
    }
    expected = ImmutableSet.of(((SimpleFeature) lines1).getAttributes(), ((SimpleFeature) lines2).getAttributes(), ((SimpleFeature) lines3).getAttributes());
    assertEquals(expected, actual);
}
Also used : List(java.util.List) SimpleFeature(org.opengis.feature.simple.SimpleFeature) Feature(org.opengis.feature.Feature) SimpleFeature(org.opengis.feature.simple.SimpleFeature) SimpleFeatureCollection(org.geotools.data.simple.SimpleFeatureCollection) Test(org.junit.Test)

Example 30 with SimpleFeatureCollection

use of org.geotools.data.simple.SimpleFeatureCollection in project OpenTripPlanner by opentripplanner.

the class PointSet method fromShapefile.

public static PointSet fromShapefile(File file, String originIDField, List<String> propertyFields) throws IOException, NoSuchAuthorityCodeException, FactoryException, EmptyPolygonException, UnsupportedGeometryException {
    if (!file.exists())
        throw new RuntimeException("Shapefile does not exist.");
    FileDataStore store = FileDataStoreFinder.getDataStore(file);
    SimpleFeatureSource featureSource = store.getFeatureSource();
    CoordinateReferenceSystem sourceCRS = featureSource.getInfo().getCRS();
    CoordinateReferenceSystem WGS84 = CRS.decode("EPSG:4326", true);
    Query query = new Query();
    query.setCoordinateSystem(sourceCRS);
    query.setCoordinateSystemReproject(WGS84);
    SimpleFeatureCollection featureCollection = featureSource.getFeatures(query);
    // Set up fields based on first feature in collection
    // This assumes that all features have the same set of properties, which I think is always the case for shapefiles
    SimpleFeatureIterator it = featureCollection.features();
    SimpleFeature protoFt = it.next();
    if (propertyFields == null) {
        propertyFields = new ArrayList<String>();
        // No property fields specified, so use all property fields
        for (Property p : protoFt.getProperties()) {
            propertyFields.add(p.getName().toString());
        }
        // If ID field is specified, don't use it as a property
        if (originIDField != null && propertyFields.contains(originIDField)) {
            propertyFields.remove(originIDField);
        }
    }
    // Reset iterator
    it = featureCollection.features();
    PointSet ret = new PointSet(featureCollection.size());
    int i = 0;
    while (it.hasNext()) {
        SimpleFeature feature = it.next();
        Geometry geom = (Geometry) feature.getDefaultGeometry();
        PointFeature ft = new PointFeature();
        ft.setGeom(geom);
        // Set feature's ID to the specified ID field, or to index if none is specified
        if (originIDField == null) {
            ft.setId(Integer.toString(i));
        } else {
            ft.setId(feature.getProperty(originIDField).getValue().toString());
        }
        for (Property prop : feature.getProperties()) {
            String propName = prop.getName().toString();
            if (propertyFields.contains(propName)) {
                Object binding = prop.getType().getBinding();
                // attempt to coerce the prop's value into an integer
                int val;
                if (binding.equals(Integer.class)) {
                    val = (Integer) prop.getValue();
                } else if (binding.equals(Long.class)) {
                    val = ((Long) prop.getValue()).intValue();
                } else if (binding.equals(String.class)) {
                    try {
                        val = Integer.parseInt((String) prop.getValue());
                    } catch (NumberFormatException ex) {
                        continue;
                    }
                } else {
                    LOG.debug("Property {} of feature {} could not be interpreted as int, skipping", prop.getName().toString(), ft.getId());
                    continue;
                }
                ft.addAttribute(propName, val);
            } else {
                LOG.debug("Property {} not requested; igoring", propName);
            }
        }
        ret.addFeature(ft, i);
        i++;
    }
    ArrayList<String> IDlist = new ArrayList<String>();
    for (String id : ret.ids) {
        IDlist.add(id);
    }
    LOG.debug("Created PointSet from shapefile with IDs {}", IDlist);
    return ret;
}
Also used : Query(org.geotools.data.Query) SimpleFeatureSource(org.geotools.data.simple.SimpleFeatureSource) ArrayList(java.util.ArrayList) SimpleFeature(org.opengis.feature.simple.SimpleFeature) SimpleFeatureCollection(org.geotools.data.simple.SimpleFeatureCollection) Geometry(com.vividsolutions.jts.geom.Geometry) SimpleFeatureIterator(org.geotools.data.simple.SimpleFeatureIterator) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) FileDataStore(org.geotools.data.FileDataStore) Property(org.opengis.feature.Property)

Aggregations

SimpleFeatureCollection (org.geotools.data.simple.SimpleFeatureCollection)43 SimpleFeature (org.opengis.feature.simple.SimpleFeature)27 SimpleFeatureIterator (org.geotools.data.simple.SimpleFeatureIterator)24 SimpleFeatureSource (org.geotools.data.simple.SimpleFeatureSource)21 IOException (java.io.IOException)15 SimpleFeatureStore (org.geotools.data.simple.SimpleFeatureStore)15 Test (org.junit.Test)15 SimpleFeatureType (org.opengis.feature.simple.SimpleFeatureType)12 Feature (org.opengis.feature.Feature)10 ArrayList (java.util.ArrayList)9 MemoryDataStore (org.geotools.data.memory.MemoryDataStore)9 SimpleFeatureBuilder (org.geotools.feature.simple.SimpleFeatureBuilder)9 ListFeatureCollection (org.geotools.data.collection.ListFeatureCollection)7 File (java.io.File)6 HashMap (java.util.HashMap)6 ShapefileDataStore (org.geotools.data.shapefile.ShapefileDataStore)6 AttributeDescriptor (org.opengis.feature.type.AttributeDescriptor)6 CoordinateReferenceSystem (org.opengis.referencing.crs.CoordinateReferenceSystem)6 DataStore (org.geotools.data.DataStore)5 Query (org.geotools.data.Query)5