Search in sources :

Example 1 with FeatureAssociationRole

use of org.opengis.feature.FeatureAssociationRole in project geotoolkit by Geomatys.

the class GeoJSONReader method fillFeature.

/**
 * Recursively fill a ComplexAttribute with properties map
 *
 * @param feature
 * @param properties
 */
private void fillFeature(Feature feature, Map<String, Object> properties) throws BackingStoreException {
    final FeatureType featureType = feature.getType();
    for (final PropertyType type : featureType.getProperties(true)) {
        final String attName = type.getName().toString();
        final Object value = properties.get(attName);
        if (value == null) {
            continue;
        }
        if (type instanceof FeatureAssociationRole) {
            final FeatureAssociationRole asso = (FeatureAssociationRole) type;
            final FeatureType assoType = asso.getValueType();
            final Class<?> valueClass = value.getClass();
            if (valueClass.isArray()) {
                Class<?> base = value.getClass().getComponentType();
                if (!Map.class.isAssignableFrom(base)) {
                    LOGGER.log(Level.WARNING, "Invalid complex property value " + value);
                }
                final int size = Array.getLength(value);
                if (size > 0) {
                    // list of objects
                    final List<Feature> subs = new ArrayList<>();
                    for (int i = 0; i < size; i++) {
                        Object subValue = Array.get(value, i);
                        final Feature subComplexAttribute;
                        if (subValue instanceof Map) {
                            subComplexAttribute = assoType.newInstance();
                            fillFeature(subComplexAttribute, (Map) Array.get(value, i));
                        } else if (subValue instanceof GeoJSONFeature) {
                            subComplexAttribute = toFeature((GeoJSONFeature) subValue, assoType);
                        } else {
                            throw new IllegalArgumentException("Sub value must be a GeoJSONFeature or a map");
                        }
                        subs.add(subComplexAttribute);
                    }
                    feature.setPropertyValue(attName, subs);
                }
            } else if (value instanceof Map) {
                final Feature subComplexAttribute = assoType.newInstance();
                fillFeature(subComplexAttribute, (Map) value);
                feature.setPropertyValue(attName, subComplexAttribute);
            } else if (value instanceof GeoJSONFeature) {
                final Feature subComplexAttribute = toFeature((GeoJSONFeature) value, assoType);
                feature.setPropertyValue(attName, subComplexAttribute);
            } else if (value instanceof GeoJSONFeatureCollection) {
                GeoJSONFeatureCollection collection = (GeoJSONFeatureCollection) value;
                final List<Feature> subFeatures = new ArrayList<>();
                for (GeoJSONFeature subFeature : collection.getFeatures()) {
                    subFeatures.add(toFeature(subFeature, assoType));
                }
                feature.setPropertyValue(attName, subFeatures);
            } else {
                LOGGER.warning("Unexpected attribute value type:" + value.getClass());
            }
        } else if (type instanceof AttributeType) {
            final Attribute<?> property = (Attribute<?>) feature.getProperty(type.getName().toString());
            fillProperty(property, value);
        }
    }
}
Also used : FeatureType(org.opengis.feature.FeatureType) Attribute(org.opengis.feature.Attribute) GeoJSONFeature(org.geotoolkit.internal.geojson.binding.GeoJSONFeature) ArrayList(java.util.ArrayList) PropertyType(org.opengis.feature.PropertyType) Feature(org.opengis.feature.Feature) GeoJSONFeature(org.geotoolkit.internal.geojson.binding.GeoJSONFeature) GeoJSONFeatureCollection(org.geotoolkit.internal.geojson.binding.GeoJSONFeatureCollection) AttributeType(org.opengis.feature.AttributeType) GeoJSONObject(org.geotoolkit.internal.geojson.binding.GeoJSONObject) ArrayList(java.util.ArrayList) List(java.util.List) FeatureAssociationRole(org.opengis.feature.FeatureAssociationRole) HashMap(java.util.HashMap) Map(java.util.Map) AbstractMap(java.util.AbstractMap)

