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;
}
}
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;
}
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;
}
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*/
}
}
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()]);
}
Aggregations