org.geotools.data.FeatureReader Java Examples

The following examples show how to use org.geotools.data.FeatureReader. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: WFSTemporalQueryTest.java    From geowave with Apache License 2.0 6 votes vote down vote up
@Test
public void testTemporal() throws CQLException, IOException, ParseException {

  populate();
  final Transaction transaction2 = new DefaultTransaction();
  final Query query =
      new Query(
          "geostuff",
          CQL.toFilter(
              "BBOX(geometry,44,27,42,30) and start during 2005-05-16T20:32:56Z/2005-05-20T21:32:56Z and end during 2005-05-18T20:32:56Z/2005-05-22T21:32:56Z"),
          new String[] {"geometry", "start", "end", "pid"});
  final FeatureReader<SimpleFeatureType, SimpleFeature> reader =
      dataStore.getFeatureReader(query, transaction2);
  int c = 0;
  while (reader.hasNext()) {
    reader.next();
    c++;
  }
  reader.close();
  transaction2.commit();
  transaction2.close();
  assertEquals(2, c);
}
 
Example #2
Source File: GeoWaveFeatureReaderTest.java    From geowave with Apache License 2.0 6 votes vote down vote up
@Test
public void testSmallBBOX() throws IllegalArgumentException, NoSuchElementException, IOException {
  final FilterFactoryImpl factory = new FilterFactoryImpl();
  final Query query =
      new Query(
          "GeoWaveFeatureReaderTest",
          factory.bbox("geometry", 28, 41, 28.5, 41.5, "EPSG:4326"),
          new String[] {"geometry", "pid"});

  final FeatureReader<SimpleFeatureType, SimpleFeature> reader =
      dataStore.getFeatureReader(query, Transaction.AUTO_COMMIT);
  int count = 0;
  while (reader.hasNext()) {
    final SimpleFeature feature = reader.next();
    assertTrue(fids.contains(feature.getID()));
    count++;
  }
  assertEquals(1, count);
}
 
Example #3
Source File: GeoWaveFeatureReaderTest.java    From geowave with Apache License 2.0 6 votes vote down vote up
@Test
public void testBBOX() throws IllegalArgumentException, NoSuchElementException, IOException {
  final FilterFactoryImpl factory = new FilterFactoryImpl();
  final Query query =
      new Query(
          "GeoWaveFeatureReaderTest",
          factory.bbox("geometry", -180, -90, 180, 90, "EPSG:4326"),
          new String[] {"geometry", "pid"});

  final FeatureReader<SimpleFeatureType, SimpleFeature> reader =
      dataStore.getFeatureReader(query, Transaction.AUTO_COMMIT);
  int count = 0;
  while (reader.hasNext()) {
    final SimpleFeature feature = reader.next();
    assertTrue(fids.contains(feature.getID()));
    count++;
  }
  assertTrue(count > 0);
}
 
Example #4
Source File: GeoWaveFeatureReaderTest.java    From rya with Apache License 2.0 6 votes vote down vote up
@Test
public void testLike() throws IllegalArgumentException, NoSuchElementException, IOException, CQLException {
    final Query query = new Query(
            "GeoWaveFeatureReaderTest",
            ECQL.toFilter("pid like '" + pids.get(
                    0).substring(
                    0,
                    1) + "%'"),
            new String[] {
                "geometry",
                "pid"
            });
    final FeatureReader<SimpleFeatureType, SimpleFeature> reader =
        dataStore.getFeatureReader(query, Transaction.AUTO_COMMIT);
    int count = 0;
    while (reader.hasNext()) {
        final SimpleFeature feature = reader.next();
        assertTrue(fids.contains(feature.getID()));
        count++;
    }
    assertEquals(1, count);
}
 
Example #5
Source File: GeoWaveFeatureReaderTest.java    From geowave with Apache License 2.0 6 votes vote down vote up
@Test
public void testLike()
    throws IllegalArgumentException, NoSuchElementException, IOException, CQLException {
  System.out.println(pids);
  final Query query =
      new Query(
          "GeoWaveFeatureReaderTest",
          ECQL.toFilter("pid like '" + pids.get(0).substring(0, 1) + "%'"),
          new String[] {"geometry", "pid"});
  final FeatureReader<SimpleFeatureType, SimpleFeature> reader =
      dataStore.getFeatureReader(query, Transaction.AUTO_COMMIT);
  int count = 0;
  while (reader.hasNext()) {
    final SimpleFeature feature = reader.next();
    assertTrue(fids.contains(feature.getID()));
    count++;
  }
  assertEquals(1, count);
}
 
