Search in sources :

Example 96 with PropertyType

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());
}
Also used : FeatureType(org.opengis.feature.FeatureType) Connection(java.sql.Connection) PropertyType(org.opengis.feature.PropertyType) Operation(org.opengis.feature.Operation) FeatureAssociationRole(org.opengis.feature.FeatureAssociationRole) Test(org.junit.Test)

Example 97 with PropertyType

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());
        }
    }
}
Also used : FeatureType(org.opengis.feature.FeatureType) GenericName(org.opengis.util.GenericName) AttributeType(org.opengis.feature.AttributeType) PropertyType(org.opengis.feature.PropertyType)

Example 98 with PropertyType

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 + "]");
            }
        }
    }
}
Also used : Geometry(org.locationtech.jts.geom.Geometry) PropertyType(org.opengis.feature.PropertyType) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) DataStoreException(org.apache.sis.storage.DataStoreException)

Example 99 with PropertyType

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);
}
Also used : FeatureType(org.opengis.feature.FeatureType) Query(org.apache.sis.storage.Query) Hints(org.geotoolkit.factory.Hints) UnsupportedQueryException(org.apache.sis.storage.UnsupportedQueryException) PropertyType(org.opengis.feature.PropertyType) Envelope(org.locationtech.jts.geom.Envelope) GenericName(org.opengis.util.GenericName) LooseBBox(org.geotoolkit.filter.binaryspatial.LooseBBox) AttributeType(org.opengis.feature.AttributeType) ShapefileFeatureReader(org.geotoolkit.data.shapefile.ShapefileFeatureReader) FeatureReader(org.geotoolkit.storage.feature.FeatureReader) DataStoreException(org.apache.sis.storage.DataStoreException) IOException(java.io.IOException) AbstractOperation(org.apache.sis.feature.AbstractOperation) FilterAttributeExtractor(org.geotoolkit.filter.visitor.FilterAttributeExtractor) Filter(org.opengis.filter.Filter) ResourceId(org.opengis.filter.ResourceId) CloseableCollection(org.geotoolkit.index.CloseableCollection)

Example 100 with PropertyType

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());
}
Also used : FeatureTypeBuilder(org.apache.sis.feature.builder.FeatureTypeBuilder) FeatureType(org.opengis.feature.FeatureType) PropertyNotFoundException(org.opengis.feature.PropertyNotFoundException) PropertyType(org.opengis.feature.PropertyType) Geometry(org.locationtech.jts.geom.Geometry) AttributeType(org.opengis.feature.AttributeType) Test(org.junit.Test)

Aggregations

PropertyType (org.opengis.feature.PropertyType)124 AttributeType (org.opengis.feature.AttributeType)55 FeatureType (org.opengis.feature.FeatureType)48 Feature (org.opengis.feature.Feature)41 ArrayList (java.util.ArrayList)38 Geometry (org.locationtech.jts.geom.Geometry)37 FeatureAssociationRole (org.opengis.feature.FeatureAssociationRole)26 GenericName (org.opengis.util.GenericName)26 FeatureTypeBuilder (org.apache.sis.feature.builder.FeatureTypeBuilder)24 CoordinateReferenceSystem (org.opengis.referencing.crs.CoordinateReferenceSystem)22 PropertyNotFoundException (org.opengis.feature.PropertyNotFoundException)20 Operation (org.opengis.feature.Operation)18 DataStoreException (org.apache.sis.storage.DataStoreException)16 Test (org.junit.Test)13 LineString (org.locationtech.jts.geom.LineString)12 HashMap (java.util.HashMap)11 Filter (org.opengis.filter.Filter)11 IOException (java.io.IOException)9 SQLException (java.sql.SQLException)9 FeatureSet (org.apache.sis.storage.FeatureSet)9