Java Code Examples for org.geotools.styling.Rule#setFilter()

The following examples show how to use org.geotools.styling.Rule#setFilter() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ExtractAttributesTest.java    From sldeditor with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void testNotFilter() {
    DummyInternalSLDFile2 dummy = new DummyInternalSLDFile2();

    StyledLayerDescriptor sld = createTestSLD(dummy);
    List<Rule> ruleList = getRuleList(sld);

    ExtractAttributes extract = new ExtractAttributes();
    Rule rule = DefaultSymbols.createNewRule();
    // Try with NOT
    extract = new ExtractAttributes();
    Filter filter = ff.not(ff.greater(ff.literal(42.1), ff.property("dble")));
    rule.setFilter(filter);
    ruleList.clear();
    ruleList.add(rule);
    extract.extractDefaultFields(sld);

    // Check fields extracted ok
    List<DataSourceAttributeData> actualFieldnameList = extract.getFields();
    assertEquals(1, actualFieldnameList.size());
    DataSourceAttributeData dataSourceField = actualFieldnameList.get(0);
    assertEquals(Double.class, dataSourceField.getType());
}
 
Example 2
Source File: ExtractAttributesTest.java    From sldeditor with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void testMultiComparatorFilter() {
    DummyInternalSLDFile2 dummy = new DummyInternalSLDFile2();

    StyledLayerDescriptor sld = createTestSLD(dummy);
    List<Rule> ruleList = getRuleList(sld);

    Rule rule = DefaultSymbols.createNewRule();

    // Try with something complex
    Filter filter =
            ff.and(
                    ff.greater(ff.literal(42), ff.property("int")),
                    ff.less(ff.literal(12), ff.property("abc")));
    rule.setFilter(filter);
    ruleList.clear();
    ruleList.add(rule);
    ExtractAttributes extract = new ExtractAttributes();
    extract.extractDefaultFields(sld);

    // Check fields extracted ok
    List<DataSourceAttributeData> actualFieldnameList = extract.getFields();
    assertEquals(2, actualFieldnameList.size());
    DataSourceAttributeData dataSourceField = actualFieldnameList.get(0);
    assertEquals(Integer.class, dataSourceField.getType());
}
 
Example 3
Source File: ExtractAttributesTest.java    From sldeditor with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void testBinaryTemporalFilter() {
    DummyInternalSLDFile2 dummy = new DummyInternalSLDFile2();

    StyledLayerDescriptor sld = createTestSLD(dummy);
    List<Rule> ruleList = getRuleList(sld);

    Rule rule = DefaultSymbols.createNewRule();

    // Try begins
    Filter filter = ff.begins(ff.property("test"), ff.literal("1234"));
    rule.setFilter(filter);
    ruleList.clear();
    ruleList.add(rule);
    ExtractAttributes extract = new ExtractAttributes();
    extract.extractDefaultFields(sld);

    // Check fields extracted ok
    List<DataSourceAttributeData> actualFieldnameList = extract.getFields();
    assertEquals(1, actualFieldnameList.size());
    DataSourceAttributeData dataSourceField = actualFieldnameList.get(0);
    assertEquals(Integer.class, dataSourceField.getType());
}
 
Example 4
Source File: ExtractAttributesTest.java    From sldeditor with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void testIsNull() {
    DummyInternalSLDFile2 dummy = new DummyInternalSLDFile2();

    StyledLayerDescriptor sld = createTestSLD(dummy);
    List<Rule> ruleList = getRuleList(sld);

    Rule rule = DefaultSymbols.createNewRule();

    // Try isNull
    Filter filter = ff.isNull(ff.property("test"));
    rule.setFilter(filter);
    ruleList.clear();
    ruleList.add(rule);
    ExtractAttributes extract = new ExtractAttributes();
    extract.extractDefaultFields(sld);

    // Check fields extracted ok
    List<DataSourceAttributeData> actualFieldnameList = extract.getFields();
    assertEquals(1, actualFieldnameList.size());
    DataSourceAttributeData dataSourceField = actualFieldnameList.get(0);
    assertEquals(String.class, dataSourceField.getType());
}
 
