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);
}
}
}
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();
}
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"));
}
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());
}
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);
}
}
}
}
Aggregations