我加载一个现有的shp文件,然后通过单击“点”按钮然后在屏幕上单击来添加点,然后将被单击的点保存到新图层,然后将该图层添加到MapContent。接下来,我要导出具有两层的shapefile 我想将所有图层保存到一个新的shapefile中
package org.geotools;
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.JButton;
import javax.swing.JFileChooser;
import javax.swing.JToolBar;
import javax.swing.filechooser.FileNameExtensionFilter;
import org.geotools.data.FileDataStore;
import org.geotools.data.FileDataStoreFinder;
import org.geotools.data.simple.SimpleFeatureSource;
import org.geotools.feature.DefaultFeatureCollection;
import org.geotools.feature.simple.SimpleFeatureBuilder;
import org.geotools.feature.simple.SimpleFeatureTypeBuilder;
import org.geotools.geometry.DirectPosition2D;
import org.geotools.geometry.jts.JTSFactoryFinder;
import org.geotools.map.FeatureLayer;
import org.geotools.map.Layer;
import org.geotools.map.MapContent;
import org.geotools.referencing.crs.DefaultGeographicCRS;
import org.geotools.styling.SLD;
import org.geotools.styling.Style;
import org.geotools.swing.JMapFrame;
import org.geotools.swing.event.MapMouseEvent;
import org.geotools.swing.tool.CursorTool;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.GeometryFactory;
import org.locationtech.jts.geom.MultiPoint;
import org.locationtech.jts.geom.Point;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
//Class MapUI
public class MapUI {
private static File sourceFile;
private SimpleFeatureSource featureSource;
private static MapContent map;
private static JMapFrame mapFrame;
private static JButton btnDrawPoint;
private static JButton btnExport;
/*
* method
*/
public void addControls() throws IOException {
JFileChooser jfc = new JFileChooser("./shapeFile/");
jfc.setDialogTitle("Select an file shp");
jfc.setAcceptAllFileFilterUsed(false);
FileNameExtensionFilter filter = new FileNameExtensionFilter("shp file", "shp");
jfc.addChoosableFileFilter(filter);
int returnValue = jfc.showOpenDialog(null);
if (returnValue == JFileChooser.APPROVE_OPTION) {
sourceFile = jfc.getSelectedFile();
} else {
return;
}
FileDataStore store = FileDataStoreFinder.getDataStore(sourceFile);
featureSource = store.getFeatureSource();
System.out.println(featureSource);
// Create a map context and add our shapefile to it
map = new MapContent();
map.setTitle("Quickstart");
Style style = SLD.createSimpleStyle(featureSource.getSchema());
Layer layer = new FeatureLayer(featureSource, style);
layer.setTitle("Layer 1");
map.layers().add(layer);
mapFrame = new JMapFrame(map);
mapFrame.enableToolBar(true);
mapFrame.enableStatusBar(true);
JToolBar toolBar = mapFrame.getToolBar();
btnDrawPoint = new JButton("Point");
toolBar.add(btnDrawPoint);
btnExport = new JButton("export");
toolBar.add(btnExport);
}
// Method add event click mouse
public void addEvent() {
/*
* when clicking the button will allow adding points
* on the map by clicking on the map location
*/
btnDrawPoint.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
mapFrame.getMapPane().setCursorTool(new CursorTool() {
@Override
public void onMouseClicked(MapMouseEvent ev) {
drawPoint(ev);
}
});
}
});
/*
* button export file
*/
btnExport.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
exportFile();
}
});
}
/*
* method show map
*/
public void showWindow() {
// mapFrame.showMap(map);
mapFrame.setSize(800, 600);
mapFrame.setVisible(true);
}
//Method draw point
private void drawPoint(MapMouseEvent ev) {
DirectPosition2D pos = ev.getMapPosition();
double x = pos.x;
double y = pos.y;
SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder();
builder.setName("MyFeatureType");
builder.setCRS(DefaultGeographicCRS.WGS84); // set crs
builder.add("location", MultiPoint.class); // add geometry
// build the type
SimpleFeatureType TYPE = builder.buildFeatureType();
// create features using the type defined
SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(TYPE);
GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory(null);
Coordinate coord = new Coordinate(x, y);
Point point = geometryFactory.createPoint(coord);
DefaultFeatureCollection featureCollection = new DefaultFeatureCollection();
featureBuilder.add(point);
SimpleFeature feature = featureBuilder.buildFeature("FeaturePoint" + point.getNumPoints());
featureCollection.add(feature); // Add feature 1, 2, 3, etc
Style style = SLD.createPointStyle("circle", Color.BLUE, Color.BLUE, 0.3f, 15);
Layer layer = new FeatureLayer(featureCollection, style);
layer.setTitle("Click Point");
map.layers().add(layer);
}
我要在这里导出文件
private void exportFile() {
// I don't know how to write here
}
public static void main(String[] args) throws IOException {
MapUI mapUI = new MapUI();
mapUI.addControls();
mapUI.addEvent();
mapUI.showWindow();
}
}