Example 2 with FeatureAssociationRole

use of org.opengis.feature.FeatureAssociationRole in project geotoolkit by Geomatys.

the class GeoJSONWriter method writeProperties.

/**
 * Write ComplexAttribute.
 *
 * @param edited
 * @param fieldName
 * @param writeFieldName
 * @throws IOException
 * @throws IllegalArgumentException
 */
private void writeProperties(Feature edited, String fieldName, boolean writeFieldName, Set<Feature> alreadyWritten) throws IOException, IllegalArgumentException {
    if (writeFieldName) {
        writer.writeObjectFieldStart(fieldName);
    } else {
        writer.writeStartObject();
    }
    FeatureType type = edited.getType();
    PropertyType defGeom = FeatureExt.getDefaultGeometrySafe(type).flatMap(Features::toAttribute).orElse(null);
    Collection<? extends PropertyType> descriptors = type.getProperties(true).stream().filter(GeoJSONUtils.IS_NOT_CONVENTION).filter(it -> !Objects.equals(defGeom, it)).collect(Collectors.toList());
    for (PropertyType propType : descriptors) {
        final String name = propType.getName().tip().toString();
        final Object value = edited.getPropertyValue(propType.getName().toString());
        if (propType instanceof AttributeType) {
            final AttributeType attType = (AttributeType) propType;
            if (attType.getMaximumOccurs() > 1) {
                writer.writeArrayFieldStart(name);
                for (Object v : (Collection) value) {
                    writeProperty(name, v, false, alreadyWritten);
                }
                writer.writeEndArray();
            } else {
                writeProperty(name, value, true, alreadyWritten);
            }
        } else if (propType instanceof FeatureAssociationRole) {
            final FeatureAssociationRole asso = (FeatureAssociationRole) propType;
            if (asso.getMaximumOccurs() > 1) {
                writer.writeFieldName(name);
                writeFeatureCollection((List<Feature>) value, asso.getValueType());
            } else {
                writeProperty(name, value, true, alreadyWritten);
            }
        } else if (propType instanceof Operation) {
            writeProperty(name, value, true, alreadyWritten);
        }
    }
    writer.writeEndObject();
}
Also used : GeoJSONMultiPoint(org.geotoolkit.internal.geojson.binding.GeoJSONGeometry.GeoJSONMultiPoint) GeoJSONParser(org.geotoolkit.internal.geojson.GeoJSONParser) Link(org.geotoolkit.atom.xml.Link) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) TRUNCATE_EXISTING(java.nio.file.StandardOpenOption.TRUNCATE_EXISTING) NumberFormat(java.text.NumberFormat) Envelope(org.opengis.geometry.Envelope) Level(java.util.logging.Level) GeoJSONGeometry(org.geotoolkit.internal.geojson.binding.GeoJSONGeometry) GeoJSONMultiPolygon(org.geotoolkit.internal.geojson.binding.GeoJSONGeometry.GeoJSONMultiPolygon) FeatureType(org.opengis.feature.FeatureType) Operation(org.opengis.feature.Operation) JsonEncoding(com.fasterxml.jackson.core.JsonEncoding) Locale(java.util.Locale) GeoJSONMultiLineString(org.geotoolkit.internal.geojson.binding.GeoJSONGeometry.GeoJSONMultiLineString) GeoJSONPoint(org.geotoolkit.internal.geojson.binding.GeoJSONGeometry.GeoJSONPoint) FeatureAssociationRole(org.opengis.feature.FeatureAssociationRole) CommonCRS(org.apache.sis.referencing.CommonCRS) GeoJSONUtils(org.geotoolkit.internal.geojson.GeoJSONUtils) Path(java.nio.file.Path) WRITE(java.nio.file.StandardOpenOption.WRITE) Feature(org.opengis.feature.Feature) Utilities(org.apache.sis.util.Utilities) IdentityHashMap(java.util.IdentityHashMap) Files(java.nio.file.Files) Collection(java.util.Collection) Set(java.util.Set) AttributeType(org.opengis.feature.AttributeType) Attribute(org.opengis.feature.Attribute) Features(org.apache.sis.feature.Features) Collectors(java.util.stream.Collectors) GeoJSONLineString(org.geotoolkit.internal.geojson.binding.GeoJSONGeometry.GeoJSONLineString) PropertyNotFoundException(org.opengis.feature.PropertyNotFoundException) Objects(java.util.Objects) List(java.util.List) JsonFactory(com.fasterxml.jackson.core.JsonFactory) PropertyType(org.opengis.feature.PropertyType) java.io(java.io) AttributeConvention(org.apache.sis.internal.feature.AttributeConvention) CREATE(java.nio.file.StandardOpenOption.CREATE) GeoJSONGeometryCollection(org.geotoolkit.internal.geojson.binding.GeoJSONGeometry.GeoJSONGeometryCollection) FeatureExt(org.geotoolkit.feature.FeatureExt) Optional(java.util.Optional) Geometry(org.locationtech.jts.geom.Geometry) GeoJSONPolygon(org.geotoolkit.internal.geojson.binding.GeoJSONGeometry.GeoJSONPolygon) GeoJSONConstants(org.geotoolkit.storage.geojson.GeoJSONConstants) Collections(java.util.Collections) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) FeatureType(org.opengis.feature.FeatureType) AttributeType(org.opengis.feature.AttributeType) Collection(java.util.Collection) GeoJSONGeometryCollection(org.geotoolkit.internal.geojson.binding.GeoJSONGeometry.GeoJSONGeometryCollection) List(java.util.List) PropertyType(org.opengis.feature.PropertyType) GeoJSONMultiLineString(org.geotoolkit.internal.geojson.binding.GeoJSONGeometry.GeoJSONMultiLineString) GeoJSONLineString(org.geotoolkit.internal.geojson.binding.GeoJSONGeometry.GeoJSONLineString) Operation(org.opengis.feature.Operation) FeatureAssociationRole(org.opengis.feature.FeatureAssociationRole)