Example #6
Source File: GeoWaveFeatureReaderTest.java    From rya with Apache License 2.0 6 votes vote down vote up
@Test
public void testPidFilterQuery() throws IllegalArgumentException, NoSuchElementException, IOException, CQLException {
    // Filter it so that it only queries for everything but the first pid.
    // There's only 2 pids total so it should just return the second one.
    final String pidsString = pids.subList(1, pids.size()).stream().collect(Collectors.joining("','", "'", "'"));
    final Filter filter = ECQL.toFilter("pid IN (" + pidsString + ")");
    final Query query = new Query(
            "GeoWaveFeatureReaderTest",
            filter,
            new String[] {
                "geometry",
                "pid"
            });
    final FeatureReader<SimpleFeatureType, SimpleFeature> reader =
        dataStore.getFeatureReader(query, Transaction.AUTO_COMMIT);
    int count = 0;
    while (reader.hasNext()) {
        final SimpleFeature feature = reader.next();
        assertTrue(fids.contains(feature.getID()));
        count++;
    }
    assertTrue(count == pids.size() - 1);
}
 
Example #7
Source File: GeoWaveFeatureReaderTest.java    From rya with Apache License 2.0 6 votes vote down vote up
@Test
public void testFidFilterQuery() throws IllegalArgumentException, NoSuchElementException, IOException, CQLException {
    final String fidsString = fids.stream().collect(Collectors.joining("','", "'", "'"));
    final Filter filter = ECQL.toFilter("IN (" + fidsString + ")");
    final Query query = new Query(
            "GeoWaveFeatureReaderTest",
            filter,
            new String[] {
                "geometry",
                "pid"
            });
    final FeatureReader<SimpleFeatureType, SimpleFeature> reader =
        dataStore.getFeatureReader(query, Transaction.AUTO_COMMIT);
    int count = 0;
    while (reader.hasNext()) {
        final SimpleFeature feature = reader.next();
        assertTrue(fids.contains(feature.getID()));
        count++;
    }
    assertTrue(count == fids.size());
}
 
Example #8
Source File: GeoToolsAttributesSubsetTest.java    From geowave with Apache License 2.0 6 votes vote down vote up
@Test
public void testSubsetAttributes() throws CQLException, IOException {
  final Query query =
      new Query(
          typeName,
          CQL.toFilter(cqlPredicate),
          new String[] {geometry_attribute, string_attribute});
  final FeatureReader<SimpleFeatureType, SimpleFeature> reader =
      geotoolsDataStore.getFeatureReader(query, Transaction.AUTO_COMMIT);
  int count = 0;
  while (reader.hasNext()) {
    final SimpleFeature feature = reader.next();
    count++;
    Assert.assertTrue(feature.getAttribute(geometry_attribute) != null);
    Assert.assertTrue(feature.getAttribute(long_attribute) == null);
    Assert.assertTrue(feature.getAttribute(string_attribute) != null);
  }
  Assert.assertTrue(count == 3);
}
 
Example #9
Source File: GeoWaveFeatureReaderTest.java    From geowave with Apache License 2.0 5 votes vote down vote up
@Test
public void testMax() throws IllegalArgumentException, NoSuchElementException, IOException {
  final FeatureReader<SimpleFeatureType, SimpleFeature> reader =
      dataStore.getFeatureReader(query, Transaction.AUTO_COMMIT);
  final MaxVisitor visitor = new MaxVisitor("start", type);
  unwrapDelegatingFeatureReader(reader).getFeatureCollection().accepts(visitor, null);
  assertTrue(visitor.getMax().equals(mtime));
}
 
