/** * Array of all the elements. * * @return an array containing all of the elements in this collection. */ public Object[] toArray() { // code based on AbstractFeatureCollection // TODO: base this on reader ArrayList<SimpleFeature> array = new ArrayList<SimpleFeature>(); FeatureIterator<SimpleFeature> e = null; try { e = features(); while (e.hasNext()) { array.add(e.next()); } return array.toArray(new SimpleFeature[array.size()]); } finally { if (e != null) { e.close(); } } }
@SuppressWarnings("unchecked") public <T> T[] toArray(T[] array) { int size = size(); if (array.length < size) { array = (T[]) java.lang.reflect.Array.newInstance( array.getClass().getComponentType(), size); } FeatureIterator<SimpleFeature> it = features(); try { Object[] result = array; for (int i = 0; it.hasNext() && i < size; i++) { result[i] = it.next(); } if (array.length > size) { array[size] = null; } return array; } finally { if (it != null) { it.close(); } } }
e = this.features(); if (o == null) { while (e.hasNext()) {
@Override public Geometry loadFootprint(String pathNoExtension) throws Exception { File file = new File(pathNoExtension + ".shp"); if (file.exists()) { ShapefileDataStore ds = new ShapefileDataStore(URLs.fileToUrl(file)); SimpleFeatureIterator fi = null; try { fi = ds.getFeatureSource().getFeatures().features(); if (!fi.hasNext()) { return null; } else { SimpleFeature sf = fi.next(); Geometry result = (Geometry) sf.getDefaultGeometry(); if (fi.hasNext()) { throw new IOException( "Found more than one footprint record in the shapefile " + file.getCanonicalPath()); } return result; } } finally { if (fi != null) { fi.close(); } ds.dispose(); } } return null; }
public void testFeatureIteratorNextContract() throws Exception { try (SimpleFeatureIterator features = featureSource.getFeatures().features()) { // 1) non empty iterator, calling next() should just return the feature SimpleFeature f = features.next(); assertNotNull(f); } }
public void testGetFeaturesWithInvalidFilter() throws Exception { FilterFactory ff = CommonFactoryFinder.getFilterFactory(null); PropertyIsEqualTo f = ff.equals(ff.property("invalidAttribute"), ff.literal(5)); // make sure a complaint related to the invalid filter is thrown here try (SimpleFeatureIterator fi = featureSource.getFeatures(f).features()) { fail("This query should have failed, it contains an invalid filter"); } catch (Exception e) { java.util.logging.Logger.getGlobal().log(java.util.logging.Level.INFO, "", e); // fine } }
dataStore.virtualTables.get("invalid_attribute").sql = vt.sql.replace(flow, notValid); try (SimpleFeatureIterator fi = fs.getFeatures().features()) { fail("We should not have gotten here, we were supposed to get a sql exception"); } catch (RuntimeException e) {
public void testNativeFilterExecution() throws Exception { // build the filter that will be send to the database BBOX boundingBoxFilter = filterFactory.bbox( tname("location"), -5, -5, 5, 5, DefaultGeographicCRS.WGS84.toString()); Filter filter = filterFactory.and(boundingBoxFilter, getNativeFilter()); // retrieve the features that match the filter above ContentFeatureSource featureSource = dataStore.getFeatureSource(tname("gt_jdbc_test_measurements")); assertThat(featureSource, notNullValue()); SimpleFeatureIterator iterator = featureSource.getFeatures(filter).features(); List<SimpleFeature> features = new ArrayList<>(); while (iterator.hasNext()) { features.add(iterator.next()); } iterator.close(); // check that we retrieved the necessary features assertThat(features.size(), is(1)); assertThat(features.get(0).getAttribute(tname("code")), is("#2")); }
public void testNaturalSortingAsc() throws Exception { Query q = new Query(featureSource.getSchema().getTypeName()); q.setSortBy(new SortBy[] {SortBy.NATURAL_ORDER}); try (SimpleFeatureIterator features = featureSource.getFeatures(q).features()) { String prevId = null; while (features.hasNext()) { String currId = features.next().getID(); if (prevId != null) assertTrue(prevId.compareTo(currId) <= 0); prevId = currId; } } }
public void testNaturalSortingdesc() throws Exception { Query q = new Query(featureSource.getSchema().getTypeName()); q.setSortBy(new SortBy[] {SortBy.REVERSE_ORDER}); try (SimpleFeatureIterator features = featureSource.getFeatures(q).features()) { String prevId = null; while (features.hasNext()) { String currId = features.next().getID(); if (prevId != null) assertTrue(prevId.compareTo(currId) >= 0); prevId = currId; } } }
public void testGetFeaturesWithInvalidQuery() { FilterFactory ff = CommonFactoryFinder.getFilterFactory(null); PropertyIsEqualTo f = ff.equals(ff.property("invalidAttribute"), ff.literal(5)); // make sure a complaint related to the invalid filter is thrown here try (SimpleFeatureIterator fi = featureSource.getFeatures(new Query("ft1", f)).features()) { fail("This query should have failed, it contains an invalid filter"); } catch (Exception e) { // e.printStackTrace(); // fine } }
public void testFeatureIteratorEmptyContract() throws Exception { FilterFactory ff = dataStore.getFilterFactory(); PropertyIsEqualTo filter = ff.equals(ff.property(aname("stringProperty")), ff.literal("not_there")); try (SimpleFeatureIterator features = featureSource.getFeatures(filter).features()) { // 1) non empty iterator, calling next() should just return the feature SimpleFeature f = features.next(); assertNotNull(f); } catch (NoSuchElementException e) { // ok } }
public void testAssignedMultiPKeyView() throws Exception { JDBCFeatureStore fs = (JDBCFeatureStore) dataStore.getFeatureSource(tname("assignedmultipk")); assertEquals(2, fs.getPrimaryKey().getColumns().size()); assertTrue( fs.getPrimaryKey().getColumns().get(0) instanceof NonIncrementingPrimaryKeyColumn); assertTrue( fs.getPrimaryKey().getColumns().get(1) instanceof NonIncrementingPrimaryKeyColumn); try (FeatureIterator i = fs.getFeatures().features()) { for (int j = 1; i.hasNext(); j++) { SimpleFeature f = (SimpleFeature) i.next(); assertEquals(tname("assignedmultipk") + "." + j + "." + (j + 1), f.getID()); } } }
try (SimpleFeatureIterator fi = featureStore.getFeatures().features()) { while (fi.hasNext()) { fi.next();
void doTestSimpleJoin(boolean exposePrimaryKeys) throws Exception { dataStore.setExposePrimaryKeyColumns(exposePrimaryKeys); try (SimpleFeatureIterator ita = dataStore.getFeatureSource(tname("ft1")).getFeatures().features()) { try (SimpleFeatureIterator itb = dataStore.getFeatureSource(tname("ftjoin")).getFeatures().features()) {
void doTestSimpleJoinInvertedAliases(boolean exposePrimaryKeys) throws Exception { dataStore.setExposePrimaryKeyColumns(exposePrimaryKeys); try (SimpleFeatureIterator ita = dataStore.getFeatureSource(tname("ft1")).getFeatures().features(); SimpleFeatureIterator itb = dataStore.getFeatureSource(tname("ftjoin")).getFeatures().features()) { FilterFactory ff = dataStore.getFilterFactory(); Query q = new Query(tname("ft1"));
protected int getCountInternal(Query query) throws IOException { ContentFeatureCollection cfc = this.getFeatures(query); int count = 0; SimpleFeatureIterator iter = cfc.features(); while(iter.hasNext()) { iter.next(); count++; } iter.close(); return count; // CsvReader reader = getDataStore().read(); // try { // boolean connect = reader.readHeaders(); // if( connect == false ){ // throw new IOException("Unable to connect"); // } // int count = 0; // while( reader.readRecord() ){ // count += 1; // } // return count; // } // finally { // reader.close(); // } }
ShapefileDataStore dataStore = new ShapefileDataStore(file.toURL()); ContentFeatureSource featureSource = dataStore.getFeatureSource(); ContentFeatureCollection featureCollection = featureSource.getFeatures(); SimpleFeatureIterator iterator = featureCollection.features(); // get dynamically the CRS of your data: SimpleFeatureType schema = featureSource.getSchema(); CoordinateReferenceSystem sourceCRS = schema.getCoordinateReferenceSystem(); // OR fallback to hardcoded 3035 if the above fails: // CoordinateReferenceSystem sourceCRS = CRS.decode("EPSG:3035") CoordinateReferenceSystem targetCRS = CRS.decode("EPSG:4326") // the coordinates system you want to reproject the data to // define a MathTransform object MathTransform transform = CRS.findMathTransform(sourceCRS, targetCRS); while (iterator.hasNext()) { SimpleFeature feature = iterator.next(); Collection<Property> properties = feature.getProperties(); // get the geometry of the actual feature Geometry sourceGeometry = feature.getDefaultGeometry() // transform the geometry and save it in a new variable Geometry reprojectedGeometry = JTS.transform(sourceGeometry, transform) // set the reprojected geometry as the geometry of the actual feature feature.setDefaultGeometry(reprojectedGeometry) // ..... }
@Test public void testScreenMap() throws Exception { // Test a single point to make sure the feature tree itself (with the same bounds as the // point it contains) doesn't write to the ScreenMap deleteAndAdd(points2); deleteAndAdd(points3); geogig.command(CommitOp.class).setMessage("drop to 1 point").call(); Query query = new Query(pointsName); ScreenMap screenMap = new ScreenMap(-180, -90, 360, 180); screenMap.setSpans(1.0, 1.0); screenMap.setTransform(IdentityTransform.create(2)); query.getHints().put(Hints.SCREENMAP, screenMap); SimpleFeatureIterator iter = pointsSource.getFeatures(query).features(); assertTrue(iter.hasNext()); assertEquals(points1.getIdentifier().getID(), iter.next().getID()); assertTrue(screenMap.get(boundsOf(points1))); }