Example 3 with FeatureAssociationRole

use of org.opengis.feature.FeatureAssociationRole in project geotoolkit by Geomatys.

the class GridCoverageFeatureSetTest method coverageRecordRGBTest.

/**
 * Test coverage rgb mapped as a feature.
 *
 * @throws DataStoreException
 */
@Test
public void coverageRecordRGBTest() throws DataStoreException {
    // create coverage
    final BufferedImage image = new BufferedImage(2, 2, BufferedImage.TYPE_INT_ARGB);
    image.setRGB(0, 0, Color.BLUE.getRGB());
    image.setRGB(1, 0, Color.RED.getRGB());
    image.setRGB(0, 1, Color.GREEN.getRGB());
    image.setRGB(1, 1, Color.PINK.getRGB());
    final GridCoverageBuilder gcb = new GridCoverageBuilder();
    gcb.setValues(image);
    gcb.setDomain(new GridGeometry(null, PixelInCell.CELL_CENTER, new AffineTransform2D(2, 0, 0, 2, 31, 11), CommonCRS.WGS84.normalizedGeographic()));
    final GridCoverage coverage = gcb.build();
    // test mapped feature type
    final FeatureType coverageType = GridCoverageFeatureSet.createCoverageType(coverage);
    final FeatureAssociationRole role = (FeatureAssociationRole) coverageType.getProperty(TypeConventions.RANGE_ELEMENTS_PROPERTY.toString());
    final FeatureType recordType = role.getValueType();
    assertEquals("Coverage", coverageType.getName().toString());
    assertTrue(TypeConventions.COVERAGE_TYPE.isAssignableFrom(coverageType));
    assertEquals("Record", recordType.getName().tip().toString());
    assertTrue(TypeConventions.COVERAGE_RECORD_TYPE.isAssignableFrom(recordType));
    // convert coverage to feature
    final Feature feature = coverageType.newInstance();
    feature.setProperty(GridCoverageFeatureSet.coverageRecords(coverage, role));
    // check records
    final Collection col = (Collection) feature.getPropertyValue(TypeConventions.RANGE_ELEMENTS_PROPERTY.toString());
    assertEquals(4, col.size());
    final Iterator<Feature> ite = col.iterator();
    final Feature r1 = ite.next();
    final Feature r2 = ite.next();
    final Feature r3 = ite.next();
    final Feature r4 = ite.next();
    assertFalse(ite.hasNext());
    assertEquals(0.0, r1.getPropertyValue("Red"));
    assertEquals(0.0, r1.getPropertyValue("Green"));
    assertEquals(255.0, r1.getPropertyValue("Blue"));
    assertEquals(255.0, r1.getPropertyValue("Transparency"));
    assertEquals(Color.BLUE, r1.getPropertyValue("color"));
    assertEquals(255.0, r2.getPropertyValue("Red"));
    assertEquals(0.0, r2.getPropertyValue("Green"));
    assertEquals(0.0, r2.getPropertyValue("Blue"));
    assertEquals(255.0, r2.getPropertyValue("Transparency"));
    assertEquals(Color.RED, r2.getPropertyValue("color"));
    assertEquals(0.0, r3.getPropertyValue("Red"));
    assertEquals(255.0, r3.getPropertyValue("Green"));
    assertEquals(0.0, r3.getPropertyValue("Blue"));
    assertEquals(255.0, r3.getPropertyValue("Transparency"));
    assertEquals(Color.GREEN, r3.getPropertyValue("color"));
    assertEquals(255.0, r4.getPropertyValue("Red"));
    assertEquals(175.0, r4.getPropertyValue("Green"));
    assertEquals(175.0, r4.getPropertyValue("Blue"));
    assertEquals(255.0, r4.getPropertyValue("Transparency"));
    assertEquals(Color.PINK, r4.getPropertyValue("color"));
}
Also used : GridGeometry(org.apache.sis.coverage.grid.GridGeometry) FeatureType(org.opengis.feature.FeatureType) BufferedGridCoverage(org.apache.sis.coverage.grid.BufferedGridCoverage) GridCoverage(org.apache.sis.coverage.grid.GridCoverage) GridCoverageBuilder(org.apache.sis.coverage.grid.GridCoverageBuilder) Collection(java.util.Collection) FeatureAssociationRole(org.opengis.feature.FeatureAssociationRole) Feature(org.opengis.feature.Feature) BufferedImage(java.awt.image.BufferedImage) AffineTransform2D(org.apache.sis.internal.referencing.j2d.AffineTransform2D) Test(org.junit.Test)

