use of org.geotoolkit.feature.ReprojectMapper in project geotoolkit by Geomatys.
the class MIFFeatureSet method add.
@Override
public void add(Iterator<? extends Feature> newFeatures) throws DataStoreException {
final FeatureType ft = getType();
final MIFReader reader = new MIFReader(store.manager, ft);
try (final MIFWriter writer = new MIFWriter(store.manager, reader)) {
// We remove the features as we get them. We don't need to write them as the default writing behaviour is append mode.
while (writer.hasNext()) {
writer.next();
writer.remove();
}
if (store.manager.getWrittenCRS() != null) {
ReprojectMapper mapper = null;
while (newFeatures.hasNext()) {
Feature f = newFeatures.next();
if (mapper == null) {
final FeatureType type = f.getType();
mapper = new ReprojectMapper(type, store.manager.getWrittenCRS());
}
f = mapper.apply(f);
final Feature candidate = writer.next();
FeatureExt.copy(f, candidate, false);
writer.write();
}
} else {
while (newFeatures.hasNext()) {
final Feature f = newFeatures.next();
final Feature candidate = writer.next();
FeatureExt.copy(f, candidate, false);
writer.write();
}
}
}
}
use of org.geotoolkit.feature.ReprojectMapper in project geotoolkit by Geomatys.
the class FeatureStreamsTest method testReprojectFeatureIterator.
@Test
public void testReprojectFeatureIterator() throws DataStoreException, FactoryException {
Query query = new Query(collection.getType().getName());
FeatureReader reader = collection.getSession().getFeatureStore().getFeatureReader(query);
final CoordinateReferenceSystem targetCRS = CommonCRS.WGS84.geographic();
FeatureReader retyped = FeatureStreams.decorate(reader, new ReprojectMapper(reader.getFeatureType(), targetCRS), new Hints());
int mask = 0;
Feature f;
while (retyped.hasNext()) {
f = retyped.next();
final Object id = f.getPropertyValue(AttributeConvention.IDENTIFIER);
assertEquals(4, f.getType().getProperties(true).size());
assertEquals(targetCRS, JTS.findCoordinateReferenceSystem((Geometry) f.getProperty("att_geom").getValue()));
if (id1.equals(id)) {
mask |= 1 << 0;
assertEquals(GF.createPoint(new Coordinate(0, 3)).toString(), f.getProperty("att_geom").getValue().toString());
} else if (id2.equals(id)) {
mask |= 1 << 1;
assertEquals(GF.createPoint(new Coordinate(0, 1)).toString(), f.getProperty("att_geom").getValue().toString());
} else if (id3.equals(id)) {
mask |= 1 << 2;
assertEquals(GF.createPoint(new Coordinate(0, 2)).toString(), f.getProperty("att_geom").getValue().toString());
}
}
if (mask != 7) {
fail("missing features in iterations");
}
// check has next do not iterate
reader = collection.getSession().getFeatureStore().getFeatureReader(query);
retyped = FeatureStreams.decorate(reader, new ReprojectMapper(reader.getFeatureType(), CommonCRS.WGS84.geographic()), new Hints());
testIterationOnNext(retyped, 3);
// check sub iterator is properly closed
reader = collection.getSession().getFeatureStore().getFeatureReader(query);
CheckCloseFeatureIterator checkIte = new CheckCloseFeatureIterator(reader);
assertFalse(checkIte.isClosed());
retyped = FeatureStreams.decorate(checkIte, new ReprojectMapper(checkIte.getFeatureType(), CommonCRS.WGS84.geographic()), new Hints());
while (retyped.hasNext()) retyped.next();
retyped.close();
assertTrue(checkIte.isClosed());
}
use of org.geotoolkit.feature.ReprojectMapper in project geotoolkit by Geomatys.
the class ReprojectFeatureTypeTest method reprojectAttributeTest.
@Test
public void reprojectAttributeTest() {
final FeatureTypeBuilder ftb = new FeatureTypeBuilder();
ftb.setName("test");
ftb.addAttribute(Point.class).setName("attGeom").setCRS(CommonCRS.WGS84.geographic());
final FeatureType baseType = ftb.build();
// test view type
final ReprojectMapper reprojType = new ReprojectMapper(baseType, CommonCRS.WGS84.normalizedGeographic());
final Collection<? extends PropertyType> properties = reprojType.getMappedType().getProperties(true);
assertEquals(1, properties.size());
// test feature
final Feature baseFeature = baseType.newInstance();
baseFeature.setPropertyValue("attGeom", GF.createPoint(new Coordinate(10, 20)));
final Feature reprojFeature = reprojType.apply(baseFeature);
assertEquals(GF.createPoint(new Coordinate(20, 10)), reprojFeature.getPropertyValue("attGeom"));
}
use of org.geotoolkit.feature.ReprojectMapper in project geotoolkit by Geomatys.
the class NetcdfFeatureReader method getFeatureFromFOI.
protected final Feature getFeatureFromFOI(final AnyFeature foi) {
if (foi instanceof SamplingFeature) {
final SamplingFeature feature = (SamplingFeature) foi;
final org.opengis.geometry.Geometry isoGeom = feature.getGeometry();
try {
final Geometry geom;
if (isoGeom instanceof AbstractGeometry) {
geom = GeometrytoJTS.toJTS((AbstractGeometry) isoGeom, AxisResolve.STRICT);
} else {
geom = null;
}
if (firstCRS && isoGeom != null) {
// configure crs in the feature type
final CoordinateReferenceSystem crs = ((AbstractGeometry) isoGeom).getCoordinateReferenceSystem(false);
type = new ReprojectMapper(type, crs).getMappedType();
firstCRS = false;
}
final Feature f = type.newInstance();
f.setPropertyValue(AttributeConvention.IDENTIFIER, feature.getId());
f.setPropertyValue(OMFeatureTypes.ATT_DESC.toString(), feature.getDescription());
f.setPropertyValue(OMFeatureTypes.ATT_NAME.toString(), feature.getName());
f.setPropertyValue(OMFeatureTypes.ATT_POSITION.toString(), geom);
final List<String> sampleds = new ArrayList<>();
for (FeatureProperty featProp : feature.getSampledFeatures()) {
if (featProp.getHref() != null) {
sampleds.add(featProp.getHref());
}
}
f.setPropertyValue(OMFeatureTypes.ATT_SAMPLED.toString(), sampleds);
return f;
} catch (FactoryException ex) {
LOGGER.log(Level.WARNING, "error while transforming GML geometry to JTS", ex);
}
} else {
LOGGER.warning("unable to find a valid feature of interest in the observation");
}
return null;
}
use of org.geotoolkit.feature.ReprojectMapper in project geotoolkit by Geomatys.
the class ReprojectFeatureTypeTest method reprojectOperationTest.
@Test
public void reprojectOperationTest() {
final FeatureTypeBuilder ftb = new FeatureTypeBuilder();
ftb.setName("test");
ftb.addAttribute(Point.class).setName("attGeom").setCRS(CommonCRS.WGS84.geographic()).addRole(AttributeRole.DEFAULT_GEOMETRY);
final FeatureType baseType = ftb.build();
// test view type
final ReprojectMapper viewType = new ReprojectMapper(baseType, CommonCRS.WGS84.normalizedGeographic());
final Collection<? extends PropertyType> properties = viewType.getMappedType().getProperties(true);
assertEquals(3, properties.size());
assertTrue(viewType.getMappedType().getProperty("attGeom") instanceof AttributeType);
assertTrue(viewType.getMappedType().getProperty("sis:geometry") instanceof Operation);
// test feature
final Feature baseFeature = baseType.newInstance();
baseFeature.setPropertyValue("attGeom", GF.createPoint(new Coordinate(10, 20)));
final Feature viewFeature = viewType.apply(baseFeature);
assertEquals(GF.createPoint(new Coordinate(20, 10)), viewFeature.getPropertyValue("attGeom"));
assertEquals(GF.createPoint(new Coordinate(20, 10)), viewFeature.getPropertyValue("sis:geometry"));
}
Aggregations