import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.UIManager;
import org.geotools.data.DataUtilities;
import org.geotools.data.FeatureSource;
import org.geotools.data.FileDataStore;
import org.geotools.data.FileDataStoreFinder;
import org.geotools.factory.CommonFactoryFinder;
import org.geotools.feature.FeatureCollection;
import org.geotools.feature.FeatureCollections;
import org.geotools.feature.SchemaException;
import org.geotools.feature.simple.SimpleFeatureBuilder;
import org.geotools.geometry.jts.JTSFactoryFinder;
import org.geotools.map.DefaultMapContext;
import org.geotools.map.MapContext;
import org.geotools.renderer.lite.StreamingRenderer;
import org.geotools.styling.FeatureTypeStyle;
import org.geotools.styling.Fill;
import org.geotools.styling.Graphic;
import org.geotools.styling.LineSymbolizer;
import org.geotools.styling.Mark;
import org.geotools.styling.PointSymbolizer;
import org.geotools.styling.PolygonSymbolizer;
import org.geotools.styling.Rule;
import org.geotools.styling.Stroke;
import org.geotools.styling.Style;
import org.geotools.styling.StyleFactory;
import org.geotools.swing.JMapPane;
import org.geotools.swing.data.JFileDataStoreChooser;
import org.geotools.swing.tool.PanTool;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import org.opengis.filter.FilterFactory;
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.Point;
/**
*
*
* @version
*
*
*/
public class MyGeoToolsShapeViewer extends JFrame implements ActionListener {
JLabel statusBar;
JMapPane mapPane = null;
MapContext mapContext = null;
// für Menü
private JMenuItem jMenuItem_Data_LoadShp;
private JMenuItem jMenuItem_Data_Exit;
private JMenuItem jMenuItem_View_Refresh;
private JMenuItem jMenuItem_View_Position;
private JMenuItem jMenuItem_View_Pan;
private JMenuItem jMenuItem_Paint_Point;
private JMenuItem jMenuItem_Paint_Line;
static SimpleFeatureType type;
static double x, y;
static StyleFactory styleFactory = CommonFactoryFinder
.getStyleFactory(null);
static FilterFactory filterFactory = CommonFactoryFinder
.getFilterFactory(null);
public MyGeoToolsShapeViewer() {
super();
setSize(600, 400);
setTitle("MyGeoToolsShapeViewer");
this.getContentPane().add(getMapPane(), BorderLayout.CENTER);
statusBar = new JLabel("Start");
this.getContentPane().add(statusBar, BorderLayout.SOUTH);
this.setJMenuBar(makeJMenuBar());
}
public JMapPane getMapPane() {
if (mapPane == null) { // Initialisierung
mapPane = new JMapPane();
mapPane.setBackground(Color.WHITE);
mapPane.setRenderer(new StreamingRenderer());
}
return mapPane;
}
/**
*
* eine Shape-Datei dem Kartenpanel hinzufügen
*
* @param shpFile
* Sph-Datei
* @throws IOException
*/
public void addShpLayer(File shpFile) throws IOException {
boolean isNewMap = false;
if (mapContext == null) {
mapContext = new DefaultMapContext();
isNewMap = true;
}
// Layer erzeugen
FileDataStore store = FileDataStoreFinder.getDataStore(shpFile);
FeatureSource featureSource = store.getFeatureSource();
Style style = createPolygonStyle();
mapContext.addLayer(featureSource, (org.geotools.styling.Style) style);
if (isNewMap) {
// MapContext muss immer einen Layer beinhalten (ansonsten
// NullPointerException)
mapPane.setMapContext(mapContext);
mapPane.setRepaint(true);
mapPane.reset();
}
mapPane.repaint();
}
public void addShpLayerWhite(File shpFile) throws IOException {
boolean isNewMap = false;
if (mapContext == null) {
mapContext = new DefaultMapContext();
isNewMap = true;
}
// Layer erzeugen
FileDataStore store = FileDataStoreFinder.getDataStore(shpFile);
FeatureSource featureSource = store.getFeatureSource();
Style style = createPolygonStyleWhite();
mapContext.addLayer(featureSource, (org.geotools.styling.Style) style);
if (isNewMap) {
mapPane.setMapContext(mapContext);
mapPane.setRepaint(true);
mapPane.reset();
}
mapPane.repaint();
}
/**
*
* vordefinierte Styles für ein Layer
*
* @return Style
*/
private static Style createPolygonStyle() {
StyleFactory styleFactory = CommonFactoryFinder.getStyleFactory(null);
FilterFactory filterFactory = CommonFactoryFinder
.getFilterFactory(null);
// create a partially opaque outline stroke
Stroke stroke = styleFactory.createStroke(filterFactory
.literal(Color.BLACK), filterFactory.literal(1), filterFactory
.literal(0.5));
// create a partial opaque fill
Fill fill = styleFactory.createFill(filterFactory
.literal(Color.LIGHT_GRAY), filterFactory.literal(0.5));
/*
* Setting the geometryPropertyName arg to null signals that we want to
* draw the default geomettry of features
*/
PolygonSymbolizer sym = styleFactory.createPolygonSymbolizer(stroke,
fill, null);
Rule rule = styleFactory.createRule();
rule.symbolizers().add(sym);
FeatureTypeStyle fts = styleFactory
.createFeatureTypeStyle(new Rule[] { rule });
Style style = (Style) styleFactory.createStyle();
((org.geotools.styling.Style) style).featureTypeStyles().add(fts);
return style;
}
private static Style createPolygonStyleWhite() {
StyleFactory styleFactory = CommonFactoryFinder.getStyleFactory(null);
FilterFactory filterFactory = CommonFactoryFinder
.getFilterFactory(null);
// create a partially opaque outline stroke
Stroke stroke = styleFactory.createStroke(filterFactory
.literal(Color.white), filterFactory.literal(1), filterFactory
.literal(0.5));
// create a partial opaque fill
Fill fill = styleFactory.createFill(filterFactory.literal(Color.white),
filterFactory.literal(0.5));
/*
* Setting the geometryPropertyName arg to null signals that we want to
* draw the default geomettry of features
*/
PolygonSymbolizer sym = styleFactory.createPolygonSymbolizer(stroke,
fill, null);
Rule rule = styleFactory.createRule();
rule.symbolizers().add(sym);
FeatureTypeStyle fts = styleFactory
.createFeatureTypeStyle(new Rule[] { rule });
Style style = (Style) styleFactory.createStyle();
((org.geotools.styling.Style) style).featureTypeStyles().add(fts);
return style;
}
static private Style createPointStyle() {
Graphic gr = styleFactory.createDefaultGraphic();
Mark mark = styleFactory.getCircleMark();
mark.setStroke(styleFactory.createStroke(filterFactory
.literal(Color.BLUE), filterFactory.literal(1)));
mark
.setFill(styleFactory.createFill(filterFactory
.literal(Color.CYAN)));
gr.graphicalSymbols().clear();
gr.graphicalSymbols().add(mark);
gr.setSize(filterFactory.literal(5));
/*
* Setting the geometryPropertyName arg to null signals that we want to
* draw the default geomettry of features
*/
PointSymbolizer sym = styleFactory.createPointSymbolizer(gr, null);
Rule rule = styleFactory.createRule();
rule.symbolizers().add(sym);
FeatureTypeStyle fts = styleFactory
.createFeatureTypeStyle(new Rule[] { rule });
Style style = styleFactory.createStyle();
style.featureTypeStyles().add(fts);
return style;
}
private 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);
Rule rule = styleFactory.createRule();
rule.symbolizers().add(sym);
FeatureTypeStyle fts = styleFactory
.createFeatureTypeStyle(new Rule[] { rule });
Style style = styleFactory.createStyle();
style.featureTypeStyles().add(fts);
return style;
}
/**
*
* Verarbeitung der Menüereignissen
*
*/
public void actionPerformed(ActionEvent event) {
if (event.getSource() == jMenuItem_Data_LoadShp) {
try {
File file = JFileDataStoreChooser.showOpenFile("shp", null);
if (file == null) {
return;
}
addShpLayer(file);
getMapPane().repaint();
} catch (IOException e) {
e.printStackTrace();
return;
}
}
if (event.getSource() == jMenuItem_Data_Exit) {
System.exit(0);
}
if (event.getSource() == jMenuItem_View_Refresh) {
getMapPane().repaint();
}
if (event.getSource() == jMenuItem_View_Position) {
if (mapPane == null)
return;
mapPane.setCursorTool(new PositionTool(this));
}
if (event.getSource() == jMenuItem_View_Pan) {
if (mapPane == null)
return;
statusBar.setText("Modus Pan");
mapPane.setCursorTool(new PanTool());
}
if (event.getSource() == jMenuItem_Paint_Point) {
System.out.println("Punkt zeichnen");
File file = new File("recht.shp");
try {
addShpLayerWhite(file);
} catch (IOException e1) {
e1.printStackTrace();
}
mapPane.setCursorTool(new PointTool(this));
}
if (event.getSource() == jMenuItem_Paint_Line) {
mapPane.setCursorTool(new PointTool(this));
}
}
/**
*
* Menü erzeugen
*
* @return Menü
*/
JMenuBar makeJMenuBar() {
JMenuBar jMenuBar = new JMenuBar();
// Daten-Menü
JMenu jMenu_Data = new JMenu("Daten");
jMenuItem_Data_LoadShp = new JMenuItem("Vektordaten hinzufügen ...");
jMenuItem_Data_LoadShp.addActionListener(this);
jMenu_Data.add(jMenuItem_Data_LoadShp);
jMenu_Data.addSeparator();
jMenuItem_Data_Exit = new JMenuItem("Beenden");
jMenuItem_Data_Exit.addActionListener(this);
jMenu_Data.add(jMenuItem_Data_Exit);
jMenuBar.add(jMenu_Data);
// Ansicht-Menü
JMenu jMenu_View = new JMenu("Ansicht");
jMenuItem_View_Refresh = new JMenuItem("Aktualisieren");
jMenuItem_View_Refresh.addActionListener(this);
jMenu_View.add(jMenuItem_View_Refresh);
jMenuItem_View_Position = new JMenuItem("Tool: Positionsanzeige");
jMenuItem_View_Position.addActionListener(this);
jMenu_View.add(jMenuItem_View_Position);
jMenuItem_View_Pan = new JMenuItem("Tool: Pan (Default)");
jMenuItem_View_Pan.addActionListener(this);
jMenu_View.add(jMenuItem_View_Pan);
jMenuBar.add(jMenu_View);
// Zeichen
JMenu jMenu_Paint = new JMenu("Zeichen");
jMenuItem_Paint_Point = new JMenuItem("Punkt");
jMenuItem_Paint_Point.addActionListener(this);
jMenu_Paint.add(jMenuItem_Paint_Point);
jMenuItem_Paint_Line = new JMenuItem("Linie");
jMenuItem_Paint_Line.addActionListener(this);
jMenu_Paint.add(jMenuItem_Paint_Line);
jMenuBar.add(jMenu_Paint);
return jMenuBar;
}
/**
*
* Programmeintrittspunkt
*
* @param args
* Kommandozeile, wird nicht verarbeitet
*/
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
new MyGeoToolsShapeViewer().setVisible(true);
}
}