Skip to content

Commit

Permalink
[GEOS-6771] wmsGetFeatureInfo Request in 2.5, 2.6 doesnt work on Text…
Browse files Browse the repository at this point in the history
…Symbolizers
  • Loading branch information
aaime committed Jan 17, 2015
1 parent d34faf6 commit 36c7518
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 18 deletions.
Expand Up @@ -7,7 +7,6 @@


import java.awt.Color; import java.awt.Color;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
Expand All @@ -17,20 +16,26 @@
import org.geotools.styling.Fill; import org.geotools.styling.Fill;
import org.geotools.styling.Graphic; import org.geotools.styling.Graphic;
import org.geotools.styling.LineSymbolizer; import org.geotools.styling.LineSymbolizer;
import org.geotools.styling.PointSymbolizer;
import org.geotools.styling.PolygonSymbolizer; import org.geotools.styling.PolygonSymbolizer;
import org.geotools.styling.Rule; import org.geotools.styling.Rule;
import org.geotools.styling.RuleImpl; import org.geotools.styling.RuleImpl;
import org.geotools.styling.Stroke; import org.geotools.styling.Stroke;
import org.geotools.styling.Style; import org.geotools.styling.Style;
import org.geotools.styling.StyleBuilder; import org.geotools.styling.StyleBuilder;
import org.geotools.styling.Symbolizer;
import org.opengis.feature.type.FeatureType; import org.opengis.feature.type.FeatureType;
import org.opengis.feature.type.GeometryDescriptor; import org.opengis.feature.type.GeometryDescriptor;
import org.opengis.filter.Filter; import org.opengis.filter.Filter;
import org.opengis.filter.expression.Expression; import org.opengis.filter.expression.Expression;
import org.opengis.filter.expression.PropertyName; import org.opengis.filter.expression.PropertyName;


import com.vividsolutions.jts.geom.Geometry; import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.LineString;
import com.vividsolutions.jts.geom.MultiLineString;
import com.vividsolutions.jts.geom.MultiPoint;
import com.vividsolutions.jts.geom.MultiPolygon; import com.vividsolutions.jts.geom.MultiPolygon;
import com.vividsolutions.jts.geom.Point;
import com.vividsolutions.jts.geom.Polygon; import com.vividsolutions.jts.geom.Polygon;


