kijato

OpenJump, Beanshell - loadCustomDataFromFile

May 22nd, 2020
427
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.65 KB | None | 0 0
  1. /*
  2. [http://ojwiki.soldin.de/index.php?title=How_to_display_customer_xyz_data_from_a_file]
  3.  
  4. How to display customer xyz data from a file
  5.  
  6. Question:
  7.  
  8. Hello,
  9.  
  10. I'm a new openjump user, and i'm now trying to make a GIS viewer tool. Some layers have been read from typical shp GIS files and is shown good. How can i draw my own data item on the map according to the coordinate that I already constructed from the shp file? (the data is not from GIS files)
  11.  
  12. thanks for your help!
  13.  
  14. Stefan's answer:
  15.  
  16.     first you need to know the geometry type (Point,Line,Polygon), that you want to create.
  17.     Then you transform your data to Point/Line/Polygon from the JTS Geometry Library. JTS has therefore a GeometryFactory which provides several methods to build geometries. (note: making Polygons needs to create LinearRing before that)
  18.     After you need to create a Feature from the Geometry. If you dont't need attributes it is very simply using FeatureDatasetFactory. If you like to have Features with attributes you must first a) create the FeatureSchema, b) then create a BasicFeature using that schema, c) then set the geometry for that BasicFeature, d) then set the attributes, e) create a FeatureDataset a subclass of FeatureCollection (see next step) and add every BasicFeature to that collection
  19.     if you have the features, then create a feature collection from it
  20.  
  21. (all features in a collection need to have the same attributes = FeatureSchema)
  22.  
  23.     display the FeatureCollection
  24.  
  25. i attach some code which reads, x y z coordinates from a file (note the file reader is in an external class: jmath - part of openjump) and creates a point layer without additional attributes.
  26.  
  27. Note: the code below uses an additional library called JMathTools. This library has been refactored/renamed in 2007 to JMathIO and unfortunately the MatlabSyntax class does not exist anymore.
  28. */
  29.  
  30. package ch.unizh.geo.degen.christian;
  31.  
  32. import java.awt.Component;
  33. import java.util.ArrayList;
  34. import java.util.Iterator;
  35.  
  36. import javax.swing.JComboBox;
  37. import javax.swing.JFileChooser;
  38.  
  39. import org.jmat.MatlabSyntax;
  40. import org.jmat.data.Matrix;
  41.  
  42. import com.vividsolutions.jts.geom.Coordinate;
  43. import com.vividsolutions.jts.geom.Geometry;
  44. import com.vividsolutions.jts.geom.GeometryFactory;
  45. import com.vividsolutions.jts.geom.Point;
  46. import com.vividsolutions.jump.feature.AttributeType;
  47. import com.vividsolutions.jump.feature.Feature;
  48. import com.vividsolutions.jump.feature.FeatureCollection;
  49. import com.vividsolutions.jump.feature.FeatureDataset;
  50. import com.vividsolutions.jump.feature.FeatureDatasetFactory;
  51. import com.vividsolutions.jump.feature.FeatureSchema;
  52. import com.vividsolutions.jump.feature.FeatureUtil;
  53. import com.vividsolutions.jump.task.TaskMonitor;
  54. import com.vividsolutions.jump.workbench.WorkbenchContext;
  55. import com.vividsolutions.jump.workbench.model.StandardCategoryNames;
  56. import com.vividsolutions.jump.workbench.plugin.EnableCheckFactory;
  57. import com.vividsolutions.jump.workbench.plugin.MultiEnableCheck;
  58. import com.vividsolutions.jump.workbench.plugin.PlugInContext;
  59. import com.vividsolutions.jump.workbench.plugin.ThreadedBasePlugIn;
  60. import com.vividsolutions.jump.workbench.ui.GUIUtil;
  61. import com.vividsolutions.jump.workbench.ui.MultiInputDialog;
  62. import com.vividsolutions.jump.workbench.ui.plugin.FeatureInstaller;
  63.  
  64. /**
  65.  * @author sstein
  66.  *
  67.  *
  68.  */
  69. public class LoadxyzDataPlugIn extends ThreadedBasePlugIn{
  70.    // is faster if it is not threaded .. but
  71.    // looks better since a dialog is shown and no graphic errors appear
  72.    private static String LAST_FORMAT_KEY = LoadxyzDataPlugIn.class.getName() +
  73.                                 " - LAST FORMAT";
  74.      
  75.    private Matrix pointMat = null;
  76.    private MultiInputDialog dialog;
  77.    
  78.        
  79.    public void initialize(PlugInContext context) throws Exception {
  80.         FeatureInstaller featureInstaller = new FeatureInstaller(context.getWorkbenchContext());
  81.         featureInstaller.addMainMenuItem(
  82.                 this,                               //exe
  83.                 new String[] {"Generalisation", "loadxyz"},     //menu path
  84.                 this.getName(), //name methode .getName recieved by AbstractPlugIn
  85.                 false,          //checkbox
  86.                 null,           //icon
  87.                 createEnableCheck(context.getWorkbenchContext())); //enable check        
  88.     }
  89.    
  90.    public boolean execute(PlugInContext context) throws Exception{
  91.         this.pointMat= this.loadAsciiDataMatrix(context);
  92.         return true;
  93.         }
  94.  
  95.    /**
  96.    * this function is called after execute
  97.    * Action on menu item selection:
  98.    */
  99.    public void run(TaskMonitor monitor, PlugInContext myContext) throws Exception{
  100.         System.gc();
  101.         FeatureCollection fc = this.makeNewDataset(this.pointMat, monitor);
  102.         this.showDataset(myContext,fc);
  103.     }
  104.    
  105.    
  106.    private Matrix loadAsciiDataMatrix(PlugInContext myContext) throws Exception{
  107.             String filename = this.getFilename(myContext);    
  108.             Matrix inMat = MatlabSyntax.load(filename);
  109.             return inMat;
  110.     }
  111.  
  112.    private FeatureCollection makeNewDataset(Matrix inMat, TaskMonitor monitor){    
  113.         ArrayList geoms = new ArrayList();
  114.         int nopoints = inMat.getRowDimension();
  115.         //nopoints = 1000;
  116.         for (int i = 0; i < nopoints; i++) {
  117.             double x = inMat.get(i,0);
  118.             double y = inMat.get(i,1);
  119.             double z = inMat.get(i,2);
  120.             Coordinate p = new Coordinate(x,y,z);
  121.             Point pt = new GeometryFactory().createPoint(p);
  122.             geoms.add(pt);
  123.             }
  124.         FeatureCollection points = FeatureDatasetFactory.createFromGeometry(geoms);
  125.         return points;     
  126.     }
  127.    
  128.  
  129.     private void showDataset(PlugInContext myContext, FeatureCollection myFC){
  130.             myContext.addLayer(StandardCategoryNames.WORKING, "pointsxy", myFC);
  131.     }
  132.    
  133.  
  134.     public String getFilename(PlugInContext context) throws Exception {
  135.         String fname = "";        
  136.         JFileChooser chooser = new JFileChooser();
  137.         chooser.setDialogTitle("load point ascii matrix ");
  138.         Component parent = context.getWorkbenchFrame();
  139.         int returnVal = chooser.showOpenDialog(parent);      
  140.         if(returnVal == JFileChooser.APPROVE_OPTION) {
  141.                 fname = chooser.getSelectedFile().getAbsolutePath();
  142.         }                
  143.         return fname;
  144.     }
  145.  
  146.     public static MultiEnableCheck createEnableCheck(WorkbenchContext workbenchContext) {
  147.         EnableCheckFactory checkFactory = new EnableCheckFactory(workbenchContext);
  148.  
  149.         return new MultiEnableCheck()
  150.         .add(checkFactory.createWindowWithLayerNamePanelMustBeActiveCheck())
  151.         .add(checkFactory.createAtLeastNLayersMustBeSelectedCheck(0));
  152.     }
  153.    
  154. }//end class
Add Comment
Please, Sign In to add comment