public Object[] toArray(Object[] arg0) { List list = new ArrayList(); Iterator it = collections.iterator(); while (it.hasNext()) { FeatureCollection col = (FeatureCollection) it.next(); FeatureIterator it2 = col.features(); while (it2.hasNext()) { list.add(it.next()); } it2.close(); } return list.toArray(arg0); }
File file = new File("mayshapefile.shp"); try { Map connect = new HashMap(); connect.put("url", file.toURL()); DataStore dataStore = DataStoreFinder.getDataStore(connect); String[] typeNames = dataStore.getTypeNames(); String typeName = typeNames[0]; System.out.println("Reading content " + typeName); FeatureSource featureSource = dataStore.getFeatureSource(typeName); FeatureCollection collection = featureSource.getFeatures(); FeatureIterator iterator = collection.features(); try { while (iterator.hasNext()) { Feature feature = iterator.next(); Geometry sourceGeometry = feature.getDefaultGeometry(); } } finally { iterator.close(); } } catch (Throwable e) {}
@Test public void testForce() throws Exception { FeatureTypeInfo fti = getCatalog().getFeatureTypeByName(MockData.BASIC_POLYGONS.getLocalPart()); assertEquals("EPSG:4269", fti.getSRS()); assertEquals(ProjectionPolicy.FORCE_DECLARED, fti.getProjectionPolicy()); FeatureCollection fc = fti.getFeatureSource(null, null).getFeatures(); assertEquals(CRS.decode("EPSG:4269"), fc.getSchema().getCoordinateReferenceSystem()); FeatureIterator fi = fc.features(); Feature f = fi.next(); fi.close(); assertEquals(CRS.decode("EPSG:4269"), f.getType().getCoordinateReferenceSystem()); }
@Test public void testFeautureSourceFidFilter() throws Exception { // grab the last feature in the collection (there are more than one) SimpleFeatureSource fs = rts.getFeatureSource(RENAMED); // build a filter that will retrieve that feature only FilterFactory ff = CommonFactoryFinder.getFilterFactory(null); final String fid = RENAMED + ".1107531701011"; Filter fidFilter = ff.id(Collections.singleton(ff.featureId(fid))); SimpleFeatureCollection fc = fs.getFeatures(new Query(RENAMED, fidFilter)); assertEquals(RENAMED, fc.getSchema().getName().getLocalPart()); assertEquals(1, fc.size()); FeatureIterator<SimpleFeature> it = fc.features(); assertTrue(it.hasNext()); SimpleFeature sf = it.next(); assertFalse(it.hasNext()); it.close(); assertEquals(fid, sf.getID()); }
/** We have to cache all the features in the supplemental file, because * if we try to load them on the fly, GeoTools wigs out. */ private void ensureCached() { if (cache != null) { return; } cache = new HashMap<String, SimpleFeature>(); try { FeatureCollection<SimpleFeatureType, SimpleFeature> features = joinedSource .getFeatures(); FeatureIterator<SimpleFeature> it = features.features(); while (it.hasNext()) { SimpleFeature feature = it.next(); String joinedKeyValue = toHashableString(feature.getAttribute(joinedKey)); if (joinedKeyValue != null) { cache.put(joinedKeyValue, feature); } else { log.warn("Feature " + feature.getID() + " has null value for its joinedKey (" + joinedKey + ")"); } } it.close(); } catch (IOException e) { throw new RuntimeException("Could not cache values for joined shapefile", e); } }
@Test public void testFeautureSourceFidFilter() throws Exception { // grab the last feature in the collection (there are more than one) SimpleFeatureSource fs = rts.getFeatureSource(RENAMED); // build a filter that will retrieve that feature only FilterFactory ff = CommonFactoryFinder.getFilterFactory(null); final String fid = RENAMED + ".f001"; Filter fidFilter = ff.id(Collections.singleton(ff.featureId(fid))); SimpleFeatureCollection fc = fs.getFeatures(new Query(RENAMED, fidFilter)); assertEquals(RENAMED, fc.getSchema().getName().getLocalPart()); assertEquals(1, fc.size()); FeatureIterator<SimpleFeature> it = fc.features(); assertTrue(it.hasNext()); SimpleFeature sf = it.next(); assertFalse(it.hasNext()); it.close(); assertEquals(fid, sf.getID()); }
@Test public void testLeaveNative() throws Exception { FeatureTypeInfo fti = getCatalog().getFeatureTypeByName(MockData.LINES.getLocalPart()); assertEquals("EPSG:3004", fti.getSRS()); assertEquals(ProjectionPolicy.NONE, fti.getProjectionPolicy()); FeatureCollection fc = fti.getFeatureSource(null, null).getFeatures(); assertEquals(CRS.decode("EPSG:32615"), fc.getSchema().getCoordinateReferenceSystem()); FeatureIterator fi = fc.features(); Feature f = fi.next(); // test that the geometry was left in tact Geometry g = (Geometry) f.getDefaultGeometryProperty().getValue(); assertTrue(g.equalsExact(WKT.read("LINESTRING(500125 500025,500175 500075)"))); fi.close(); assertEquals(CRS.decode("EPSG:32615"), f.getType().getCoordinateReferenceSystem()); }
public boolean hasNext() { // is there a current iterator that has another element if ((iterator != null) && iterator.hasNext()) { return true; } // get the next iterator while (index < collections.size()) { // close current before we move to next if (iterator != null) { iterator.close(); } // grap next iterator = collections.get(index++).features(); if (iterator.hasNext()) { return true; } } // no more if (iterator != null) { // close the last iterator iterator.close(); } return false; }
@SuppressWarnings("unchecked") public <O> O[] toArray(O[] a) { int size = size(); if (a.length < size) { a = (O[]) java.lang.reflect.Array.newInstance(a.getClass().getComponentType(), size); } FeatureIterator<F> it = features(); try { Object[] result = a; for (int i = 0; i < size; i++) result[i] = it.next(); if (a.length > size) a[size] = null; return a; } finally { it.close(); } }
@Test public void testReproject() throws Exception { FeatureTypeInfo fti = getCatalog().getFeatureTypeByName(MockData.POLYGONS.getLocalPart()); assertEquals("EPSG:4326", fti.getSRS()); assertEquals(ProjectionPolicy.REPROJECT_TO_DECLARED, fti.getProjectionPolicy()); FeatureCollection fc = fti.getFeatureSource(null, null).getFeatures(); assertEquals(CRS.decode("EPSG:4326"), fc.getSchema().getCoordinateReferenceSystem()); FeatureIterator fi = fc.features(); Feature f = fi.next(); // test that geometry was actually reprojected Geometry g = (Geometry) f.getDefaultGeometryProperty().getValue(); assertFalse( g.equalsExact( WKT.read( "POLYGON((500225 500025,500225 500075,500275 500050,500275 500025,500225 500025))"))); fi.close(); assertEquals(CRS.decode("EPSG:4326"), f.getType().getCoordinateReferenceSystem()); }
public <T> T[] toArray(T[] arg0) { List<T> list = new ArrayList<T>(); Iterator it = collections.iterator(); while (it.hasNext()) { FeatureCollection col = (FeatureCollection) it.next(); FeatureIterator it2 = col.features(); try { while (it2.hasNext()) { list.add((T) it.next()); } } finally { it2.close(); } } return list.toArray(arg0); }
@Test public void testGetFeaturesFeatureSource() throws Exception { // check the schemas in feature source and feature collection SimpleFeatureSource fs = rts.getFeatureSource(RENAMED); assertEquals(RENAMED, fs.getSchema().getName().getLocalPart()); SimpleFeatureCollection fc = fs.getFeatures(); assertEquals(RENAMED, fc.getSchema().getName().getLocalPart()); assertTrue(fc.size() > 0); // make sure the feature schema is good as well FeatureIterator<SimpleFeature> it = fc.features(); SimpleFeature sf = it.next(); it.close(); assertEquals(RENAMED, sf.getFeatureType().getName().getLocalPart()); // check the feature ids have been renamed as well assertTrue( "Feature id has not been renamed, it's still " + sf.getID(), sf.getID().startsWith(RENAMED)); }
/** * Copies the provided features into a List. * * @param featureCollection * @return List of features copied into memory */ public static <F extends Feature> List<F> list(FeatureCollection<?, F> featureCollection) { final ArrayList<F> list = new ArrayList<F>(); FeatureIterator<F> iter = featureCollection.features(); try { while (iter.hasNext()) { F feature = iter.next(); list.add(feature); } } finally { iter.close(); } return list; } /**
@Test public void testWithRename() throws Exception { FeatureTypeInfo fti = getCatalog().getFeatureTypeByName("MyPoints"); assertEquals("EPSG:4326", fti.getSRS()); assertEquals(ProjectionPolicy.REPROJECT_TO_DECLARED, fti.getProjectionPolicy()); FeatureCollection fc = fti.getFeatureSource(null, null).getFeatures(); assertEquals(CRS.decode("EPSG:4326"), fc.getSchema().getCoordinateReferenceSystem()); FeatureIterator fi = fc.features(); Feature f = fi.next(); // test that geometry was reprojected Geometry g = (Geometry) f.getDefaultGeometryProperty().getValue(); assertFalse(g.equalsExact(WKT.read("POINT(500050 500050)"))); fi.close(); assertEquals(CRS.decode("EPSG:4326"), f.getType().getCoordinateReferenceSystem()); }
public boolean hasNext() { // is there a current iterator that has another element if ((iterator != null) && iterator.hasNext()) { return true; } // get the next iterator while (index < collections.size()) { // close current before we move to next if (iterator != null) { iterator.close(); } // grap next iterator = ((FeatureCollection) collections.get(index++)).features(); if (iterator.hasNext()) { return true; } } // no more if (iterator != null) { // close the last iterator iterator.close(); } return false; }