Example #10
Source File: GeoWaveFeatureReaderTest.java    From geowave with Apache License 2.0 5 votes vote down vote up
@Test
public void testMin() throws IllegalArgumentException, NoSuchElementException, IOException {
  final FeatureReader<SimpleFeatureType, SimpleFeature> reader =
      dataStore.getFeatureReader(query, Transaction.AUTO_COMMIT);
  final MinVisitor visitor = new MinVisitor("start", type);
  unwrapDelegatingFeatureReader(reader).getFeatureCollection().accepts(visitor, null);
  assertTrue(visitor.getMin().equals(stime));
}
 
Example #11
Source File: GeoWaveFeatureReaderTest.java    From geowave with Apache License 2.0 5 votes vote down vote up
private GeoWaveFeatureReader unwrapDelegatingFeatureReader(
    final FeatureReader<SimpleFeatureType, SimpleFeature> reader) {
  // GeoTools uses decorator pattern to wrap FeatureReaders
  // we need to get down to the inner GeoWaveFeatureReader
  FeatureReader<SimpleFeatureType, SimpleFeature> currReader = reader;
  while (!(currReader instanceof GeoWaveFeatureReader)) {
    currReader =
        ((DelegatingFeatureReader<SimpleFeatureType, SimpleFeature>) currReader).getDelegate();
  }
  return (GeoWaveFeatureReader) currReader;
}
 
Example #12
Source File: WFSSpatialTest.java    From geowave with Apache License 2.0 5 votes vote down vote up
@Test
public void test() throws IOException, CQLException, ParseException {
  final Transaction transaction1 = new DefaultTransaction();

  final FeatureWriter<SimpleFeatureType, SimpleFeature> writer =
      dataStore.getFeatureWriter(type.getTypeName(), transaction1);
  assertFalse(writer.hasNext());
  SimpleFeature newFeature = writer.next();
  newFeature.setAttribute("pop", Long.valueOf(100));
  newFeature.setAttribute("pid", UUID.randomUUID().toString());
  newFeature.setAttribute("when", DateUtilities.parseISO("2005-05-19T18:33:55Z"));
  newFeature.setAttribute("geometry", factory.createPoint(new Coordinate(27.25, 41.25)));

  newFeature = writer.next();
  newFeature.setAttribute("pop", Long.valueOf(100));
  newFeature.setAttribute("pid", UUID.randomUUID().toString());
  newFeature.setAttribute("when", DateUtilities.parseISO("2005-05-19T20:33:55Z"));
  newFeature.setAttribute("geometry", factory.createPoint(new Coordinate(27.25, 41.25)));
  writer.write();
  writer.close();

  final FeatureReader<SimpleFeatureType, SimpleFeature> reader =
      dataStore.getFeatureReader(query, transaction1);
  assertTrue(reader.hasNext());
  final SimpleFeature priorFeature = reader.next();
  assertEquals(newFeature.getAttribute("pid"), priorFeature.getAttribute("pid"));
  assertFalse(reader.hasNext());
  reader.close();

  transaction1.commit();
  transaction1.close();
}
 
Example #13
Source File: GeoWaveFeatureReaderTest.java    From geowave with Apache License 2.0 5 votes vote down vote up
@Test
public void testRangeIndex()
    throws IllegalArgumentException, NoSuchElementException, IOException {
  final FeatureReader<SimpleFeatureType, SimpleFeature> reader =
      dataStore.getFeatureReader(query, Transaction.AUTO_COMMIT);
  int count = 0;
  while (reader.hasNext()) {
    final SimpleFeature feature = reader.next();
    assertTrue(fids.contains(feature.getID()));
    count++;
  }
  assertEquals(1, count);
}
 
Example #14
Source File: GeoToolsAttributesSubsetTest.java    From geowave with Apache License 2.0 5 votes vote down vote up
@Test
public void testAllAttributes() throws CQLException, IOException {
  final Query query = new Query(typeName, CQL.toFilter(cqlPredicate), Query.ALL_PROPERTIES);
  final FeatureReader<SimpleFeatureType, SimpleFeature> reader =
      geotoolsDataStore.getFeatureReader(query, Transaction.AUTO_COMMIT);
  int count = 0;
  while (reader.hasNext()) {
    final SimpleFeature feature = reader.next();
    count++;
    Assert.assertTrue(feature.getAttribute(geometry_attribute) != null);
    Assert.assertTrue(feature.getAttribute(long_attribute) != null);
    Assert.assertTrue(feature.getAttribute(string_attribute) != null);
  }
  Assert.assertTrue(count == 3);
}
 
