Search in sources :

Example 1 with FeatureStoreRuntimeException

use of org.geotoolkit.storage.feature.FeatureStoreRuntimeException in project geotoolkit by Geomatys.

the class CategoryStyleBuilder method analyze.

public void analyze(final MapLayer layer) {
    Resource resource = layer.getData();
    if (!(resource instanceof FeatureSet)) {
        throw new IllegalArgumentException("Layer resource must be a FeatureSet");
    }
    this.layer = layer;
    fts.rules().clear();
    properties.clear();
    if (layer != null) {
        FeatureType schema;
        try {
            schema = ((FeatureSet) resource).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 (!Geometry.class.isAssignableFrom(type)) {
                    properties.add(ff.property(desc.getName().tip().toString()));
                }
            }
        }
        // find the geometry class for template
        Class<?> geoClass = null;
        try {
            PropertyType geo = FeatureExt.getDefaultGeometry(schema);
            geoClass = Features.toAttribute(geo).map(AttributeType::getValueClass).orElse(null);
        } catch (PropertyNotFoundException ex) {
            LOGGER.log(Level.FINE, "No sis:geometry property found", ex);
        }
        if (geoClass == null) {
            return;
        }
        if (Polygon.class.isAssignableFrom(geoClass) || MultiPolygon.class.isAssignableFrom(geoClass)) {
            Stroke stroke = sf.stroke(Color.BLACK, 1);
            Fill fill = sf.fill(Color.BLUE);
            template = sf.polygonSymbolizer(stroke, fill, null);
            expectedType = PolygonSymbolizer.class;
        } else if (LineString.class.isAssignableFrom(geoClass) || MultiLineString.class.isAssignableFrom(geoClass)) {
            Stroke stroke = sf.stroke(Color.BLUE, 2);
            template = sf.lineSymbolizer(stroke, null);
            expectedType = LineSymbolizer.class;
        } else {
            Stroke stroke = sf.stroke(Color.BLACK, 1);
            Fill fill = sf.fill(Color.BLUE);
            List<GraphicalSymbol> symbols = new ArrayList<>();
            symbols.add(sf.mark(StyleConstants.MARK_CIRCLE, fill, stroke));
            Graphic gra = sf.graphic(symbols, ff.literal(1), ff.literal(12), ff.literal(0), sf.anchorPoint(), sf.displacement());
            template = sf.pointSymbolizer(gra, null);
            expectedType = PointSymbolizer.class;
        }
        // try to rebuild the previous analyze if it was one
        List<? extends FeatureTypeStyle> ftss = layer.getStyle().featureTypeStyles();
        if (ftss.size() == 1) {
            FeatureTypeStyle fts = ftss.get(0);
            // defensive copy avoid synchronization
            List<? extends Rule> candidateRules = new ArrayList<>(fts.rules());
            for (Rule r : candidateRules) {
                // defensive copy avoid synchronization
                List<? extends Symbolizer> candidateSymbols = new ArrayList<>(r.symbolizers());
                if (candidateSymbols.size() != 1)
                    break;
                Symbolizer symbol = candidateSymbols.get(0);
                if (expectedType.isInstance(symbol)) {
                    if (r.isElseFilter()) {
                        // it looks like it's a valid classification "other" rule
                        this.fts.rules().add((MutableRule) r);
                        template = symbol;
                        other = true;
                    } else {
                        Filter f = r.getFilter();
                        if (f != null && f.getOperatorType() == ComparisonOperatorName.PROPERTY_IS_EQUAL_TO) {
                            BinaryComparisonOperator equal = (BinaryComparisonOperator) f;
                            Expression exp1 = equal.getOperand1();
                            Expression exp2 = equal.getOperand2();
                            if (exp1 instanceof ValueReference && exp2 instanceof Literal) {
                                if (properties.contains(exp1)) {
                                    // it looks like it's a valid classification property rule
                                    this.fts.rules().add((MutableRule) r);
                                    template = symbol;
                                    currentProperty = (ValueReference) exp1;
                                } else {
                                    // property is not in the schema
                                    break;
                                }
                            } else if (exp2 instanceof ValueReference && exp1 instanceof Literal) {
                                if (properties.contains(exp2)) {
                                    // it looks like it's a valid classification property rule
                                    this.fts.rules().add((MutableRule) r);
                                    template = symbol;
                                    currentProperty = (ValueReference) exp2;
                                } else {
                                    // property is not in the schema
                                    break;
                                }
                            } else {
                                // mismatch analyze structure
                                break;
                            }
                        }
                    }
                } else {
                    break;
                }
            }
        }
    }
}
Also used : FeatureType(org.opengis.feature.FeatureType) Fill(org.opengis.style.Fill) PropertyNotFoundException(org.opengis.feature.PropertyNotFoundException) ArrayList(java.util.ArrayList) PropertyType(org.opengis.feature.PropertyType) AttributeType(org.opengis.feature.AttributeType) Literal(org.opengis.filter.Literal) List(java.util.List) ArrayList(java.util.ArrayList) Polygon(org.locationtech.jts.geom.Polygon) MultiPolygon(org.locationtech.jts.geom.MultiPolygon) ValueReference(org.opengis.filter.ValueReference) PointSymbolizer(org.opengis.style.PointSymbolizer) DataStoreException(org.apache.sis.storage.DataStoreException) Stroke(org.opengis.style.Stroke) Graphic(org.opengis.style.Graphic) Resource(org.apache.sis.storage.Resource) PointSymbolizer(org.opengis.style.PointSymbolizer) PolygonSymbolizer(org.opengis.style.PolygonSymbolizer) LineSymbolizer(org.opengis.style.LineSymbolizer) Symbolizer(org.opengis.style.Symbolizer) Geometry(org.locationtech.jts.geom.Geometry) MutableRule(org.geotoolkit.style.MutableRule) MultiPolygon(org.locationtech.jts.geom.MultiPolygon) Filter(org.opengis.filter.Filter) Expression(org.opengis.filter.Expression) LineSymbolizer(org.opengis.style.LineSymbolizer) FeatureStoreRuntimeException(org.geotoolkit.storage.feature.FeatureStoreRuntimeException) FeatureSet(org.apache.sis.storage.FeatureSet) MutableFeatureTypeStyle(org.geotoolkit.style.MutableFeatureTypeStyle) FeatureTypeStyle(org.opengis.style.FeatureTypeStyle) Rule(org.opengis.style.Rule) MutableRule(org.geotoolkit.style.MutableRule) BinaryComparisonOperator(org.opengis.filter.BinaryComparisonOperator)

