Search in sources :

Example 1 with PropertyType

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

the class KmlFeatureUtilities method buildKMLFeature.

/**
 * create a {@link Geometry from a Feature.}
 *
 * @param noKmlFeature feature from an other type
 * @param defaultIdStyle style defined on document.
 * @return a valid kml {@link Feature}
 */
public static Feature buildKMLFeature(Feature noKmlFeature, IdAttributes defaultIdStyle) {
    // Transform geometry
    final KmlFactory kmlFactory = DefaultKmlFactory.getInstance();
    final Feature placemark = kmlFactory.createPlacemark();
    final String geoColumn = AttributeConvention.GEOMETRY;
    final AbstractGeometry ag = buildKMLGeometry((Geometry) noKmlFeature.getPropertyValue(geoColumn));
    placemark.setPropertyValue(KmlConstants.TAG_GEOMETRY, ag);
    try {
        placemark.setPropertyValue(KmlConstants.TAG_STYLE_URL, new URI("#" + defaultIdStyle.getId()));
    } catch (URISyntaxException e) {
        LOGGER.log(Level.WARNING, "unnable to define style URI", e);
    }
    // TODO : transform datas
    final List<Data> simpleDatas = new ArrayList<>(0);
    for (final PropertyType type : noKmlFeature.getType().getProperties(true)) {
        final Property property = noKmlFeature.getProperty(type.getName().toString());
        String localPartName = property.getName().tip().toString();
        final Object value = property.getValue();
        if (localPartName.equalsIgnoreCase(KmlConstants.TAG_NAME)) {
            placemark.setPropertyValue(KmlConstants.TAG_NAME, value);
        } else if (!(property.getName().toString().equalsIgnoreCase(geoColumn) || localPartName.equalsIgnoreCase("fid"))) {
            if (value != null) {
                Data simpleData = kmlFactory.createData();
                simpleData.setName(localPartName);
                simpleData.setValue(value.toString());
                simpleDatas.add(simpleData);
            }
        }
    }
    if (!simpleDatas.isEmpty()) {
        ExtendedData extendedData = kmlFactory.createExtendedData();
        extendedData.setDatas(simpleDatas);
        placemark.setPropertyValue(KmlConstants.TAG_EXTENDED_DATA, extendedData);
    }
    return placemark;
}
Also used : AbstractGeometry(org.geotoolkit.data.kml.model.AbstractGeometry) ExtendedData(org.geotoolkit.data.kml.model.ExtendedData) DefaultExtendedData(org.geotoolkit.data.kml.model.DefaultExtendedData) ArrayList(java.util.ArrayList) SimpleData(org.geotoolkit.data.kml.model.SimpleData) ExtendedData(org.geotoolkit.data.kml.model.ExtendedData) SchemaData(org.geotoolkit.data.kml.model.SchemaData) DefaultExtendedData(org.geotoolkit.data.kml.model.DefaultExtendedData) Data(org.geotoolkit.data.kml.model.Data) MultiLineString(org.locationtech.jts.geom.MultiLineString) LineString(org.locationtech.jts.geom.LineString) URISyntaxException(java.net.URISyntaxException) PropertyType(org.opengis.feature.PropertyType) Feature(org.opengis.feature.Feature) URI(java.net.URI) Property(org.opengis.feature.Property)

Example 2 with PropertyType

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

the class KmzContextInterpreter method writeFeature.

/**
 * Transforms a feature into KML feature (Placemak if original
 * features contents a geometry, or Folder otherwise).
 */
