Search in sources :

Example 26 with AttributeType

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

the class MapfileReader method readElement.

private static Feature readElement(final Feature parent, final LineNumberReader reader, String line) throws IOException {
    if (line == null) {
        line = reader.readLine().trim();
    }
    if (line == null) {
        return null;
    }
    final String typeName;
    String value;
    final int spaceIndex = line.indexOf(' ');
    if (spaceIndex < 0) {
        // value is on next lines
        typeName = line;
        value = null;
    } else {
        // value is on the line
        typeName = line.substring(0, spaceIndex);
        value = line.substring(spaceIndex);
    }
    if (typeName.equalsIgnoreCase("INCLUDE")) {
    // TODO open the related file and append all string to it
    }
    final FeatureType ft = getType(typeName);
    if (value == null && ft != null) {
        // it's a feature type
        final Feature f = ft.newInstance();
        // read until element END
        line = reader.readLine().trim();
        while (!line.equalsIgnoreCase("END")) {
            final Feature prop = readElement(f, reader, line);
            if (prop != null) {
                final String name = prop.getType().getName().toString();
                final Object oldValues = f.getPropertyValue(name);
                if (oldValues instanceof Collection) {
                    final List<Feature> values = new ArrayList<>((Collection) oldValues);
                    values.add(prop);
                    f.setPropertyValue(name, values);
                } else {
                    f.setPropertyValue(name, prop);
                }
            }
            line = reader.readLine().trim();
        }
        return f;
    } else {
        // read the full value if needed
        if (value == null) {
            // read all until next END element
            line = reader.readLine().trim();
            while (!line.equalsIgnoreCase("END")) {
                value += line;
                line = reader.readLine().trim();
            }
        }
        // it's a single property from parent type
        final AttributeType desc = getDescriptorIgnoreCase(parent.getType(), typeName);
        if (desc != null) {
            parent.setPropertyValue(desc.getName().toString(), convertType(value, desc));
        }
        return null;
    }
}
Also used : FeatureType(org.opengis.feature.FeatureType) AttributeType(org.opengis.feature.AttributeType) ArrayList(java.util.ArrayList) Collection(java.util.Collection) Feature(org.opengis.feature.Feature)

Example 27 with AttributeType

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

the class Query method reproject.

/**
 * Create a simple query with columns which transform all geometric types to the given crs.
 */
public static FeatureQuery reproject(FeatureType type, final CoordinateReferenceSystem crs) {
    final FilterFactory ff = FilterUtilities.FF;
    final Literal crsLiteral = ff.literal(crs);
    final FeatureQuery query = new FeatureQuery();
    final List<FeatureQuery.NamedExpression> columns = new ArrayList<>();
    for (PropertyType pt : type.getProperties(true)) {
        final GenericName name = pt.getName();
        Expression property = ff.property(name.toString());
        // unroll operation
        IdentifiedType result = pt;
        while (result instanceof Operation) {
            result = ((Operation) result).getResult();
        }
        if (result instanceof AttributeType) {
            AttributeType at = (AttributeType) result;
            if (Geometry.class.isAssignableFrom(at.getValueClass())) {
                property = ff.function("ST_Transform", property, crsLiteral);
            }
        }
        columns.add(new FeatureQuery.NamedExpression(property, name));
    }
    query.setProjection(columns.toArray(new FeatureQuery.NamedExpression[0]));
    return query;
}
Also used : ArrayList(java.util.ArrayList) FeatureQuery(org.apache.sis.storage.FeatureQuery) PropertyType(org.opengis.feature.PropertyType) Operation(org.opengis.feature.Operation) FilterFactory(org.opengis.filter.FilterFactory) GenericName(org.opengis.util.GenericName) Expression(org.opengis.filter.Expression) AttributeType(org.opengis.feature.AttributeType) Literal(org.opengis.filter.Literal) IdentifiedType(org.opengis.feature.IdentifiedType)

Example 28 with AttributeType

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

the class DefaultFeatureMapper method transform.