Example 2 with FeatureStoreRuntimeException

use of org.geotoolkit.storage.feature.FeatureStoreRuntimeException in project geotoolkit by Geomatys.

the class CategoryStyleBuilder method create.

public List<MutableRule> create() {
    // search the different values
    final Set<Object> differentValues = new HashSet<>();
    final ValueReference property = currentProperty;
    final FeatureSet resource = (FeatureSet) layer.getData();
    final Query query = new Query();
    try {
        query.setTypeName(resource.getType().getName());
    } catch (DataStoreException ex) {
        LOGGER.log(Level.FINE, "Error while accessing data", ex);
    }
    query.setProperties(new String[] { property.getXPath() });
    try (Stream<Feature> stream = resource.subset(query).features(false)) {
        final Iterator<Feature> features = stream.iterator();
        while (features.hasNext()) {
            final Feature feature = features.next();
            differentValues.add(property.apply(feature));
        }
    } catch (DataStoreException | FeatureStoreRuntimeException ex) {
        LOGGER.log(Level.FINE, "Error while accessing data", ex);
    }
    // generate the different rules
    fts.rules().clear();
    for (Object obj : differentValues) {
        fts.rules().add(createRule(property, obj));
    }
    // generate the other rule if asked
    if (other) {
        MutableRule r = sf.rule(createSymbolizer());
        r.setElseFilter(true);
        r.setDescription(sf.description("other", "other"));
        fts.rules().add(r);
    }
    return fts.rules();
}
Also used : DataStoreException(org.apache.sis.storage.DataStoreException) Query(org.geotoolkit.storage.feature.query.Query) Feature(org.opengis.feature.Feature) MutableRule(org.geotoolkit.style.MutableRule) FeatureStoreRuntimeException(org.geotoolkit.storage.feature.FeatureStoreRuntimeException) FeatureSet(org.apache.sis.storage.FeatureSet) HashSet(java.util.HashSet) ValueReference(org.opengis.filter.ValueReference)

