use of org.opengis.feature.PropertyType in project geotoolkit by Geomatys.
the class PostgresComplexTypeTest method testCrossSchemaRelation.
@Test
public void testCrossSchemaRelation() throws DataStoreException, VersioningException, SQLException {
reload(false);
try (Connection cnx = store.getDataSource().getConnection()) {
cnx.createStatement().executeUpdate("CREATE TABLE \"localtable\" (id serial, other integer);");
cnx.createStatement().executeUpdate("DROP SCHEMA IF EXISTS someothertestschema CASCADE;");
cnx.createStatement().executeUpdate("CREATE SCHEMA someothertestschema;");
cnx.createStatement().executeUpdate("CREATE TABLE \"someothertestschema\".\"othertable\" (ident serial PRIMARY KEY, field double precision);");
cnx.createStatement().executeUpdate("ALTER TABLE \"localtable\" ADD FOREIGN KEY (other) REFERENCES someothertestschema.othertable(ident)");
}
store.refreshMetaModel();
final FeatureType ft = store.getFeatureType("localtable");
assertEquals("localtable", ft.getName().tip().toString());
assertEquals(3, ft.getProperties(true).size());
assertNotNull(ft.getProperty("id"));
assertEquals(Integer.class, ((AttributeType) ft.getProperty("id")).getValueClass());
final PropertyType desc = ft.getProperty("other");
assertNotNull(desc);
assertTrue(desc instanceof Operation);
FeatureAssociationRole far = (FeatureAssociationRole) ((Operation) desc).getResult();
final FeatureType ct = far.getValueType();
assertEquals(3, ct.getProperties(true).size());
assertNotNull(ct.getProperty("ident"));
assertEquals(Integer.class, ((AttributeType) ct.getProperty("ident")).getValueClass());
assertNotNull(ct.getProperty("field"));
assertEquals(Double.class, ((AttributeType) ct.getProperty("field")).getValueClass());
}
use of org.opengis.feature.PropertyType in project geotoolkit by Geomatys.
the class PostgresComplexTypeTest method lazyCompare.
private void lazyCompare(final FeatureType refType, final FeatureType candidate) {
final GenericName name = refType.getName();
assertEquals(refType.getName().tip().toString(), name.tip().toString());
if (refType instanceof FeatureType) {
final FeatureType ct = (FeatureType) refType;
final FeatureType cct = (FeatureType) candidate;
// +1 for generated fid field
assertEquals(ct.getProperties(true).size() + 1, cct.getProperties(true).size());
for (PropertyType desc : ct.getProperties(true)) {
final PropertyType cdesc = cct.getProperty(desc.getName().toString());
assertEquals(desc, cdesc);
}
} else {
final AttributeType at = (AttributeType) refType;
final AttributeType cat = (AttributeType) candidate;
if (at.getValueClass() == Date.class) {
assertEquals(Timestamp.class, cat.getValueClass());
} else {
assertEquals(at.getValueClass(), cat.getValueClass());
}
}
}
use of org.opengis.feature.PropertyType in project geotoolkit by Geomatys.
the class ShapefileRTreeReadWriteTest method compare.
public static void compare(final Feature f1, final Feature f2) throws Exception {
Collection<? extends PropertyType> descs = f1.getType().getProperties(true);
if (descs.size() != f2.getType().getProperties(true).size()) {
throw new Exception("Unequal number of attributes");
}
for (PropertyType desc : descs) {
final String name = desc.getName().toString();
Object att1 = f1.getPropertyValue(name);
Object att2 = f2.getPropertyValue(name);
if (att1 instanceof Geometry && att2 instanceof Geometry) {
Geometry g1 = ((Geometry) att1);
Geometry g2 = ((Geometry) att2);
g1.normalize();
g2.normalize();
if (!g1.equalsExact(g2)) {
throw new Exception("Different geometries (" + name + "):\n" + g1 + "\n" + g2);
}
} else {
if (!att1.equals(att2)) {
throw new Exception("Different attribute (" + name + "): [" + att1 + "] - [" + att2 + "]");
}
}
}
}
use of org.opengis.feature.PropertyType in project geotoolkit by Geomatys.
the class IndexedShapefileFeatureStore method getFeatureReader.
/**
* Use the spatial index if available and adds a small optimization: if no
* attributes are going to be read, don't uselessly open and read the dbf
* file.
*/
@Override
public FeatureReader getFeatureReader(final Query query) throws DataStoreException {
if (!(query instanceof org.geotoolkit.storage.feature.query.Query))
throw new UnsupportedQueryException();
final org.geotoolkit.storage.feature.query.Query gquery = (org.geotoolkit.storage.feature.query.Query) query;
final FeatureType baseType = getFeatureType();
final String queryTypeName = gquery.getTypeName();
final String[] queryPropertyNames = gquery.getPropertyNames();
final Hints queryHints = gquery.getHints();
final double[] queryRes = gquery.getResolution();
Filter queryFilter = gquery.getSelection();
// check if we must read the 3d values
final boolean read3D = true;
// find the properties we will read and return --------------------------
final AttributeType idAttribute = (AttributeType) baseType.getProperty(AttributeConvention.IDENTIFIER);
Set<AttributeType> readProperties;
Set<PropertyType> returnedProperties;
if (queryPropertyNames == null) {
// return all properties. Note : preserve order by using a linked set implementation
readProperties = new LinkedHashSet<>(getAttributes(baseType, true));
returnedProperties = new LinkedHashSet<>((Collection) baseType.getProperties(true));
} else {
// return only a subset of properties. Note : preserve order by using a linked set implementation
readProperties = new LinkedHashSet<>(queryPropertyNames.length);
returnedProperties = new LinkedHashSet<>(queryPropertyNames.length);
for (String n : queryPropertyNames) {
final PropertyType cdt = baseType.getProperty(n);
if (cdt instanceof AttributeType) {
readProperties.add((AttributeType) cdt);
} else if (cdt instanceof AbstractOperation) {
final Set<String> deps = ((AbstractOperation) cdt).getDependencies();
for (String dep : deps) readProperties.add((AttributeType) baseType.getProperty(dep));
}
returnedProperties.add(cdt);
}
// add filter properties
final FilterAttributeExtractor fae = new FilterAttributeExtractor();
fae.visit(queryFilter, null);
final Set<GenericName> filterPropertyNames = fae.getAttributeNameSet();
for (GenericName n : filterPropertyNames) {
final PropertyType cdt = baseType.getProperty(n.toString());
if (cdt instanceof AttributeType) {
readProperties.add((AttributeType) cdt);
} else if (cdt instanceof AbstractOperation) {
final Set<String> deps = ((AbstractOperation) cdt).getDependencies();
for (String dep : deps) readProperties.add((AttributeType) baseType.getProperty(dep));
}
}
}
final Set<PropertyType> allProperties = new LinkedHashSet<>(returnedProperties);
allProperties.addAll(readProperties);
// create a reader ------------------------------------------------------
final FeatureType readType;
final FeatureReader reader;
try {
final GenericName[] readPropertyNames = new GenericName[allProperties.size()];
int i = 0;
for (PropertyType prop : allProperties) {
readPropertyNames[i++] = prop.getName();
}
readType = FeatureTypeExt.createSubType(baseType, readPropertyNames);
if (queryFilter.getOperatorType() == SpatialOperatorName.BBOX) {
// in case we have a BBOX filter only, which is very commun, we can speed
// the process by relying on the quadtree estimations
final Envelope bbox = ExtractBoundsFilterVisitor.bbox(queryFilter, null);
final boolean loose = (queryFilter instanceof LooseBBox);
queryFilter = Filter.include();
final List<AttributeType> attsProperties = new ArrayList<>(readProperties);
attsProperties.remove(idAttribute);
reader = createFeatureReader(getBBoxAttributesReader(attsProperties, bbox, loose, queryHints, read3D, queryRes), readType, queryHints);
} else if (queryFilter instanceof ResourceId && ((ResourceId) queryFilter).getIdentifier() == null) {
// in case we have an empty id set (TODO: should never happen, maybe we should remove this case).
return FeatureStreams.emptyReader(getFeatureType());
} else {
final List<AttributeType> attsProperties = new ArrayList<>(readProperties);
attsProperties.remove(idAttribute);
reader = createFeatureReader(getAttributesReader(attsProperties, queryFilter, read3D, queryRes), readType, queryHints);
}
} catch (IOException ex) {
throw new DataStoreException(ex);
}
// handle remaining query parameters ------------------------------------
final org.geotoolkit.storage.feature.query.Query qb = new org.geotoolkit.storage.feature.query.Query(queryTypeName);
if (readProperties.equals(returnedProperties)) {
qb.setProperties(queryPropertyNames);
}
qb.setSelection(queryFilter);
qb.setHints(queryHints);
qb.setSortBy(gquery.getSortBy());
qb.setOffset(gquery.getOffset());
gquery.getLimit().ifPresent(qb::setLimit);
return FeatureStreams.subset(reader, qb);
}
use of org.opengis.feature.PropertyType in project geotoolkit by Geomatys.
the class FeatureExtTest method findDefaultGeometry.
@Test
public void findDefaultGeometry() {
FeatureTypeBuilder builder = new FeatureTypeBuilder();
builder.setName("base type");
builder.addAttribute(String.class).setName("first");
builder.addAttribute(Float.class).setName("second");
builder.addAttribute(Geometry.class).setName("secondary_geometry");
final FeatureType baseType = builder.build();
// There's only one geometry here, no confusion possible.
PropertyType defaultGeom = FeatureExt.getDefaultGeometry(baseType);
assertNotNull("We should find the only geometry defined in the feature type.", defaultGeom);
assertEquals("We should have found the attribute attached to SIS convention.", "secondary_geometry", defaultGeom.getName().tip().toString());
builder = new FeatureTypeBuilder(baseType);
builder.setName("with sis convention");
builder.addAttribute(Geometry.class).setName("main_geometry").setCRS(CommonCRS.WGS84.normalizedGeographic()).addRole(AttributeRole.DEFAULT_GEOMETRY);
final FeatureType conventionedType = builder.build();
// We should find main geometry as we defined a convention for it.
defaultGeom = FeatureExt.getDefaultGeometry(conventionedType);
assertNotNull("We should find one geometry attribute", defaultGeom);
assertEquals("We should have found the attribute attached to SIS convention.", AttributeConvention.GEOMETRY_PROPERTY, defaultGeom.getName());
builder = new FeatureTypeBuilder(baseType);
builder.setName("without sis convention");
builder.addAttribute(Geometry.class).setName("main_geometry").setCRS(CommonCRS.WGS84.normalizedGeographic());
try {
FeatureExt.getDefaultGeometry(builder.build());
fail("We should not find any geometry as there's multiple geometric attributes but no convention defined.");
} catch (PropertyNotFoundException | IllegalStateException e) {
// expected result
}
// We also test we find the geometry after reprojection, and it's the good one, the reprojected.
final ReprojectMapper reprojected = new ReprojectMapper(conventionedType, CommonCRS.WGS84.geographic());
defaultGeom = FeatureExt.getDefaultGeometry(reprojected.getMappedType());
assertNotNull("We should find one geometry attribute", defaultGeom);
assertEquals("We should have found the attribute attached to SIS convention.", AttributeConvention.GEOMETRY_PROPERTY, defaultGeom.getName());
// Check we've got a definition matching reprojection
final Optional<AttributeType<?>> geomAttr = Features.toAttribute(defaultGeom);
assertTrue(geomAttr.isPresent());
AttributeType<?> crsCharacteristic = geomAttr.get().characteristics().get(AttributeConvention.CRS_CHARACTERISTIC.toString());
assertNotNull("No CRS characteristic found in returned geometry", crsCharacteristic);
assertEquals("CRS defined in returned geometry is not correct !", CommonCRS.WGS84.geographic(), crsCharacteristic.getDefaultValue());
}
Aggregations