/** /**
Expand All @@ -49,6 +54,10 @@ class FeatureInfoStylePreprocessor extends SymbolizerFilteringVisitor {


Set<Expression> geometriesOnLineSymbolizer = new HashSet<Expression>(); Set<Expression> geometriesOnLineSymbolizer = new HashSet<Expression>();


Set<Expression> geometriesOnPointSymbolizer = new HashSet<Expression>();

Set<Expression> geometriesOnTextSymbolizer = new HashSet<Expression>();

Set<Rule> extraRules = new HashSet<Rule>(); Set<Rule> extraRules = new HashSet<Rule>();


private PropertyName defaultGeometryExpression; private PropertyName defaultGeometryExpression;
Expand All @@ -62,6 +71,7 @@ public FeatureInfoStylePreprocessor(FeatureType schema) {


public void visit(org.geotools.styling.TextSymbolizer ts) { public void visit(org.geotools.styling.TextSymbolizer ts) {
pages.push(null); pages.push(null);
addGeometryExpression(ts.getGeometry(), geometriesOnTextSymbolizer);
} }


@Override @Override
Expand Down Expand Up @@ -101,6 +111,12 @@ public void visit(LineSymbolizer line) {
addGeometryExpression(line.getGeometry(), geometriesOnLineSymbolizer); addGeometryExpression(line.getGeometry(), geometriesOnLineSymbolizer);
} }


@Override
public void visit(PointSymbolizer ps) {
super.visit(ps);
addGeometryExpression(ps.getGeometry(), geometriesOnPointSymbolizer);
}

private void addGeometryExpression(Expression geometry, private void addGeometryExpression(Expression geometry,
Set<Expression> expressions) { Set<Expression> expressions) {
if(isDefaultGeometry(geometry)) { if(isDefaultGeometry(geometry)) {
Expand Down Expand Up @@ -175,6 +191,8 @@ public void visit(FeatureTypeStyle fts) {
public void visit(Rule rule) { public void visit(Rule rule) {
geometriesOnLineSymbolizer.clear(); geometriesOnLineSymbolizer.clear();
geometriesOnPolygonSymbolizer.clear(); geometriesOnPolygonSymbolizer.clear();
geometriesOnPointSymbolizer.clear();
geometriesOnTextSymbolizer.clear();
addSolidLineSymbolier = false; addSolidLineSymbolier = false;
super.visit(rule); super.visit(rule);
Rule copy = (Rule) pages.peek(); Rule copy = (Rule) pages.peek();
Expand All @@ -197,19 +215,60 @@ public void visit(Rule rule) {
} else if(geometryType.equals(Geometry.class)) { } else if(geometryType.equals(Geometry.class)) {
// dynamic, we need to add an extra rule then to paint as polygon // dynamic, we need to add an extra rule then to paint as polygon
// only if the actual geometry is a polygon type // only if the actual geometry is a polygon type
Filter polygon = ff.equal(ff.function("geometryType", geom), ff.literal("Polygon"), false); RuleImpl extra = buildDynamicGeometryRule(copy, geom, sb.createPolygonSymbolizer(),
Filter multiPolygon = ff.equal(ff.function("geometryType", geom), ff.literal("MultiPolygon"), false); "Polygon", "MultiPolygon");
Filter geomCheck = ff.or(Arrays.asList(polygon, multiPolygon));
Filter ruleFilter = copy.getFilter();
Filter filter = ruleFilter == null || ruleFilter == Filter.INCLUDE ? geomCheck : ff.and(geomCheck, ruleFilter);
RuleImpl extra = new RuleImpl(copy);
extra.setFilter(filter);
extra.symbolizers().clear();
extra.symbolizers().add(sb.createPolygonSymbolizer());
extraRules.add(extra); extraRules.add(extra);
} }

} }
// check all the geometries that are on text, but not on any other symbolizer (pure labels)
// that we won't hit otherwise
geometriesOnTextSymbolizer.removeAll(geometriesOnPolygonSymbolizer);
geometriesOnTextSymbolizer.removeAll(geometriesOnLineSymbolizer);
geometriesOnTextSymbolizer.removeAll(geometriesOnPointSymbolizer);
for (Expression geom : geometriesOnTextSymbolizer) {
Object result = geom.evaluate(schema);
Class geometryType = getTargetGeometryType(result);
if (Polygon.class.isAssignableFrom(geometryType)
|| MultiPolygon.class.isAssignableFrom(geometryType)) {
copy.symbolizers().add(sb.createPolygonSymbolizer());
} else if (LineString.class.isAssignableFrom(geometryType)
|| MultiLineString.class.isAssignableFrom(geometryType)) {
copy.symbolizers().add(sb.createLineSymbolizer());
} else if (Point.class.isAssignableFrom(geometryType)
|| MultiPoint.class.isAssignableFrom(geometryType)) {
copy.symbolizers().add(sb.createPointSymbolizer());
} else {
// ouch, it's a generic geometry... now this is going to be painful, we have to
// build a dynamic symbolizer for each possible geometry type
RuleImpl extra = buildDynamicGeometryRule(copy, geom, sb.createPolygonSymbolizer(),
"Polygon", "MultiPolygon");
extraRules.add(extra);
extra = buildDynamicGeometryRule(copy, geom, sb.createLineSymbolizer(),
"LineString", "LinearRing", "MultiLineString");
extraRules.add(extra);
extra = buildDynamicGeometryRule(copy, geom, sb.createPointSymbolizer(), "Point",
"MultiPoint");
extraRules.add(extra);
}
}
}

private RuleImpl buildDynamicGeometryRule(Rule base, Expression geom, Symbolizer symbolizer,
String... geometryTypes) {
List<Filter> typeChecks = new ArrayList<>();
for (String geometryType : geometryTypes) {
typeChecks.add(ff.equal(ff.function("geometryType", geom), ff.literal(geometryType),
false));
}
Filter geomCheck = ff.or(typeChecks);
Filter ruleFilter = base.getFilter();
Filter filter = ruleFilter == null || ruleFilter == Filter.INCLUDE ? geomCheck : ff.and(
geomCheck, ruleFilter);
RuleImpl extra = new RuleImpl(base);
extra.setFilter(filter);
extra.symbolizers().clear();
extra.symbolizers().add(symbolizer);
return extra;
} }


private Class getTargetGeometryType(Object descriptor) { private Class getTargetGeometryType(Object descriptor) {
Expand Down
@@ -1,6 +1,6 @@
package org.geoserver.wms.featureinfo; package org.geoserver.wms.featureinfo;


import static org.junit.Assert.*; import static org.junit.Assert.assertEquals;


import java.io.File; import java.io.File;
import java.util.Collections; import java.util.Collections;
Expand All @@ -22,10 +22,10 @@ public class RenderingBasedFeatureInfoTest extends WMSTestSupport {
public static QName REPEATED = new QName(MockData.CITE_URI, "repeated", MockData.CITE_PREFIX); public static QName REPEATED = new QName(MockData.CITE_URI, "repeated", MockData.CITE_PREFIX);




@Override // @Override
protected String getLogConfiguration() { // protected String getLogConfiguration() {
return "/DEFAULT_LOGGING.properties"; // return "/DEFAULT_LOGGING.properties";
} // }


@Override @Override
protected void onSetUp(SystemTestData testData) throws Exception { protected void onSetUp(SystemTestData testData) throws Exception {
Expand All @@ -49,6 +49,7 @@ protected void onSetUp(SystemTestData testData) throws Exception {
testData.addStyle("dashed", "dashed.sld",this.getClass(), getCatalog()); testData.addStyle("dashed", "dashed.sld",this.getClass(), getCatalog());
testData.addStyle("polydash", "polydash.sld", this.getClass(), getCatalog()); testData.addStyle("polydash", "polydash.sld", this.getClass(), getCatalog());
testData.addStyle("doublepoly", "doublepoly.sld", this.getClass(), getCatalog()); testData.addStyle("doublepoly", "doublepoly.sld", this.getClass(), getCatalog());
testData.addStyle("pureLabel", "pureLabel.sld", this.getClass(), getCatalog());

This comment has been minimized.

Copy link
@jodygarnett

jodygarnett Jan 18, 2015

Member

@aaime you missed adding pureLabel.sld to your commit :)

} }


@After @After
Expand Down Expand Up @@ -154,7 +155,6 @@ public void testTwoFeatureTypeStyles() throws Exception {
+ "&styles=two-fts" + "&styles=two-fts"
+ "&width=20&height=20&x=10&y=10&info_format=application/json"; + "&width=20&height=20&x=10&y=10&info_format=application/json";


System.out.println("The response iTESTs: " + getAsString(request));
JSONObject result = (JSONObject) getAsJSON(request); JSONObject result = (JSONObject) getAsJSON(request);
// we used to get two results when two rules matched the same feature // we used to get two results when two rules matched the same feature
// print(result); // print(result);
Expand Down Expand Up @@ -226,5 +226,31 @@ public void testRepeatedLine() throws Exception {
assertEquals(2, result.getJSONArray("features").size()); assertEquals(2, result.getJSONArray("features").size());
} }



@Test
public void testPureLabelGenericGeometry() throws Exception {
String layer = getLayerId(MockData.GENERICENTITY);
String request = "wms?REQUEST=GetFeatureInfo&&BBOX=0.778809%2C45.421875%2C12.021973%2C59.921875&SERVICE=WMS"
+ "&INFO_FORMAT=application/json&QUERY_LAYERS="
+ layer
+ "&Layers="
+ layer
+ "&WIDTH=397&HEIGHT=512&format=image%2Fpng&styles=pureLabel&srs=EPSG%3A4326&version=1.1.1&x=182&y=241";
JSONObject result = (JSONObject) getAsJSON(request);
// we used to get no results
assertEquals(1, result.getJSONArray("features").size());
}

@Test
public void testPureLabelPolygon() throws Exception {
String layer = getLayerId(MockData.FORESTS);
String request = "wms?version=1.1.1&bbox=-0.002,-0.002,0.002,0.002&format=jpeg"
+ "&request=GetFeatureInfo&layers=" + layer + "&query_layers=" + layer
+ "&styles=pureLabel"
+ "&width=20&height=20&x=10&y=10&info_format=application/json";

JSONObject result = (JSONObject) getAsJSON(request);
// we used to get two results when two rules matched the same feature
// print(result);
assertEquals(1, result.getJSONArray("features").size());
}
} }

0 comments on commit 36c7518

Please sign in to comment.