Example 3 with FeatureStoreRuntimeException

use of org.geotoolkit.storage.feature.FeatureStoreRuntimeException in project geotoolkit by Geomatys.

the class MIFReader method next.

/**
 * {@inheritDoc}
 */
@Override
public Feature next() throws FeatureStoreRuntimeException {
    try {
        checkScanners();
    } catch (DataStoreException | IOException ex) {
        throw new FeatureStoreRuntimeException("Cannot read data", ex);
    }
    Feature resFeature = null;
    // We check the MIF file first, because it will define the feature count to reach the next good typed data.
    if (readMif) {
        String currentPattern;
        while (mifScanner.hasNextLine()) {
            currentPattern = mifScanner.findInLine(GEOMETRY_ID_PATTERN);
            if (currentPattern == null) {
                // Empty or non-compliant line. We just ignore it.
                LOGGER.log(Level.FINE, "Invalid line : " + mifScanner.findInLine(".*"));
            } else {
                final MIFUtils.GeometryType geomType = MIFUtils.getGeometryType(currentPattern);
                if (geomType == null) {
                    LOGGER.log(Level.WARNING, "Unrecognized geometry flag in MIF file : " + currentPattern);
                } else {
                    resFeature = geomType.binding.buildType(master.getMifCRS(), readType).newInstance();
                    try {
                        geomType.readGeometry(mifScanner, resFeature, master.getTransform());
                    } catch (Exception ex) {
                        LOGGER.log(Level.WARNING, ex.getLocalizedMessage(), ex);
                    }
                    break;
                // We must check if we're on a Geometry naming line to increment the counter of past geometries.
                }
            }
            mifScanner.nextLine();
        }
    }
    // No geometry has been read.
    if (resFeature == null) {
        resFeature = readType.newInstance();
    }
    if (readMid) {
        // parse MID line.
        while (midCounter < mifCounter) {
            midScanner.nextLine();
            midCounter++;
        }
        final String line = midScanner.nextLine();
        final CharSequence[] split = CharSequences.split(line, master.mifDelimiter);
        for (final Map.Entry<String, Integer> entry : propertiesToRead.entrySet()) {
            final Integer idx = entry.getValue();
            try {
                resFeature.setPropertyValue(entry.getKey(), converters[idx].apply(split[idx]));
            } catch (RuntimeException e) {
                Logging.recoverableException(LOGGER, MIFReader.class, "next", e);
            }
        }
        midCounter++;
    }
    if (readMif) {
        mifCounter++;
    }
    return resFeature;
}
Also used : DataStoreException(org.apache.sis.storage.DataStoreException) IOException(java.io.IOException) Feature(org.opengis.feature.Feature) DataStoreException(org.apache.sis.storage.DataStoreException) ParseException(java.text.ParseException) FeatureStoreRuntimeException(org.geotoolkit.storage.feature.FeatureStoreRuntimeException) IOException(java.io.IOException) PropertyNotFoundException(org.opengis.feature.PropertyNotFoundException) FeatureStoreRuntimeException(org.geotoolkit.storage.feature.FeatureStoreRuntimeException) FeatureStoreRuntimeException(org.geotoolkit.storage.feature.FeatureStoreRuntimeException) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with FeatureStoreRuntimeException

use of org.geotoolkit.storage.feature.FeatureStoreRuntimeException in project geotoolkit by Geomatys.

the class OMFeatureTypes method getFeatureTypes.

