use of org.geotoolkit.storage.feature.FeatureIterator in project geotoolkit by Geomatys.
the class ConvexHullProcess method computeConvexHull.
/**
* Compute the convex hull from a feature collection on a geometry attribute name
*
* @return the convex hull geometry
*/
private Geometry computeConvexHull(final FeatureCollection inputFeatureList, String geometryName) {
Geometry convexHull = JTS.getFactory().buildGeometry(Collections.EMPTY_LIST);
CoordinateReferenceSystem crs = null;
try (final FeatureIterator iter = inputFeatureList.iterator()) {
while (iter.hasNext()) {
final Feature feature = iter.next();
// in the first pass, if the geometry attribute name is null, we use the default geometry attribute name
if (geometryName == null) {
geometryName = AttributeConvention.GEOMETRY;
}
for (PropertyType property : feature.getType().getProperties(true)) {
if (AttributeConvention.isGeometryAttribute(property)) {
final String name = property.getName().toString();
if (name.equals(geometryName)) {
crs = FeatureExt.getCRS(property);
final Geometry tmpGeom = (Geometry) feature.getPropertyValue(name);
convexHull = convexHull.union(tmpGeom);
convexHull = convexHull.convexHull();
}
}
}
}
}
if (crs != null) {
JTS.setCRS(convexHull, crs);
} else {
fireWarningOccurred("Referencing output geometry", Float.NaN, new IllegalStateException("No coordinate reference system available. Output geometry will not have any CRS attached"));
}
return convexHull;
}
use of org.geotoolkit.storage.feature.FeatureIterator in project geotoolkit by Geomatys.
the class ShapefileDataStoreTest method runWriteReadTest.
private void runWriteReadTest(final Geometry geom, final boolean d3) throws Exception {
// make features
final FeatureTypeBuilder ftb = new FeatureTypeBuilder();
ftb.setName("Junk");
ftb.addAttribute(geom.getClass()).setName("a").setCRS(CommonCRS.WGS84.normalizedGeographic()).addRole(AttributeRole.DEFAULT_GEOMETRY);
final FeatureType type = ftb.build();
Collection<Feature> features = new ArrayList<>();
for (int i = 0, ii = 20; i < ii; i++) {
final Feature feature = type.newInstance();
feature.setPropertyValue("a", (Geometry) geom.clone());
features.add(feature);
}
// set up file
File tmpFile = getTempFile();
tmpFile.delete();
// write features
ShapefileFeatureStore shapeFeatureStore = new ShapefileFeatureStore(tmpFile.toURI());
shapeFeatureStore.createFeatureType(type);
writeFeatures(shapeFeatureStore, features);
// read features
shapeFeatureStore = new ShapefileFeatureStore(tmpFile.toURI());
FeatureCollection fc = loadFeatures(shapeFeatureStore);
// verify
try (FeatureIterator fci = fc.iterator()) {
while (fci.hasNext()) {
Feature f = fci.next();
Geometry fromShape = FeatureExt.getDefaultGeometryValue(f).filter(Geometry.class::isInstance).map(Geometry.class::cast).orElseThrow(() -> new IllegalArgumentException("No geometry found in feature " + f));
if (fromShape instanceof GeometryCollection) {
if (!(geom instanceof GeometryCollection)) {
fromShape = ((GeometryCollection) fromShape).getGeometryN(0);
}
}
try {
Coordinate[] c1 = geom.getCoordinates();
Coordinate[] c2 = fromShape.getCoordinates();
for (int cc = 0, ccc = c1.length; cc < ccc; cc++) {
if (d3)
assertTrue(c1[cc].equals3D(c2[cc]));
else
assertTrue(c1[cc].equals2D(c2[cc]));
}
} catch (Throwable t) {
fail("Bogus : " + Arrays.asList(geom.getCoordinates()) + " : " + Arrays.asList(fromShape.getCoordinates()));
}
}
}
tmpFile.delete();
}
use of org.geotoolkit.storage.feature.FeatureIterator in project geotoolkit by Geomatys.
the class PostgresComplexTypeTest method testComplex2Insert.
/**
* 3 level depths feature test.
*/
@Test
public void testComplex2Insert() throws DataStoreException, VersioningException {
reload(false);
store.createFeatureType(FTYPE_COMPLEX2);
final FeatureType soundingType = store.getFeatureType(store.getNames().iterator().next().toString());
final FeatureType recordType = ((FeatureAssociationRole) soundingType.getProperty("records")).getValueType();
final FeatureType dataType = ((FeatureAssociationRole) recordType.getProperty("datas")).getValueType();
final Feature sounding = soundingType.newInstance();
sounding.setPropertyValue("identifier", 120l);
final Feature record1 = recordType.newInstance();
record1.setPropertyValue("time", new Date(5000000));
final Feature data11 = dataType.newInstance();
data11.setPropertyValue("values", new Float[] { 1f, 2f, 3f });
final Feature data12 = dataType.newInstance();
data12.setPropertyValue("values", new Float[] { 4f, 5f, 6f });
record1.setPropertyValue("datas", Arrays.asList(data11, data12));
final Feature record2 = recordType.newInstance();
record2.setPropertyValue("time", new Date(6000000));
final Feature data21 = dataType.newInstance();
data21.setPropertyValue("values", new Float[] { 7f, 8f, 9f });
record2.setPropertyValue("datas", Arrays.asList(data21));
sounding.setPropertyValue("records", Arrays.asList(record1, record2));
List<ResourceId> addedIds = store.addFeatures(soundingType.getName().toString(), Collections.singleton(sounding));
assertEquals(1, addedIds.size());
assertEquals(FF.resourceId("Sounding.1"), addedIds.get(0));
final Session session = store.createSession(false);
final FeatureCollection col = session.getFeatureCollection(new Query(soundingType.getName()));
assertEquals(1, col.size());
final FeatureIterator ite = store.getFeatureReader(new Query(soundingType.getName()));
try {
final Feature resFeature = ite.next();
assertNotNull(resFeature);
assertEquals(120l, resFeature.getPropertyValue("identifier"));
final Collection<Feature> records = (Collection<Feature>) resFeature.getPropertyValue("records");
assertEquals(2, records.size());
final boolean[] found = new boolean[2];
for (Feature record : records) {
final Timestamp time = (Timestamp) record.getPropertyValue("time");
if (time.getTime() == 5000000) {
found[0] = true;
final Collection<Feature> datas = (Collection<Feature>) record.getPropertyValue("datas");
assertEquals(2, datas.size());
final boolean[] dfound = new boolean[2];
for (Feature data : datas) {
final Float[] values = (Float[]) data.getPropertyValue("values");
if (Arrays.equals(values, new Float[] { 1f, 2f, 3f })) {
dfound[0] = true;
} else if (Arrays.equals(values, new Float[] { 4f, 5f, 6f })) {
dfound[1] = true;
} else {
fail("Unexpected property \n" + data);
}
}
for (boolean b : dfound) assertTrue(b);
} else if (time.getTime() == 6000000) {
found[1] = true;
final Collection<Feature> datas = (Collection<Feature>) record.getPropertyValue("datas");
assertEquals(1, datas.size());
final boolean[] dfound = new boolean[1];
for (Feature data : datas) {
final Float[] values = (Float[]) data.getPropertyValue("values");
if (Arrays.equals(values, new Float[] { 7f, 8f, 9f })) {
dfound[0] = true;
} else {
fail("Unexpected property \n" + data);
}
}
for (boolean b : dfound) assertTrue(b);
} else {
fail("Unexpected property \n" + record);
}
}
for (boolean b : found) assertTrue(b);
} finally {
ite.close();
}
}
use of org.geotoolkit.storage.feature.FeatureIterator in project geotoolkit by Geomatys.
the class PostgresComplexTypeTest method testComplex3Insert.
/**
* multiple complex properties of same type
* @throws org.apache.sis.storage.DataStoreException
* @throws org.geotoolkit.version.VersioningException
*/
@Test
public void testComplex3Insert() throws DataStoreException, VersioningException {
reload(false);
store.createFeatureType(FTYPE_COMPLEX3);
final FeatureType recordType = store.getFeatureType(store.getNames().iterator().next().toString());
final Feature record = FTYPE_RECORD.newInstance();
record.setPropertyValue("identifier", 120);
final Feature data1 = FTYPE_DATA.newInstance();
data1.setPropertyValue("value", 5f);
final Feature data2 = FTYPE_DATA.newInstance();
data2.setPropertyValue("value", 10f);
final Feature data3 = FTYPE_DATA.newInstance();
data3.setPropertyValue("value", 15f);
record.setPropertyValue("datas", Arrays.asList(data1, data2, data3));
List<ResourceId> addedIds = store.addFeatures(recordType.getName().toString(), Collections.singleton(record));
assertEquals(1, addedIds.size());
assertEquals(FF.resourceId("Record.1"), addedIds.get(0));
final Session session = store.createSession(false);
final FeatureCollection col = session.getFeatureCollection(new Query(recordType.getName()));
assertEquals(1, col.size());
final FeatureIterator ite = store.getFeatureReader(new Query(recordType.getName()));
try {
final Feature resFeature = ite.next();
assertNotNull(resFeature);
assertEquals(120l, resFeature.getPropertyValue("identifier"));
assertNotNull(resFeature.getProperty("data1"));
assertNotNull(resFeature.getProperty("data2"));
assertNotNull(resFeature.getProperty("data3"));
assertEquals(5f, ((Feature) resFeature.getPropertyValue("data1")).getPropertyValue("value"));
assertEquals(10f, ((Feature) resFeature.getPropertyValue("data2")).getPropertyValue("value"));
assertEquals(15f, ((Feature) resFeature.getPropertyValue("data3")).getPropertyValue("value"));
} finally {
ite.close();
}
}
use of org.geotoolkit.storage.feature.FeatureIterator in project geotoolkit by Geomatys.
the class PostgresVersioningTest method testVersioningSynchrone.
/**
* Check versions are created on each call on the session.
*/
@Test
public void testVersioningSynchrone() throws DataStoreException, VersioningException {
reload(true);
List<Version> versions;
Version version;
Feature feature;
ResourceId fid;
FeatureIterator ite;
Query qb = new Query();
final FeatureType refType = FTYPE_SIMPLE;
store.createFeatureType(refType);
final VersionControl vc = store.getVersioning(refType.getName().toString());
// //////////////////////////////////////////////////////////////////////
// start versioning /////////////////////////////////////////////////////
vc.startVersioning();
versions = vc.list();
assertTrue(versions.isEmpty());
final Session session = store.createSession(false);
// //////////////////////////////////////////////////////////////////////
// make an insert ///////////////////////////////////////////////////////
final Point firstPoint = GF.createPoint(new Coordinate(56, 45));
feature = refType.newInstance();
feature.setPropertyValue("id", "0");
feature.setPropertyValue("boolean", Boolean.TRUE);
feature.setPropertyValue("integer", 14);
feature.setPropertyValue("point", firstPoint);
feature.setPropertyValue("string", "someteststring");
session.addFeatures(refType.getName().toString(), Collections.singleton(feature));
// we should have one version
versions = vc.list();
assertEquals(1, versions.size());
version = versions.get(0);
Date date = version.getDate();
// ensure normal reading is correct without version----------------------
qb = new Query();
qb.setTypeName(refType.getName().toString());
ite = session.getFeatureCollection(qb).iterator();
try {
feature = ite.next();
fid = FeatureExt.getId(feature);
} finally {
ite.close();
}
try {
// wait a bit just to have some space between version dates
Thread.sleep(1000);
} catch (InterruptedException ex) {
fail(ex.getMessage());
}
// //////////////////////////////////////////////////////////////////////
// make an update 1 /////////////////////////////////////////////////////
final Point secondPoint = GF.createPoint(new Coordinate(-12, 21));
Map<String, Object> updates = new HashMap<>();
updates.put("boolean", Boolean.FALSE);
updates.put("integer", -3);
updates.put("point", secondPoint);
updates.put("string", "anothertextupdated");
session.updateFeatures(refType.getName().toString(), fid, updates);
// we should have two versions
versions = vc.list();
assertEquals(2, versions.size());
// //////////////////////////////////////////////////////////////////////
// make an update 2 /////////////////////////////////////////////////////
final Point thirdPoint = GF.createPoint(new Coordinate(48, -51));
updates = new HashMap<>();
updates.put("boolean", Boolean.TRUE);
updates.put("integer", -89);
updates.put("point", thirdPoint);
updates.put("string", "thridupdatetext");
session.updateFeatures(refType.getName().toString(), fid, updates);
// we should have three versions
versions = vc.list();
assertEquals(3, versions.size());
// //////////////////////////////////////////////////////////////////////
// delete record ////////////////////////////////////////////////////////
session.removeFeatures(refType.getName().toString(), fid);
qb = new Query();
qb.setTypeName(refType.getName().toString());
assertEquals(0, session.getCount(qb));
// we should have four versions
versions = vc.list();
assertEquals(4, versions.size());
// //////////////////////////////////////////////////////////////////////
// make an insert ///////////////////////////////////////////////////////
Point fourthPoint = GF.createPoint(new Coordinate(66, 11));
feature = refType.newInstance();
feature.setPropertyValue("id", "0");
feature.setPropertyValue("boolean", Boolean.FALSE);
feature.setPropertyValue("integer", 22);
feature.setPropertyValue("point", fourthPoint);
feature.setPropertyValue("string", "fourthupdateString");
session.addFeatures(refType.getName().toString(), Collections.singleton(feature));
// we should have five versions
versions = vc.list();
assertEquals(5, versions.size());
}
Aggregations