Example 4 with FeatureAssociationRole

use of org.opengis.feature.FeatureAssociationRole in project geotoolkit by Geomatys.

the class GridCoverageFeatureSetTest method coverageRecord2DTest.

/**
 * Test coverage 2D mapped as a feature.
 *
 * @throws DataStoreException
 */
@Test
public void coverageRecord2DTest() throws DataStoreException {
    // create coverage
    final BufferedImage image = BufferedImages.createImage(2, 2, 2, DataBuffer.TYPE_INT);
    final WritableRaster raster = image.getRaster();
    raster.setPixel(0, 0, new int[] { 10, 2 });
    raster.setPixel(1, 0, new int[] { 30, 4 });
    raster.setPixel(0, 1, new int[] { 50, 6 });
    raster.setPixel(1, 1, new int[] { 70, 8 });
    SampleDimension.Builder sdb = new SampleDimension.Builder();
    sdb.setName("values");
    sdb.addQuantitative("valuesCat", NumberRange.create(0, true, 1000, true), (MathTransform1D) MathTransforms.linear(10, -5), null);
    final SampleDimension sdim1 = sdb.build();
    sdb.clear();
    sdb.setName("quality");
    sdb.addQuantitative("qualityCat", NumberRange.create(0, true, 100, true), (MathTransform1D) MathTransforms.linear(1, 0), null);
    final SampleDimension sdim2 = sdb.build();
    final GridCoverageBuilder gcb = new GridCoverageBuilder();
    gcb.setValues(image);
    gcb.setDomain(new GridGeometry(null, PixelInCell.CELL_CENTER, new AffineTransform2D(2, 0, 0, 2, 31, 11), CommonCRS.WGS84.normalizedGeographic()));
    gcb.setRanges(sdim1, sdim2);
    final GridCoverage coverage = gcb.build();
    // test mapped feature type
    final FeatureType coverageType = GridCoverageFeatureSet.createCoverageType(coverage);
    final FeatureAssociationRole role = (FeatureAssociationRole) coverageType.getProperty(TypeConventions.RANGE_ELEMENTS_PROPERTY.toString());
    final FeatureType recordType = role.getValueType();
    assertEquals("Coverage", coverageType.getName().toString());
    assertTrue(TypeConventions.COVERAGE_TYPE.isAssignableFrom(coverageType));
    assertEquals("Record", recordType.getName().tip().toString());
    assertTrue(TypeConventions.COVERAGE_RECORD_TYPE.isAssignableFrom(recordType));
    // convert coverage to feature
    final Feature feature = coverageType.newInstance();
    feature.setProperty(GridCoverageFeatureSet.coverageRecords(coverage, role));
    // check records
    final Collection col = (Collection) feature.getPropertyValue(TypeConventions.RANGE_ELEMENTS_PROPERTY.toString());
    assertEquals(4, col.size());
    final Iterator<Feature> ite = col.iterator();
    final Feature r1 = ite.next();
    final Feature r2 = ite.next();
    final Feature r3 = ite.next();
    final Feature r4 = ite.next();
    assertFalse(ite.hasNext());
    assertEquals(10.0 * 10 - 5, r1.getPropertyValue("values"));
    assertEquals(2.0, r1.getPropertyValue("quality"));
    assertEquals(GF.createPolygon(new LiteCoordinateSequence(new double[] { 30, 10, 32, 10, 32, 12, 30, 12, 30, 10 })), r1.getProperty("geometry").getValue());
    assertEquals(30.0 * 10 - 5, r2.getPropertyValue("values"));
    assertEquals(4.0, r2.getPropertyValue("quality"));
    assertEquals(GF.createPolygon(new LiteCoordinateSequence(new double[] { 32, 10, 34, 10, 34, 12, 32, 12, 32, 10 })), r2.getProperty("geometry").getValue());
    assertEquals(50.0 * 10 - 5, r3.getPropertyValue("values"));
    assertEquals(6.0, r3.getPropertyValue("quality"));
    assertEquals(GF.createPolygon(new LiteCoordinateSequence(new double[] { 30, 12, 32, 12, 32, 14, 30, 14, 30, 12 })), r3.getProperty("geometry").getValue());
    assertEquals(70.0 * 10 - 5, r4.getPropertyValue("values"));
    assertEquals(8.0, r4.getPropertyValue("quality"));
    assertEquals(GF.createPolygon(new LiteCoordinateSequence(new double[] { 32, 12, 34, 12, 34, 14, 32, 14, 32, 12 })), r4.getProperty("geometry").getValue());
}
Also used : GridGeometry(org.apache.sis.coverage.grid.GridGeometry) FeatureType(org.opengis.feature.FeatureType) GridCoverageBuilder(org.apache.sis.coverage.grid.GridCoverageBuilder) SampleDimension(org.apache.sis.coverage.SampleDimension) Feature(org.opengis.feature.Feature) BufferedImage(java.awt.image.BufferedImage) LiteCoordinateSequence(org.geotoolkit.geometry.jts.coordinatesequence.LiteCoordinateSequence) BufferedGridCoverage(org.apache.sis.coverage.grid.BufferedGridCoverage) GridCoverage(org.apache.sis.coverage.grid.GridCoverage) GridCoverageBuilder(org.apache.sis.coverage.grid.GridCoverageBuilder) WritableRaster(java.awt.image.WritableRaster) Collection(java.util.Collection) FeatureAssociationRole(org.opengis.feature.FeatureAssociationRole) AffineTransform2D(org.apache.sis.internal.referencing.j2d.AffineTransform2D) Test(org.junit.Test)