Example 5
Source File: ExtractAttributesTest.java    From sldeditor with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void testIsLike() {
    DummyInternalSLDFile2 dummy = new DummyInternalSLDFile2();

    StyledLayerDescriptor sld = createTestSLD(dummy);
    List<Rule> ruleList = getRuleList(sld);

    Rule rule = DefaultSymbols.createNewRule();

    // Try isLike
    Filter filter = ff.like(ff.property("test"), "abcd1");
    rule.setFilter(filter);
    ruleList.clear();
    ruleList.add(rule);
    ExtractAttributes extract = new ExtractAttributes();
    extract.extractDefaultFields(sld);

    // Check fields extracted ok
    List<DataSourceAttributeData> actualFieldnameList = extract.getFields();
    assertEquals(1, actualFieldnameList.size());
    DataSourceAttributeData dataSourceField = actualFieldnameList.get(0);
    assertEquals(String.class, dataSourceField.getType());
}
 
Example 6
Source File: ExtractAttributesTest.java    From sldeditor with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void testIsBetween() {
    DummyInternalSLDFile2 dummy = new DummyInternalSLDFile2();

    StyledLayerDescriptor sld = createTestSLD(dummy);
    List<Rule> ruleList = getRuleList(sld);

    Rule rule = DefaultSymbols.createNewRule();

    // Try isBetween
    Filter filter = ff.between(ff.property("test"), ff.literal("1.23"), ff.literal(4));
    rule.setFilter(filter);
    ruleList.clear();
    ruleList.add(rule);
    ExtractAttributes extract = new ExtractAttributes();
    extract.extractDefaultFields(sld);

    // Check fields extracted ok
    List<DataSourceAttributeData> actualFieldnameList = extract.getFields();
    assertEquals(1, actualFieldnameList.size());
    DataSourceAttributeData dataSourceField = actualFieldnameList.get(0);
    assertEquals(Double.class, dataSourceField.getType());
}
 
Example 7
Source File: ExtractAttributesTest.java    From sldeditor with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void testBinarySpatialOperator() {
    DummyInternalSLDFile2 dummy = new DummyInternalSLDFile2();

    StyledLayerDescriptor sld = createTestSLD(dummy);
    List<Rule> ruleList = getRuleList(sld);

    Rule rule = DefaultSymbols.createNewRule();

    // Try bbox
    String expectedGeometryFieldName = "test geometry";
    Filter filter = ff.bbox(expectedGeometryFieldName, -1.0, 49.0, 2.0, 55.0, "EPSG:4326");
    rule.setFilter(filter);
    ruleList.clear();
    ruleList.add(rule);
    ExtractAttributes extract = new ExtractAttributes();
    extract.extractDefaultFields(sld);

    // Check fields extracted ok
    List<String> actualGeometryFields = extract.getGeometryFields();
    assertEquals(1, actualGeometryFields.size());
    assertEquals(expectedGeometryFieldName, actualGeometryFields.get(0));
    List<DataSourceAttributeData> actualFieldnameList = extract.getFields();
    assertEquals(0, actualFieldnameList.size());
}
 
Example 8
Source File: ExtractAttributesTest.java    From sldeditor with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void testFunctionExpression() {
    DummyInternalSLDFile2 dummy = new DummyInternalSLDFile2();

    StyledLayerDescriptor sld = createTestSLD(dummy);
    List<Rule> ruleList = getRuleList(sld);

    ExtractAttributes extract = new ExtractAttributes();
    Rule rule = DefaultSymbols.createNewRule();

    String expectedEnvVar = "testenvvar";
    Function function = ff.function("env", ff.property(expectedEnvVar));

    Filter filter = ff.equal(ff.literal("not equal"), function, true);
    rule.setFilter(filter);
    ruleList.clear();
    ruleList.add(rule);
    extract.extractDefaultFields(sld);

    // Check fields extracted ok
    List<DataSourceAttributeData> actualFieldnameList = extract.getFields();
    assertEquals(1, actualFieldnameList.size());
    DataSourceAttributeData dataSourceField = actualFieldnameList.get(0);
    assertEquals(String.class, dataSourceField.getType());
}
 
Example 9
Source File: StyleGenerator.java    From constellation with Apache License 2.0 5 votes vote down vote up
private static Rule makeFillRule(final SimpleFeature feature) {
    final Rule rule = makeFillRule();
    rule.setName((String) feature.getAttribute(ATTRIBUTE));

    // create a filter based on name
    final FilterFactory2 ff = CommonFactoryFinder.getFilterFactory2();
    rule.setFilter(ff.equals(ff.property(ATTRIBUTE), ff.literal(feature.getAttribute(ATTRIBUTE))));

    return rule;
}
 
