Hi,
Sorry, I thought maybe you see directly a fault in my code.
Here is (a complete new) code now, where the failure still exists. If you start
the program, you will directly see the problem. I hope the code is clear enough.
import java.awt.*;
import java.util.HashMap;
import java.util.Map;
import javax.swing.JFrame;
import org.geotools.data.FeatureSource;
import org.geotools.data.memory.MemoryDataStore;
import org.geotools.factory.CommonFactoryFinder;
import org.geotools.feature.AttributeTypeFactory;
import org.geotools.feature.FeatureTypeBuilder;
import org.geotools.feature.simple.SimpleFeatureBuilder;
import org.geotools.map.*;
import org.geotools.referencing.crs.DefaultGeographicCRS;
import org.geotools.renderer.GTRenderer;
import org.geotools.renderer.lite.StreamingRenderer;
import org.geotools.styling.*;
import org.geotools.styling.Font;
import org.geotools.styling.Stroke;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import org.opengis.filter.FilterFactory;
import com.vividsolutions.jts.geom.*;
import com.vividsolutions.jts.geom.Point;
public class GeoToolsTest extends JFrame
{
public static void main(String[] args) throws Exception
{
new GeoToolsTest().test();
}
private GTRenderer renderer = new StreamingRenderer();
private MapContext context;
public void test() throws Exception
{
GeometryFactory geomFac = new GeometryFactory();
String myFeatureName = "myTestFeature";
FeatureTypeBuilder featureBuilder =
FeatureTypeBuilder.newInstance(myFeatureName);
featureBuilder.setName(myFeatureName);
featureBuilder.addType(AttributeTypeFactory.newAttributeType("geometry",
Point.class, true, Integer.MAX_VALUE, null, DefaultGeographicCRS.WGS84));
SimpleFeatureType pointType = featureBuilder.getFeatureType();
MemoryDataStore storeWithShield = new MemoryDataStore();
storeWithShield.createSchema(pointType);
FeatureSource<SimpleFeatureType, SimpleFeature> sourceWithShield =
storeWithShield.getFeatureSource(myFeatureName);
MemoryDataStore storeWithoutShield = new MemoryDataStore();
storeWithoutShield.createSchema(pointType);
FeatureSource<SimpleFeatureType, SimpleFeature> sourceWithoutShield =
storeWithoutShield.getFeatureSource(myFeatureName);
SimpleFeature pointFeature;
// create Features for testing shields (are shown at bottom by starting the
program)
Coordinate c = new Coordinate(30, 50);
Point point = geomFac.createPoint(c);
pointFeature = SimpleFeatureBuilder.build(pointType, new Object[] {point},
"id0");
storeWithShield.addFeature(pointFeature);
c = new Coordinate(80, 50);
point = geomFac.createPoint(c);
pointFeature = SimpleFeatureBuilder.build(pointType, new Object[] {point},
"id1");
storeWithShield.addFeature(pointFeature);
// create features for tests without shields (are shown on top by starting
the program)
c = new Coordinate(30, 80);
point = geomFac.createPoint(c);
pointFeature = SimpleFeatureBuilder.build(pointType, new Object[] {point},
"id0");
storeWithoutShield.addFeature(pointFeature);
c = new Coordinate(80, 80);
point = geomFac.createPoint(c);
pointFeature = SimpleFeatureBuilder.build(pointType, new Object[] {point},
"id1");
storeWithoutShield.addFeature(pointFeature);
DefaultMapLayer shieldLayer = new DefaultMapLayer(sourceWithShield,
getStyle(true), "shieldLayer");
DefaultMapLayer withoutShieldLayer = new
DefaultMapLayer(sourceWithoutShield, getStyle(false), "withoutShieldLayer");
context = new DefaultMapContext();
context.setCoordinateReferenceSystem(DefaultGeographicCRS.WGS84);
context.addLayer(shieldLayer);
context.addLayer(withoutShieldLayer);
context.setAreaOfInterest(new com.vividsolutions.jts.geom.Envelope(new
Coordinate(0, 0), new Coordinate(100, 100)));
renderer.setContext(context);
RenderingHints hints = new RenderingHints(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
renderer.setJava2DHints(hints);
Map rendererParams = new HashMap();
this.setSize(500, 500);
this.setVisible(true);
}
@Override
public void paint(Graphics g)
{
g.setColor(Color.white);
g.fillRect(0, 0, this.getWidth(), this.getHeight());
renderer.paint((Graphics2D) g, new Rectangle(0, 0, this.getWidth(),
this.getHeight()), context.getAreaOfInterest());
}
private Style getStyle(boolean withShield) throws Exception
{
StyleFactory styleFactory = CommonFactoryFinder.getStyleFactory(null);
FilterFactory filterFactory = CommonFactoryFinder.getFilterFactory(null);
double fontSize = 12;
String fontName = "Arial";
Font gtFont = styleFactory.createFont(filterFactory.literal(fontName),
filterFactory.literal(Font.Style.NORMAL),
filterFactory.literal(Font.Weight.NORMAL),
filterFactory.literal(fontSize));
Fill fill = styleFactory.createFill(filterFactory.literal("#000000"),
filterFactory.literal(1));
AnchorPoint ap = styleFactory.createAnchorPoint(filterFactory.literal(0.5),
filterFactory.literal(0.0));
Displacement dp =
styleFactory.createDisplacement(filterFactory.literal(0.0),
filterFactory.literal(20.0));
LabelPlacement placement = styleFactory.createPointPlacement(ap, dp,
filterFactory.literal(0.0));
Stroke stroke =
styleFactory.createStroke(filterFactory.literal(Color.black),
filterFactory.literal(1.0), filterFactory.literal(1.0f));
Fill fillBg = styleFactory.createFill(filterFactory.literal(Color.white),
filterFactory.literal(0.0));
Mark mark =
styleFactory.createMark(filterFactory.literal(styleFactory.getCircleMark()),
stroke, fillBg, filterFactory.literal(1), filterFactory
.literal(0.0));
String addText;
if (withShield) {
addText = "\n(with Shield)";
}
else {
addText = "\n(without Shield)";
}
TextSymbolizer2 textSymbolizer = (TextSymbolizer2)
styleFactory.createTextSymbolizer(fill, new Font[] {gtFont}, null, filterFactory
.literal("This is aaaaaaaaaaaaaaaa Test"+addText), placement, null);
textSymbolizer.setGeometryPropertyName("geometry");
if (withShield)
{
Graphic shield = styleFactory.createGraphic(null, new Mark[] {mark},
null, null, filterFactory.literal(50), filterFactory.literal(0.0));
textSymbolizer.setGraphic(shield);
}
PointSymbolizer pointSymbolizer = styleFactory.createPointSymbolizer();
Graphic graphic = styleFactory.createGraphic(null, null, new Symbol[]
{styleFactory.getCircleMark()}, filterFactory.literal(1), filterFactory
.literal(filterFactory.property("size")), filterFactory.literal(0));
pointSymbolizer.setGraphic(graphic);
Rule rule = styleFactory.createRule();
rule.symbolizers().add(textSymbolizer);
rule.symbolizers().add(pointSymbolizer);
FeatureTypeStyle fts = styleFactory.createFeatureTypeStyle();
fts.rules().add(rule);
StyleBuilder sb = new StyleBuilder();
Style style = sb.createStyle();
style.addFeatureTypeStyle(fts);
return style;
}
}
Cheers,
Christian
-----Ursprüngliche Nachricht-----
Von: Andrea Aime [mailto:[email protected]]
Gesendet: Freitag, 30. April 2010 11:12
An: Christian Dubray
Cc: [email protected]
Betreff: Re: AW: [Geotools-gt2-users] Problem with the LabelShiel-hack of
TextSymbolizer2
Christian Dubray ha scritto:
> Thanks for you help.
>
> Here the reduced code, where the fault still exists.
> I´ve changed some attributes, but there is always the same problem. The
> labels are filtered out in paintPointLabelInternal(...) of LabelCacheImpl.
> displayArea.contains(transformed) returns false.
>
> AnchorPoint ap = styleFactory.createAnchorPoint(filterFactory.literal(0.5),
> filterFactory.literal(0.0));
> Displacement dp = styleFactory.createDisplacement(filterFactory.literal(0.0),
> filterFactory.literal(-20.0));
> LabelPlacement placement = styleFactory.createPointPlacement(ap, dp,
> filterFactory.literal(0.0));
> Fill bgFill = styleFactory.createFill(filterFactory.literal(bgColor),
> filterFactory.literal(1));
> Stroke stroke = styleFactory.createStroke(filterFactory.literal(Color.black),
> filterFactory.literal(1.0), filterFactory.literal(1.0f));
> Fill fillBg = styleFactory.createFill(filterFactory.literal(bgColor),
> filterFactory.literal(1.0));
> Graphic graphicBg = styleFactory.createGraphic(null, new Mark[]
> {StyleFactoryFinder.createStyleFactory().getCrossMark()}, null, null,
> filterFactory.literal(20), filterFactory
> TextSymbolizer2 textSymbolizer = (TextSymbolizer2)
> styleFactory.createTextSymbolizer(fill, new Font[] {gtFont}, null,
> filterFactory.literal("This is a test"), placement, null);
> textSymbolizer.setGeometryPropertyName("geometry");
> textSymbolizer.setGraphic(graphicBg);
>
> As I said, to the right side all works correct. Only the left side makes the
> problems. Depending on the length of the label, the label is hided earlier
> although there would be enough place to show it. But the label-bounds seem to
> be calculated correct.
Sorry, can't use the above.
I will be pleased to have a look if, as said before, you provide
me with "a data set and a little self
contained program that shows the behaviour you deem to be unsatisfactory".
That means a full runnable java file with a main that will
draw a map showing the issue along with the data set
that makes the problem occur.
I don't have time to replicate the issue by myself.
Cheers
Andrea
--
Andrea Aime
OpenGeo - http://opengeo.org
Expert service straight from the developers.
------------------------------------------------------------------------------
_______________________________________________
Geotools-gt2-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users