use of org.geotoolkit.data.mapinfo.mif.MIFStore 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.data.mapinfo.mif.MIFStore 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);
}
}
use of org.geotoolkit.data.mapinfo.mif.MIFStore in project geotoolkit by Geomatys.
the class MIFStoreTest method multipleGeometryTypes.
/**
* Check that we're capable of writing then read back features with
* different type of geometries (point, polygon, etc.) into a single MIF-MID
* file.
*/
@Test
public void multipleGeometryTypes() throws Exception {
final Path tmpFile = Files.createTempFile(tempDir, "multiple", ".mif");
try (final MIFStore store = new MIFStore(tmpFile.toUri())) {
final FeatureTypeBuilder ftb = new FeatureTypeBuilder();
ftb.setName("multi");
ftb.addAttribute(String.class).setName("otherProperty");
ftb.addAttribute(Geometry.class).setName("geometry").setCRS(CommonCRS.WGS84.normalizedGeographic()).addRole(AttributeRole.DEFAULT_GEOMETRY);
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();
final Point point = GF.createPoint(new Coordinate(10.3, 15.7));
feature1.setPropertyValue("otherProperty", "here is a point");
feature1.setPropertyValue("geometry", point);
final Feature feature2 = ft.newInstance();
final Coordinate startPoint = new Coordinate(-1.6, -5.4);
Coordinate[] coords = { startPoint, new Coordinate(0, 0), new Coordinate(3.1, 3.4) };
final MultiLineString line = GF.createMultiLineString(new LineString[] { GF.createLineString(coords) });
feature2.setPropertyValue("otherProperty", "here is a line");
feature2.setPropertyValue("geometry", line);
final Feature feature3 = ft.newInstance();
coords = new Coordinate[] { startPoint, new Coordinate(-6, -6), new Coordinate(-3.1, -3.4), startPoint };
final MultiPolygon polygon = GF.createMultiPolygon(new Polygon[] { GF.createPolygon(coords) });
feature3.setPropertyValue("otherProperty", "here is a polygon");
feature3.setPropertyValue("geometry", polygon);
final Feature feature4 = ft.newInstance();
final Geometry[] geometries = new Geometry[] { polygon, line, GF.createMultiPoint(new Point[] { point }) };
feature4.setPropertyValue("otherProperty", "here is a collection of geometries.");
feature4.setPropertyValue("geometry", GF.createGeometryCollection(geometries));
final List<Feature> expectedFeatures = new ArrayList<>(4);
expectedFeatures.add(feature1);
expectedFeatures.add(feature2);
expectedFeatures.add(feature3);
expectedFeatures.add(feature4);
resource.add(expectedFeatures.iterator());
checkFeatures(expectedFeatures, resource, new FeatureQuery());
}
}
use of org.geotoolkit.data.mapinfo.mif.MIFStore in project geotoolkit by Geomatys.
the class MIFStoreTest method testCreate.
/**
* Test store creation.
*
* @throws Exception
*/
@Test
public void testCreate() throws Exception {
final Path f = Files.createTempFile(tempDir, "test", ".mif");
final MIFProvider ff = (MIFProvider) DataStores.getProviderById("MIF-MID");
final Parameters params = Parameters.castOrWrap(ff.getOpenParameters().createValue());
params.getOrCreate(MIFProvider.PATH).setValue(f.toUri());
// create new store from scratch
try (final MIFStore store = (MIFStore) ff.open(params)) {
assertNotNull(store);
// 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());
WritableFeatureSet resource = (WritableFeatureSet) store.components().iterator().next();
FeatureType ft = resource.getType();
for (PropertyType desc : featureType.getProperties(true)) {
if (!(desc instanceof AttributeType))
// ignore conventions and operations
continue;
PropertyType td = ft.getProperty(desc.getName().toString());
assertNotNull(td);
assertTrue(((AttributeType) td).getValueClass().isAssignableFrom(((AttributeType) desc).getValueClass()));
}
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)));
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)));
final List<Feature> expectedFeatures = new ArrayList<>(2);
expectedFeatures.add(feature1);
expectedFeatures.add(feature2);
resource.add(Arrays.asList(feature1, feature2).iterator());
checkFeatures(expectedFeatures, resource, new FeatureQuery());
}
}
Aggregations