Search in sources :

Example 16 with InMemoryFeatureSet

use of org.geotoolkit.storage.memory.InMemoryFeatureSet in project geotoolkit by Geomatys.

the class JAXPStreamFeatureReader method readFeatureCollection.

private WritableFeatureSet readFeatureCollection(final String id) throws XMLStreamException {
    InMemoryFeatureSet collection = null;
    while (reader.hasNext()) {
        int event = reader.next();
        // we are looking for the root mark
        if (event == START_ELEMENT) {
            QName qName = reader.getName();
            final String markupName = qName.getLocalPart();
            if (markupName.equalsIgnoreCase("ExceptionReport")) {
                throw ExceptionReport.readException(reader);
            }
            final GenericName name = nameCache.get(qName);
            String fid = null;
            if (reader.getAttributeCount() > 0) {
                fid = reader.getAttributeValue(0);
            }
            // HACK : In WFS response, GML features are wrapped in a <wfs:(featureM|m)embers?> markup
            if (markupName.equals("featureMember") || markupName.equals("featureMembers") || markupName.equals("member")) {
                continue;
            } else if (markupName.equals("boundedBy")) {
                while (reader.hasNext()) {
                    event = reader.next();
                    if (event == START_ELEMENT) {
                        break;
                    }
                }
                String srsName = null;
                if (reader.getAttributeCount() > 0) {
                    srsName = reader.getAttributeValue(0);
                }
                final JTSEnvelope2D bounds = readBounds(srsName);
            } else {
                if (fid == null) {
                    LOGGER.info("Missing feature id : generating a random one");
                    fid = UUID.randomUUID().toString();
                }
                boolean find = false;
                StringBuilder expectedFeatureType = new StringBuilder();
                try {
                    FeatureType ft = featureTypes.get(name.toString());
                    if (collection == null) {
                        collection = new InMemoryFeatureSet(id, ft);
                    }
                    collection.add(Collections.singleton((Feature) readFeature(ft)).iterator());
                    find = true;
                } catch (IllegalNameException ex) {
                    for (GenericName n : featureTypes.getNames()) {
                        expectedFeatureType.append(n).append('\n');
                    }
                }
                if (!find) {
                    throw new IllegalArgumentException("The xml does not describe the same type of feature: \n " + "Expected: " + expectedFeatureType.toString() + '\n' + "But was: " + name);
                }
            }
        }
    }
    return collection;
}
Also used : FeatureType(org.opengis.feature.FeatureType) InMemoryFeatureSet(org.geotoolkit.storage.memory.InMemoryFeatureSet) GenericName(org.opengis.util.GenericName) JTSEnvelope2D(org.geotoolkit.geometry.jts.JTSEnvelope2D) QName(javax.xml.namespace.QName) Feature(org.opengis.feature.Feature) IllegalNameException(org.apache.sis.storage.IllegalNameException)

Example 17 with InMemoryFeatureSet

use of org.geotoolkit.storage.memory.InMemoryFeatureSet in project geotoolkit by Geomatys.

the class GMLStore method add.

@Override
public void add(Iterator<? extends Feature> features) throws DataStoreException {
    final Path tempFile = file.resolveSibling(file.getFileName().toString() + ".update");
    final FeatureType type = getType();
    final JAXPStreamFeatureWriter writer = new JAXPStreamFeatureWriter("3.2.1", "1.0.0", null);
    writer.getProperties().put(JAXPStreamFeatureReader.LONGITUDE_FIRST, longitudeFirst);
    try {
        // concatenate existing and new feature sets
        final List<Feature> lst = new ArrayList<>();
        while (features.hasNext()) {
            final Feature feature = features.next();
            if (!type.isAssignableFrom(feature.getType())) {
                throw new DataStoreException(feature.getType().getName() + " is not of type " + type.getName());
            }
            lst.add(feature);
        }
        final FeatureSet newfs = new InMemoryFeatureSet(type, lst);
        final FeatureSet all = ConcatenatedFeatureSet.create(this, newfs);
        try {
            UPDATELOCK.writeLock().lock();
            // write
            writer.write(all, tempFile);
            // replace files
            try {
                MAINLOCK.writeLock().lock();
                Files.move(tempFile, file, StandardCopyOption.REPLACE_EXISTING);
            } finally {
                MAINLOCK.writeLock().unlock();
            }
        } finally {
            UPDATELOCK.writeLock().unlock();
        }
    } catch (IOException | XMLStreamException ex) {
        throw new DataStoreException(ex.getMessage(), ex);
    } finally {
        try {
            writer.dispose();
        } catch (IOException | XMLStreamException ex) {
            throw new DataStoreException(ex.getMessage(), ex);
        }
    }
}
Also used : Path(java.nio.file.Path) FeatureType(org.opengis.feature.FeatureType) DataStoreException(org.apache.sis.storage.DataStoreException) InMemoryFeatureSet(org.geotoolkit.storage.memory.InMemoryFeatureSet) XMLStreamException(javax.xml.stream.XMLStreamException) ArrayList(java.util.ArrayList) WritableFeatureSet(org.apache.sis.storage.WritableFeatureSet) InMemoryFeatureSet(org.geotoolkit.storage.memory.InMemoryFeatureSet) ConcatenatedFeatureSet(org.apache.sis.internal.storage.ConcatenatedFeatureSet) FeatureSet(org.apache.sis.storage.FeatureSet) IOException(java.io.IOException) JAXPStreamFeatureWriter(org.geotoolkit.feature.xml.jaxp.JAXPStreamFeatureWriter) Feature(org.opengis.feature.Feature)