Example #15
Source File: GeoWaveFeatureReaderTest.java    From rya with Apache License 2.0 5 votes vote down vote up
private GeoWaveFeatureReader unwrapDelegatingFeatureReader(final FeatureReader<SimpleFeatureType, SimpleFeature> reader ) {
    // GeoTools uses decorator pattern to wrap FeatureReaders
    // we need to get down to the inner GeoWaveFeatureReader
    FeatureReader<SimpleFeatureType, SimpleFeature> currReader = reader;
    while (!(currReader instanceof GeoWaveFeatureReader)) {
        currReader = ((DelegatingFeatureReader<SimpleFeatureType, SimpleFeature>) currReader).getDelegate();
    }
    return (GeoWaveFeatureReader) currReader;
}
 
Example #16
Source File: GeoWaveFeatureReaderTest.java    From geowave with Apache License 2.0 5 votes vote down vote up
@Test
public void testFID()
    throws IllegalArgumentException, NoSuchElementException, IOException, CQLException {
  final FeatureReader<SimpleFeatureType, SimpleFeature> reader =
      dataStore.getFeatureReader(query, Transaction.AUTO_COMMIT);
  int count = 0;
  while (reader.hasNext()) {
    final SimpleFeature feature = reader.next();
    assertTrue(fids.contains(feature.getID()));
    count++;
  }
  assertTrue(count > 0);
}
 
Example #17
Source File: GeoWaveFeatureSource.java    From geowave with Apache License 2.0 5 votes vote down vote up
@Override
protected FeatureReader<SimpleFeatureType, SimpleFeature> getReaderInternal(final Query query)
    throws IOException {
  final GeoWaveTransactionState state = getDataStore().getMyTransactionState(transaction, this);
  return new GeoWaveFeatureReader(
      query,
      state.getGeoWaveTransaction(query.getTypeName()),
      components);
}
 
Example #18
Source File: GeoWaveFeatureReaderTest.java    From rya with Apache License 2.0 5 votes vote down vote up
@Test
public void testMin() throws IllegalArgumentException, NoSuchElementException, IOException {
    final FeatureReader<SimpleFeatureType, SimpleFeature> reader =
        dataStore.getFeatureReader(query, Transaction.AUTO_COMMIT);
    final MinVisitor visitor = new MinVisitor("start", type);
    unwrapDelegatingFeatureReader(reader).getFeatureCollection().accepts(visitor, null);
    assertTrue(visitor.getMin().equals(stime));
}
 
Example #19
Source File: GeoWaveFeatureReaderTest.java    From rya with Apache License 2.0 5 votes vote down vote up
@Test
public void testMax() throws IllegalArgumentException, NoSuchElementException, IOException {
    final FeatureReader<SimpleFeatureType, SimpleFeature> reader =
        dataStore.getFeatureReader(query, Transaction.AUTO_COMMIT);
    final MaxVisitor visitor = new MaxVisitor("start", type);
    unwrapDelegatingFeatureReader(reader).getFeatureCollection().accepts(visitor, null);
    assertTrue(visitor.getMax().equals(etime));
}
 
Example #20
Source File: GeoWaveFeatureReaderTest.java    From rya with Apache License 2.0 5 votes vote down vote up
@Test
public void testRemoveFeature() throws IllegalArgumentException, NoSuchElementException, IOException, CQLException {
    final Query query = new Query(
            "GeoWaveFeatureReaderTest",
            ECQL.toFilter("pid like '" + pids.get(
                    0).substring(
                    0,
                    1) + "%'"),
            new String[] {
                "geometry",
                "pid"
            });
    final FeatureReader<SimpleFeatureType, SimpleFeature> reader =
        dataStore.getFeatureReader(query, Transaction.AUTO_COMMIT);
    int count = 0;
    while (reader.hasNext()) {
        final SimpleFeature feature = reader.next();
        assertTrue(fids.contains(feature.getID()));
        count++;
    }
    assertEquals(1, count);

    // Remove
    final FeatureWriter<SimpleFeatureType, SimpleFeature> writer =
        dataStore.getFeatureWriter(type.getTypeName(), Transaction.AUTO_COMMIT);
    try {
        while (writer.hasNext()) {
            writer.next();
            writer.remove();
        }
    } finally {
        writer.close();
    }

    // Re-query
    final FeatureReader<SimpleFeatureType, SimpleFeature> reader2 =
            dataStore.getFeatureReader(query, Transaction.AUTO_COMMIT);
    int recount = 0;
    while (reader2.hasNext()) {
        reader2.next();
        recount++;
    }
    assertEquals(0, recount);
}
 