Example 10
Source File: ExtractAttributesTest.java    From sldeditor with GNU General Public License v3.0 5 votes vote down vote up
/** Test filter. */
@Test
public void testFilter() {
    DummyInternalSLDFile2 dummy = new DummyInternalSLDFile2();

    StyledLayerDescriptor sld = createTestSLD(dummy);
    List<Rule> ruleList = getRuleList(sld);
    ExtractAttributes extract = new ExtractAttributes();
    Rule rule = DefaultSymbols.createNewRule();

    // Try it 1) property 2) Literal
    Filter filter = ff.greater(ff.property("width"), ff.literal(42.1));
    rule.setFilter(filter);
    ruleList.add(rule);
    extract.extractDefaultFields(sld);

    // Check fields extracted ok
    List<DataSourceAttributeData> actualFieldnameList = extract.getFields();
    assertEquals(1, actualFieldnameList.size());
    DataSourceAttributeData dataSourceField = actualFieldnameList.get(0);
    assertEquals(Double.class, dataSourceField.getType());
    // Check geometry fields extracted ok
    List<String> actualGeometryFields = extract.getGeometryFields();
    assertEquals(1, actualGeometryFields.size());

    // Try it 1) literal 2) property
    filter = ff.greater(ff.literal(42.1), ff.property("dble"));
    rule.setFilter(filter);
    ruleList.clear();
    ruleList.add(rule);
    extract.extractDefaultFields(sld);

    // Check fields extracted ok
    actualFieldnameList = extract.getFields();
    assertEquals(2, actualFieldnameList.size());
    dataSourceField = actualFieldnameList.get(0);
    assertEquals(Double.class, dataSourceField.getType());
}
 
Example 11
Source File: ExtractAttributesTest.java    From sldeditor with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testFunctionImpl() {
    DummyInternalSLDFile2 dummy = new DummyInternalSLDFile2();

    StyledLayerDescriptor sld = createTestSLD(dummy);
    List<Rule> ruleList = getRuleList(sld);

    ExtractAttributes extract = new ExtractAttributes();
    Rule rule = DefaultSymbols.createNewRule();

    String expectedAttribute = "world";
    FunctionImpl function = new ConcatenateFunction();
    List<Expression> params = new ArrayList<Expression>();
    params.add(ff.property(expectedAttribute));
    params.add(ff.property(expectedAttribute));
    function.setParameters(params);

    Filter filter = ff.equal(ff.literal("not equal"), function, true);
    rule.setFilter(filter);
    ruleList.clear();
    ruleList.add(rule);
    extract.extractDefaultFields(sld);

    // Check fields extracted ok
    List<DataSourceAttributeData> actualFieldnameList = extract.getFields();
    assertEquals(1, actualFieldnameList.size());
    DataSourceAttributeData dataSourceField = actualFieldnameList.get(0);
    assertEquals(String.class, dataSourceField.getType());
}
 
Example 12
Source File: StyleConverterServiceImpl.java    From geomajas-project-server with GNU Affero General Public License v3.0 5 votes vote down vote up
private Rule createRule(Filter filter, FeatureStyleInfo featureStyle) throws LayerException {
	Rule rule = styleBuilder.createRule(createGeometrySymbolizer(featureStyle));
	if (filter.equals(Filter.INCLUDE)) {
		rule.setElseFilter(true);
	} else {
		rule.setFilter(filter);
	}
	rule.setName(featureStyle.getName());
	rule.setTitle(featureStyle.getName());
	return rule;
}
 
Example 13
Source File: UniqueValueRenderer.java    From sldeditor with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Creates the filter.
 *
 * @param rule the rule
 * @param fieldsElement the fields element
 * @param fieldDelimiterElement the field delimiter element
 * @param value the value
 */