Example 18 with InMemoryFeatureSet

use of org.geotoolkit.storage.memory.InMemoryFeatureSet in project geotoolkit by Geomatys.

the class XmlFeatureTest method testWriteReferenceCollection.

@Test
public void testWriteReferenceCollection() throws JAXBException, IOException, XMLStreamException, DataStoreException, ParserConfigurationException, SAXException {
    final StringWriter temp = new StringWriter();
    final XmlFeatureWriter writer = new JAXPStreamFeatureWriter("3.2.1", "1.1.0", null);
    Feature f1 = typeReference.newInstance();
    Feature f2 = typeReference.newInstance();
    Feature f3 = typeReference.newInstance();
    Feature f4 = typeReference.newInstance();
    f1.setPropertyValue("identifier", "id1");
    f2.setPropertyValue("identifier", "id2");
    f3.setPropertyValue("identifier", "id3");
    f4.setPropertyValue("identifier", "id4");
    f1.setPropertyValue("username", "einstein");
    f2.setPropertyValue("username", "sobel");
    f3.setPropertyValue("username", "snow-white");
    f4.setPropertyValue("username", "admin");
    f4.setPropertyValue("link", f1);
    f4.setPropertyValue("linkedTo", Arrays.asList(f2, f3));
    final InMemoryFeatureSet fs = new InMemoryFeatureSet(NamesExt.create("one-of-a-kind-ID"), typeReference, Arrays.asList(f1, f2, f3, f4));
    writer.write(fs, temp);
    String result = temp.toString();
    result = result.replaceAll("timeStamp=\"[^\"]*\" ", "timeStamp=\"2002-05-30T09:00:00\" ");
    String expResult = IOUtilities.toString(XmlFeatureTest.class.getResourceAsStream("/org/geotoolkit/feature/xml/CollectionReference.xml"));
    DomCompare.compare(expResult, result);
}
Also used : InMemoryFeatureSet(org.geotoolkit.storage.memory.InMemoryFeatureSet) StringWriter(java.io.StringWriter) JAXPStreamFeatureWriter(org.geotoolkit.feature.xml.jaxp.JAXPStreamFeatureWriter) Feature(org.opengis.feature.Feature)

Aggregations

InMemoryFeatureSet (org.geotoolkit.storage.memory.InMemoryFeatureSet)18 Feature (org.opengis.feature.Feature)18 FeatureType (org.opengis.feature.FeatureType)17 FeatureTypeBuilder (org.apache.sis.feature.builder.FeatureTypeBuilder)11 FeatureSet (org.apache.sis.storage.FeatureSet)11 MapLayer (org.apache.sis.portrayal.MapLayer)10 ArrayList (java.util.ArrayList)9 GeneralEnvelope (org.apache.sis.geometry.GeneralEnvelope)8 Coordinate (org.locationtech.jts.geom.Coordinate)8 MapLayers (org.apache.sis.portrayal.MapLayers)7 MutableStyle (org.geotoolkit.style.MutableStyle)7 Test (org.junit.Test)7 Dimension (java.awt.Dimension)6 WritableFeatureSet (org.apache.sis.storage.WritableFeatureSet)6 BufferedImage (java.awt.image.BufferedImage)5 IOException (java.io.IOException)5 DataStoreException (org.apache.sis.storage.DataStoreException)5 SampleDimension (org.apache.sis.coverage.SampleDimension)4 Geometry (org.locationtech.jts.geom.Geometry)4 Point (org.locationtech.jts.geom.Point)4