Java Geotools

Simifahrer

Neues Mitglied
hallo,
ich habe in der uni folgende aufgabe gestellt bekommen: Entwickeln Sie Funktionen zum Zeichnen und Hinzufügen von Punkten, Linien und Flächen in einem neuen Layer dargestellt in einem Karten-Panel.

bis jetzt kann ich nur ein shape einlesen, weiter aber nichts.
es würde auch reichen wenn ich linien und punkte auf einen shape zeichen könnte
Kann mir jemand helfen??
 
Was meinst du mit einlesen?
Codeansatz? Wie weit bist du mit Code?

Eig. gibts alle notwendigen Funktionen zum zeichnen in
Code:
Graphics
.
 
ich habe es bis jetzt sogar geschaft punkte zu zeichnen aber wie ich linien oder flächen zeichen, hab ich keine ahnung. vielleicht hilft euch mein quelltext weiter.


Java:
import java.awt.Color;
import java.awt.Component;
import java.awt.Cursor;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Polygon;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.swing.JFileChooser;

import org.geotools.data.DataUtilities;
import org.geotools.data.DefaultTransaction;
import org.geotools.data.FeatureSource;
import org.geotools.data.FileDataStore;
import org.geotools.data.FileDataStoreFinder;
import org.geotools.data.Transaction;
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.data.shapefile.ShapefileDataStoreFactory;
import org.geotools.data.simple.SimpleFeatureSource;
import org.geotools.data.simple.SimpleFeatureStore;
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.DirectPosition2D;
import org.geotools.geometry.jts.JTSFactoryFinder;
import org.geotools.map.DefaultMapContext;
import org.geotools.map.MapContext;
import org.geotools.styling.FeatureTypeStyle;
import org.geotools.styling.Graphic;
import org.geotools.styling.Mark;
import org.geotools.styling.PointSymbolizer;
import org.geotools.styling.Rule;
import org.geotools.styling.Style;
import org.geotools.styling.StyleFactory;
import org.geotools.swing.data.JFileDataStoreChooser;
import org.geotools.swing.event.MapMouseEvent;
import org.geotools.swing.tool.CursorTool;
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;

public class PointTool extends CursorTool {
//	 GeoToolsShpViewer frame;
	MyGeoToolsShapeViewer frame;
//	Aufgabe3 frame;
	Cursor cursor;
	Point point;
	MapContext mapContext;

	static StyleFactory styleFactory = CommonFactoryFinder
			.getStyleFactory(null);
	static FilterFactory filterFactory = CommonFactoryFinder
			.getFilterFactory(null);
	static SimpleFeatureType type;
	
	public PointTool(MyGeoToolsShapeViewer frame) {
		this.frame = frame;
		cursor = new Cursor(Cursor.DEFAULT_CURSOR);

	}

	/**
	 * 
	 * stellt einen Cursor bereit
	 * 
	 */
	public Cursor getCursor() {
		return cursor;
	}

	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;
	}

	public void onMouseClicked(MapMouseEvent ev) {
		

		DirectPosition2D pos = ev.getMapPosition();
		System.out.println(Math.round((pos.x) * 100.0) / 100.0 + ","
				+ Math.round((pos.y) * 100.0) / 100.0);

		
		
		try {
			type = DataUtilities.createType("Location", "location:Point");
		} catch (SchemaException e) {
			System.out.println("Fehler bei type");
			e.printStackTrace();
		}
		FeatureCollection collection = FeatureCollections.newCollection();
		GeometryFactory geometryFactory = JTSFactoryFinder
				.getGeometryFactory(null);
		SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(type);
		Point point = geometryFactory.createPoint(new Coordinate(pos.x, pos.y));
		featureBuilder.add(point);
		SimpleFeature feature = featureBuilder.buildFeature(null);
		collection.add(feature);
		Style style = createPointStyle();

		frame.mapContext.addLayer(collection, style);
		frame.getMapPane().repaint();
	}
	public void onMouseMoved(MapMouseEvent ev) {
		DirectPosition2D pos = ev.getMapPosition();
		// @todo: sinnvollerweise Stellenzahl begrenzen
		frame.statusBar.setText("Position: " + Math.round(pos.x*100.)/100. + "," + Math.round(pos.y*100.)/100.);
	}
}

und die dazugehörige class

Java:
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);
	}
}
 

Neue Themen


Zurück
Oben