From: Oliver G. <ol...@dr...> - 2009-12-17 22:01:13
|
OMG LOL T h a n k . Y o u ! Well I guess I went the long way around the block on that one! :-) Here is the much less complicated working code with the LinePlacement! Ugh package org.geotools.demo; import org.geotools.factory.CommonFactoryFinder; import org.geotools.feature.FeatureCollection; import org.geotools.feature.FeatureCollections; import org.geotools.feature.simple.SimpleFeatureBuilder; import org.geotools.feature.simple.SimpleFeatureTypeBuilder; import org.geotools.map.DefaultMapContext; import org.geotools.map.MapContext; import org.geotools.referencing.crs.DefaultGeographicCRS; import org.geotools.styling.AnchorPoint; import org.geotools.styling.Displacement; import org.geotools.styling.FeatureTypeStyle; import org.geotools.styling.Fill; import org.geotools.styling.Font; import org.geotools.styling.Graphic; import org.geotools.styling.LabelPlacement; import org.geotools.styling.LinePlacement; import org.geotools.styling.LineSymbolizer; import org.geotools.styling.Mark; import org.geotools.styling.PointSymbolizer; import org.geotools.styling.Rule; import org.geotools.styling.Stroke; import org.geotools.styling.Style; import org.geotools.styling.StyleFactory; import org.geotools.styling.TextSymbolizer; import org.geotools.swing.JMapFrame; import java.awt.Color; import java.util.*; import org.geotools.geometry.jts.JTSFactoryFinder; import org.opengis.feature.simple.SimpleFeature; import org.opengis.feature.simple.SimpleFeatureType; import org.opengis.filter.FilterFactory; import org.opengis.referencing.crs.CoordinateReferenceSystem; import com.vividsolutions.jts.geom.Coordinate; import com.vividsolutions.jts.geom.CoordinateArrays; import com.vividsolutions.jts.geom.GeometryFactory; import com.vividsolutions.jts.geom.LineString; public class DisplayMultiLineForced { static StyleFactory styleFactory = CommonFactoryFinder.getStyleFactory(null); static FilterFactory filterFactory = CommonFactoryFinder.getFilterFactory(null); static final CoordinateReferenceSystem crs = DefaultGeographicCRS.WGS84; static final int MAX_NAME_LENGTH = 20; public static void main(String[] args) throws Exception { SimpleFeatureTypeBuilder typeBuilder = new SimpleFeatureTypeBuilder(); typeBuilder.setName("mytype"); typeBuilder.setCRS(crs); typeBuilder.add("route", LineString.class); typeBuilder.length(MAX_NAME_LENGTH).add("name", String.class); final SimpleFeatureType TYPE = typeBuilder.buildFeatureType(); FeatureCollection<SimpleFeatureType, SimpleFeature> collection = FeatureCollections.newCollection(); double x[][] = {{10, 20}, {20, 30}, {30, 40}, {40, 50}}; double y[][] = {{10, 20}, {20, 10}, {10, 20}, {20, 10}}; //double x[][] = {{20, 10}, {30, 20}, {40, 30}, {50, 40}}; //double y[][] = {{20, 10}, {10, 20}, {20, 10}, {10, 20}}; String[] names = {"L1", "L2", "L3", "L4" }; GeometryFactory factory = JTSFactoryFinder.getGeometryFactory(null); SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(TYPE); ArrayList<Coordinate> coordList = new ArrayList<Coordinate>(); for (int line = 0; line < x.length; line++) { for (int i = 0; i < x[line].length; i++) { coordList.add(new Coordinate(x[line][i], y[line][i])); } Coordinate[] coords = CoordinateArrays.toCoordinateArray(coordList); LineString lineString = factory.createLineString(coords); featureBuilder.add(lineString); featureBuilder.add(names[line]); collection.add(featureBuilder.buildFeature(null)); coordList.clear(); } MapContext map = new DefaultMapContext(); map.setTitle("My beautiful lines"); map.addLayer(collection, createLineStyle()); // Now display the map JMapFrame.showMap(map); } /** * Create a Style to draw line features as thin blue lines */ private static Style createLineStyle() { Stroke stroke = styleFactory.createStroke( filterFactory.literal(Color.BLUE), filterFactory.literal(1)); /* * Setting the geometryPropertyName arg to null signals that we want to * draw the default geomettry of features */ LineSymbolizer sym = styleFactory.createLineSymbolizer(stroke, null); //Label Formatting String labelField="name"; Fill labelFill = styleFactory.createFill(filterFactory.literal(Color.black)); LinePlacement placement = styleFactory.createLinePlacement(filterFactory.literal(5)); Font font = styleFactory.getDefaultFont(); font=styleFactory.createFont(filterFactory.literal("Arial"), filterFactory.literal("italic"), filterFactory.literal(8), filterFactory.literal(8)); TextSymbolizer textSym = styleFactory.createTextSymbolizer( labelFill, new Font[]{font}, null, filterFactory.property(labelField), placement, null); Rule rule = styleFactory.createRule(); rule.symbolizers().add(sym); rule.symbolizers().add(textSym); FeatureTypeStyle fts = styleFactory.createFeatureTypeStyle(new Rule[]{rule}); Style style = styleFactory.createStyle(); style.featureTypeStyles().add(fts); return style; } } -----Original Message----- From: "Ian Turton" <ijt...@gm...> Sent: Thursday, December 17, 2009 4:41pm To: "Oliver Gottwald" <ol...@dr...> Cc: "geouser" <geo...@li...> Subject: Re: [Geotools-gt2-users] How to: Make Transparent Overlay Layer? If you use a LinePlacement instead of a PointPlacement then the label will align with the line and save you calculating the angle for each label. Ian -- Ian Turton Sent from Chatham, New Jersey, United States |