Beanshell:Converting JML files to shapefiles

From OpenJUMP Wiki
Jump to navigation Jump to search

Batch to convert JML files into shapefiles.

It is recommended that you load BeanshellEditor to be able to edit this script and change directory names with ease.

You can load it from Beanshell-Editor-JUMP Plugin (bsheditor4jump-0.2.jar).

It will add a plugin in the Customize menu.

Then, copy paste this piece of code in the editor and change the source and target directory names.

////////////////////////////////////////////////////////////
//
// BATCH TO CONVERT JML FILES TO SHP
//
// ©Michaël Michaud 2011-04-29
////////////////////////////////////////////////////////////

import com.vividsolutions.jump.io.*;
import java.io.File;

// Directory to read JML files from
SRC_DIR = new File("C:/Temp/test");
// Directory to write shapefile to
TGT_DIR = new File("C:/Temp/test2");

// Method to convert a single jml file to the target dir
convert(File srcFile, File targetDir) {
    print(srcFile.path);
    try {
        reader = new JMLReader();
        features = reader.read(new DriverProperties(srcFile.path));
        writer = new ShapefileWriter();
        target = new File(targetDir, srcFile.name.replace(".jml",".shp"));
        writer.write(features, new DriverProperties(target.path));        
    } catch(Exception e) {
        print(e.getStackTrace());
    }
}

// Method to convert recursively a full directory
convertAll(File srcDir, File targetDir) {
    files = srcDir.listFiles();
    for (f : files) {
        if (f.isDirectory()) checkdir(f);
        else if (f.getName().toLowerCase().endsWith(".jml")) convert(f, targetDir);
        else;
    }
}

// calling convertAll method and apply it to your directory
convertAll(SRC_DIR, TGT_DIR);

This plugin can be improved in several ways :

  • include a small user interface to ask the user for the source and the target directory names
  • let the user choose source format and target format
  • ...

If the script does not need to enter parameters by hand any more, it can be included in the beantools and called directly from the beantool menu