private void createFilter(Rule rule, JsonElement fieldsElement,
    JsonElement fieldDelimiterElement, String value)
{
    if(fieldsElement == null) return;

    if(value == null)
    {
        rule.setElseFilter(true);
    }
    else
    {
        List<String> fieldList = new ArrayList<String>();

        JsonArray fieldArray = fieldsElement.getAsJsonArray();

        for(int fieldIndex = 0; fieldIndex < fieldArray.size(); fieldIndex ++)
        {
            JsonElement jsonFieldElement = fieldArray.get(fieldIndex);
            if(jsonFieldElement != null)
            {
                JsonObject fieldObj = jsonFieldElement.getAsJsonObject();
                fieldList.add(fieldObj.get("name").getAsString());
            }
        }

        String[] values = null;

        if(fieldDelimiterElement != null)
        {
            values = value.split(fieldDelimiterElement.getAsString());
        }
        else
        {
            values = new String[1];
            values[0] = value;
        }

        List<Filter> filterList = new ArrayList<Filter>();

        int index = 0;
        while(index < values.length)
        {
            Expression fieldExpression = filterFactory.property(fieldList.get(index));
            Expression valueExpression = filterFactory.literal(values[index]);

            PropertyIsEqualTo filter = filterFactory.equals(fieldExpression, valueExpression);

            filterList.add(filter);
            index ++;
        }

        Filter completeFilter = null;

        if(filterList.size() > 1)
        {
            completeFilter = filterFactory.and(filterList);
        }
        else if(filterList.size() == 1)
        {
            completeFilter = filterList.get(0);
        }

        rule.setFilter(completeFilter);
    }
}
 
Example 14
Source File: RuleRenderVisitor.java    From sldeditor with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Visit.
 *
 * @param rule the rule
 */
/*
 * (non-Javadoc)
 *
 * @see org.geotools.styling.visitor.DuplicatingStyleVisitor#visit(org.geotools.styling.Rule)
 */
@SuppressWarnings("deprecation")
@Override
public void visit(Rule rule) {
    Rule copy = null;

    Symbolizer[] symsCopy = null;

    if (!displayOverall
            && ((symbolizerIndex >= 0) && (symbolizerIndex < rule.getSymbolizers().length))) {
        symsCopy = new Symbolizer[1];
        symsCopy[0] = copy(rule.getSymbolizers()[symbolizerIndex]);
    }

    // As a catch all copy everything
    if (symsCopy == null) {
        symsCopy = rule.getSymbolizers();
        for (int i = 0; i < symsCopy.length; i++) {
            symsCopy[i] = copy(symsCopy[i]);
        }
    }

    Graphic[] legendCopy = rule.getLegendGraphic();
    for (int i = 0; i < legendCopy.length; i++) {
        legendCopy[i] = copy(legendCopy[i]);
    }

    Description descCopy = rule.getDescription();
    descCopy = copy(descCopy);

    copy = sf.createRule();
    copy.setSymbolizers(symsCopy);
    copy.setDescription(descCopy);
    copy.setLegendGraphic(legendCopy);
    copy.setName(rule.getName());
    Filter filterCopy = null;
    copy.setFilter(filterCopy);
    copy.setElseFilter(rule.isElseFilter());
    // Do not copy the min and max scales

    if (STRICT && !copy.equals(rule)) {
        throw new IllegalStateException("Was unable to duplicate provided Rule:" + rule);
    }
    pages.push(copy);
}
 
