use of org.geotoolkit.style.MutableRule in project geotoolkit by Geomatys.
the class CategoryStyleBuilder method createRule.
public MutableRule createRule(final ValueReference property, final Object obj) {
MutableRule r = sf.rule(createSymbolizer());
r.setFilter(ff.equal(property, ff.literal(obj)));
r.setDescription(sf.description(obj.toString(), obj.toString()));
return r;
}
use of org.geotoolkit.style.MutableRule 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;
}
}
}
}
}
use of org.geotoolkit.style.MutableRule in project geotoolkit by Geomatys.
the class CategoryStyleBuilder method create.
public List<MutableRule> create() {
// search the different values
final Set<Object> differentValues = new HashSet<>();
final ValueReference property = currentProperty;
final FeatureSet resource = (FeatureSet) layer.getData();
final Query query = new Query();
try {
query.setTypeName(resource.getType().getName());
} catch (DataStoreException ex) {
LOGGER.log(Level.FINE, "Error while accessing data", ex);
}
query.setProperties(new String[] { property.getXPath() });
try (Stream<Feature> stream = resource.subset(query).features(false)) {
final Iterator<Feature> features = stream.iterator();
while (features.hasNext()) {
final Feature feature = features.next();
differentValues.add(property.apply(feature));
}
} catch (DataStoreException | FeatureStoreRuntimeException ex) {
LOGGER.log(Level.FINE, "Error while accessing data", ex);
}
// generate the different rules
fts.rules().clear();
for (Object obj : differentValues) {
fts.rules().add(createRule(property, obj));
}
// generate the other rule if asked
if (other) {
MutableRule r = sf.rule(createSymbolizer());
r.setElseFilter(true);
r.setDescription(sf.description("other", "other"));
fts.rules().add(r);
}
return fts.rules();
}
use of org.geotoolkit.style.MutableRule in project geotoolkit by Geomatys.
the class IntervalStyleBuilder method generateRules.
public List<MutableRule> generateRules(final IntervalPalette palette, Filter combinedFilter) {
analyze();
List<MutableRule> rules = new ArrayList<MutableRule>();
final Expression exp;
if (normalize == null || normalize.equals(noValue)) {
exp = classification;
} else {
exp = ff.divide(classification, normalize);
}
for (int i = 1; i < values.length; i++) {
final MutableRule rule = sf.rule();
double start = values[i - 1];
double end = values[i];
// create the filter and title
final Filter interval;
final String title;
if (i == values.length - 1) {
// last element
Filter above = ff.greaterOrEqual(exp, ff.literal(start));
Filter under = ff.lessOrEqual(exp, ff.literal(end));
interval = ff.and(above, under);
title = "[ " + FORMAT.format(start) + " -> " + FORMAT.format(end) + " ]";
} else {
Filter above = ff.greaterOrEqual(exp, ff.literal(start));
Filter under = ff.less(exp, ff.literal(end));
interval = ff.and(above, under);
title = "[ " + FORMAT.format(start) + " -> " + FORMAT.format(end) + " [";
}
if (Filter.include().equals(combinedFilter) || combinedFilter == null) {
rule.setFilter(interval);
} else {
rule.setFilter(ff.and(combinedFilter, interval));
}
rule.setName(title);
rule.setDescription(sf.description(title, title));
// create the style
Symbolizer symbol = createSymbolizer(palette, (double) (i - 1) / (values.length - 2));
rule.symbolizers().add(symbol);
rules.add(rule);
}
return rules;
}
use of org.geotoolkit.style.MutableRule in project geotoolkit by Geomatys.
the class SEforSLD100Test method testRule.
@Test
public void testRule() throws JAXBException {
final Unmarshaller UNMARSHALLER = POOL.acquireUnmarshaller();
final Marshaller MARSHALLER = POOL.acquireMarshaller();
// Read test
Object obj = UNMARSHALLER.unmarshal(FILE_SE_RULE);
assertNotNull(obj);
Rule jax = (Rule) obj;
MutableRule rule = TRANSFORMER_GT.visitRule(jax);
assertNotNull(rule);
assertEquals(rule.getName(), valueName);
assertEquals(rule.getDescription().getTitle().toString(), valueTitle);
assertEquals(rule.getDescription().getAbstract().toString(), valueAbstract);
assertEquals(rule.getMinScaleDenominator(), 500d, DELTA);
assertEquals(rule.getMaxScaleDenominator(), 1000d, DELTA);
assertNull(rule.getLegend());
assertNotNull(rule.getFilter());
assertEquals(rule.symbolizers().size(), 4);
// Write test
Rule pvt = TRANSFORMER_OGC.visit(rule, null);
assertNotNull(pvt);
assertEquals(pvt.getName(), valueName);
assertEquals(pvt.getTitle(), valueTitle);
assertEquals(pvt.getAbstract(), valueAbstract);
assertEquals(pvt.getMinScaleDenominator(), 500d, DELTA);
assertEquals(pvt.getMaxScaleDenominator(), 1000d, DELTA);
assertNull(pvt.getLegendGraphic());
assertEquals(pvt.getSymbolizer().size(), 4);
MARSHALLER.marshal(pvt, TEST_FILE_SE_RULE);
POOL.recycle(MARSHALLER);
POOL.recycle(UNMARSHALLER);
}
Aggregations