private Feature writeFeature(final Feature feature) {
    Feature kmlFeature = null;
    for (final PropertyType type : feature.getType().getProperties(true)) {
        final Object val = feature.getPropertyValue(type.getName().toString());
        if (val instanceof Feature) {
            kmlFeature = KML_FACTORY.createFolder();
            kmlFeature.setPropertyValue(KmlConstants.TAG_FEATURES, writeFeature((Feature) val));
        } else if (val instanceof Geometry) {
            kmlFeature = KML_FACTORY.createPlacemark();
            kmlFeature.setPropertyValue(KmlConstants.TAG_GEOMETRY, val);
        } else {
        // System.out.println("PAS FEATURE.");
        }
    }
    // Search feature style URI
    for (Entry<Rule, URI> e : IDENTIFICATORS_MAP) {
        final Rule rule = e.getKey();
        if (rule.getFilter().test(feature)) {
            kmlFeature.setPropertyValue(KmlConstants.TAG_STYLE_URL, e.getValue());
            for (Symbolizer s : rule.symbolizers()) {
                if (s instanceof TextSymbolizer) {
                    final Expression label = ((TextSymbolizer) s).getLabel();
                    if (label != null) {
                        kmlFeature.setPropertyValue(KmlConstants.TAG_NAME, writeExpression(label, String.class, feature));
                    }
                }
            }
            break;
        }
    }
    return kmlFeature;
}
Also used : AbstractGeometry(org.geotoolkit.data.kml.model.AbstractGeometry) Geometry(org.locationtech.jts.geom.Geometry) TextSymbolizer(org.opengis.style.TextSymbolizer) Expression(org.opengis.filter.Expression) PropertyType(org.opengis.feature.PropertyType) Rule(org.opengis.style.Rule) LineString(org.locationtech.jts.geom.LineString) Feature(org.opengis.feature.Feature) URI(java.net.URI) ExtensionSymbolizer(org.opengis.style.ExtensionSymbolizer) PointSymbolizer(org.opengis.style.PointSymbolizer) PolygonSymbolizer(org.opengis.style.PolygonSymbolizer) LineSymbolizer(org.opengis.style.LineSymbolizer) RasterSymbolizer(org.opengis.style.RasterSymbolizer) TextSymbolizer(org.opengis.style.TextSymbolizer) Symbolizer(org.opengis.style.Symbolizer)

Example 3 with PropertyType

use of org.opengis.feature.PropertyType 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 4 with PropertyType

use of org.opengis.feature.PropertyType 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 5 with PropertyType

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

the class CategoryStyleBuilder method analyze.

