use of org.geotoolkit.storage.feature.query.Query in project geotoolkit by Geomatys.
the class Copy method execute.
/**
* {@inheritDoc }
*/
@Override
protected void execute() throws ProcessException {
final FeatureStore sourceDS = inputParameters.getValue(SOURCE_STORE);
final FeatureStore targetDS = inputParameters.getValue(TARGET_STORE);
Session targetSS = inputParameters.getValue(TARGET_SESSION);
final Boolean eraseParam = inputParameters.getValue(ERASE);
final Boolean newVersion = inputParameters.getValue(NEW_VERSION);
// Type name can be removed, it's embedded in the query param.
final String typenameParam = inputParameters.getValue(TYPE_NAME);
final Query queryParam = inputParameters.getValue(QUERY);
final boolean doCommit = targetSS == null;
final Session sourceSS = sourceDS.createSession(false);
if (targetSS == null) {
if (targetDS != null) {
targetSS = targetDS.createSession(true);
} else {
throw new ProcessException("Input target_session or target_datastore missing.", this, null);
}
}
boolean reBuildQuery = false;
final String queryName;
if (queryParam != null) {
queryName = queryParam.getTypeName();
reBuildQuery = true;
} else if (typenameParam != null) {
queryName = typenameParam;
} else {
queryName = "*";
}
final Set<GenericName> names;
if ("*".equals(queryName)) {
// all values
try {
names = sourceDS.getNames();
} catch (DataStoreException ex) {
throw new ProcessException(ex.getMessage(), this, ex);
}
} else {
// pick only the wanted names
names = new HashSet<>();
final List<String> wanted = UnmodifiableArrayList.wrap(queryName.split(","));
for (String s : wanted) {
try {
final FeatureType type = sourceDS.getFeatureType(s);
names.add(type.getName());
} catch (DataStoreException ex) {
throw new ProcessException(ex.getMessage(), this, ex);
}
}
}
final float size = names.size();
int inc = 0;
for (GenericName n : names) {
fireProgressing("Copying " + n + ".", (int) ((inc * 100f) / size), false);
try {
Query query;
if (reBuildQuery) {
Query builder = new Query();
builder.copy(queryParam);
builder.setTypeName(n);
query = builder;
} else {
query = queryParam != null ? queryParam : new Query(n);
}
insert(n, sourceSS, targetSS, query, eraseParam, newVersion);
} catch (DataStoreException ex) {
throw new ProcessException(ex.getMessage(), this, ex);
}
inc++;
}
try {
Date lastVersionDate = null;
if (doCommit) {
LOGGER.log(Level.INFO, "Commit all changes");
targetSS.commit();
// find last version
for (GenericName n : names) {
if (targetSS.getFeatureStore().getQueryCapabilities().handleVersioning()) {
final List<Version> versions = targetSS.getFeatureStore().getVersioning(n.toString()).list();
if (!versions.isEmpty()) {
if (lastVersionDate == null || versions.get(versions.size() - 1).getDate().getTime() > lastVersionDate.getTime()) {
lastVersionDate = versions.get(versions.size() - 1).getDate();
}
}
}
}
}
if (lastVersionDate != null) {
outputParameters.getOrCreate(VERSION).setValue(lastVersionDate);
}
} catch (DataStoreException ex) {
throw new ProcessException(ex.getMessage(), this, ex);
} catch (VersioningException ex) {
throw new ProcessException(ex.getMessage(), this, ex);
}
}
use of org.geotoolkit.storage.feature.query.Query in project geotoolkit by Geomatys.
the class CategoryStyleBuilder method create.
public List<MutableRule> create() {
// search the different values
final Set<Object> differentValues = new HashSet<>();
final ValueReference property = currentProperty;
final FeatureSet resource = (FeatureSet) layer.getData();
final Query query = new Query();
try {
query.setTypeName(resource.getType().getName());
} catch (DataStoreException ex) {
LOGGER.log(Level.FINE, "Error while accessing data", ex);
}
query.setProperties(new String[] { property.getXPath() });
try (Stream<Feature> stream = resource.subset(query).features(false)) {
final Iterator<Feature> features = stream.iterator();
while (features.hasNext()) {
final Feature feature = features.next();
differentValues.add(property.apply(feature));
}
} catch (DataStoreException | FeatureStoreRuntimeException ex) {
LOGGER.log(Level.FINE, "Error while accessing data", ex);
}
// generate the different rules
fts.rules().clear();
for (Object obj : differentValues) {
fts.rules().add(createRule(property, obj));
}
// generate the other rule if asked
if (other) {
MutableRule r = sf.rule(createSymbolizer());
r.setElseFilter(true);
r.setDescription(sf.description("other", "other"));
fts.rules().add(r);
}
return fts.rules();
}
use of org.geotoolkit.storage.feature.query.Query 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.storage.feature.query.Query in project geotoolkit by Geomatys.
the class FeatureStreamsTest method testTransformFeatureIterator.
@Test
public void testTransformFeatureIterator() throws DataStoreException {
FeatureType originalType = buildOriginalFT();
final FeatureTypeBuilder builder = new FeatureTypeBuilder();
final GenericName name = NamesExt.create("http://test.com", "TestSchema");
builder.setName(name);
builder.addAttribute(String.class).setName(AttributeConvention.IDENTIFIER_PROPERTY);
builder.addAttribute(LineString.class).setName("att_geom").setCRS(CommonCRS.WGS84.normalizedGeographic()).addRole(AttributeRole.DEFAULT_GEOMETRY);
final FeatureType type = builder.build();
final LineString geom = GF.createLineString(new Coordinate[] { new Coordinate(0, 0), // dx 15 , dy 12
new Coordinate(15, 12), // dx 7 , dy 16
new Coordinate(8, 28), // dx 1 , dy 3
new Coordinate(9, 31), // dx 14 , dy 20
new Coordinate(-5, 11), // dx 4 , dy 2
new Coordinate(-1, 9) });
final FeatureCollection collection = FeatureStoreUtilities.collection("id", type);
Feature sf = type.newInstance();
sf.setPropertyValue("att_geom", geom);
collection.add(sf);
// get the reader -------------------------------------------------------
Query query = new Query(originalType.getName());
FeatureReader reader = collection.getSession().getFeatureStore().getFeatureReader(query);
// create the decimate reader -------------------------------------------
final Hints hints = new Hints();
GeometryTransformer decim = new GeometryScaleTransformer(10, 10);
final TransformMapper ttype = new TransformMapper(reader.getFeatureType(), decim);
FeatureReader retyped = FeatureStreams.decorate(reader, ttype, hints);
assertTrue(retyped.hasNext());
LineString decimated = (LineString) retyped.next().getPropertyValue(AttributeConvention.GEOMETRY);
assertFalse(retyped.hasNext());
retyped.close();
assertEquals(4, decimated.getNumPoints());
assertEquals(geom.getGeometryN(0).getCoordinate(), decimated.getGeometryN(0).getCoordinate());
assertEquals(geom.getGeometryN(1).getCoordinate(), decimated.getGeometryN(1).getCoordinate());
assertEquals(geom.getGeometryN(2).getCoordinate(), decimated.getGeometryN(2).getCoordinate());
assertEquals(geom.getGeometryN(4).getCoordinate(), decimated.getGeometryN(3).getCoordinate());
// check the original geometry has not been modified
reader = collection.getSession().getFeatureStore().getFeatureReader(query);
assertTrue(reader.hasNext());
LineString notDecimated = (LineString) reader.next().getPropertyValue(AttributeConvention.GEOMETRY);
assertEquals(6, notDecimated.getNumPoints());
assertFalse(reader.hasNext());
reader.close();
// same test but with reuse hint ---------------------------------------
reader = collection.getSession().getFeatureStore().getFeatureReader(query);
decim = new GeometryScaleTransformer(10, 10);
retyped = FeatureStreams.decorate(reader, new TransformMapper(reader.getFeatureType(), decim), hints);
assertTrue(retyped.hasNext());
decimated = (LineString) retyped.next().getPropertyValue(AttributeConvention.GEOMETRY);
assertFalse(retyped.hasNext());
retyped.close();
assertEquals(4, decimated.getNumPoints());
assertEquals(geom.getGeometryN(0).getCoordinate(), decimated.getGeometryN(0).getCoordinate());
assertEquals(geom.getGeometryN(1).getCoordinate(), decimated.getGeometryN(1).getCoordinate());
assertEquals(geom.getGeometryN(2).getCoordinate(), decimated.getGeometryN(2).getCoordinate());
assertEquals(geom.getGeometryN(4).getCoordinate(), decimated.getGeometryN(3).getCoordinate());
// check the original geometry has not been modified
reader = collection.getSession().getFeatureStore().getFeatureReader(query);
assertTrue(reader.hasNext());
notDecimated = (LineString) reader.next().getPropertyValue(AttributeConvention.GEOMETRY);
assertEquals(6, notDecimated.getNumPoints());
assertFalse(reader.hasNext());
reader.close();
}
use of org.geotoolkit.storage.feature.query.Query in project geotoolkit by Geomatys.
the class PostgresComplexTypeTest method testComplexInsert.
/**
* 2 level depths feature test.
*/
@Test
public void testComplexInsert() throws DataStoreException, VersioningException {
reload(false);
final GeometryFactory gf = JTS.getFactory();
store.createFeatureType(FTYPE_COMPLEX);
final FeatureType resType = store.getFeatureType(store.getNames().iterator().next().toString());
final Feature voyage = resType.newInstance();
voyage.setPropertyValue("identifier", 120l);
final Feature driver = FTYPE_DRIVER.newInstance();
driver.setPropertyValue("name", "jean-michel");
driver.setPropertyValue("code", "BHF:123456");
voyage.setPropertyValue("driver", driver);
final Feature stop1 = FTYPE_STOP.newInstance();
stop1.setPropertyValue("location", gf.createPoint(new Coordinate(-10, 60)));
stop1.setPropertyValue("time", new Date(5000000));
final Feature stop2 = FTYPE_STOP.newInstance();
stop2.setPropertyValue("location", gf.createPoint(new Coordinate(30, 15)));
stop2.setPropertyValue("time", new Date(6000000));
final Feature stop3 = FTYPE_STOP.newInstance();
stop3.setPropertyValue("location", gf.createPoint(new Coordinate(40, -70)));
stop3.setPropertyValue("time", new Date(7000000));
voyage.setPropertyValue("stops", Arrays.asList(stop1, stop2, stop3));
List<ResourceId> addedIds = store.addFeatures(resType.getName().toString(), Collections.singleton(voyage));
assertEquals(1, addedIds.size());
assertEquals(FF.resourceId("Voyage.1"), addedIds.get(0));
final Session session = store.createSession(false);
final FeatureCollection col = session.getFeatureCollection(new Query(resType.getName()));
assertEquals(1, col.size());
final FeatureIterator ite = col.iterator();
try {
final Feature resFeature = ite.next();
assertNotNull(resFeature);
assertEquals(120l, resFeature.getPropertyValue("identifier"));
final Feature resDriver = (Feature) resFeature.getProperty("driver");
assertEquals("jean-michel", resDriver.getPropertyValue("name"));
assertEquals("BHF:123456", resDriver.getPropertyValue("code"));
final Collection<Feature> stops = (Collection<Feature>) resFeature.getPropertyValue("stops");
assertEquals(3, stops.size());
final boolean[] found = new boolean[3];
for (Feature stop : stops) {
final Timestamp time = (Timestamp) stop.getPropertyValue("time");
final Point location = (Point) stop.getPropertyValue("location");
if (time.getTime() == 5000000) {
assertEquals(stop1.getPropertyValue("location"), location);
found[0] = true;
} else if (time.getTime() == 6000000) {
assertEquals(stop2.getPropertyValue("location"), location);
found[1] = true;
} else if (time.getTime() == 7000000) {
assertEquals(stop3.getPropertyValue("location"), location);
found[2] = true;
} else {
fail("Unexpected property \n" + stop);
}
}
for (boolean b : found) assertTrue(b);
} finally {
ite.close();
}
}
Aggregations