import com.vividsolutions.jump.workbench.plugin.AbstractPlugIn; import com.vividsolutions.jump.workbench.plugin.PlugInContext; import com.vividsolutions.jump.*; import com.vividsolutions.jump.feature.*; import com.vividsolutions.jts.geom.*; import java.util.*; import java.io.File; import javax.swing.*; import java.awt.Component; import com.vividsolutions.jump.workbench.ui.*; import com.vividsolutions.jump.io.*; import com.vividsolutions.jump.workbench.model.*; import com.vividsolutions.jts.geom.GeometryFactory.*; public class BufferBatch extends AbstractPlugIn { //private Layer itemLayer = null; public void initialize(PlugInContext context){ context.getFeatureInstaller().addMainMenuItem(this, new String[] { "Cviko", "MBR" }, "batch", false, null, null); } public boolean execute(PlugInContext context) throws Exception { //String fname =this.nahraj(context); File fname =this.nahraj(context); this.cyklus(fname); return true; } private void cyklus (File fname){ File[] files = fname.listFiles(); for (File f : files) { if ( f.getName().toLowerCase().endsWith(".shp")) { //MultiInputDialog dialog = new MultiInputDialog(context.getWorkbenchFrame(),getName(),true); //dialog.addLabel(f.getAbsolutePath()); //GUIUtil.centreOnWindow(dialog); //dialog.setVisible(true); // try { //List features = fc.getFeatures(); File target = new File(fname, "mbr"+f.getName()); this.shp(f, target); //context.addLayer(StandardCategoryNames.RESULT, "myLayerName" , bbox ); // } catch(Exception e) { // } } } } private void shp (File f, File target){ try{ ShapefileReader reader = new ShapefileReader(); FeatureCollection fc = reader.read(new DriverProperties(f.getAbsolutePath())); FeatureCollection bbox = this.mbr(fc); ShapefileWriter writer = new ShapefileWriter(); writer.write(bbox, new DriverProperties(target.getAbsolutePath())); } catch(Exception e) { } } private File nahraj(PlugInContext context) throws Exception { File fname = new File(""); JFileChooser chooser = new JFileChooser(); chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); chooser.setDialogTitle("Vyber soubor"); Component parent = context.getWorkbenchFrame(); int returnVal = chooser.showOpenDialog(parent); if(returnVal == JFileChooser.APPROVE_OPTION) { //fname = chooser.getSelectedFile().getPath(); fname = chooser.getSelectedFile(); } return fname; } public FeatureCollection mbr (FeatureCollection fc){ Envelope env = fc.getEnvelope(); double maxX = env.getMaxX(); double maxY = env.getMaxY(); double minX = env.getMinX(); double minY = env.getMinY(); //String souradnice = "min X: " + minX + " min Y: " + minY + "\n max X: " + maxX + " max Y: " + maxY; Coordinate [] coordinates = new Coordinate [5]; coordinates [0] = new Coordinate (minX,minY); coordinates [1] = new Coordinate (maxX,minY); coordinates [2] = new Coordinate (maxX,maxY); coordinates [3] = new Coordinate (minX,maxY); coordinates [4] = new Coordinate (minX, minY); GeometryFactory factory = new GeometryFactory(); LinearRing linie = factory.createLinearRing(coordinates); Polygon MBR = factory.createPolygon(linie,null); ArrayList polygon; polygon = new ArrayList(); polygon.add(MBR); FeatureCollection bbox = FeatureDatasetFactory.createFromGeometry(polygon); return bbox; } }