Search in sources :

Example 1 with FeatureSource

use of org.geotools.data.FeatureSource in project GeoGig by boundlessgeo.

the class WorkingTreeTest method testInsertPagingFeatureSource.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testInsertPagingFeatureSource() throws Exception {
    assertEquals(2, super.getGeogig().getPlatform().availableProcessors());
    final List<SimpleFeature> features = ImmutableList.of((SimpleFeature) points1, (SimpleFeature) points2, (SimpleFeature) points3);
    MemoryDataStore store = new MemoryDataStore();
    store.addFeatures(features);
    final QueryCapabilities caps = mock(QueryCapabilities.class);
    when(caps.isOffsetSupported()).thenReturn(true);
    FeatureSource source = new ForwardingFeatureSource(store.getFeatureSource(pointsName)) {

        @Override
        public QueryCapabilities getQueryCapabilities() {
            return caps;
        }

        @Override
        public FeatureCollection getFeatures(Query query) throws IOException {
            Integer startIndex = query.getStartIndex();
            if (startIndex == null) {
                return super.getFeatures();
            }
            int toIndex = (int) Math.min((long) startIndex + query.getMaxFeatures(), features.size());
            List<SimpleFeature> result = features.subList(startIndex, toIndex);
            return DataUtilities.collection(result);
        }
    };
    assertTrue(source.getQueryCapabilities().isOffsetSupported());
    String treePath = "target_typename";
    workTree.insert(treePath, source, Query.ALL, LISTENER);
    assertEquals(3, workTree.countUnstaged(treePath).featureCount());
}
Also used : ForwardingFeatureSource(org.locationtech.geogig.api.data.ForwardingFeatureSource) FeatureSource(org.geotools.data.FeatureSource) QueryCapabilities(org.geotools.data.QueryCapabilities) Query(org.geotools.data.Query) MemoryDataStore(org.geotools.data.memory.MemoryDataStore) ForwardingFeatureSource(org.locationtech.geogig.api.data.ForwardingFeatureSource) SimpleFeature(org.opengis.feature.simple.SimpleFeature) Test(org.junit.Test)

Example 2 with FeatureSource

use of org.geotools.data.FeatureSource in project GeoGig by boundlessgeo.

the class ImportOp method getFeatureSource.

@SuppressWarnings({ "rawtypes", "unchecked" })
private FeatureSource getFeatureSource(String typeName) {
    FeatureSource featureSource;
    try {
        featureSource = dataStore.getFeatureSource(typeName);
    } catch (Exception e) {
        throw new GeoToolsOpException(StatusCode.UNABLE_TO_GET_FEATURES);
    }
    return new ForwardingFeatureSource(featureSource) {

        @Override
        public FeatureCollection getFeatures(Query query) throws IOException {
            final FeatureCollection features = super.getFeatures(query);
            return new ForwardingFeatureCollection(features) {

                @Override
                public FeatureIterator features() {
                    final FeatureType featureType = getSchema();
                    final String fidPrefix = featureType.getName().getLocalPart() + ".";
                    FeatureIterator iterator = delegate.features();
                    return new FidAndFtReplacerIterator(iterator, fidAttribute, fidPrefix, (SimpleFeatureType) featureType);
                }
            };
        }
    };
}
Also used : FeatureIterator(org.geotools.feature.FeatureIterator) ForwardingFeatureIterator(org.locationtech.geogig.api.data.ForwardingFeatureIterator) ForwardingFeatureSource(org.locationtech.geogig.api.data.ForwardingFeatureSource) JDBCFeatureSource(org.geotools.jdbc.JDBCFeatureSource) FeatureSource(org.geotools.data.FeatureSource) ResolveFeatureType(org.locationtech.geogig.api.plumbing.ResolveFeatureType) RevFeatureType(org.locationtech.geogig.api.RevFeatureType) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) FeatureType(org.opengis.feature.type.FeatureType) Query(org.geotools.data.Query) ForwardingFeatureCollection(org.locationtech.geogig.api.data.ForwardingFeatureCollection) ForwardingFeatureCollection(org.locationtech.geogig.api.data.ForwardingFeatureCollection) FeatureCollection(org.geotools.feature.FeatureCollection) ForwardingFeatureSource(org.locationtech.geogig.api.data.ForwardingFeatureSource) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException)

Example 3 with FeatureSource

use of org.geotools.data.FeatureSource in project sldeditor by robward-scisys.

the class DataSourceImpl method getUserLayerFeatureSource.

/*
     * (non-Javadoc)
     * 
     * @see com.sldeditor.datasource.DataSourceInterface#getUserLayerFeatureSource()
     */
@Override
public // CHECKSTYLE:OFF
Map<UserLayer, FeatureSource<SimpleFeatureType, SimpleFeature>> getUserLayerFeatureSource() {
    // CHECKSTYLE:ON
    Map<UserLayer, FeatureSource<SimpleFeatureType, SimpleFeature>> map = new HashMap<UserLayer, FeatureSource<SimpleFeatureType, SimpleFeature>>();
    for (DataSourceInfo dsInfo : userLayerDataSourceInfo) {
        FeatureSource<SimpleFeatureType, SimpleFeature> features = dsInfo.getFeatures();
        UserLayer userLayer = dsInfo.getUserLayer();
        map.put(userLayer, features);
    }
    return map;
}
Also used : FeatureSource(org.geotools.data.FeatureSource) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) HashMap(java.util.HashMap) UserLayer(org.geotools.styling.UserLayer) SimpleFeature(org.opengis.feature.simple.SimpleFeature)

Example 4 with FeatureSource

use of org.geotools.data.FeatureSource in project polymap4-core by Polymap4.

the class RDataStore method updateSchema.