public static GenericNameIndex<FeatureType> getFeatureTypes(final String name) {
    final GenericNameIndex<FeatureType> types = new GenericNameIndex<>();
    final GenericName OM_TN_SAMPLINGPOINT = NamesExt.create(OM_NAMESPACE, name);
    final FeatureTypeBuilder featureTypeBuilder = new FeatureTypeBuilder();
    featureTypeBuilder.setName(OM_TN_SAMPLINGPOINT);
    featureTypeBuilder.setSuperTypes(GMLConvention.ABSTRACTFEATURETYPE_31);
    featureTypeBuilder.addAttribute(String.class).setName(ATT_DESC).setMinimumOccurs(0).setMaximumOccurs(1);
    featureTypeBuilder.addAttribute(String.class).setName(ATT_NAME).setMinimumOccurs(1).setMaximumOccurs(Integer.MAX_VALUE);
    featureTypeBuilder.addAttribute(String.class).setName(ATT_SAMPLED).setMinimumOccurs(0).setMaximumOccurs(Integer.MAX_VALUE).addCharacteristic(GMLConvention.NILLABLE_CHARACTERISTIC);
    featureTypeBuilder.addAttribute(Geometry.class).setName(ATT_POSITION).addRole(AttributeRole.DEFAULT_GEOMETRY);
    try {
        types.add(null, OM_TN_SAMPLINGPOINT, featureTypeBuilder.build());
    } catch (IllegalNameException ex) {
        // won't happen
        throw new FeatureStoreRuntimeException(ex);
    }
    return types;
}
Also used : FeatureType(org.opengis.feature.FeatureType) FeatureTypeBuilder(org.apache.sis.feature.builder.FeatureTypeBuilder) GenericName(org.opengis.util.GenericName) GenericNameIndex(org.geotoolkit.storage.feature.GenericNameIndex) FeatureStoreRuntimeException(org.geotoolkit.storage.feature.FeatureStoreRuntimeException) IllegalNameException(org.apache.sis.storage.IllegalNameException)

Example 5 with FeatureStoreRuntimeException

use of org.geotoolkit.storage.feature.FeatureStoreRuntimeException in project geotoolkit by Geomatys.

the class FeatureCollectionToReferenceConverter method convert.

/**
 * {@inheritDoc}
 */
