Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Color;
- import java.io.IOException;
- import org.geotools.data.FeatureSource;
- import org.geotools.data.memory.MemoryDataStore;
- import org.geotools.factory.CommonFactoryFinder;
- import org.geotools.feature.simple.SimpleFeatureBuilder;
- import org.geotools.feature.simple.SimpleFeatureTypeBuilder;
- import org.geotools.filter.FilterFactory;
- import org.geotools.geometry.jts.JTSFactoryFinder;
- import org.geotools.map.DefaultMapContext;
- import org.geotools.map.DefaultMapLayer;
- import org.geotools.map.MapContext;
- import org.geotools.map.MapLayer;
- import org.geotools.math.Line;
- import org.geotools.referencing.crs.DefaultGeographicCRS;
- import org.geotools.styling.AnchorPoint;
- import org.geotools.styling.Font;
- import org.geotools.styling.Graphic;
- import org.geotools.styling.Mark;
- import org.geotools.styling.PointPlacement;
- import org.geotools.styling.PointSymbolizer;
- import org.geotools.styling.Style;
- import org.geotools.styling.StyleBuilder;
- import org.geotools.styling.StyleFactory;
- import org.geotools.styling.Symbolizer;
- import org.geotools.styling.TextSymbolizer;
- import org.geotools.swing.JMapFrame;
- import org.opengis.feature.simple.SimpleFeature;
- import org.opengis.feature.simple.SimpleFeatureType;
- import org.opengis.feature.type.FeatureType;
- import org.opengis.geometry.primitive.Point;
- public class MapPreviewFrame extends JMapFrame {
- public static void main(String args[]) throws Exception {
- new MapPreviewFrame();
- }
- private Style buildStyle() throws Exception {
- StyleBuilder sb = new StyleBuilder();
- FilterFactory ff = (FilterFactory) sb.getFilterFactory();
- Style style = sb.createStyle();
- style.setName("MyStyle");
- // "testPoint" feature type style
- Mark testMark = sb.createMark(sb.attributeExpression("name"),
- sb.createFill(Color.RED, 0.5), null);
- Graphic graph = sb.createGraphic(null, new Mark[] { testMark }, null,
- sb.literalExpression(1), sb.attributeExpression("size"),
- sb.attributeExpression("rotation"));
- style.addFeatureTypeStyle(sb.createFeatureTypeStyle("testPoint",
- new Symbolizer[] { sb.createPointSymbolizer(graph) }));
- // "labelPoint" feature type style
- AnchorPoint anchorPoint = sb.createAnchorPoint(sb.attributeExpression("X"),
- sb.attributeExpression("Y"));
- PointPlacement pointPlacement = sb.createPointPlacement(anchorPoint, null,
- sb.literalExpression(0));
- TextSymbolizer textSymbolizer = sb.createTextSymbolizer(sb.createFill(Color.BLACK),
- new Font[] { (Font) sb.createFont("Lucida Sans", 10), sb.createFont("Arial", 10) },
- sb.createHalo(), sb.attributeExpression("name"), pointPlacement, null);
- Mark circle = sb.createMark(StyleBuilder.MARK_CIRCLE, Color.RED);
- Graphic graph2 = sb.createGraphic(null, circle, null, 1, 4, 0);
- PointSymbolizer pointSymbolizer = sb.createPointSymbolizer(graph2);
- style.addFeatureTypeStyle(sb.createFeatureTypeStyle("labelPoint",
- new Symbolizer[] { textSymbolizer, pointSymbolizer }));
- return style;
- }
- // http://it-republik.de/jaxenter/artikel/Wege-jenseits-von-Google-Maps-mit-GeoTools-und-uDig-1934.html
- // MapLayer can be added to a MapContext Object via addLayer
- private MapLayer createMemoryMapLayer() throws Exception {
- String myFeatureName = "myTestFeature";
- SimpleFeatureTypeBuilder featureBuilder = new SimpleFeatureTypeBuilder();
- featureBuilder.setName(myFeatureName);
- featureBuilder.setCRS(DefaultGeographicCRS.WGS84);
- featureBuilder.add("geometry", Line.class);
- featureBuilder.add("id", Integer.class);
- //featureBuilder.add("name", String.class);
- FeatureType featureType = featureBuilder.buildFeatureType();
- MemoryDataStore memoryDataStore = new MemoryDataStore();
- memoryDataStore.createSchema((SimpleFeatureType) featureType);
- FeatureSource featureSource = memoryDataStore.getFeatureSource(myFeatureName);
- StyleBuilder styleBuilder = new StyleBuilder();
- Style myStyle = buildStyle(); //styleBuilder.createStyle();
- DefaultMapLayer myLayer = new DefaultMapLayer(featureSource,myStyle,"myLayerName");
- return myLayer;
- }
- public MapPreviewFrame() throws Exception {
- // http://docs.geotools.org/latest/tutorials/quickstart/eclipse.html
- MapContext map = new DefaultMapContext();
- map.setTitle("Quickstart");
- map.addLayer(createMemoryMapLayer());
- showMap(map);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement