Search in sources :

Example 46 with Query

use of org.geotoolkit.storage.feature.query.Query in project geotoolkit by Geomatys.

the class IntervalStyleBuilder method genericAnalyze.

private void genericAnalyze() {
    if (genericAnalyze)
        return;
    genericAnalyze = true;
    properties.clear();
    minimum = Double.POSITIVE_INFINITY;
    maximum = Double.NEGATIVE_INFINITY;
    count = 0;
    sum = 0;
    median = 0;
    mean = 0;
    // search the different numeric attributs
    if (layer.getData() instanceof FeatureSet) {
        throw new IllegalArgumentException("MapLayer resource must be a FeatureSet");
    }
    FeatureSet data = (FeatureSet) layer.getData();
    final FeatureType schema;
    try {
        schema = data.getType();
    } catch (DataStoreException ex) {
        throw new FeatureStoreRuntimeException(ex.getMessage(), ex);
    }
    for (PropertyType desc : schema.getProperties(true)) {
        if (desc instanceof AttributeType) {
            Class<?> type = ((AttributeType) desc).getValueClass();
            if (Number.class.isAssignableFrom(type) || type == byte.class || type == short.class || type == int.class || type == long.class || type == float.class || type == double.class) {
                properties.add(ff.property(desc.getName().tip().toString()));
            }
        }
    }
    // find the geometry class for template
    Class<?> geoClass = null;
    try {
        PropertyType geo = FeatureExt.getDefaultGeometry(schema);
        geoClass = ((AttributeType) ((Operation) geo).getResult()).getValueClass();
    } catch (PropertyNotFoundException | IllegalStateException ex) {
    }
    if (geoClass == null) {
        return;
    }
    if (template == null) {
        if (Polygon.class.isAssignableFrom(geoClass) || MultiPolygon.class.isAssignableFrom(geoClass)) {
            template = createPolygonTemplate();
        } else if (LineString.class.isAssignableFrom(geoClass) || MultiLineString.class.isAssignableFrom(geoClass)) {
            template = createLineTemplate();
        } else {
            template = createPointTemplate();
        }
    }
    // search the extreme values
    final Query query = new Query(schema.getName().toString());
    if (classification == null || layer == null)
        return;
    if (!properties.contains(classification))
        return;
    final Set<String> qp = new HashSet<String>();
    qp.add(classification.getXPath());
    if (normalize != null && !normalize.equals(noValue)) {
        qp.add(normalize.getXPath());
    }
    query.setProperties(qp.toArray(new String[0]));
    Iterator<Feature> features = null;
    try (Stream<Feature> stream = data.subset(query).features(false)) {
        features = stream.iterator();
        List<Double> values = new ArrayList<Double>();
        while (features.hasNext()) {
            Feature sf = features.next();
            count++;
            Number classifValue = (Number) classification.apply(sf);
            double value;
            if (classifValue == null) {
                // skip null values in analyze
                continue;
            }
            if (normalize == null || normalize.equals(noValue)) {
                value = classifValue.doubleValue();
            } else {
                Number normalizeValue = (Number) normalize.apply(sf);
                value = classifValue.doubleValue() / normalizeValue.doubleValue();
            }
            values.add(value);
            sum += value;
            if (value < minimum) {
                minimum = value;
            }
            if (value > maximum) {
                maximum = value;
            }
        }
        mean = (minimum + maximum) / 2;
        // find the median
        allValues = values.toArray(new Double[values.size()]);
        Arrays.sort(allValues);
        if (values.isEmpty()) {
            median = 0;
        } else if (values.size() % 2 == 0) {
            median = (allValues[(allValues.length / 2) - 1] + allValues[allValues.length / 2]) / 2.0;
        } else {
            median = allValues[allValues.length / 2];
        }
    } catch (DataStoreException ex) {
        ex.printStackTrace();
    }
}
Also used : FeatureType(org.opengis.feature.FeatureType) PropertyNotFoundException(org.opengis.feature.PropertyNotFoundException) Query(org.geotoolkit.storage.feature.query.Query) ArrayList(java.util.ArrayList) PropertyType(org.opengis.feature.PropertyType) Operation(org.opengis.feature.Operation) MultiLineString(org.locationtech.jts.geom.MultiLineString) LineString(org.locationtech.jts.geom.LineString) Feature(org.opengis.feature.Feature) AttributeType(org.opengis.feature.AttributeType) Polygon(org.locationtech.jts.geom.Polygon) MultiPolygon(org.locationtech.jts.geom.MultiPolygon) HashSet(java.util.HashSet) DataStoreException(org.apache.sis.storage.DataStoreException) MultiPolygon(org.locationtech.jts.geom.MultiPolygon) FeatureStoreRuntimeException(org.geotoolkit.storage.feature.FeatureStoreRuntimeException) FeatureSet(org.apache.sis.storage.FeatureSet)

Example 47 with Query