@Override
public Reference convert(final FeatureCollection source, final Map<String, Object> params) throws UnconvertibleObjectException {
    if (params.get(TMP_DIR_PATH) == null) {
        throw new UnconvertibleObjectException("The output directory should be defined.");
    }
    if (params.get(TMP_DIR_URL) == null) {
        throw new UnconvertibleObjectException("The output directory URL should be defined.");
    }
    if (source == null) {
        throw new UnconvertibleObjectException("The output data should be defined.");
    }
    // TODO : useless test, null test above is all we need, fix this and other converters
    if (!(source instanceof FeatureCollection)) {
        throw new UnconvertibleObjectException("The requested output data is not an instance of FeatureCollection.");
    }
    Reference reference = new Reference();
    reference.setMimeType((String) params.get(MIME));
    reference.setEncoding((String) params.get(ENCODING));
    final FeatureType ft = source.getType();
    final String namespace = NamesExt.getNamespace(ft.getName());
    final Map<String, String> schemaLocation = new HashMap<>();
    final String randomFileName = UUID.randomUUID().toString();
    if (WPSMimeType.APP_GEOJSON.val().equalsIgnoreCase(reference.getMimeType())) {
        // create file
        final Path dataFile = buildPath(params, randomFileName + ".json");
        try (OutputStream fos = Files.newOutputStream(dataFile, CREATE, TRUNCATE_EXISTING, WRITE);
            GeoJSONStreamWriter writer = new GeoJSONStreamWriter(fos, ft, WPSConvertersUtils.FRACTION_DIGITS);
            Stream<Feature> st = source.features(false)) {
            Iterator<Feature> iterator = st.iterator();
            while (iterator.hasNext()) {
                Feature next = iterator.next();
                Feature neww = writer.next();
                FeatureExt.copy(next, neww, false);
                writer.write();
            }
        } catch (DataStoreException e) {
            throw new UnconvertibleObjectException("Can't write Feature into GeoJSON output stream.", e);
        } catch (IOException e) {
            throw new UnconvertibleObjectException(e);
        }
        final String relLoc = getRelativeLocation(dataFile, params);
        reference.setHref(params.get(TMP_DIR_URL) + "/" + relLoc);
        reference.setSchema(null);
    } else if (WPSMimeType.APP_GML.val().equalsIgnoreCase(reference.getMimeType()) || WPSMimeType.TEXT_XML.val().equalsIgnoreCase(reference.getMimeType()) || WPSMimeType.TEXT_GML.val().equalsIgnoreCase(reference.getMimeType())) {
        try {
            reference.setSchema(WPSConvertersUtils.writeSchema(ft, params));
            schemaLocation.put(namespace, reference.getSchema());
        } catch (JAXBException ex) {
            throw new UnconvertibleObjectException("Can't write FeatureType into xsd schema.", ex);
        } catch (IOException ex) {
            throw new UnconvertibleObjectException("Can't create xsd schema file.", ex);
        }
        // Write Feature
        final JAXPStreamFeatureWriter featureWriter = new JAXPStreamFeatureWriter(schemaLocation);
        // create file
        final Path dataFile = buildPath(params, randomFileName + ".xml");
        try (final OutputStream dataStream = Files.newOutputStream(dataFile);
            final AutoCloseable xmlCloser = () -> featureWriter.dispose()) {
            // Write feature in file
            featureWriter.write(source, dataStream);
            final String relLoc = getRelativeLocation(dataFile, params);
            reference.setHref(params.get(TMP_DIR_URL) + "/" + relLoc);
        } catch (XMLStreamException ex) {
            throw new UnconvertibleObjectException("Stax exception while writing the feature collection", ex);
        } catch (DataStoreException ex) {
            throw new UnconvertibleObjectException("FeatureStore exception while writing the feature collection", ex);
        } catch (FeatureStoreRuntimeException ex) {
            throw new UnconvertibleObjectException("FeatureStoreRuntimeException exception while writing the feature collection", ex);
        } catch (Exception ex) {
            throw new UnconvertibleObjectException(ex);
        }
    } else {
        throw new UnconvertibleObjectException("Unsupported mime-type for " + this.getClass().getName() + " : " + reference.getMimeType());
    }
    return reference;
}
Also used : Path(java.nio.file.Path) FeatureType(org.opengis.feature.FeatureType) DataStoreException(org.apache.sis.storage.DataStoreException) HashMap(java.util.HashMap) Reference(org.geotoolkit.wps.xml.v200.Reference) JAXBException(javax.xml.bind.JAXBException) JAXPStreamFeatureWriter(org.geotoolkit.feature.xml.jaxp.JAXPStreamFeatureWriter) Feature(org.opengis.feature.Feature) XMLStreamException(javax.xml.stream.XMLStreamException) DataStoreException(org.apache.sis.storage.DataStoreException) FeatureStoreRuntimeException(org.geotoolkit.storage.feature.FeatureStoreRuntimeException) JAXBException(javax.xml.bind.JAXBException) UnconvertibleObjectException(org.apache.sis.util.UnconvertibleObjectException) UnconvertibleObjectException(org.apache.sis.util.UnconvertibleObjectException) XMLStreamException(javax.xml.stream.XMLStreamException) FeatureCollection(org.geotoolkit.storage.feature.FeatureCollection) GeoJSONStreamWriter(org.geotoolkit.storage.geojson.GeoJSONStreamWriter) FeatureStoreRuntimeException(org.geotoolkit.storage.feature.FeatureStoreRuntimeException)

Aggregations

FeatureStoreRuntimeException (org.geotoolkit.storage.feature.FeatureStoreRuntimeException)35 DataStoreException (org.apache.sis.storage.DataStoreException)21 IOException (java.io.IOException)17 Feature (org.opengis.feature.Feature)16 HashMap (java.util.HashMap)8 FeatureType (org.opengis.feature.FeatureType)8 PropertyNotFoundException (org.opengis.feature.PropertyNotFoundException)5 PropertyType (org.opengis.feature.PropertyType)5 Path (java.nio.file.Path)4 XMLStreamException (javax.xml.stream.XMLStreamException)4 FeatureSet (org.apache.sis.storage.FeatureSet)4 JAXPStreamFeatureWriter (org.geotoolkit.feature.xml.jaxp.JAXPStreamFeatureWriter)4 Query (org.geotoolkit.storage.feature.query.Query)4 AttributeType (org.opengis.feature.AttributeType)4 ResourceId (org.opengis.filter.ResourceId)4 Date (java.util.Date)3 JAXBException (javax.xml.bind.JAXBException)3 UnconvertibleObjectException (org.apache.sis.util.UnconvertibleObjectException)3 FeatureIterator (org.geotoolkit.storage.feature.FeatureIterator)3 GeoJSONStreamWriter (org.geotoolkit.storage.geojson.GeoJSONStreamWriter)3