Advertisement
Guest User

Untitled

a guest
Feb 18th, 2011
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.48 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.io.IOException;
  3.  
  4. import org.geotools.data.FeatureSource;
  5. import org.geotools.data.memory.MemoryDataStore;
  6. import org.geotools.factory.CommonFactoryFinder;
  7. import org.geotools.feature.simple.SimpleFeatureBuilder;
  8. import org.geotools.feature.simple.SimpleFeatureTypeBuilder;
  9. import org.geotools.filter.FilterFactory;
  10. import org.geotools.geometry.jts.JTSFactoryFinder;
  11. import org.geotools.map.DefaultMapContext;
  12. import org.geotools.map.DefaultMapLayer;
  13. import org.geotools.map.MapContext;
  14. import org.geotools.map.MapLayer;
  15. import org.geotools.math.Line;
  16. import org.geotools.referencing.crs.DefaultGeographicCRS;
  17. import org.geotools.styling.AnchorPoint;
  18. import org.geotools.styling.Font;
  19. import org.geotools.styling.Graphic;
  20. import org.geotools.styling.Mark;
  21. import org.geotools.styling.PointPlacement;
  22. import org.geotools.styling.PointSymbolizer;
  23. import org.geotools.styling.Style;
  24. import org.geotools.styling.StyleBuilder;
  25. import org.geotools.styling.StyleFactory;
  26. import org.geotools.styling.Symbolizer;
  27. import org.geotools.styling.TextSymbolizer;
  28. import org.geotools.swing.JMapFrame;
  29. import org.opengis.feature.simple.SimpleFeature;
  30. import org.opengis.feature.simple.SimpleFeatureType;
  31. import org.opengis.feature.type.FeatureType;
  32. import org.opengis.geometry.primitive.Point;
  33.  
  34.  
  35. public class MapPreviewFrame extends JMapFrame {
  36.     public static void main(String args[]) throws Exception {
  37.         new MapPreviewFrame();
  38.     }
  39.    
  40.     private Style buildStyle() throws Exception {
  41.         StyleBuilder sb = new StyleBuilder();
  42.         FilterFactory ff = (FilterFactory) sb.getFilterFactory();
  43.         Style style = sb.createStyle();
  44.         style.setName("MyStyle");
  45.  
  46.         // "testPoint" feature type style
  47.         Mark testMark = sb.createMark(sb.attributeExpression("name"),
  48.                 sb.createFill(Color.RED, 0.5), null);
  49.         Graphic graph = sb.createGraphic(null, new Mark[] { testMark }, null,
  50.                 sb.literalExpression(1), sb.attributeExpression("size"),
  51.                 sb.attributeExpression("rotation"));
  52.         style.addFeatureTypeStyle(sb.createFeatureTypeStyle("testPoint",
  53.                 new Symbolizer[] { sb.createPointSymbolizer(graph) }));
  54.  
  55.         // "labelPoint" feature type style
  56.         AnchorPoint anchorPoint = sb.createAnchorPoint(sb.attributeExpression("X"),
  57.                 sb.attributeExpression("Y"));
  58.         PointPlacement pointPlacement = sb.createPointPlacement(anchorPoint, null,
  59.                 sb.literalExpression(0));
  60.         TextSymbolizer textSymbolizer = sb.createTextSymbolizer(sb.createFill(Color.BLACK),
  61.                 new Font[] { (Font) sb.createFont("Lucida Sans", 10), sb.createFont("Arial", 10) },
  62.                 sb.createHalo(), sb.attributeExpression("name"), pointPlacement, null);
  63.         Mark circle = sb.createMark(StyleBuilder.MARK_CIRCLE, Color.RED);
  64.         Graphic graph2 = sb.createGraphic(null, circle, null, 1, 4, 0);
  65.         PointSymbolizer pointSymbolizer = sb.createPointSymbolizer(graph2);
  66.         style.addFeatureTypeStyle(sb.createFeatureTypeStyle("labelPoint",
  67.                 new Symbolizer[] { textSymbolizer, pointSymbolizer }));
  68.  
  69.         return style;
  70.     }
  71.    
  72.     // http://it-republik.de/jaxenter/artikel/Wege-jenseits-von-Google-Maps-mit-GeoTools-und-uDig-1934.html
  73.     // MapLayer can be added to a MapContext Object via addLayer
  74.     private MapLayer createMemoryMapLayer() throws Exception {
  75.         String myFeatureName = "myTestFeature";
  76.         SimpleFeatureTypeBuilder featureBuilder = new SimpleFeatureTypeBuilder();
  77.         featureBuilder.setName(myFeatureName);
  78.         featureBuilder.setCRS(DefaultGeographicCRS.WGS84);
  79.         featureBuilder.add("geometry", Line.class);
  80.         featureBuilder.add("id", Integer.class);
  81.         //featureBuilder.add("name", String.class);
  82.         FeatureType featureType = featureBuilder.buildFeatureType();
  83.         MemoryDataStore memoryDataStore = new MemoryDataStore();
  84.         memoryDataStore.createSchema((SimpleFeatureType) featureType);
  85.         FeatureSource featureSource = memoryDataStore.getFeatureSource(myFeatureName);
  86.  
  87.         StyleBuilder styleBuilder = new StyleBuilder();
  88.         Style myStyle = buildStyle(); //styleBuilder.createStyle();
  89.         DefaultMapLayer myLayer = new DefaultMapLayer(featureSource,myStyle,"myLayerName");
  90.         return myLayer;
  91.     }
  92.  
  93.     public MapPreviewFrame() throws Exception {
  94.         // http://docs.geotools.org/latest/tutorials/quickstart/eclipse.html
  95.         MapContext map = new DefaultMapContext();
  96.         map.setTitle("Quickstart");
  97.         map.addLayer(createMemoryMapLayer());
  98.         showMap(map);
  99.     }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement