如何根据属性的值对图层的不同元素设置不同的样式?
例如,我有一个时区数据,其中包含一个属性,该属性将 1 到 8 的整数与每个时区相关联,以便为 map 上的时区着色。如何将样式与属性的每个值相关联,并使用它以不同颜色为时区着色?
最佳答案
此答案适用于 Geotools 9.2
下面是一个您可以采用的示例,请参阅下面的一些想法,了解如何将其应用到您的要求中。
这个例子是关于多边形的。对于选定的和可见的特征以及小尺度和大尺度,它具有不同的样式:
protected final StyleFactory styleFactory =
CommonFactoryFinder.getStyleFactory(GeoTools.getDefaultHints());
protected final FilterFactory2 filterFactory =
CommonFactoryFinder.getFilterFactory2();
protected Rule createLayerRule
( Color outlineColor
, float strokeWidth
, Color fillColor
, float opacity
, Filter filter
, double minScaleDenominator
, double maxScaleDenominator)
{
Stroke stroke =
outlineColor != null
? styleFactory.createStroke
( filterFactory.literal(outlineColor)
, filterFactory.literal(strokeWidth)
, filterFactory.literal(opacity))
: null;//Stroke.NULL;
Fill fill =
fillColor != null
? styleFactory.createFill
( filterFactory.literal(fillColor)
, filterFactory.literal(opacity))
: null;//Fill.NULL;
PolygonSymbolizer symbolizer =
styleFactory.createPolygonSymbolizer(stroke, fill, null);
return createRule(filter, minScaleDenominator, maxScaleDenominator, symbolizer);
}
// IDs of visible features, programmatically updated.
protected final Set<FeatureId> visibleFeatureIDs = new HashSet<FeatureId>();
// IDs of selected features, programmatically updated.
protected final Set<FeatureId> selectedFeatureIDs = new HashSet<FeatureId>();
protected Style createLayerStyle()
{
Filter selectionFilter = filterFactory.id(selectedFeatureIDs);
Filter visibilityFilter = filterFactory.and
( Arrays.asList
( filterFactory.not
(selectionFilter)
, filterFactory.id(visibleFeatureIDs)
)
);
FeatureTypeStyle fts = styleFactory.createFeatureTypeStyle
( new Rule[]
{
// hope constants below are self explaining
createLayerRule
( SELECTED_OUTLINE_COLOR
, STROKE_WIDTH_LARGE_SCALE
, SELECTED_FILL_COLOR
, SELECTED_OPACITY
, selectionFilter
, STYLE_SCALE_LIMIT
, Double.NaN)
, createLayerRule
( UNSELECTED_OUTLINE_COLOR
, STROKE_WIDTH_LARGE_SCALE
, UNSELECTED_FILL_COLOR
, UNSELECTED_OPACITY
, visibilityFilter
, STYLE_SCALE_LIMIT
, Double.NaN)
, createLayerRule
( SELECTED_OUTLINE_COLOR
, STROKE_WIDTH_SMALL_SCALE
, SELECTED_FILL_COLOR
, SELECTED_OPACITY
, selectionFilter
, Double.NaN
, STYLE_SCALE_LIMIT)
, createLayerRule
( UNSELECTED_OUTLINE_COLOR
, STROKE_WIDTH_SMALL_SCALE
, UNSELECTED_FILL_COLOR
, UNSELECTED_OPACITY
, visibilityFilter
, Double.NaN
, STYLE_SCALE_LIMIT)
}
);
Style style = styleFactory.createStyle();
style.featureTypeStyles().add(fts);
return style;
}
// layer creation
FeatureLayer someMethode()
{
...
FeatureLayer layer = new FeatureLayer
( dataStore.getFeatureSource()
, createLayerStyle()
, "Zipcodes"
);
...
return layer;
}
// style update if visible or selected features have changed
void someOtherMethod(FeatureLayer layer)
{
... // update selectedFeatureIDs or visibleFeatureIDs
layer.setStyle(createLayerStyle());
}
当然,优化是可能的,也是受欢迎的。
对于您的要求,以下内容可能会有所帮助:
- 如果你想为所有尺度使用一种样式(或者 16 条用于小型和大型尺度),你需要 8 条规则(参见上面的 createRule(..))。
- 使用
FilterFactory.equals(Expression, Expression)
定义 8 个过滤器,其中第一个表达式的类型为AttributeExpressionImpl
,第二个表达式的类型为LiteralExpressionImpl
< - 请注意 FilterFactory2 中还有另一个方法
equal
(没有 s)
关于java - 基于属性的 Geotools 条件样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20425423/