Example #21
Source File: GeoWaveFeatureReaderTest.java    From rya with Apache License 2.0 5 votes vote down vote up
@Test
public void testRangeIndex() throws IllegalArgumentException, NoSuchElementException, IOException {
    final FeatureReader<SimpleFeatureType, SimpleFeature> reader =
        dataStore.getFeatureReader(query, Transaction.AUTO_COMMIT);
    int count = 0;
    while (reader.hasNext()) {
        final SimpleFeature feature = reader.next();
        assertTrue(fids.contains(feature.getID()));
        count++;
    }
    assertEquals(1, count);
}
 
Example #22
Source File: GeoWaveFeatureReaderTest.java    From rya with Apache License 2.0 5 votes vote down vote up
@Test
public void testBBOX() throws IllegalArgumentException, NoSuchElementException, IOException {
    final FilterFactoryImpl factory = new FilterFactoryImpl();
    final Query query = new Query(
            "GeoWaveFeatureReaderTest",
            factory.bbox(
                    "",
                    -180,
                    -90,
                    180,
                    90,
                    "EPSG:4326"),
            new String[] {
                "geometry",
                "pid"
            });

    final FeatureReader<SimpleFeatureType, SimpleFeature> reader =
        dataStore.getFeatureReader(query, Transaction.AUTO_COMMIT);
    int count = 0;
    while (reader.hasNext()) {
        final SimpleFeature feature = reader.next();
        assertTrue(fids.contains(feature.getID()));
        count++;
    }
    assertTrue(count > 0);
}
 
Example #23
Source File: GeoWaveFeatureReaderTest.java    From rya with Apache License 2.0 5 votes vote down vote up
@Test
public void testFID() throws IllegalArgumentException, NoSuchElementException, IOException, CQLException {
    final FeatureReader<SimpleFeatureType, SimpleFeature> reader =
        dataStore.getFeatureReader(query, Transaction.AUTO_COMMIT);
    int count = 0;
    while (reader.hasNext()) {
        final SimpleFeature feature = reader.next();
        assertTrue(fids.contains(feature.getID()));
        count++;
    }
    assertTrue(count > 0);
}
 
Example #24
Source File: ElasticFeatureSource.java    From elasticgeo with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected FeatureReader<SimpleFeatureType, SimpleFeature> getReaderInternal(Query query) throws IOException {
    LOGGER.fine("getReaderInternal");
    FeatureReader<SimpleFeatureType, SimpleFeature> reader;
    try {
        final ElasticDataStore dataStore = getDataStore();
        final String docType = dataStore.getDocType(entry.getName());
        final boolean scroll = !useSortOrPagination(query) && dataStore.getScrollEnabled();
        final ElasticRequest searchRequest = prepareSearchRequest(query, scroll);
        final ElasticResponse sr = dataStore.getClient().search(dataStore.getIndexName(), docType, searchRequest);
        if (LOGGER.isLoggable(Level.FINE)) {
            LOGGER.fine("Search response: " + sr);
        }
        if (!scroll) {
            reader = new ElasticFeatureReader(getState(), sr);
        } else {
            reader = new ElasticFeatureReaderScroll(getState(), sr, getSize(query));
        }
        if (!filterFullySupported) {
            reader = new FilteringFeatureReader<>(reader, query.getFilter());
        }
    } catch (Exception e) {
        LOGGER.log(Level.SEVERE, e.getMessage(), e);
        throw new IOException("Error executing query search", e);
    }
    return reader;
}
 