Example 15
Source File: RuleDetailsTest.java    From sldeditor with GNU General Public License v3.0 4 votes vote down vote up
@Test
public void testRuleEditFilter() {
    TestRuleDetails panel = new TestRuleDetails();
    panel.populate(null);

    // Set up test data
    StyledLayerDescriptor sld = DefaultSymbols.createNewSLD();
    SelectedSymbol.getInstance().createNewSLD(sld);
    NamedLayer namedLayer = DefaultSymbols.createNewNamedLayer();
    String expectedNameLayerValue = "named layer test value";
    namedLayer.setName(expectedNameLayerValue);
    Style style = DefaultSymbols.createNewStyle();
    String expectedNameStyleValue = "style test value";
    style.setName(expectedNameStyleValue);
    namedLayer.addStyle(style);
    FeatureTypeStyle fts = DefaultSymbols.createNewFeatureTypeStyle();
    String expectedNameFTSValue = "feature type style test value";
    fts.setName(expectedNameFTSValue);
    style.featureTypeStyles().add(fts);
    Rule rule = DefaultSymbols.createNewRule();
    String expectedNameValue = "rule test value";
    rule.setName(expectedNameValue);

    Symbolizer symbolizer = DefaultSymbols.createDefaultPolygonSymbolizer();
    rule.symbolizers().add(symbolizer);
    fts.rules().add(rule);
    sld.layers().add(namedLayer);
    SelectedSymbol.getInstance().setRule(rule);

    panel.populate(SelectedSymbol.getInstance());

    StyledLayerDescriptor sld2 = DefaultSymbols.createNewSLD();
    SelectedSymbol.getInstance().createNewSLD(sld2);
    NamedLayer namedLayer2 = DefaultSymbols.createNewNamedLayer();
    namedLayer2.setName(expectedNameLayerValue);
    Style style2 = DefaultSymbols.createNewStyle();
    style2.setName(expectedNameStyleValue);
    namedLayer2.addStyle(style2);
    FeatureTypeStyle fts2 = DefaultSymbols.createNewFeatureTypeStyle();
    fts2.setName(expectedNameFTSValue);
    style2.featureTypeStyles().add(fts2);
    Rule rule2 = DefaultSymbols.createNewRule();
    rule2.setName(expectedNameValue);

    FilterFactory ff = CommonFactoryFinder.getFilterFactory();
    Filter filter = ff.equal(ff.literal(1), ff.literal(2), false);
    rule2.setFilter(filter);

    rule2.symbolizers().add(symbolizer);
    fts2.rules().add(rule2);
    sld2.layers().add(namedLayer2);
    SelectedSymbol.getInstance().setRule(rule2);

    GraphicPanelFieldManager fieldDataManager = panel.getFieldDataManager();
    assertNotNull(fieldDataManager);

    int undoListSize = UndoManager.getInstance().getUndoListSize();
    ExpressionPanelFactory.setTestMode();
    panel.editFilter(FieldIdEnum.FILTER);
    FieldConfigString filterField =
            (FieldConfigString) fieldDataManager.get(FieldIdEnum.FILTER);
    String expectedString1 = "[ 1 = 2 ]";
    assertEquals(expectedString1, filterField.getStringValue());

    assertEquals(undoListSize + 1, UndoManager.getInstance().getUndoListSize());

    panel.clearFilter(FieldIdEnum.FILTER);

    assertEquals(undoListSize + 2, UndoManager.getInstance().getUndoListSize());
    String expectedString2 = "";
    assertEquals(expectedString2, filterField.getStringValue());

    UndoManager.getInstance().undo();
    assertEquals(expectedString1, filterField.getStringValue());

    UndoManager.getInstance().redo();
    assertEquals(expectedString2, filterField.getStringValue());

    ExpressionPanelFactory.destroyInstance();
}
 
Example 16
Source File: SymbolizerFilterVisitor.java    From geomajas-project-server with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * Overridden to skip some symbolizers.
 */
@Override
public void visit(Rule rule) {
	Rule copy = null;
	Filter filterCopy = null;

	if (rule.getFilter() != null) {
		Filter filter = rule.getFilter();
		filterCopy = copy(filter);
	}

	List<Symbolizer> symsCopy = new ArrayList<Symbolizer>();
	for (Symbolizer sym : rule.symbolizers()) {
		if (!skipSymbolizer(sym)) {
			Symbolizer symCopy = copy(sym);
			symsCopy.add(symCopy);
		}
	}

	Graphic[] legendCopy = rule.getLegendGraphic();
	for (int i = 0; i < legendCopy.length; i++) {
		legendCopy[i] = copy(legendCopy[i]);
	}

	Description descCopy = rule.getDescription();
	descCopy = copy(descCopy);

	copy = sf.createRule();
	copy.symbolizers().addAll(symsCopy);
	copy.setDescription(descCopy);
	copy.setLegendGraphic(legendCopy);
	copy.setName(rule.getName());
	copy.setFilter(filterCopy);
	copy.setElseFilter(rule.isElseFilter());
	copy.setMaxScaleDenominator(rule.getMaxScaleDenominator());
	copy.setMinScaleDenominator(rule.getMinScaleDenominator());

	if (STRICT && !copy.equals(rule)) {
		throw new IllegalStateException(
				"Was unable to duplicate provided Rule:" + rule);
	}
	pages.push(copy);
}