use of org.geotoolkit.storage.feature.DefiningFeatureSet in project geotoolkit by Geomatys.
the class MIFStore method add.
@Override
public synchronized Resource add(Resource resource) throws DataStoreException {
final FeatureType type;
if (resource instanceof DefiningFeatureSet) {
type = ((DefiningFeatureSet) resource).getType();
} else if (resource instanceof FeatureSet) {
type = ((FeatureSet) resource).getType();
} else {
throw new DataStoreException("Unexpected resource type");
}
try {
manager.addSchema(type.getName(), type);
} catch (URISyntaxException | IOException e) {
throw new DataStoreException("We're unable to add a schema because we can't access source files.", e);
}
// clear cache
components = null;
return findResource(type.getName().toString());
}
use of org.geotoolkit.storage.feature.DefiningFeatureSet in project geotoolkit by Geomatys.
the class MIFStoreTest method filterProperties.
/**
* Check that we can filter data properties at read time (using {@link Query#getPropertyNames() }).
* @throws Exception
*/
@Test
public void filterProperties() throws Exception {
final Path tmpFile = Files.createTempFile(tempDir, "filtered", ".mif");
try (final MIFStore store = new MIFStore(tmpFile.toUri())) {
// create a feature type
final FeatureTypeBuilder ftb = new FeatureTypeBuilder();
ftb.setName("test");
ftb.addAttribute(Integer.class).setName("integerProp");
ftb.addAttribute(Double.class).setName("doubleProp");
ftb.addAttribute(String.class).setName("stringProp");
ftb.addAttribute(Point.class).setName("geometryProp").setCRS(CommonCRS.WGS84.normalizedGeographic());
final FeatureType featureType = ftb.build();
store.add(new DefiningFeatureSet(featureType, null));
assertEquals(1, store.components().size());
final WritableFeatureSet resource = (WritableFeatureSet) store.components().iterator().next();
final FeatureType ft = resource.getType();
final Feature feature1 = ft.newInstance();
feature1.setPropertyValue("integerProp", 8);
feature1.setPropertyValue("doubleProp", 3.12);
feature1.setPropertyValue("stringProp", "hello");
feature1.setPropertyValue("geometryProp", GF.createPoint(new Coordinate(10.3, 15.7)));
final Feature feature2 = ft.newInstance();
feature2.setPropertyValue("integerProp", -15);
feature2.setPropertyValue("doubleProp", -7.1);
feature2.setPropertyValue("stringProp", "world");
feature2.setPropertyValue("geometryProp", GF.createPoint(new Coordinate(-1.6, -5.4)));
List<Feature> expectedFeatures = new ArrayList<>(2);
expectedFeatures.add(feature1);
expectedFeatures.add(feature2);
resource.add(expectedFeatures.iterator());
// filter output
final FeatureQuery query = new FeatureQuery();
final String[] filteredProps = new String[] { "stringProp", "integerProp" };
query.setProjection(new FeatureQuery.NamedExpression(FF.property("stringProp")), new FeatureQuery.NamedExpression(FF.property("integerProp")));
ftb.getProperty("doubleProp").remove();
ftb.getProperty("geometryProp").remove();
// Modify original dataset to contain only properties kept in above query.
final FeatureType filteredType = ftb.build();
expectedFeatures = expectedFeatures.stream().map(f -> {
final Feature result = filteredType.newInstance();
for (final String prop : filteredProps) {
result.setPropertyValue(prop, f.getPropertyValue(prop));
}
return result;
}).collect(Collectors.toList());
checkFeatures(expectedFeatures, resource, query);
}
}
use of org.geotoolkit.storage.feature.DefiningFeatureSet in project geotoolkit by Geomatys.
the class WeakListenerTest method testWeakStorageListener.
/**
* Test no memory leak in weak style listener
*/
@Test
public void testWeakStorageListener() throws DataStoreException {
FeatureTypeBuilder ftb = new FeatureTypeBuilder();
ftb.setName("test1");
ftb.addAttribute(Integer.class).setName("att");
final FeatureType type1 = ftb.build();
ftb = new FeatureTypeBuilder();
ftb.setName("test2");
ftb.addAttribute(Integer.class).setName("att2");
final FeatureType type2 = ftb.build();
final AtomicInteger count = new AtomicInteger(0);
final InMemoryAggregate store = new InMemoryAggregate();
StoreListener listener = new StoreListener() {
@Override
public void eventOccured(StoreEvent event) {
count.incrementAndGet();
}
};
final StorageListener.Weak ref = new StorageListener.Weak(listener);
ref.registerSource(store);
store.add(new DefiningFeatureSet(type1, null));
assertEquals(1, count.get());
listener = null;
System.gc();
store.add(new DefiningFeatureSet(type2, null));
// listener should have desapear now, so the event should not have been send
assertEquals(1, count.get());
}
use of org.geotoolkit.storage.feature.DefiningFeatureSet in project geotoolkit by Geomatys.
the class MemoryFeatureStoreDemo method main.
public static void main(String[] args) throws DataStoreException {
Demos.init();
// create the datastore
final InMemoryStore store = new InMemoryStore();
// add a schema in the datastore
final FeatureTypeBuilder ftb = new FeatureTypeBuilder();
ftb.setName("http://geomatys.com", "test");
ftb.addAttribute(String.class).setName("type");
ftb.addAttribute(Point.class).setName("the_geom").setCRS(CommonCRS.WGS84.normalizedGeographic());
final FeatureType type = ftb.build();
store.add(new DefiningFeatureSet(type, null));
// query the featurestore like any other
for (FeatureSet fs : DataStores.flatten(store, true, FeatureSet.class)) {
System.out.println(fs.getType());
}
}
use of org.geotoolkit.storage.feature.DefiningFeatureSet in project geotoolkit by Geomatys.
the class MifDemo method main.
public static void main(String[] args) throws DataStoreException, NoSuchIdentifierException {
Demos.init();
try {
// First of all, we delete the files we want to write in.
URL destinationURL = new URL(DESTINATION_MIF);
File tmpMIF = new File(destinationURL.toURI());
if (tmpMIF.exists()) {
tmpMIF.delete();
}
File tmpMID = new File(DESTINATION_MID);
if (tmpMID.exists()) {
tmpMID.delete();
}
// To build a valid MIFFeatureStore, the MIF file URL must be given to the store parameters.
URL dataLocation = MifDemo.class.getResource("/data/world/HY_WATER_AREA_POLYGON.mif");
System.out.println(MIFProvider.PARAMETERS_DESCRIPTOR);
final Parameters parameters = Parameters.castOrWrap(MIFProvider.PARAMETERS_DESCRIPTOR.createValue());
parameters.getOrCreate(MIFProvider.PATH).setValue(dataLocation.toURI());
// Initialize the store, and create a session to browse it's data.
final DataStore store1 = DataStores.open(parameters);
// Create a mif featureStore for writing operation.
final Parameters writerParam = Parameters.castOrWrap(MIFProvider.PARAMETERS_DESCRIPTOR.createValue());
writerParam.getOrCreate(MIFProvider.PATH).setValue(destinationURL.toURI());
final MIFStore writingStore = new MIFStore(writerParam);
// Here we get a function to set mid file attributes delimiter. MID file is a sort of CSV, and default
// delimiter (which is \t) can be changed by user. Here I choose coma.
writingStore.setDelimiter(',');
// file. All those types inherit from base type, so we get all attributes associated with the geometry.
for (FeatureSet fs : DataStores.flatten(store1, true, FeatureSet.class)) {
final FeatureType fType = fs.getType();
// Get all features of given type.
try (Stream<Feature> stream = fs.features(false)) {
// If the type we got don't get super type, it's the store base type. Just print info.
if (fType.getSuperTypes().isEmpty()) {
Iterator it = stream.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
} else {
// If we got the geometric data, we write it into new MIF/MID files.
// First we must specify we must add a featureType to the store. If no base Type have already been
// specified, the given feature type parent will be used. Else, we check that given type is compliant
// with stored base type.
WritableFeatureSet nr = (WritableFeatureSet) writingStore.add(new DefiningFeatureSet(fType, null));
nr.add(stream.iterator());
}
}
}
} catch (Exception ex) {
LOGGER.log(Level.SEVERE, "Unexpected exception happened.", ex);
}
}
Aggregations