From: Niranjan k <nir...@gm...> - 2006-11-30 20:56:30
|
Hi, I am working on a project using GT2.2.1. I am totally new to this environment, so please excuse my naive doubts! I am trying to display polygons, given the co-ordinates as a shapefile, which works just fine. But, now I want to also display a name within each polygon. The name to be displayed is actually present as an attribute(unique for each polygon) in the original shapefile. I tried using createTextSymbolizer(),the StyleFactory method, to do this, but it only allows me to set individual Expressions as a label, and if I pass the attribute name, nothing appears on the map. How do i display the polygon name along with each polygon when it gets displayed? I created two rules for that feature style, one PolygonSymbolizer, and one TextSymbolizer, and also added both to the same feature style. But even then, only the polygons get displayed, and not the names. Here is the code I used: StyleBuilder sb = new StyleBuilder(); StyleFactory sf = StyleFactoryFinder.createStyleFactory(); PolygonSymbolizer ps = sf.createPolygonSymbolizer(); Fill fill1 = sf.createFill(ff.createLiteralExpression("#00FF00")); Stroke stroke = sf.createStroke( ff.createLiteralExpression("#FF0000"), ff.createLiteralExpression(2)); ps.setFill(fill1); ps.setStroke(stroke); org.geotools.styling.Font font = sb.createFont(new Font("Serif", Font.PLAIN, 8)); org.geotools.styling.Font font1[] = new org.geotools.styling.Font[1]; font1[0]= font; Fill fill2 = sf.createFill(ff.createLiteralExpression("#000000")); TextSymbolizer ts = sf.createTextSymbolizer(); ts.setFill(fill2); ts.setLabel(ff.createLiteralExpression(attrs[1].getName())); ts.setFonts(font1); Fill fill3= sf.createFill(ff.createLiteralExpression ("#ffffff")); Halo halo= sf.createHalo(fill3, ff.createLiteralExpression("1")); ts.setHalo(halo); Rule rule = sf.createRule(); rule.setSymbolizers( new Symbolizer[]{ ps }); Rule rulets = sf.createRule(); rulets.setSymbolizers( new Symbolizer[]{ ts }); FeatureTypeStyle type = sf.createFeatureTypeStyle(); type.setFeatureTypeName(temp[0]); type.addRule(rule); FeatureTypeStyle typets = sf.createFeatureTypeStyle(); typets.setFeatureTypeName(temp[0]); typets.addRule(rulets); Style style = sf.createStyle(); style.addFeatureTypeStyle(type); style.addFeatureTypeStyle(typets); Is this code correct? Any help will be appreciated! Thanks in advance, Niranjan Kumar M.S. Candidate Department of Computer Science, Iowa State University, Ames, IA |