When I use a Parser and KMLConfiguration to parse a KML document, the styles
do not seem to apply to the elements that they are tied to when rendering.
What am I missing? I don't have a lot of experience using geotools or KML.
I am hoping this is something that a more experienced eye will quickly spot.
Java:
private SimpleFeature kmlToFeatureCollection(String filename) throws
Exception {
Parser parse = new Parser(new KMLConfiguration());
SimpleFeature sf = (SimpleFeature) parse.parse(new
FileInputStream(filename));
return sf;
}
StyleFactory styleFactory = CommonFactoryFinder.getStyleFactory(null);
public KMLExample(String filename) throws Exception {
SimpleFeature docFeat = kmlToFeatureCollection(filename);
Collection<SimpleFeature> features = (Collection<SimpleFeature>)
docFeat.getAttribute("Feature");
MemoryDataStore store = new MemoryDataStore();
Style style = styleFactory.createStyle();
for (SimpleFeature f : features) {
store.addFeature(f);
//I create a new style here using the one from the document and
add a rule to apply it
//only to the given feature id... if I do not do this, nothing
is rendered.
FeatureTypeStyle origFts = (FeatureTypeStyle)
f.getAttribute("Style");
Rule origRule = origFts.rules().get(0);
Symbolizer origSym = origRule.symbolizers().get(0);
Stroke stroke = ((LineSymbolizer) origSym).getStroke();
LineSymbolizer sym = styleFactory.createLineSymbolizer(stroke,
null);
Filter filter =
filterFactory.id(Collections.singleton(filterFactory.featureId(f.getID())));
Rule rule = styleFactory.createRule();
rule.symbolizers().add(sym);
rule.setFilter(filter);
FeatureTypeStyle fts = styleFactory.createFeatureTypeStyle(new
Rule[]{rule});
style.featureTypeStyles().add(fts);
}
FeatureCollection<SimpleFeatureType, SimpleFeature>
featureCollection =
store.getFeatureSource(store.getTypeNames()[0]).getFeatures();
MapContext map = new DefaultMapContext(DefaultGeographicCRS.WGS84);
map.addLayer(featureCollection, style);
GTRenderer rend = new StreamingRenderer();
Rectangle area = new Rectangle(1000, 800);
rend.setContext(map);
BufferedImage bi = new BufferedImage(1000, 800,
BufferedImage.TYPE_INT_ARGB);
Graphics2D g = (Graphics2D) bi.getGraphics();
g.setColor(Color.gray);
g.fillRect(0, 0, 1000, 800);
rend.paint(g, area, map.getLayerBounds());
ImageIO.write(bi, "png", new FileOutputStream("output.png"));
}
KML:
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document id="featureCollection">
<name>Path</name>
<Style id="redline">
<LineStyle>
<color>ffff0000</color>
<width>4</width>
</LineStyle>
</Style>
<Placemark>
<LineString>
<coordinates>
-86.358500,34.802400
-86.388400,34.794000
</coordinates>
</LineString>
<styleUrl>#redline</styleUrl>
</Placemark>
</Document>
</kml>
Using geotools 2.7.1. Thanks much for any assistance you can provide!
-Eric
------------------------------------------------------------------------------
EditLive Enterprise is the world's most technically advanced content
authoring tool. Experience the power of Track Changes, Inline Image
Editing and ensure content is compliant with Accessibility Checking.
http://p.sf.net/sfu/ephox-dev2dev
_______________________________________________
Geotools-gt2-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users