public void analyze(final MapLayer layer) {
    Resource resource = layer.getData();
    if (!(resource instanceof FeatureSet)) {
        throw new IllegalArgumentException("Layer resource must be a FeatureSet");
    }
    this.layer = layer;
    fts.rules().clear();
    properties.clear();
    if (layer != null) {
        FeatureType schema;
        try {
            schema = ((FeatureSet) resource).getType();
        } catch (DataStoreException ex) {
            throw new FeatureStoreRuntimeException(ex.getMessage(), ex);
        }
        for (PropertyType desc : schema.getProperties(true)) {
            if (desc instanceof AttributeType) {
                Class<?> type = ((AttributeType) desc).getValueClass();
                if (!Geometry.class.isAssignableFrom(type)) {
                    properties.add(ff.property(desc.getName().tip().toString()));
                }
            }
        }
        // find the geometry class for template
        Class<?> geoClass = null;
        try {
            PropertyType geo = FeatureExt.getDefaultGeometry(schema);
            geoClass = Features.toAttribute(geo).map(AttributeType::getValueClass).orElse(null);
        } catch (PropertyNotFoundException ex) {
            LOGGER.log(Level.FINE, "No sis:geometry property found", ex);
        }
        if (geoClass == null) {
            return;
        }
        if (Polygon.class.isAssignableFrom(geoClass) || MultiPolygon.class.isAssignableFrom(geoClass)) {
            Stroke stroke = sf.stroke(Color.BLACK, 1);
            Fill fill = sf.fill(Color.BLUE);
            template = sf.polygonSymbolizer(stroke, fill, null);
            expectedType = PolygonSymbolizer.class;
        } else if (LineString.class.isAssignableFrom(geoClass) || MultiLineString.class.isAssignableFrom(geoClass)) {
            Stroke stroke = sf.stroke(Color.BLUE, 2);
            template = sf.lineSymbolizer(stroke, null);
            expectedType = LineSymbolizer.class;
        } else {
            Stroke stroke = sf.stroke(Color.BLACK, 1);
            Fill fill = sf.fill(Color.BLUE);
            List<GraphicalSymbol> symbols = new ArrayList<>();
            symbols.add(sf.mark(StyleConstants.MARK_CIRCLE, fill, stroke));
            Graphic gra = sf.graphic(symbols, ff.literal(1), ff.literal(12), ff.literal(0), sf.anchorPoint(), sf.displacement());
            template = sf.pointSymbolizer(gra, null);
            expectedType = PointSymbolizer.class;
        }
        // try to rebuild the previous analyze if it was one
        List<? extends FeatureTypeStyle> ftss = layer.getStyle().featureTypeStyles();
        if (ftss.size() == 1) {
            FeatureTypeStyle fts = ftss.get(0);
            // defensive copy avoid synchronization
            List<? extends Rule> candidateRules = new ArrayList<>(fts.rules());
            for (Rule r : candidateRules) {
                // defensive copy avoid synchronization
                List<? extends Symbolizer> candidateSymbols = new ArrayList<>(r.symbolizers());
                if (candidateSymbols.size() != 1)
                    break;
                Symbolizer symbol = candidateSymbols.get(0);
                if (expectedType.isInstance(symbol)) {
                    if (r.isElseFilter()) {
                        // it looks like it's a valid classification "other" rule
                        this.fts.rules().add((MutableRule) r);
                        template = symbol;
                        other = true;
                    } else {
                        Filter f = r.getFilter();
                        if (f != null && f.getOperatorType() == ComparisonOperatorName.PROPERTY_IS_EQUAL_TO) {
                            BinaryComparisonOperator equal = (BinaryComparisonOperator) f;
                            Expression exp1 = equal.getOperand1();
                            Expression exp2 = equal.getOperand2();
                            if (exp1 instanceof ValueReference && exp2 instanceof Literal) {
                                if (properties.contains(exp1)) {
                                    // it looks like it's a valid classification property rule
                                    this.fts.rules().add((MutableRule) r);
                                    template = symbol;
                                    currentProperty = (ValueReference) exp1;
                                } else {
                                    // property is not in the schema
                                    break;
                                }
                            } else if (exp2 instanceof ValueReference && exp1 instanceof Literal) {
                                if (properties.contains(exp2)) {
                                    // it looks like it's a valid classification property rule
                                    this.fts.rules().add((MutableRule) r);
                                    template = symbol;
                                    currentProperty = (ValueReference) exp2;
                                } else {
                                    // property is not in the schema
                                    break;
                                }
                            } else {
                                // mismatch analyze structure
                                break;
                            }
                        }
                    }
                } else {
                    break;
                }
            }
        }
    }
}
Also used : FeatureType(org.opengis.feature.FeatureType) Fill(org.opengis.style.Fill) PropertyNotFoundException(org.opengis.feature.PropertyNotFoundException) ArrayList(java.util.ArrayList) PropertyType(org.opengis.feature.PropertyType) AttributeType(org.opengis.feature.AttributeType) Literal(org.opengis.filter.Literal) List(java.util.List) ArrayList(java.util.ArrayList) Polygon(org.locationtech.jts.geom.Polygon) MultiPolygon(org.locationtech.jts.geom.MultiPolygon) ValueReference(org.opengis.filter.ValueReference) PointSymbolizer(org.opengis.style.PointSymbolizer) DataStoreException(org.apache.sis.storage.DataStoreException) Stroke(org.opengis.style.Stroke) Graphic(org.opengis.style.Graphic) Resource(org.apache.sis.storage.Resource) PointSymbolizer(org.opengis.style.PointSymbolizer) PolygonSymbolizer(org.opengis.style.PolygonSymbolizer) LineSymbolizer(org.opengis.style.LineSymbolizer) Symbolizer(org.opengis.style.Symbolizer) Geometry(org.locationtech.jts.geom.Geometry) MutableRule(org.geotoolkit.style.MutableRule) MultiPolygon(org.locationtech.jts.geom.MultiPolygon) Filter(org.opengis.filter.Filter) Expression(org.opengis.filter.Expression) LineSymbolizer(org.opengis.style.LineSymbolizer) FeatureStoreRuntimeException(org.geotoolkit.storage.feature.FeatureStoreRuntimeException) FeatureSet(org.apache.sis.storage.FeatureSet) MutableFeatureTypeStyle(org.geotoolkit.style.MutableFeatureTypeStyle) FeatureTypeStyle(org.opengis.style.FeatureTypeStyle) Rule(org.opengis.style.Rule) MutableRule(org.geotoolkit.style.MutableRule) BinaryComparisonOperator(org.opengis.filter.BinaryComparisonOperator)

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