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) {}
FeatureType featureType = featureSource.getSchema(); FeatureTypeInfo ftinfo = catalog.getFactory().createFeatureType(); ftinfo.setStore(store); ftinfo.setEnabled(true); Name name = featureSource.getName(); if (name == null) { name = featureType.getName(); ftinfo.setNativeName(name.getLocalPart()); ftinfo.setName(name.getLocalPart()); NamespaceInfo namespace = catalog.getNamespaceByPrefix(workspace.getName()); if (namespace == null) { namespace = catalog.getDefaultNamespace(); CoordinateReferenceSystem crs = featureType.getCoordinateReferenceSystem(); if (crs == null && featureType.getGeometryDescriptor() != null) { crs = featureType.getGeometryDescriptor().getCoordinateReferenceSystem();
/** * Retreives the bounds for a feature source. * * <p>If the feautre source can calculate the bounds directly, those bounds are returned. * Otherwise, the underlying feature collection is retreived and asked to calculate bounds. If * that fails, an empty envelope is returned. * * @param fs The feature source. * @return The bounds. * @throws IOException Execption calculating bounds on feature source. */ public static ReferencedEnvelope getBoundingBoxEnvelope( FeatureSource<? extends FeatureType, ? extends Feature> fs) throws IOException { ReferencedEnvelope ev = fs.getBounds(); if ((ev == null) || ev.isNull()) { try { ev = fs.getFeatures().getBounds(); } catch (Throwable t) { LOGGER.log( Level.FINE, "Could not compute the data bounding box. Returning an empty envelope", t); ev = new ReferencedEnvelope(fs.getSchema().getCoordinateReferenceSystem()); } } return ev; } }
public FeatureSourceWrappingFeatureTypeInfoImpl(FeatureSource featureSource) throws IOException, TransformException, FactoryException { super(); this.featureSource = featureSource; setName(featureSource.getName().getLocalPart()); setEnabled(true); setLatLonBoundingBox( featureSource.getBounds().transform(DefaultGeographicCRS.WGS84, true)); }
@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()); }
Query query = new Query(null, filter); query.setCoordinateSystemReproject(getMapContent().getCoordinateReferenceSystem()); FeatureSource featureSource = getLayer().getFeatureSource(); Collection<PropertyDescriptor> descriptors = featureSource.getSchema().getDescriptors(); FeatureCollection queryResult = featureSource.getFeatures(query); FeatureIterator iter = queryResult.features(); while (iter.hasNext()) { Feature f = iter.next(); result.newFeature(f.getIdentifier().getID()); for (PropertyDescriptor desc : descriptors) { Name name = desc.getName(); Object value = f.getProperty(name).getValue(); iter.close();
Query query = new Query(); if (reprojection != null) { if (sourceFeatures.getSchema().getGeometryDescriptor() != null && !this.isReprojectionCrsEqual( this.mappedSource.getSchema().getCoordinateReferenceSystem(), this.reprojection)) { query.setCoordinateSystemReproject(reprojection); query.setFilter(fidFilter); matchingFeatures = this.mappedSource.getFeatures(query); try (FeatureIterator<? extends Feature> iterator = matchingFeatures.features()) { while (iterator.hasNext()) { features.add(iterator.next());
/** * Returns the collection of all values of the dimension attribute, eventually sorted if the * native capabilities allow for it * * @param typeInfo * @param dimension * @throws IOException */ FeatureCollection getDimensionCollection(FeatureTypeInfo typeInfo, DimensionInfo dimension) throws IOException { FeatureSource source = getFeatureSource(typeInfo); // build query to grab the dimension values final Query dimQuery = new Query(source.getSchema().getName().getLocalPart()); dimQuery.setPropertyNames(Arrays.asList(dimension.getAttribute())); return source.getFeatures(dimQuery); }
FeatureSource<FeatureType, Feature> source = (FeatureSource<FeatureType, Feature>) layer.getFeatureSource(); FeatureType schema = source.getSchema(); Query query = new Query(Query.ALL); Filter filter = null; LOGGER.fine("Querying layer " + schema.getName() + " with bbox: " + envelope); filter = createBBoxFilters(schema, attributes, envelopes); query = new Query(schema.getName().getLocalPart()); query.setProperties(attributes); Envelope bounds = source.getBounds(); if (bounds != null && envelope.intersects(bounds)) { LOGGER.log( QueryCapabilities qc = source.getQueryCapabilities(); if (qc != null && !qc.supportsSorting(sortBy)) { throw new IllegalArgumentException( CoordinateReferenceSystem crs = getNativeCRS(schema, attributes); if (crs != null) { Set<RenderingHints.Key> fsHints = source.getSupportedHints(); simplifier.setFeatureType(source.getSchema()); Filter simplifiedFilter = (Filter) query.getFilter().accept(simplifier, null); query.setFilter(simplifiedFilter);
CoordinateReferenceSystem sourceCRS = featureSource.getInfo().getCRS(); .createCoordinateReferenceSystem("EPSG:4326"); Query query = new Query(); query.setCoordinateSystem(sourceCRS); query.setCoordinateSystemReproject(worldCRS); .getFeatures(query); features = featureSource.getFeatures(query); FeatureIterator<SimpleFeature> it2 = features.features(); while (it2.hasNext()) { SimpleFeature feature = it2.next(); if (featureSelector != null && ! featureSelector.convert(feature)) { continue; it2.close(); it2 = null;
@Test /* * Test Bbox function with no property name specified. The default geometry should be used. */ public void testBBoxWithNoPropertyName() throws IOException { // default geometry exists // in theory, when property name is not specified, the filter should apply to all the // geometry attributes, // but it wouldn't work until the bug in GeometryFilterImpl is fixed // and our test data only have 1 geometry, so it doesn't test multiple geometries case BBOX filter = ff.bbox(ff.property(""), -1.1, 52.5, -1.1, 52.6, null); FeatureCollection<FeatureType, Feature> features = fSource.getFeatures(filter); assertEquals(2, size(features)); FeatureIterator<Feature> iterator = features.features(); try { Feature f = iterator.next(); assertEquals(f.getIdentifier().toString(), "mf1"); f = iterator.next(); assertEquals(f.getIdentifier().toString(), "mf3"); } finally { iterator.close(); } }
assertNotNull(ftChained.getGeometryDescriptor()); assertEquals( ComplexFeatureConstants.DEFAULT_GEOMETRY_LOCAL_NAME, ftChained.getGeometryDescriptor().getLocalName()); FeatureCollection fc = fs.getFeatures(filter); assertEquals(1, size(fc)); try (FeatureIterator it = fc.features()) { Feature station1 = it.next(); assertEquals("st.1", station1.getIdentifier().toString()); GeometryAttribute defaultGeom = station1.getDefaultGeometryProperty(); assertNotNull(defaultGeom); assertNotNull(defaultGeom.getValue());
Query query = new Query(); query.setCoordinateSystemReproject(reprojection); query.setFilter(filter); FeatureCollection<FeatureType, Feature> fCollection = fSource.getFeatures(query); if (fCollection instanceof MappingFeatureCollection) { try (FeatureIterator<Feature> iterator = fCollection.features()) { while (iterator.hasNext()) { matchingFeatures.add(iterator.next());
public void setQuery(Query query) { this.query = query; columns.clear(); features.clear(); FeatureType ft = layer.getFeatureSource().getSchema(); Collection<PropertyDescriptor> cols = ft.getDescriptors(); Iterator<PropertyDescriptor> ite = cols.iterator(); while (ite.hasNext()) { columns.add(ite.next()); } try { FeatureIterator<SimpleFeature> fi = (FeatureIterator<SimpleFeature>) layer.getFeatureSource().getFeatures(query.getFilter()).features(); while (fi.hasNext()) { features.add(fi.next()); } } catch (Exception ex) { ex.printStackTrace(); } }
final Query query = new Query(fs.getSchema().getName().getLocalPart()); query.setPropertyNames(Arrays.asList(di.getAttribute())); final PropertyName attribute = ff.property(di.getAttribute()); final PropertyIsBetween rangeFilter = ff.literal(range.getMinValue()), ff.literal(range.getMaxValue())); query.setFilter(rangeFilter); query.setMaxFeatures(maxItems); FeatureCollection collection = fs.getFeatures(query); collection.accepts(visitor, null); TreeSet<Object> result = new TreeSet<>(visitor.getUnique()); return result;
Catalog catalog = getCatalog(); LayerGroupInfo lg = catalog.getFactory().createLayerGroup(); StyleInfo s = catalog.getStyleByName("multiStyleGroup"); LayerGroupInfo nestedLg = catalog.getFactory().createLayerGroup(); nestedLg.setName("nestedLayerGroup"); nestedLg.getLayers().add(catalog.getLayerByName((getLayerId(MockData.PONDS)))); ((FeatureTypeInfo) layers.get(5).getResource()) .getFeatureSource(null, null) .getFeatures() .features() .next(); assertTrue(inlineFeature.getDefaultGeometry() instanceof Point); assertEquals(
public void testMulParamValid() throws Exception { FilterFactory ff = CommonFactoryFinder.getFilterFactory(null); FeatureSource fsView = dataStore.getFeatureSource("riverParam"); // let's change the mul param Query q = new Query(Query.ALL); q.setHints( new Hints(Hints.VIRTUAL_TABLE_PARAMETERS, Collections.singletonMap("mul", "10"))); q.setSortBy(new SortBy[] {ff.sort(aname("mulflow"), SortOrder.ASCENDING)}); try (FeatureIterator fi = fsView.getFeatures(q).features()) { assertTrue(fi.hasNext()); SimpleFeature f = (SimpleFeature) fi.next(); assertEquals(30.0, ((Number) f.getAttribute(aname("mulflow"))).doubleValue(), 0.1); assertTrue(fi.hasNext()); f = (SimpleFeature) fi.next(); assertEquals(45.0, ((Number) f.getAttribute(aname("mulflow"))).doubleValue(), 0.1); } }
Query query = new Query(); query.setProperties(properties); FeatureCollection<FeatureType, Feature> mfCollection = mfSource.getFeatures(query); FeatureIterator iterator = mfCollection.features(); while (iterator.hasNext()) { Feature feature = iterator.next(); assertNotNull(propertyName1.evaluate(feature)); assertNull(propertyName2.evaluate(feature)); query.setProperties(properties); mfCollection = mfSource.getFeatures(query); iterator = mfCollection.features(); while (iterator.hasNext()) { Feature feature = iterator.next(); assertNotNull(propertyName2.evaluate(feature));
@Override public String getTitle() { Name name = fs.getSchema().getName(); return name.getLocalPart(); } }
@Test public void testDefaultGeometry() throws IOException { FeatureTypeInfo featureType = getCatalog().getResourceByName("cdf", "Nulls", FeatureTypeInfo.class); GeometryDescriptor schemaDefaultGeometry = featureType.getFeatureType().getGeometryDescriptor(); FeatureIterator i = featureType.getFeatureSource(null, null).getFeatures().features(); GeometryDescriptor featureDefaultGeometry = i.next().getDefaultGeometryProperty().getDescriptor(); assertNotNull(schemaDefaultGeometry); assertNotNull(featureDefaultGeometry); assertEquals("pointProperty", schemaDefaultGeometry.getLocalName()); assertEquals(schemaDefaultGeometry, featureDefaultGeometry); } }