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;
}
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);
}
}
}
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);
}
Aggregations