use of org.geotoolkit.storage.feature.query.Query in project geotoolkit by Geomatys.

the class PostgisDemo method main.

public static void main(String[] args) throws DataStoreException {
    Demos.init();
    System.out.println(PostgresProvider.PARAMETERS_DESCRIPTOR);
    final Parameters parameters = Parameters.castOrWrap(PostgresProvider.PARAMETERS_DESCRIPTOR.createValue());
    parameters.getOrCreate(PostgresProvider.HOST).setValue("hote");
    parameters.getOrCreate(PostgresProvider.PORT).setValue(5432);
    parameters.getOrCreate(PostgresProvider.DATABASE).setValue("base");
    parameters.getOrCreate(PostgresProvider.USER).setValue("user");
    parameters.getOrCreate(PostgresProvider.PASSWORD).setValue("secret");
    final FeatureStore store = (FeatureStore) DataStores.open(parameters);
    final MapLayers context = MapBuilder.createContext();
    for (GenericName n : store.getNames()) {
        System.out.println(store.getFeatureType(n.toString()));
        final FeatureSet col = store.createSession(true).getFeatureCollection(new Query(n));
        final MapLayer layer = MapBuilder.createLayer(col);
        layer.setStyle(RandomStyleBuilder.createRandomVectorStyle(col.getType()));
        context.getComponents().add(layer);
    }
// FXMapFrame.show(context);
}
Also used : GenericName(org.opengis.util.GenericName) Parameters(org.apache.sis.parameter.Parameters) Query(org.geotoolkit.storage.feature.query.Query) MapLayer(org.apache.sis.portrayal.MapLayer) FeatureSet(org.apache.sis.storage.FeatureSet) FeatureStore(org.geotoolkit.storage.feature.FeatureStore) MapLayers(org.apache.sis.portrayal.MapLayers)

Example 48 with Query

use of org.geotoolkit.storage.feature.query.Query in project geotoolkit by Geomatys.

the class PostgisRasterDemo method main.

public static void main(String[] args) throws Exception {
    final CoordinateReferenceSystem crs = CommonCRS.defaultGeographic();
    // connect to postgres feature store
    final PostgresStore store = new PostgresStore("localhost", 5432, "table", "public", "user", "password");
    // create a feature type with a coverage attribute type
    final FeatureTypeBuilder ftb = new FeatureTypeBuilder();
    ftb.setName("SpotImages");
    ftb.addAttribute(String.class).setName("name");
    ftb.addAttribute(GridCoverage.class).setName("image").setCRS(crs);
    FeatureType type = ftb.build();
    store.createFeatureType(type);
    // type migh be a little different after insertion
    type = store.getFeatureType("SpotImages");
    // WARNING : if you use a existing table you must ensure that the srid
    // constraint is set in the raster_columns view or that the raster
    // column comment contains the srid as a comment
    // Create an image we will use as a coverage
    final BufferedImage image = new BufferedImage(180, 360, BufferedImage.TYPE_INT_ARGB);
    final Graphics2D g = image.createGraphics();
    g.setColor(Color.BLUE);
    g.fillRect(0, 0, 180, 360);
    // Create a coverage
    final GridCoverageBuilder gcb = new GridCoverageBuilder();
    gcb.setValues(image);
    gcb.setDomain(new GridGeometry(null, PixelInCell.CELL_CORNER, new AffineTransform2D(-1, 0, 0, 1, +90, -180), crs));
    final GridCoverage coverage = gcb.build();
    // Create a feature
    final Feature feature = type.newInstance();
    feature.setPropertyValue("name", "world");
    feature.setPropertyValue("image", coverage);
    // Save the feature
    store.addFeatures(type.getName().toString(), Collections.singletonList(feature));
    // Display it
    final FeatureSet col = store.createSession(false).getFeatureCollection(new Query(store.getNames().iterator().next()));
    final MapLayer layer = MapBuilder.createLayer(col);
    layer.setStyle(RandomStyleBuilder.createDefaultRasterStyle());
    final MapLayers context = MapBuilder.createContext();
    context.getComponents().add(layer);
// FXMapFrame.show(context);
}
Also used : FeatureTypeBuilder(org.apache.sis.feature.builder.FeatureTypeBuilder) FeatureType(org.opengis.feature.FeatureType) GridGeometry(org.apache.sis.coverage.grid.GridGeometry) Query(org.geotoolkit.storage.feature.query.Query) MapLayer(org.apache.sis.portrayal.MapLayer) PostgresStore(org.geotoolkit.db.postgres.PostgresStore) Feature(org.opengis.feature.Feature) BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D) GridCoverage(org.apache.sis.coverage.grid.GridCoverage) GridCoverageBuilder(org.apache.sis.coverage.grid.GridCoverageBuilder) FeatureSet(org.apache.sis.storage.FeatureSet) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) AffineTransform2D(org.apache.sis.internal.referencing.j2d.AffineTransform2D) MapLayers(org.apache.sis.portrayal.MapLayers)

