use of org.geotoolkit.storage.feature.session.Session 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.session.Session 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();
}
}
use of org.geotoolkit.storage.feature.session.Session in project geotoolkit by Geomatys.
the class PostgresSimpleTypeTest method testSimpleInsert.
@Test
public void testSimpleInsert() throws DataStoreException, VersioningException {
reload(true);
store.createFeatureType(FTYPE_SIMPLE);
FeatureType resType = store.getFeatureType(store.getNames().iterator().next().toString());
Feature feature = resType.newInstance();
feature.setPropertyValue("boolean", true);
// byte type do not exist, it's converted to smallint/int2
feature.setPropertyValue("byte", (short) 45);
feature.setPropertyValue("short", (short) 963);
feature.setPropertyValue("integer", 123456);
feature.setPropertyValue("long", 456789l);
feature.setPropertyValue("float", 7.3f);
feature.setPropertyValue("double", 14.5);
feature.setPropertyValue("string", "a string");
List<ResourceId> addedIds = store.addFeatures(resType.getName().toString(), Collections.singleton(feature));
assertEquals(1, addedIds.size());
assertEquals(FF.resourceId("1"), addedIds.get(0));
Session session = store.createSession(false);
FeatureCollection col = session.getFeatureCollection(new Query(resType.getName()));
assertEquals(1, col.size());
FeatureIterator ite = col.iterator();
try {
final Feature resFeature = ite.next();
assertNotNull(resFeature);
assertEquals(true, resFeature.getPropertyValue("boolean"));
assertEquals((short) 45, resFeature.getPropertyValue("byte"));
assertEquals((short) 963, resFeature.getPropertyValue("short"));
assertEquals(123456, resFeature.getPropertyValue("integer"));
assertEquals(456789l, resFeature.getPropertyValue("long"));
assertEquals(7.3f, resFeature.getPropertyValue("float"));
assertEquals(14.5d, resFeature.getPropertyValue("double"));
assertEquals("a string", resFeature.getPropertyValue("string"));
} finally {
ite.close();
}
// SECOND TEST for NAN values ------------------------------------------
reload(true);
store.createFeatureType(FTYPE_SIMPLE);
resType = store.getFeatureType(store.getNames().iterator().next().toString());
feature = resType.newInstance();
feature.setPropertyValue("fid", 0);
feature.setPropertyValue("boolean", true);
feature.setPropertyValue("byte", (short) 45);
feature.setPropertyValue("short", (short) 963);
feature.setPropertyValue("integer", 123456);
feature.setPropertyValue("long", 456789l);
feature.setPropertyValue("float", Float.NaN);
feature.setPropertyValue("double", Double.NaN);
feature.setPropertyValue("string", "a string");
addedIds = store.addFeatures(resType.getName().toString(), Collections.singleton(feature));
assertEquals(1, addedIds.size());
assertEquals(FF.resourceId("1"), addedIds.get(0));
session = store.createSession(false);
col = session.getFeatureCollection(new Query(resType.getName()));
assertEquals(1, col.size());
ite = col.iterator();
try {
final Feature resFeature = ite.next();
assertNotNull(resFeature);
assertEquals(true, resFeature.getPropertyValue("boolean"));
assertEquals((short) 45, resFeature.getPropertyValue("byte"));
assertEquals((short) 963, resFeature.getPropertyValue("short"));
assertEquals(123456, resFeature.getPropertyValue("integer"));
assertEquals(456789l, resFeature.getPropertyValue("long"));
assertEquals(Float.NaN, resFeature.getPropertyValue("float"));
assertEquals(Double.NaN, resFeature.getPropertyValue("double"));
assertEquals("a string", resFeature.getPropertyValue("string"));
} finally {
ite.close();
}
}
use of org.geotoolkit.storage.feature.session.Session in project geotoolkit by Geomatys.
the class PostgresSimpleTypeTest method testArrayInsert.
@Test
public void testArrayInsert() throws DataStoreException, VersioningException {
reload(true);
store.createFeatureType(FTYPE_ARRAY);
final FeatureType resType = store.getFeatureType(store.getNames().iterator().next().toString());
final Feature feature = resType.newInstance();
feature.setPropertyValue("fid", 0);
feature.setPropertyValue("boolean", new Boolean[] { true, false, true });
feature.setPropertyValue("byte", new Short[] { 3, 6, 9 });
feature.setPropertyValue("short", new Short[] { -5, 12, -50 });
feature.setPropertyValue("integer", new Integer[] { 123, 456, 789 });
feature.setPropertyValue("long", new Long[] { 111l, 222l, 333l });
feature.setPropertyValue("float", new Float[] { 1.2f, -5.9f, 8.1f });
feature.setPropertyValue("double", new Double[] { 78.3d, 41.23d, -99.66d });
feature.setPropertyValue("string", new String[] { "marc", "hubert", "samy" });
List<ResourceId> addedIds = store.addFeatures(resType.getName().toString(), Collections.singleton(feature));
assertEquals(1, addedIds.size());
assertEquals(FF.resourceId("1"), addedIds.get(0));
final Session session = store.createSession(false);
final FeatureCollection col = session.getFeatureCollection(new Query(resType.getName()));
assertEquals(1, col.size());
// Postgis allow NULL in arrays, so returned array are not primitive types
final FeatureIterator ite = col.iterator();
try {
final Feature resFeature = ite.next();
assertNotNull(resFeature);
assertArrayEquals(new Boolean[] { true, false, true }, (Boolean[]) resFeature.getPropertyValue("boolean"));
assertArrayEquals(new Short[] { 3, 6, 9 }, (Short[]) resFeature.getPropertyValue("byte"));
assertArrayEquals(new Short[] { -5, 12, -50 }, (Short[]) resFeature.getPropertyValue("short"));
assertArrayEquals(new Integer[] { 123, 456, 789 }, (Integer[]) resFeature.getPropertyValue("integer"));
assertArrayEquals(new Long[] { 111l, 222l, 333l }, (Long[]) resFeature.getPropertyValue("long"));
assertArrayEquals(new Float[] { 1.2f, -5.9f, 8.1f }, (Float[]) resFeature.getPropertyValue("float"));
assertArrayEquals(new Double[] { 78.3d, 41.23d, -99.66d }, (Double[]) resFeature.getPropertyValue("double"));
assertArrayEquals(new String[] { "marc", "hubert", "samy" }, (String[]) resFeature.getPropertyValue("string"));
} finally {
ite.close();
}
}
use of org.geotoolkit.storage.feature.session.Session in project geotoolkit by Geomatys.
the class PostgresSimpleTypeTest method testGeometryInsert.
@Test
public void testGeometryInsert() throws DataStoreException, NoSuchAuthorityCodeException, FactoryException, VersioningException {
reload(true);
// //////////////////////////////////////////////////////////////////////
final GeometryFactory gf = JTS.getFactory();
// creating a point -----------------------------------------------
final Point point = gf.createPoint(new Coordinate(56, 45));
// creating a multipoint ------------------------------------------
final MultiPoint mp = gf.createMultiPoint(new Coordinate[] { new Coordinate(23, 78), new Coordinate(-10, 43), new Coordinate(12, 94) });
// creating a linestring ------------------------------------------
final LineString ls = gf.createLineString(new Coordinate[] { new Coordinate(23, 78), new Coordinate(-10, 43), new Coordinate(12, 94) });
// creating a multilinestring -------------------------------------
final LineString ls1 = gf.createLineString(new Coordinate[] { new Coordinate(30, 45), new Coordinate(56, 29) });
final LineString ls2 = gf.createLineString(new Coordinate[] { new Coordinate(98, 12), new Coordinate(19, 87) });
final MultiLineString mls = gf.createMultiLineString(new LineString[] { ls1, ls2 });
// creating a polygon ---------------------------------------------
final LinearRing ring = gf.createLinearRing(new Coordinate[] { new Coordinate(23, 78), new Coordinate(-10, 43), new Coordinate(12, 94), new Coordinate(23, 78) });
final Polygon polygon = gf.createPolygon(ring, new LinearRing[0]);
// creating a multipolygon ----------------------------------------
final MultiPolygon mpolygon = gf.createMultiPolygon(new Polygon[] { polygon });
// creating a geometry collection ----------------------------------------
final GeometryCollection gc = gf.createGeometryCollection(new Geometry[] { point, ls, polygon });
// //////////////////////////////////////////////////////////////////////
store.createFeatureType(FTYPE_GEOMETRY);
final FeatureType resType = store.getFeatureType(store.getNames().iterator().next().toString());
final Feature feature = resType.newInstance();
feature.setPropertyValue("fid", 0);
feature.setPropertyValue("geometry", point);
feature.setPropertyValue("point", point);
feature.setPropertyValue("multipoint", mp);
feature.setPropertyValue("linestring", ls);
feature.setPropertyValue("multilinestring", mls);
feature.setPropertyValue("polygon", polygon);
feature.setPropertyValue("multipolygon", mpolygon);
feature.setPropertyValue("geometrycollection", gc);
List<ResourceId> addedIds = store.addFeatures(resType.getName().toString(), Collections.singleton(feature));
assertEquals(1, addedIds.size());
assertEquals(FF.resourceId("1"), addedIds.get(0));
final Session session = store.createSession(false);
final FeatureCollection col = session.getFeatureCollection(new Query(resType.getName()));
assertEquals(1, col.size());
// Postgis allow NULL in arrays, so returned array are not primitive types
final FeatureIterator ite = col.iterator();
try {
final Feature resFeature = ite.next();
assertNotNull(resFeature);
Geometry geom;
geom = (Geometry) resFeature.getPropertyValue("geometry");
assertEquals(point, geom);
assertEquals(CommonCRS.WGS84.normalizedGeographic(), JTS.findCoordinateReferenceSystem(geom));
geom = (Geometry) resFeature.getPropertyValue("point");
assertEquals(point, geom);
assertEquals(CommonCRS.WGS84.normalizedGeographic(), JTS.findCoordinateReferenceSystem(geom));
geom = (Geometry) resFeature.getPropertyValue("multipoint");
assertEquals(mp, geom);
assertEquals(CommonCRS.WGS84.normalizedGeographic(), JTS.findCoordinateReferenceSystem(geom));
geom = (Geometry) resFeature.getPropertyValue("linestring");
assertEquals(ls, geom);
assertEquals(CommonCRS.WGS84.normalizedGeographic(), JTS.findCoordinateReferenceSystem(geom));
geom = (Geometry) resFeature.getPropertyValue("multilinestring");
assertEquals(mls, geom);
assertEquals(CommonCRS.WGS84.normalizedGeographic(), JTS.findCoordinateReferenceSystem(geom));
geom = (Geometry) resFeature.getPropertyValue("polygon");
assertEquals(polygon, geom);
assertEquals(CommonCRS.WGS84.normalizedGeographic(), JTS.findCoordinateReferenceSystem(geom));
geom = (Geometry) resFeature.getPropertyValue("multipolygon");
assertEquals(mpolygon, geom);
assertEquals(CommonCRS.WGS84.normalizedGeographic(), JTS.findCoordinateReferenceSystem(geom));
geom = (Geometry) resFeature.getPropertyValue("geometrycollection");
assertEquals(gc, geom);
assertEquals(CommonCRS.WGS84.normalizedGeographic(), JTS.findCoordinateReferenceSystem(geom));
} finally {
ite.close();
}
}
Aggregations