@Override
public void updateSchema(Name name, final FeatureType newSchema) throws IOException {
    assert name != null && newSchema != null;
    final Updater tx = store.prepareUpdate();
    try {
        // check modified property names
        boolean namesModified = false;
        for (PropertyDescriptor desc : newSchema.getDescriptors()) {
            // set by FeatureTypeEditor/AttributeCellModifier
            String origName = (String) desc.getUserData().get(ORIG_NAME_KEY);
            if (origName != null) {
                namesModified = true;
            }
        }
        // find deleted properties
        // XXX check complex schemas
        FeatureType schema = getSchema(name);
        final List<PropertyDescriptor> deleted = new ArrayList();
        for (PropertyDescriptor desc : schema.getDescriptors()) {
            if (newSchema.getDescriptor(desc.getName()) == null) {
                deleted.add(desc);
            }
        }
        // schema name changed or prop deleted? -> update features
        final String newName = newSchema.getName().getLocalPart();
        if (!name.getLocalPart().equals(newSchema.getName().getLocalPart()) || !deleted.isEmpty() || namesModified) {
            FeatureSource fs = getFeatureSource(name);
            fs.getFeatures().accepts(new FeatureVisitor() {

                public void visit(Feature feature) {
                    try {
                        // typeName
                        ((RFeature) feature).state.put(RFeature.TYPE_KEY, newName);
                        // List<Name> origModifiedNames = new ArrayList();
                        for (PropertyDescriptor desc : newSchema.getDescriptors()) {
                            // set by FeatureTypeEditor/AttributeCellModifier
                            String origName = (String) desc.getUserData().get(ORIG_NAME_KEY);
                            if (origName != null) {
                                RAttribute prop = (RAttribute) feature.getProperty(origName);
                                if (prop != null) {
                                    if (prop.getValue() != null) {
                                        ((RFeature) feature).state.put(desc.getName().getLocalPart(), prop.getValue());
                                    }
                                    ((RFeature) feature).state.remove(prop.key.toString());
                                }
                            }
                        }
                        // deleted attributes
                        for (PropertyDescriptor desc : deleted) {
                            // XXX check complex schemas
                            RProperty prop = (RProperty) feature.getProperty(desc.getName());
                            ((RFeature) feature).state.remove(prop.key.toString());
                        }
                        tx.store(((RFeature) feature).state);
                    } catch (Exception e) {
                        // Designing a visitor interface without Exception is not a good idea!
                        throw new RuntimeException("", e);
                    }
                }
            }, null);
        }
        // update schema record
        ResultSet rs = store.find(new SimpleQuery().setMaxResults(1).eq("type", "FeatureType").eq("name", name.getLocalPart()));
        IRecordState record = rs.get(0);
        String schemaContent = schemaCoder.encode(newSchema);
        record.put("content", schemaContent);
        record.put("name", newName);
        tx.store(record);
        log.debug("Current schema: " + schemaCoder.encode(schema));
        log.debug("Updated schema: " + schemaContent);
        tx.apply();
    } catch (Throwable e) {
        log.debug("", e);
        tx.discard();
        throw new RuntimeException(e);
    }
}
Also used : FeatureType(org.opengis.feature.type.FeatureType) FeatureSource(org.geotools.data.FeatureSource) PropertyDescriptor(org.opengis.feature.type.PropertyDescriptor) SimpleQuery(org.polymap.recordstore.SimpleQuery) ArrayList(java.util.ArrayList) FeatureVisitor(org.opengis.feature.FeatureVisitor) Feature(org.opengis.feature.Feature) IOException(java.io.IOException) Updater(org.polymap.recordstore.IRecordStore.Updater) ResultSet(org.polymap.recordstore.ResultSet) IRecordState(org.polymap.recordstore.IRecordState)

Example 5 with FeatureSource

use of org.geotools.data.FeatureSource in project OpenTripPlanner by opentripplanner.

the class ShapefileBoundaryResolver method initialize.

public void initialize() throws IOException {
    if (shapefile == null) {
        throw new IllegalStateException("shapefile path is not set");
    }
    // get feature results
    ShapefileDataStore store = new ShapefileDataStore(new File(shapefile).toURI().toURL());
    String name = store.getTypeNames()[0];
    FeatureSource source = store.getFeatureSource(name);
    collection = source.getFeatures();
}
Also used : FeatureSource(org.geotools.data.FeatureSource) ShapefileDataStore(org.geotools.data.shapefile.ShapefileDataStore) File(java.io.File)

Aggregations

FeatureSource (org.geotools.data.FeatureSource)10 SimpleFeature (org.opengis.feature.simple.SimpleFeature)6 IOException (java.io.IOException)4 ForwardingFeatureSource (org.locationtech.geogig.api.data.ForwardingFeatureSource)4 SimpleFeatureType (org.opengis.feature.simple.SimpleFeatureType)4 UserLayer (org.geotools.styling.UserLayer)3 Test (org.junit.Test)3 FeatureType (org.opengis.feature.type.FeatureType)3 File (java.io.File)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 ArrayList (java.util.ArrayList)2 Query (org.geotools.data.Query)2 QueryCapabilities (org.geotools.data.QueryCapabilities)2 MemoryDataStore (org.geotools.data.memory.MemoryDataStore)2 FeatureCollection (org.geotools.feature.FeatureCollection)2 FeatureIterator (org.geotools.feature.FeatureIterator)2 JDBCFeatureSource (org.geotools.jdbc.JDBCFeatureSource)2 RevFeatureType (org.locationtech.geogig.api.RevFeatureType)2 ResolveFeatureType (org.locationtech.geogig.api.plumbing.ResolveFeatureType)2 Feature (org.opengis.feature.Feature)2