@Override
public Feature transform(final Feature feature) {
    final Feature res = typeTarget.newInstance();
    // set all default values
    for (final PropertyType desc : typeTarget.getProperties(true)) {
        final AttributeType attType = (AttributeType) desc;
        Object val = defaults.get(desc);
        if (val == null) {
            val = ((AttributeType) desc).getDefaultValue();
        }
        try {
            res.setPropertyValue(desc.getName().toString(), ObjectConverters.convert(val, attType.getValueClass()));
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    for (final PropertyType sourceDesc : mapping.keySet()) {
        final List<PropertyType> links = mapping.get(sourceDesc);
        if (links == null || links.isEmpty()) {
            continue;
        }
        final AttributeType srcType = (AttributeType) sourceDesc;
        final Object value = feature.getPropertyValue(sourceDesc.getName().toString());
        for (final PropertyType targetDesc : links) {
            Object converted = convert(value, srcType, (AttributeType) targetDesc);
            if (converted != null) {
                res.setPropertyValue(targetDesc.getName().toString(), converted);
            }
        }
    }
    return res;
}
Also used : AttributeType(org.opengis.feature.AttributeType) PropertyType(org.opengis.feature.PropertyType) Feature(org.opengis.feature.Feature) PropertyNotFoundException(org.opengis.feature.PropertyNotFoundException)

Example 29 with AttributeType

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

the class ViewFeatureTypeTest method filterOperationTest.

@Test
public void filterOperationTest() {
    final FeatureTypeBuilder ftb = new FeatureTypeBuilder();
    ftb.setName("test");
    final AttributeType<String> attString = ftb.addAttribute(String.class).setName("attString").build();
    ftb.addProperty(FeatureOperations.link(Collections.singletonMap("name", "attRef"), attString));
    final FeatureType baseType = ftb.build();
    // test view type
    final ViewMapper viewType = new ViewMapper(baseType, "attRef");
    final Collection<? extends PropertyType> properties = viewType.getMappedType().getProperties(true);
    assertEquals(1, properties.size());
    final PropertyType attRef = properties.iterator().next();
    assertTrue(attRef instanceof AttributeType);
    assertNotEquals(baseType.getProperty("attRef"), attRef);
    // test feature
    final Feature baseFeature = baseType.newInstance();
    baseFeature.setPropertyValue("attString", "hello world");
    final Feature viewFeature = viewType.apply(baseFeature);
    assertEquals("hello world", viewFeature.getPropertyValue("attRef"));
    try {
        viewFeature.getPropertyValue("attString");
        fail("Property attString should not have been accessible");
    } catch (PropertyNotFoundException ex) {
    /*ok*/
    }
}
Also used : FeatureTypeBuilder(org.apache.sis.feature.builder.FeatureTypeBuilder) FeatureType(org.opengis.feature.FeatureType) PropertyNotFoundException(org.opengis.feature.PropertyNotFoundException) AttributeType(org.opengis.feature.AttributeType) PropertyType(org.opengis.feature.PropertyType) ViewMapper(org.geotoolkit.feature.ViewMapper) Feature(org.opengis.feature.Feature) Test(org.junit.Test)

Example 30 with AttributeType

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

the class GO2Utilities method getValidCachedRules.

public static CachedRule[] getValidCachedRules(final Style style, final double scale, final FeatureType type) {
    final List<CachedRule> validRules = new ArrayList<CachedRule>();
    final List<? extends FeatureTypeStyle> ftss = style.featureTypeStyles();
    for (final FeatureTypeStyle fts : ftss) {
        final Set<GenericName> names = fts.featureTypeNames();
        // check semantic, only if we have a feature type
        if (type != null) {
            final Collection<SemanticType> semantics = fts.semanticTypeIdentifiers();
            if (!semantics.isEmpty()) {
                Class ctype;
                try {
                    ctype = Features.toAttribute(FeatureExt.getDefaultGeometry(type)).map(AttributeType::getValueClass).orElse(null);
                } catch (PropertyNotFoundException e) {
                    ctype = null;
                }
                boolean valid = false;
                for (SemanticType semantic : semantics) {
                    if (semantic == SemanticType.ANY) {
                        valid = true;
                        break;
                    } else if (semantic == SemanticType.LINE) {
                        if (ctype == LineString.class || ctype == MultiLineString.class) {
                            valid = true;
                            break;
                        }
                    } else if (semantic == SemanticType.POINT) {
                        if (ctype == Point.class || ctype == MultiPoint.class) {
                            valid = true;
                            break;
                        }
                    } else if (semantic == SemanticType.POLYGON) {
                        if (ctype == Polygon.class || ctype == MultiPolygon.class) {
                            valid = true;
                            break;
                        }
                    } else if (semantic == SemanticType.RASTER) {
                    // can not test this on feature datas
                    } else if (semantic == SemanticType.TEXT) {
                    // no text type in JTS, that's a stupid thing this Text semantic
                    }
                }
                if (!valid)
                    continue;
            }
        }
        // TODO filter correctly possibilities
        // test if the featutetype is valid
        // we move to next feature  type if not valid
        // if (false) continue;
        // if (typeName != null && !(typeName.equalsIgnoreCase(fts.getFeatureTypeName())) ) continue;
        final List<? extends Rule> rules = fts.rules();
        for (final Rule rule : rules) {
            // test if the scale is valid for this rule
            if (rule.getMinScaleDenominator() - SE_EPSILON <= scale && rule.getMaxScaleDenominator() + SE_EPSILON > scale) {
                validRules.add(getCached(rule, type));
            }
        }
    }
    return validRules.toArray(new CachedRule[validRules.size()]);
}
Also used : MultiPoint(org.locationtech.jts.geom.MultiPoint) PropertyNotFoundException(org.opengis.feature.PropertyNotFoundException) ArrayList(java.util.ArrayList) MultiPoint(org.locationtech.jts.geom.MultiPoint) Point(org.locationtech.jts.geom.Point) InterpolationPoint(org.geotoolkit.style.function.InterpolationPoint) GenericName(org.opengis.util.GenericName) AttributeType(org.opengis.feature.AttributeType) CachedRule(org.geotoolkit.display2d.style.CachedRule) CachedRule(org.geotoolkit.display2d.style.CachedRule)

Aggregations

AttributeType (org.opengis.feature.AttributeType)91 PropertyType (org.opengis.feature.PropertyType)55 FeatureType (org.opengis.feature.FeatureType)33 Feature (org.opengis.feature.Feature)29 Geometry (org.locationtech.jts.geom.Geometry)28 FeatureTypeBuilder (org.apache.sis.feature.builder.FeatureTypeBuilder)23 ArrayList (java.util.ArrayList)22 FeatureAssociationRole (org.opengis.feature.FeatureAssociationRole)22 PropertyNotFoundException (org.opengis.feature.PropertyNotFoundException)19 GenericName (org.opengis.util.GenericName)19 Operation (org.opengis.feature.Operation)16 DataStoreException (org.apache.sis.storage.DataStoreException)14 CoordinateReferenceSystem (org.opengis.referencing.crs.CoordinateReferenceSystem)12 IOException (java.io.IOException)11 HashMap (java.util.HashMap)9 Attribute (org.opengis.feature.Attribute)9 Map (java.util.Map)8 DefaultAttributeType (org.apache.sis.feature.DefaultAttributeType)8 FeatureSet (org.apache.sis.storage.FeatureSet)8 Collection (java.util.Collection)7