Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 基于属性的Geotools条件样式_Java_Geotools - Fatal编程技术网

Java 基于属性的Geotools条件样式

Java 基于属性的Geotools条件样式,java,geotools,Java,Geotools,如何根据属性的值对层的不同元素设置不同的样式 例如,我有一个时区数据,其中包含一个属性,该属性将1到8之间的整数与每个时区相关联,以便为地图上的时区着色。如何将样式与属性的每个值关联,并使用它为时区添加不同的颜色?此答案适用于Geotools 9.2 下面是一个您可以采用的示例,有关如何将其应用于您的需求的一些想法,请参见下文 这个例子是关于多边形的。对于选定的和可见的要素以及小比例和大比例,它具有不同的样式: protected final StyleFactory styleFactory

如何根据属性的值对层的不同元素设置不同的样式


例如,我有一个时区数据,其中包含一个属性,该属性将1到8之间的整数与每个时区相关联,以便为地图上的时区着色。如何将样式与属性的每个值关联,并使用它为时区添加不同的颜色?

此答案适用于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());
}
受保护的最终样式工厂样式工厂=
getStyleFactory(GeoTools.getDefaultHights());
受保护的最终过滤器工厂2过滤器工厂=
getFilterFactory2();
受保护的规则createLayerRule
(彩色大纲图)
,浮动冲程宽度
,颜色填充颜色
,浮动不透明度
,过滤器
,双分母
,双最大刻度指示器)
{
笔划=
outlineColor!=空
?styleFactory.createStroke
(filterFactory.literal(大纲颜色)
,filterFactory.literal(strokeWidth)
,filterFactory.literal(不透明度))
:null;//Stroke.null;
填充=
fillColor!=null
?styleFactory.createFill
(filterFactory.literal(fillColor)
,filterFactory.literal(不透明度))
:null;//Fill.null;
PolygonSymbolizer符号化器=
createPolygonSymbolizer(笔划、填充、空);
返回createRule(过滤器、minScaleDenominator、maxScaleDenominator、Symboler);
}
//以编程方式更新的可见功能的ID。
受保护的最终集VisibleFeatureId=new HashSet();
//选定功能的ID,以编程方式更新。
受保护的最终集SelectedFeatureId=new HashSet();
受保护样式createLayerStyle()
{
Filter selectionFilter=filterFactory.id(SelectedFeatureId);
过滤器可视性过滤器=过滤器工厂。和
(Arrays.asList)
(filterFactory.not)
(选择过滤器)
,filterFactory.id(VisibleFeatureId)
)
);
FeatureTypeStyle fts=styleFactory.createFeatureTypeStyle
(新规则[]
{
//下面的希望常数是自我解释的
createLayerRule
(选定的\u轮廓\u颜色
,笔划\宽度\大比例尺
,选定的填充颜色
,选定的不透明度
,选择过滤器
,样式\比例\限制
,Double.NaN)
,createLayerRule
(未选择的\u轮廓\u颜色
,笔划\宽度\大比例尺
,未选择的填充颜色
,未选择的不透明度
,可视性过滤器
,样式\比例\限制
,Double.NaN)
,createLayerRule
(选定的\u轮廓\u颜色
,笔划\宽度\小刻度
,选定的填充颜色
,选定的不透明度
,选择过滤器
,双男
,样式(比例限制)
,createLayerRule
(未选择的\u轮廓\u颜色
,笔划\宽度\小刻度
,未选择的填充颜色
,未选择的不透明度
,可视性过滤器
,双男
,样式(比例限制)
}
);
Style Style=styleFactory.createStyle();
style.featureTypeStyles().add(fts);
回归风格;
}
//图层创建
FeatureLayer someMethode()
{
...
FeatureLayer层=新FeatureLayer
(dataStore.getFeatureSource()
,createLayerStyle()
,“Zipcode”
);
...
返回层;
}
//如果可见特征或选定特征已更改,则更新样式
void someother方法(FeatureLayer层)
{
…//更新选定的FeatureId或VisibleFeatureId
layer.setStyle(createLayerStyle());
}
当然,优化是可能的,也是受欢迎的

对于您的要求,以下内容可能会有所帮助:

  • 如果希望所有比例都有一个样式(或大小比例都有16个),则需要8个规则(请参见上面的createRule(..)
  • 使用
    FilterFactory.equals(表达式,表达式)
    定义8个过滤器,其中第一个表达式的类型为
    AttributeExpressionImpl
    ,第二个类型为
    LiteralExpressionImpl
  • 请注意,在FilterFactory2中还有另一种方法
    equal
    (不带s

  • 谢谢一个简单概念的复杂过程@如果你对所有鳞片都有相同的风格,那就容易多了。也许有另一种方法可以让它变得更简单。然而,最后你必须把你的特征分成不同的颜色组。。。这就是过滤器的作用。