Example 5 with FeatureAssociationRole

use of org.opengis.feature.FeatureAssociationRole in project geotoolkit by Geomatys.

the class JAXPStreamFeatureReader method setValue.

public static void setValue(Feature feature, PropertyType propertyType, GenericName propName, String nameAttribute, Object value) throws XMLStreamException {
    if (value == null)
        return;
    final Object previousVal = feature.getPropertyValue(propName.toString());
    if (propertyType instanceof FeatureAssociationRole) {
        final FeatureAssociationRole role = (FeatureAssociationRole) propertyType;
        if (role.getMaximumOccurs() > 1) {
            final List vals = new ArrayList((Collection) previousVal);
            vals.add(value);
            feature.setPropertyValue(propName.toString(), vals);
        } else {
            if (previousVal != null) {
                if (previousVal instanceof List) {
                    ((List) previousVal).add(value);
                } else if (previousVal instanceof Map) {
                    if (nameAttribute != null) {
                        ((Map) previousVal).put(nameAttribute, value);
                    } else {
                        LOGGER.severe("unable to read a composite attribute : no name has been found");
                    }
                }
            } else {
                feature.setPropertyValue(propName.toString(), value);
            }
        }
    } else {
        if (previousVal != null) {
            if (previousVal instanceof Map) {
                if (nameAttribute != null) {
                    ((Map) previousVal).put(nameAttribute, value);
                } else {
                    LOGGER.severe("unable to read a composite attribute : no name has been found");
                }
            } else if (previousVal instanceof Collection) {
                final List vals = new ArrayList((Collection) previousVal);
                vals.add(value);
                feature.setPropertyValue(propName.toString(), vals);
            } else {
                throw new XMLStreamException("Expected a multivalue property");
            }
        } else {
            // new property
            if (nameAttribute != null) {
                final Map<String, Object> map = new LinkedHashMap<>();
                map.put(nameAttribute, value);
                feature.setPropertyValue(propName.toString(), map);
            } else {
                feature.setPropertyValue(propName.toString(), value);
            }
        }
    }
}
Also used : XMLStreamException(javax.xml.stream.XMLStreamException) FeatureAssociationRole(org.opengis.feature.FeatureAssociationRole)

Aggregations

FeatureAssociationRole (org.opengis.feature.FeatureAssociationRole)48 FeatureType (org.opengis.feature.FeatureType)31 PropertyType (org.opengis.feature.PropertyType)25 AttributeType (org.opengis.feature.AttributeType)21 Feature (org.opengis.feature.Feature)18 GenericName (org.opengis.util.GenericName)12 Collection (java.util.Collection)9 Test (org.junit.Test)9 Operation (org.opengis.feature.Operation)9 Connection (java.sql.Connection)7 SQLException (java.sql.SQLException)7 Statement (java.sql.Statement)6 ArrayList (java.util.ArrayList)6 QName (javax.xml.namespace.QName)6 FeatureTypeBuilder (org.apache.sis.feature.builder.FeatureTypeBuilder)6 SimpleInternationalString (org.apache.sis.util.SimpleInternationalString)5 Attribute (org.opengis.feature.Attribute)5 GridGeometry (org.apache.sis.coverage.grid.GridGeometry)4 AttributeTypeBuilder (org.apache.sis.feature.builder.AttributeTypeBuilder)4 Timestamp (java.sql.Timestamp)3