Hi all,
I am trying to allow a use of my app to set a shape file style that’s color
coded in terms of an attribute’s value.
I should also make note that I am using geotools’s JMapPane to display my
layers.
Unfortunately I can’t get this to work. I am revising some code I wrote way
back at the time of geotools 2.X or there abouts.
I am pretty sure it used to work back then, but no longer. When I set the new
style, only one of the colors gets picked up, not all of them,
and it is applied to all features. In other words none of the filters seem to
be applied.
(Perhaps it is a problem with JMapPane (being that it’s not supported directly?)
Here’s my code. In both case I am getting the data from table the user has
filled with his/her data ranges and colors and selAttr is the attribute in
question
Old code:
Style style = mylayer.getStyle();
FeatureTypeStyle[] fts = style.getFeatureTypeStyles();
int rows = tableModel.getRowCount();
double mmin, mmax;
for (int i = 0; i < rows; i++) {
Color c = (Color) tableModel.getValueAt(i, 2);
mmin = Double.parseDouble(tableModel.getValueAt(i, 0).toString());
mmax = Double.parseDouble(tableModel.getValueAt(i, 1).toString());
fts[0].addRule(makeNewRule(c, mmin, mmax));
}
repaintMap();
private Rule makeNewRule(Color color, double min, double max) {
StyleBuilder sb = new StyleBuilder();
Symbolizer[] symb = new Symbolizer[1];
Mark dot;
Graphic graphic;
switch (shape) {
case POINT: // point
dot = sb.createMark(StyleBuilder.MARK_CIRCLE, color, color, 1.0);
graphic = sb.createGraphic(null, dot, null, 1, 4, 0);
symb[0] = sb.createPointSymbolizer(graphic);
break;
case LINE: // line
symb[0] = sb.createLineSymbolizer(color, 2);
break;
case POLYGON: // polygon
symb[0] = sb.createPolygonSymbolizer(color);
break;
}
Rule rule = sb.createRule(symb);
PropertyIsBetween bf = ff.between(ff2.property(at.getName().getLocalPart()),
ff.literal(min), ff.literal(max));
rule.setFilter(bf);
rule.setName(at.getName().getLocalPart() + "*" + Double.toString(min) + "-" +
Double.toString(max));
System.out.println("New rule name : " + at.getName().getLocalPart() + "*" +
Double.toString(min) + "-" + Double.toString(max));
return rule;
}
New code:
int rows = tableModel.getRowCount();
FeatureTypeStyle style = styles.get(0);
for (int i = 0; i < rows; i++) {
Color c = (Color) tableModel.getValueAt(i, 2);
double min = Double.parseDouble(tableModel.getValueAt(i, 0).toString());
double max = Double.parseDouble(tableModel.getValueAt(i, 1).toString());
style.rules().add(makeNewRule(c, min, max));
}
repaintMap();
private Rule makeNewRule(Color color, double min, double max) {
StyleBuilder sb = new StyleBuilder(sf);
Symbolizer[] symb = new Symbolizer[1];
if (geomType == Geometries.POINT || geomType == Geometries.MULTIPOINT) {
Mark dot = sb.createMark(StyleBuilder.MARK_CIRCLE, color, color,
DEFAULT_POINT_SIZE);
Graphic graphic = sb.createGraphic(null, dot, null, opacity, 4, 0);
symb[0] = sb.createPointSymbolizer(graphic);
} else if (geomType == Geometries.LINESTRING || geomType ==
Geometries.MULTILINESTRING) {
symb[0] = sb.createLineSymbolizer(color, DEFAULT_LINE_WIDTH);
} else if (geomType == Geometries.POLYGON || geomType ==
Geometries.MULTIPOLYGON) {
Stroke stroke = sb.createStroke(borderColor, DEFAULT_LINE_WIDTH, opacity);
Fill fill = sb.createFill(color, opacity);
symb[0] = sb.createPolygonSymbolizer(stroke, fill);
}
Rule rule = sb.createRule(symb);
PropertyIsBetween bf = ff.between(ff2.property(selAttr.getLocalName()),
ff.literal(min), ff.literal(max));
rule.setFilter(bf);
rule.setName(selAttr.getLocalName() + "*" + Double.toString(min) + "-" +
Double.toString(max));
System.out.println("New rule name : " + selAttr.getLocalName() + "*" +
Double.toString(min) + "-" + Double.toString(max));
return rule;
}
Thanx a bunch
gaby
------------------------------------------------------------------------------
Monitor 25 network devices or servers for free with OpManager!
OpManager is web-based network management software that monitors
network devices and physical & virtual servers, alerts via email & sms
for fault. Monitor 25 devices for free with no restriction. Download now
http://ad.doubleclick.net/ddm/clk/292181274;119417398;o
_______________________________________________
GeoTools-GT2-Users mailing list
GeoTools-GT2-Users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users