use of org.geotoolkit.style.MutableRule in project geotoolkit by Geomatys.
the class CachedPatternSymbolizer method getMasks.
public Map.Entry<FeatureSet, MutableStyle> getMasks(final GridCoverage coverage) throws IOException, TransformException {
final List<Feature> features = new ArrayList<>();
final Map<NumberRange, List<Symbolizer>> styles = new LinkedHashMap<>();
final Map<NumberRange, Integer> stylesIndexes = new LinkedHashMap<>();
final Map<Expression, List<Symbolizer>> categorizes = styleElement.getRanges();
final Expression[] steps = categorizes.keySet().toArray(new Expression[categorizes.size()]);
Arrays.sort(steps, new Comparator<Expression>() {
@Override
public int compare(Expression t1, Expression t2) {
if (t1 == null) {
return -1;
} else if (t2 == null) {
return +1;
}
double d1 = ((Number) t1.apply(null)).doubleValue();
double d2 = ((Number) t2.apply(null)).doubleValue();
double res = d1 - d2;
if (res < 0) {
return -1;
} else if (res > 0) {
return +1;
} else {
return 0;
}
}
});
// fill the numberranges ------------------------------------------------
double last = Double.NEGATIVE_INFINITY;
double end = Double.POSITIVE_INFINITY;
NumberRange interval;
int i = 0;
int index = 0;
for (; i < steps.length - 1; i++) {
end = ((Number) steps[i + 1].apply(null)).doubleValue();
interval = NumberRange.create(last, true, end, false);
styles.put(interval, categorizes.get(steps[i]));
stylesIndexes.put(interval, index++);
last = end;
}
// last element
end = Double.POSITIVE_INFINITY;
NumberRange<Double> lastRange = NumberRange.create(last, true, end, true);
styles.put(lastRange, categorizes.get(steps[i]));
stylesIndexes.put(lastRange, index++);
// calculate the polygons -----------------------------------------------
final ProcessDescriptor descriptor = CoverageToVectorDescriptor.INSTANCE;
final Integer band = ((Number) styleElement.getChannel().apply(null)).intValue();
final ParameterValueGroup input = descriptor.getInputDescriptor().createValue();
input.parameter(CoverageToVectorDescriptor.COVERAGE.getName().getCode()).setValue(coverage);
final Set<NumberRange> nrs = styles.keySet();
input.parameter(CoverageToVectorDescriptor.RANGES.getName().getCode()).setValue(nrs.toArray(new NumberRange[nrs.size()]));
input.parameter(CoverageToVectorDescriptor.BAND.getName().getCode()).setValue(band);
final Process process = descriptor.createProcess(input);
final Geometry[] polygons;
try {
polygons = (Geometry[]) process.call().parameter(CoverageToVectorDescriptor.GEOMETRIES.getName().getCode()).getValue();
} catch (ProcessException ex) {
Logger.getLogger("org.geotoolkit.display2d.ext.pattern").log(Level.WARNING, null, ex);
throw new IOException(ex.getMessage(), ex);
}
// build the global style -----------------------------------------------
final MutableStyle style = GO2Utilities.STYLE_FACTORY.style();
final MutableFeatureTypeStyle fts = GO2Utilities.STYLE_FACTORY.featureTypeStyle();
style.featureTypeStyles().add(fts);
int idx = 0;
for (List<Symbolizer> lst : styles.values()) {
MutableRule rule = GO2Utilities.STYLE_FACTORY.rule();
rule.symbolizers().addAll(lst);
rule.setFilter(GO2Utilities.FILTER_FACTORY.equal(GO2Utilities.FILTER_FACTORY.property("category"), GO2Utilities.FILTER_FACTORY.literal(idx), true, MatchAction.ANY));
fts.rules().add(rule);
idx++;
}
// build the features ---------------------------------------------------
final CoordinateReferenceSystem crs = coverage.getCoordinateReferenceSystem();
final FeatureTypeBuilder sftBuilder = new FeatureTypeBuilder();
final String geometryField = "geometry";
sftBuilder.setName("DynamicFeature");
sftBuilder.addAttribute(Integer.class).setName("id").addRole(AttributeRole.IDENTIFIER_COMPONENT);
sftBuilder.addAttribute(Geometry.class).setName(geometryField).setCRS(crs).addRole(AttributeRole.DEFAULT_GEOMETRY);
sftBuilder.addAttribute(Integer.class).setName("category");
final FeatureType sft = sftBuilder.build();
int id = 0;
for (Geometry entry : polygons) {
if (entry == null)
continue;
NumberRange numberRange = (NumberRange) entry.getUserData();
idx = stylesIndexes.get(numberRange);
entry.setUserData(crs);
final Feature feature = sft.newInstance();
feature.setPropertyValue(geometryField, entry);
feature.setPropertyValue("id", id++);
feature.setPropertyValue("category", idx);
features.add(feature);
}
return new AbstractMap.SimpleEntry<>(new InMemoryFeatureSet(sft, features), style);
}
use of org.geotoolkit.style.MutableRule in project geotoolkit by Geomatys.
the class RenderingRoutines method prepareQuery.
/**
* Creates an optimal query to send to the datastore, knowing which properties are knowned and
* the appropriate bounding box to filter.
*/
public static Query prepareQuery(final RenderingContext2D renderingContext, FeatureSet fs, final MapLayer layer, final Set<String> styleRequieredAtts, final List<Rule> rules, double symbolsMargin) throws PortrayalException {
final FeatureType schema;
try {
schema = fs.getType();
} catch (DataStoreException ex) {
throw new PortrayalException(ex.getMessage(), ex);
}
// Note: do not use layer boundary to define the target bbox, because it can be expensive.
// Anyway, the target resource will be better to determine clipping between rendering boundaries and its own.
final Envelope bbox = optimizeBBox(renderingContext, fs, symbolsMargin);
final CoordinateReferenceSystem layerCRS = FeatureExt.getCRS(schema);
final RenderingHints hints = renderingContext.getRenderingHints();
/*
* To restrict queried values to the rendering area, we must identify what geometries are used by the style.
* For each applied symbol, there are 3 possible cases:
* - if the rule uses default geometries, they will be added to the geometry property list after the loop
* - The geometric expression is a value reference, we can safely register it in geometric properties. The
* reference xpath is unwrapped in a set to ensure we won't create any doublon filters.
* - If the geometry property is a complex expression(Ex: a value computed from non geometric fields), we keep
* it as is to apply a filter directly upon it. Note that even if it's an expression derived from geometric
* fields, we cannot apply spatial filter on them, because the expression could drastically change topology.
* For example, if the expression is 'buffer', the result geometry would be larger than any of its operands.
* TODO: such cases are maybe manageable by replacing bbox filter by a distance filter based upon the buffer
* distance. But would it do more good than harm ?
*/
boolean isDefaultGeometryNeeded = rules == null || rules.isEmpty();
final Set<String> geomProperties = new HashSet<>();
final Set<Expression> complexProperties = new HashSet<>();
if (rules != null) {
for (Rule r : rules) {
for (Symbolizer s : r.symbolizers()) {
final Expression expGeom = s.getGeometry();
if (isNil(expGeom))
isDefaultGeometryNeeded = true;
else if (expGeom instanceof ValueReference)
geomProperties.add(((ValueReference) expGeom).getXPath());
else
complexProperties.add(expGeom);
}
}
}
if (isDefaultGeometryNeeded) {
try {
final PropertyType defaultGeometry = FeatureExt.getDefaultGeometry(schema);
final String geomName = Features.getLinkTarget(defaultGeometry).orElseGet(() -> defaultGeometry.getName().toString());
geomProperties.add(geomName);
} catch (PropertyNotFoundException e) {
throw new PortrayalException("Default geometry cannot be determined. " + "However, it is needed to properly define filtering rules.");
} catch (IllegalStateException e) {
// If there's multiple geometric properties, and no primary one, we will use them all
schema.getProperties(true).stream().filter(p -> !Features.getLinkTarget(p).isPresent()).filter(AttributeConvention::isGeometryAttribute).map(p -> p.getName().toString()).forEach(geomProperties::add);
}
}
if (!complexProperties.isEmpty()) {
LOGGER.fine("A style rule uses complex geometric properties. It can severly affect performance");
}
final Optional<Filter> spatialFilter = Stream.concat(geomProperties.stream().map(FILTER_FACTORY::property), complexProperties.stream()).<Filter>map(expression -> FILTER_FACTORY.bbox(expression, bbox)).reduce(FILTER_FACTORY::or);
Filter userFilter = null;
// concatenate geographic filter with data filter if there is one
if (layer != null) {
Query query = layer.getQuery();
if (query instanceof FeatureQuery) {
userFilter = ((FeatureQuery) query).getSelection();
}
}
Filter filter;
if (spatialFilter.isPresent()) {
if (userFilter == null)
filter = spatialFilter.get();
else
// Note: we give priority to the spatial filter here, because it is our main use case: rendering is driven
// by bounding box.
filter = FILTER_FACTORY.and(spatialFilter.get(), userFilter);
} else if (userFilter == null) {
throw new PortrayalException("No spatial filter can be determined from style rules, and no user filter specified." + "We refuse dataset full-scan. To authorize it, manually specify Filter 'INCLUDE' on your map layer.");
} else {
LOGGER.warning("Spatial filter cannot be determined for rendering. However, user has provided a custom filter that we'll use as sole filtering policy");
filter = userFilter;
}
final Set<String> copy = new HashSet<>();
final FeatureType expected;
final String[] atts;
if (styleRequieredAtts == null) {
// all properties are requiered
expected = schema;
atts = null;
} else {
final Set<String> attributs = styleRequieredAtts;
copy.addAll(attributs);
copy.addAll(geomProperties);
try {
// always include the identifier if it exist
schema.getProperty(AttributeConvention.IDENTIFIER);
copy.add(AttributeConvention.IDENTIFIER);
} catch (PropertyNotFoundException ex) {
// no id, ignore it
}
atts = copy.toArray(new String[copy.size()]);
// then we reduce it to the first parent property.
for (int i = 0; i < atts.length; i++) {
String attName = atts[i];
int index = attName.indexOf('/');
if (index == 0) {
// remove all xpath elements
// remove first slash
attName = attName.substring(1);
final Pattern pattern = Pattern.compile("(\\{[^\\{\\}]*\\})|(\\[[^\\[\\]]*\\])|/{1}");
final Matcher matcher = pattern.matcher(attName);
final StringBuilder sb = new StringBuilder();
int position = 0;
while (matcher.find()) {
final String match = matcher.group();
sb.append(attName.substring(position, matcher.start()));
position = matcher.end();
if (match.charAt(0) == '/') {
// we don't query precisely sub elements
position = attName.length();
break;
} else if (match.charAt(0) == '{') {
sb.append(match);
} else if (match.charAt(0) == '[') {
// strip indexes or xpath searches
}
}
sb.append(attName.substring(position));
atts[i] = sb.toString();
}
}
try {
expected = new ViewMapper(schema, atts).getMappedType();
} catch (MismatchedFeatureException ex) {
throw new PortrayalException(ex);
}
}
// combine the filter with rule filters----------------------------------
if (rules != null) {
List<Filter<Object>> rulefilters = new ArrayList<>();
for (Rule rule : rules) {
if (rule.isElseFilter()) {
// we can't append styling filters, an else rule match all features
rulefilters = null;
break;
}
final Filter rf = rule.getFilter();
if (rf == null || rf == Filter.include()) {
// we can't append styling filters, this rule matchs all features.
rulefilters = null;
break;
}
rulefilters.add(rf);
}
if (rulefilters != null) {
final Filter combined;
if (rulefilters.size() == 1) {
// we can optimze here, since we pass the filter on the query, we can remove
// the filter on the rule.
final MutableRule mr = StyleUtilities.copy(rules.get(0));
mr.setFilter(null);
rules.set(0, mr);
combined = rulefilters.get(0);
} else {
combined = FILTER_FACTORY.or(rulefilters);
}
if (filter != Filter.include()) {
filter = FILTER_FACTORY.and(filter, combined);
} else {
filter = combined;
}
}
}
// optimize the filter---------------------------------------------------
filter = FilterUtilities.prepare(filter, Feature.class, expected);
final Hints queryHints = new Hints();
final org.geotoolkit.storage.feature.query.Query qb = new org.geotoolkit.storage.feature.query.Query();
qb.setTypeName(schema.getName());
qb.setSelection(filter);
qb.setProperties(atts);
// resampling and ignore flag only works when we know the layer crs
if (layerCRS != null) {
// add resampling -------------------------------------------------------
Boolean resample = (hints == null) ? null : (Boolean) hints.get(GO2Hints.KEY_GENERALIZE);
if (!Boolean.FALSE.equals(resample)) {
// we only disable resampling if it is explictly specified
double[] res = renderingContext.getResolution(layerCRS);
// adjust with the generalization factor
final Number n = (hints == null) ? null : (Number) hints.get(GO2Hints.KEY_GENERALIZE_FACTOR);
final double factor;
if (n != null) {
factor = n.doubleValue();
} else {
factor = GO2Hints.GENERALIZE_FACTOR_DEFAULT.doubleValue();
}
res[0] *= factor;
res[1] *= factor;
qb.setResolution(res);
try {
res = renderingContext.getResolution(CRS.forCode("EPSG:3395"));
res[0] *= factor;
res[1] *= factor;
qb.setLinearResolution(Quantities.create(res[0], Units.METRE));
} catch (FactoryException ex) {
throw new PortrayalException(ex.getMessage(), ex);
}
}
// add ignore flag ------------------------------------------------------
// TODO this is efficient but erases values, when plenty of then are to be rendered
// we should find another way to handle this
// if(!GO2Utilities.visibleMargin(rules, 1.01f, renderingContext)){
// //style does not expend itself further than the feature geometry
// //that mean geometries smaller than a pixel will not be renderer or barely visible
// queryHints.put(Hints.KEY_IGNORE_SMALL_FEATURES, renderingContext.getResolution(layerCRS));
// }
}
// add reprojection -----------------------------------------------------
// we don't reproject, the reprojection may produce curves but JTS can not represent those.
// so we generate those curves in java2d shapes by doing the transformation ourself.
// TODO wait for a new geometry implementation
// qb.setCRS(renderingContext.getObjectiveCRS2D());
// set the acumulated hints
qb.setHints(queryHints);
return qb;
}
use of org.geotoolkit.style.MutableRule in project geotoolkit by Geomatys.
the class RenderingRoutines method mergeStyles.
/**
* Merge a layer style with a selection style.
* The selection style fts elements will be placed after those of the default style.
*/
public static MutableStyle mergeStyles(MutableStyle style, Filter selectionFilter, MutableStyle selectionStyle) {
if (selectionFilter == null || Filter.exclude().equals(selectionFilter) || selectionStyle == null) {
// unmodified
return style;
}
final Filter exclusionFilter = FILTER_FACTORY.not(selectionFilter);
final MutableStyle result = GO2Utilities.STYLE_FACTORY.style();
for (FeatureTypeStyle fts : style.featureTypeStyles()) {
final MutableFeatureTypeStyle resultfts = GO2Utilities.STYLE_FACTORY.featureTypeStyle();
resultfts.setDescription(fts.getDescription());
resultfts.setFeatureInstanceIDs(fts.getFeatureInstanceIDs());
resultfts.setName(fts.getName());
resultfts.setOnlineResource(fts.getOnlineResource());
result.featureTypeStyles().add(resultfts);
for (Rule rule : fts.rules()) {
final MutableRule modifiedRule = STYLE_FACTORY.rule(rule.symbolizers().toArray(new Symbolizer[0]));
Filter f = rule.getFilter();
if (f == null) {
f = exclusionFilter;
} else {
f = FILTER_FACTORY.and(f, exclusionFilter);
}
modifiedRule.setFilter(f);
resultfts.rules().add(modifiedRule);
}
}
if (selectionStyle != null) {
for (FeatureTypeStyle fts : selectionStyle.featureTypeStyles()) {
final MutableFeatureTypeStyle resultfts = GO2Utilities.STYLE_FACTORY.featureTypeStyle();
resultfts.setDescription(fts.getDescription());
resultfts.setFeatureInstanceIDs(fts.getFeatureInstanceIDs());
resultfts.setName(fts.getName());
resultfts.setOnlineResource(fts.getOnlineResource());
result.featureTypeStyles().add(resultfts);
for (Rule rule : fts.rules()) {
final MutableRule modifiedRule = STYLE_FACTORY.rule(rule.symbolizers().toArray(new Symbolizer[0]));
Filter f = rule.getFilter();
if (f == null) {
f = selectionFilter;
} else {
f = FILTER_FACTORY.and(f, selectionFilter);
}
modifiedRule.setFilter(f);
resultfts.rules().add(modifiedRule);
}
}
}
return result;
}
use of org.geotoolkit.style.MutableRule in project geotoolkit by Geomatys.
the class SE100toGTTransformer method visitRule.
/**
* Trasnform SLD v1.0 rule in GT Rule.
*/
public MutableRule visitRule(final org.geotoolkit.sld.xml.v100.Rule rt) {
final MutableRule rule = styleFactory.rule();
rule.setName(rt.getName());
final InternationalString title = (rt.getTitle() == null) ? null : new SimpleInternationalString(rt.getTitle());
final InternationalString abs = (rt.getAbstract() == null) ? null : new SimpleInternationalString(rt.getAbstract());
rule.setDescription(styleFactory.description(title, abs));
rule.setElseFilter(rt.getElseFilter() != null);
rule.setFilter(visitFilter(rt.getFilter()));
rule.setLegendGraphic(visitLegend(rt.getLegendGraphic()));
rule.setMaxScaleDenominator((rt.getMaxScaleDenominator() == null) ? Double.MAX_VALUE : rt.getMaxScaleDenominator());
rule.setMinScaleDenominator((rt.getMinScaleDenominator() == null) ? 0 : rt.getMinScaleDenominator());
if (rt.getSymbolizer() == null || rt.getSymbolizer().isEmpty()) {
} else {
for (final JAXBElement<? extends org.geotoolkit.sld.xml.v100.SymbolizerType> jax : rt.getSymbolizer()) {
final org.geotoolkit.sld.xml.v100.SymbolizerType st = jax.getValue();
if (st == null) {
continue;
}
if (st instanceof org.geotoolkit.sld.xml.v100.PointSymbolizer) {
final org.geotoolkit.sld.xml.v100.PointSymbolizer pst = (org.geotoolkit.sld.xml.v100.PointSymbolizer) st;
rule.symbolizers().add(visit(pst));
} else if (st instanceof org.geotoolkit.sld.xml.v100.LineSymbolizer) {
final org.geotoolkit.sld.xml.v100.LineSymbolizer pst = (org.geotoolkit.sld.xml.v100.LineSymbolizer) st;
rule.symbolizers().add(visit(pst));
} else if (st instanceof org.geotoolkit.sld.xml.v100.PolygonSymbolizer) {
final org.geotoolkit.sld.xml.v100.PolygonSymbolizer pst = (org.geotoolkit.sld.xml.v100.PolygonSymbolizer) st;
rule.symbolizers().add(visit(pst));
} else if (st instanceof org.geotoolkit.sld.xml.v100.TextSymbolizer) {
final org.geotoolkit.sld.xml.v100.TextSymbolizer pst = (org.geotoolkit.sld.xml.v100.TextSymbolizer) st;
rule.symbolizers().add(visit(pst));
} else if (st instanceof org.geotoolkit.sld.xml.v100.RasterSymbolizer) {
final org.geotoolkit.sld.xml.v100.RasterSymbolizer pst = (org.geotoolkit.sld.xml.v100.RasterSymbolizer) st;
rule.symbolizers().add(visit(pst));
}
}
}
return rule;
}
use of org.geotoolkit.style.MutableRule in project geotoolkit by Geomatys.
the class SE110toGTTransformer method visitRule.
/**
* Trasnform SLD v1.1 rule in GT Rule.
*/
public MutableRule visitRule(final org.geotoolkit.se.xml.v110.RuleType rt) throws FactoryException {
final MutableRule rule = styleFactory.rule();
rule.setName(rt.getName());
rule.setDescription(visitDescription(rt.getDescription()));
rule.setElseFilter(rt.getElseFilter() != null);
rule.setFilter(visitFilter(rt.getFilter()));
rule.setLegendGraphic(visitLegend(rt.getLegendGraphic()));
rule.setMaxScaleDenominator((rt.getMaxScaleDenominator() == null) ? Double.MAX_VALUE : rt.getMaxScaleDenominator());
rule.setMinScaleDenominator((rt.getMinScaleDenominator() == null) ? 0 : rt.getMinScaleDenominator());
if (rt.getSymbolizer() == null || rt.getSymbolizer().isEmpty()) {
} else {
for (final JAXBElement<?> jax : rt.getSymbolizer()) {
final Object st = jax.getValue();
if (st == null) {
continue;
} else if (st instanceof SymbolizerType) {
rule.symbolizers().add(visit((SymbolizerType) st));
} else if (st instanceof Symbolizer) {
rule.symbolizers().add((Symbolizer) st);
}
}
}
return rule;
}
Aggregations