Example #25
Source File: ElasticFeatureSource.java    From elasticgeo with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected int getCountInternal(Query query) throws IOException {
    LOGGER.fine("getCountInternal");
    int hits = 0;
    final ElasticRequest searchRequest = prepareSearchRequest(query, false);
    try {
        if (!filterFullySupported) {
            try (FeatureReader<SimpleFeatureType, SimpleFeature> reader = getReaderInternal(query)) {
                while (reader.hasNext()) {
                    reader.next();
                    hits++;
                }
            }
        } else {
            searchRequest.setSize(0);
            final ElasticDataStore dataStore = getDataStore();
            final String docType = dataStore.getDocType(entry.getName());
            final ElasticResponse sr = dataStore.getClient().search(dataStore.getIndexName(), docType, searchRequest);
            final int totalHits = (int) sr.getTotalNumHits();
            final int size = getSize(query);
            final int from = getStartIndex(query);
            hits = Math.max(0, Math.min(totalHits - from, size));
        }
    } catch (Exception e) {
        LOGGER.log(Level.SEVERE, e.getMessage(), e);
        throw new IOException("Error executing count search", e);
    }

    return hits;
}
 
Example #26
Source File: ElasticFeatureSource.java    From elasticgeo with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Implementation that generates the total bounds
 */
@Override
protected ReferencedEnvelope getBoundsInternal(Query query) throws IOException {
    LOGGER.fine("getBoundsInternal");
    final CoordinateReferenceSystem crs = getSchema().getCoordinateReferenceSystem();
    final ReferencedEnvelope bounds = new ReferencedEnvelope(crs);

    try (FeatureReader<SimpleFeatureType, SimpleFeature> featureReader = getReaderInternal(query)) {
        while (featureReader.hasNext()) {
            final SimpleFeature feature = featureReader.next();
            bounds.include(feature.getBounds());
        }
    }
    return bounds;
}
 
Example #27
Source File: AbstractGeotoolsDataStoreImporter.java    From TomboloDigitalConnector with MIT License 5 votes vote down vote up
private void withSubjects(FeatureReader<SimpleFeatureType, SimpleFeature> featureReader, DataStore dataStore, BiConsumer<SimpleFeature, Subject> fn) throws IOException, FactoryException, TransformException {
    MathTransform crsTransform = GeotoolsDataStoreUtils.makeCrsTransform(getSourceEncoding());

    while(featureReader.hasNext()) {
        SimpleFeature feature = featureReader.next();
        buildSubjectFromFeature(feature, crsTransform).ifPresent(subject -> {
            fn.accept(feature, subject);
            subjectBuffer.add(subject);
        });
        flushBufferIfRequired();
    }

    flushBuffer();
}
 
Example #28
Source File: AbstractGeotoolsDataStoreImporter.java    From TomboloDigitalConnector with MIT License 5 votes vote down vote up
@Override
final public void importDatasource(Datasource datasource, List<String> geographyScope, List<String> temporalScope, List<String> datasourceLocation) throws Exception {
    DataStore dataStore = getDataStoreForDatasource(datasource);
    FeatureReader<SimpleFeatureType, SimpleFeature> featureReader = GeotoolsDataStoreUtils.getFeatureReader(dataStore, getTypeNameForDatasource(datasource));

    // Load attribute values
    withSubjects(featureReader, dataStore, (feature, subject) -> {
        timedValueBuffer.addAll(buildTimedValuesFromFeature(datasource, feature, subject));
        fixedValueBuffer.addAll(buildFixedValuesFromFeature(datasource, feature, subject));
    });

    featureReader.close();
    dataStore.dispose();
}
 
Example #29
Source File: GeoWaveFeatureCollection.java    From geowave with Apache License 2.0 4 votes vote down vote up
@Override
public FeatureReader<SimpleFeatureType, SimpleFeature> reader() {
  return reader;
}
 
Example #30
Source File: GeotoolsDataStoreUtils.java    From TomboloDigitalConnector with MIT License 4 votes vote down vote up
public static FeatureReader<SimpleFeatureType, SimpleFeature> getFeatureReader(DataStore store, String typeName) throws IOException {
    DefaultQuery query = new DefaultQuery(typeName);
    return store.getFeatureReader(query, Transaction.AUTO_COMMIT);
}