Example 49 with Query

use of org.geotoolkit.storage.feature.query.Query in project geotoolkit by Geomatys.

the class DBRelationOperation method apply.

@Override
public Property apply(Feature ftr, ParameterValueGroup pvg) {
    final Object key = ftr.getPropertyValue(relation.getCurrentColumn());
    final Query qb = new Query();
    qb.setTypeName(NamesExt.create(relation.getForeignTable()));
    qb.setSelection(relation.toFilter(key));
    final FeatureCollection res = store.createSession(false).getFeatureCollection(qb);
    final Object value;
    if (type.getMaximumOccurs() == 1) {
        try (FeatureIterator ite = res.iterator()) {
            if (ite.hasNext()) {
                value = ite.next();
            } else {
                value = null;
            }
        }
    } else {
        value = res;
    }
    return new AbstractAssociation(type) {

        @Override
        public Feature getValue() throws MultiValuedPropertyException {
            if (value == null || value instanceof Feature) {
                return (Feature) value;
            }
            throw new MultiValuedPropertyException();
        }

        @Override
        public Collection<Feature> getValues() {
            if (value == null) {
                return Collections.EMPTY_LIST;
            } else if (value instanceof Feature) {
                return Arrays.asList((Feature) value);
            } else {
                return res;
            }
        }

        @Override
        public void setValue(Feature value) throws InvalidPropertyValueException {
            throw new UnsupportedOperationException("Not supported.");
        }
    };
}
Also used : FeatureIterator(org.geotoolkit.storage.feature.FeatureIterator) AbstractAssociation(org.apache.sis.feature.AbstractAssociation) Query(org.geotoolkit.storage.feature.query.Query) FeatureCollection(org.geotoolkit.storage.feature.FeatureCollection) MultiValuedPropertyException(org.opengis.feature.MultiValuedPropertyException) Feature(org.opengis.feature.Feature)

Example 50 with Query

use of org.geotoolkit.storage.feature.query.Query in project geotoolkit by Geomatys.

the class StringToFeatureCollectionConverter method apply.

@Override
public FeatureCollection apply(final String s) throws UnconvertibleObjectException {
    if (s == null)
        throw new UnconvertibleObjectException("Empty FeatureCollection");
    try {
        String url;
        if (s.startsWith("file:")) {
            url = s;
        } else {
            url = "file:" + s;
        }
        final Map<String, Serializable> parameters = new HashMap<String, Serializable>();
        parameters.put(DataStoreProvider.LOCATION, URI.create(url));
        final FeatureStore store = (FeatureStore) DataStores.open(parameters);
        if (store == null) {
            throw new UnconvertibleObjectException("Invalid URL");
        }
        if (store.getNames().size() != 1) {
            throw new UnconvertibleObjectException("More than one FeatureCollection in the file");
        }
        final FeatureCollection collection = store.createSession(true).getFeatureCollection(new Query(store.getNames().iterator().next()));
        if (collection != null) {
            return collection;
        } else {
            throw new UnconvertibleObjectException("Collection not found");
        }
    } catch (DataStoreException ex) {
        throw new UnconvertibleObjectException(ex);
    }
}
Also used : UnconvertibleObjectException(org.apache.sis.util.UnconvertibleObjectException) Serializable(java.io.Serializable) DataStoreException(org.apache.sis.storage.DataStoreException) Query(org.geotoolkit.storage.feature.query.Query) HashMap(java.util.HashMap) FeatureCollection(org.geotoolkit.storage.feature.FeatureCollection) FeatureStore(org.geotoolkit.storage.feature.FeatureStore)

Aggregations

Query (org.geotoolkit.storage.feature.query.Query)82 Test (org.junit.Test)50 Feature (org.opengis.feature.Feature)43 FeatureType (org.opengis.feature.FeatureType)36 FeatureCollection (org.geotoolkit.storage.feature.FeatureCollection)31 FeatureIterator (org.geotoolkit.storage.feature.FeatureIterator)30 Session (org.geotoolkit.storage.feature.session.Session)20 Point (org.locationtech.jts.geom.Point)18 ResourceId (org.opengis.filter.ResourceId)18 GenericName (org.opengis.util.GenericName)15 Coordinate (org.locationtech.jts.geom.Coordinate)14 FeatureWriter (org.geotoolkit.storage.feature.FeatureWriter)13 File (java.io.File)10 FeatureTypeBuilder (org.apache.sis.feature.builder.FeatureTypeBuilder)9 FeatureStore (org.geotoolkit.storage.feature.FeatureStore)9 Filter (org.opengis.filter.Filter)9 URL (java.net.URL)8 Date (java.util.Date)8 DataStoreException (org.apache.sis.storage.DataStoreException)8 FeatureReader (org.geotoolkit.storage.feature.FeatureReader)8