Search in sources :

Example 1 with DelegateSimpleFeatureReader

use of org.geotools.data.simple.DelegateSimpleFeatureReader in project geoprism-registry by terraframe.

the class ShapefileImporter method process.

/**
 * Imports the entities from the shapefile
 *
 * @param writer
 *          Log file writer
 * @throws InvocationTargetException
 * @throws IOException
 * @throws InterruptedException
 */
@Request
private void process(ImportStage stage, File shp) throws InvocationTargetException, IOException, InterruptedException {
    /*
     * Check permissions
     */
    ImportConfiguration config = this.getObjectImporter().getConfiguration();
    config.enforceCreatePermissions();
    FileDataStore myData = FileDataStoreFinder.getDataStore(shp);
    SimpleFeatureSource source = myData.getFeatureSource();
    SimpleFeatureCollection featCol = source.getFeatures();
    this.progressListener.setWorkTotal((long) featCol.size());
    if (this.getStartIndex() > 0) {
        Query query = new Query();
        query.setStartIndex(Math.toIntExact(this.getStartIndex()));
        featCol = source.getFeatures(query);
    }
    SimpleFeatureIterator featIt = featCol.features();
    SimpleFeatureReader fr = new DelegateSimpleFeatureReader(source.getSchema(), featIt);
    FilterFactory ff = CommonFactoryFinder.getFilterFactory(null);
    // We want to sort the features by the parentId column for better lookup
    // performance (more cache hits) and
    // also so that we have predictable ordering if we want to resume the import
    // later.
    List<SortBy> sortBy = new ArrayList<SortBy>();
    // We also sort by featureId because it's
    sortBy.add(SortBy.NATURAL_ORDER);
    // guaranteed to be unique.
    if (this.config.getLocations().size() > 0) {
        ShapefileFunction loc = this.config.getLocations().get(0).getFunction();
        if (loc instanceof BasicColumnFunction) {
            // TODO
            sortBy.add(ff.sort(loc.toJson().toString(), SortOrder.ASCENDING));
        // :
        // This
        // assumes
        // loc.tojson()
        // returns
        // only
        // the
        // attribute
        // name.
        }
    }
    try (SimpleFeatureReader sr = new SortedFeatureReader(fr, sortBy.toArray(new SortBy[sortBy.size()]), 5000)) {
        while (sr.hasNext()) {
            SimpleFeature feature = sr.next();
            if (stage.equals(ImportStage.VALIDATE)) {
                this.objectImporter.validateRow(new SimpleFeatureRow(feature));
            } else {
                this.objectImporter.importRow(new SimpleFeatureRow(feature));
            }
        }
    } catch (Throwable t) {
        t.printStackTrace();
    } finally {
        myData.dispose();
    }
}
Also used : Query(org.geotools.data.Query) SimpleFeatureSource(org.geotools.data.simple.SimpleFeatureSource) SortBy(org.opengis.filter.sort.SortBy) BasicColumnFunction(net.geoprism.data.importer.BasicColumnFunction) ArrayList(java.util.ArrayList) SimpleFeatureReader(org.geotools.data.simple.SimpleFeatureReader) DelegateSimpleFeatureReader(org.geotools.data.simple.DelegateSimpleFeatureReader) SortedFeatureReader(org.geotools.data.sort.SortedFeatureReader) FilterFactory(org.opengis.filter.FilterFactory) SimpleFeature(org.opengis.feature.simple.SimpleFeature) SimpleFeatureCollection(org.geotools.data.simple.SimpleFeatureCollection) SimpleFeatureIterator(org.geotools.data.simple.SimpleFeatureIterator) ShapefileFunction(net.geoprism.data.importer.ShapefileFunction) SimpleFeatureRow(net.geoprism.data.importer.SimpleFeatureRow) FileDataStore(org.geotools.data.FileDataStore) DelegateSimpleFeatureReader(org.geotools.data.simple.DelegateSimpleFeatureReader) Request(com.runwaysdk.session.Request)

Aggregations

Request (com.runwaysdk.session.Request)1 ArrayList (java.util.ArrayList)1 BasicColumnFunction (net.geoprism.data.importer.BasicColumnFunction)1 ShapefileFunction (net.geoprism.data.importer.ShapefileFunction)1 SimpleFeatureRow (net.geoprism.data.importer.SimpleFeatureRow)1 FileDataStore (org.geotools.data.FileDataStore)1 Query (org.geotools.data.Query)1 DelegateSimpleFeatureReader (org.geotools.data.simple.DelegateSimpleFeatureReader)1 SimpleFeatureCollection (org.geotools.data.simple.SimpleFeatureCollection)1 SimpleFeatureIterator (org.geotools.data.simple.SimpleFeatureIterator)1 SimpleFeatureReader (org.geotools.data.simple.SimpleFeatureReader)1 SimpleFeatureSource (org.geotools.data.simple.SimpleFeatureSource)1 SortedFeatureReader (org.geotools.data.sort.SortedFeatureReader)1 SimpleFeature (org.opengis.feature.simple.SimpleFeature)1 FilterFactory (org.opengis.filter.FilterFactory)1 SortBy (org.opengis.filter.sort.SortBy)1