How to display customer xyz data from a file

From OpenJUMP Wiki
Jump to navigation Jump to search

Question:

Hello,

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)

thanks for your help!

Stefan's answer:

  1. first you need to know the geometry type (Point,Line,Polygon), that you want to create.
  2. 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)
  3. 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
  4. if you have the features, then create a feature collection from it

(all features in a collection need to have the same attributes = FeatureSchema)

  1. display the FeatureCollection

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.

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.


package ch.unizh.geo.degen.christian;

import java.awt.Component;
import java.util.ArrayList;
import java.util.Iterator;

import javax.swing.JComboBox;
import javax.swing.JFileChooser;

import org.jmat.MatlabSyntax;
import org.jmat.data.Matrix;

import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.Point;
import com.vividsolutions.jump.feature.AttributeType;
import com.vividsolutions.jump.feature.Feature;
import com.vividsolutions.jump.feature.FeatureCollection;
import com.vividsolutions.jump.feature.FeatureDataset;
import com.vividsolutions.jump.feature.FeatureDatasetFactory;
import com.vividsolutions.jump.feature.FeatureSchema;
import com.vividsolutions.jump.feature.FeatureUtil;
import com.vividsolutions.jump.task.TaskMonitor;
import com.vividsolutions.jump.workbench.WorkbenchContext;
import com.vividsolutions.jump.workbench.model.StandardCategoryNames;
import com.vividsolutions.jump.workbench.plugin.EnableCheckFactory;
import com.vividsolutions.jump.workbench.plugin.MultiEnableCheck;
import com.vividsolutions.jump.workbench.plugin.PlugInContext;
import com.vividsolutions.jump.workbench.plugin.ThreadedBasePlugIn;
import com.vividsolutions.jump.workbench.ui.GUIUtil;
import com.vividsolutions.jump.workbench.ui.MultiInputDialog;
import com.vividsolutions.jump.workbench.ui.plugin.FeatureInstaller;

/**
 * @author sstein
 *
 *
 */
public class LoadxyzDataPlugIn extends ThreadedBasePlugIn{
   // is faster if it is not threaded .. but 
   // looks better since a dialog is shown and no graphic errors appear
   private static String LAST_FORMAT_KEY = LoadxyzDataPlugIn.class.getName() +
    							" - LAST FORMAT";
     
   private Matrix pointMat = null;
   private MultiInputDialog dialog;
    
       
   public void initialize(PlugInContext context) throws Exception {
        FeatureInstaller featureInstaller = new FeatureInstaller(context.getWorkbenchContext());
    	featureInstaller.addMainMenuItem(
    	        this,								//exe
                new String[] {"Generalisation", "loadxyz"}, 	//menu path
                this.getName(), //name methode .getName recieved by AbstractPlugIn 
                false,			//checkbox
                null,			//icon
                createEnableCheck(context.getWorkbenchContext())); //enable check        
    }
   
   public boolean execute(PlugInContext context) throws Exception{
	    this.pointMat= this.loadAsciiDataMatrix(context);
        return true;
        }

   /** 
   * this function is called after execute
   * Action on menu item selection: 
   */
   public void run(TaskMonitor monitor, PlugInContext myContext) throws Exception{
	    System.gc();
	    FeatureCollection fc = this.makeNewDataset(this.pointMat, monitor);
	    this.showDataset(myContext,fc);
	}
	
	
   private Matrix loadAsciiDataMatrix(PlugInContext myContext) throws Exception{
            String filename = this.getFilename(myContext);    
            Matrix inMat = MatlabSyntax.load(filename);
            return inMat;
	}

   private FeatureCollection makeNewDataset(Matrix inMat, TaskMonitor monitor){	    
	    ArrayList geoms = new ArrayList();
	    int nopoints = inMat.getRowDimension();
	    //nopoints = 1000;
	    for (int i = 0; i < nopoints; i++) {
	        double x = inMat.get(i,0);
	        double y = inMat.get(i,1);
	        double z = inMat.get(i,2);
            Coordinate p = new Coordinate(x,y,z);
            Point pt = new GeometryFactory().createPoint(p);
            geoms.add(pt);
            }
	    FeatureCollection points = FeatureDatasetFactory.createFromGeometry(geoms);
	    return points;	    
	}
	

    private void showDataset(PlugInContext myContext, FeatureCollection myFC){
            myContext.addLayer(StandardCategoryNames.WORKING, "pointsxy", myFC);
	}
	

    public String getFilename(PlugInContext context) throws Exception {
        String fname = "";        
        JFileChooser chooser = new JFileChooser();
        chooser.setDialogTitle("load point ascii matrix ");
        Component parent = context.getWorkbenchFrame();
        int returnVal = chooser.showOpenDialog(parent);      
        if(returnVal == JFileChooser.APPROVE_OPTION) {
                fname = chooser.getSelectedFile().getAbsolutePath();
        }                 
        return fname;
    }

    public static MultiEnableCheck createEnableCheck(WorkbenchContext workbenchContext) {
        EnableCheckFactory checkFactory = new EnableCheckFactory(workbenchContext);

        return new MultiEnableCheck()
        .add(checkFactory.createWindowWithLayerNamePanelMustBeActiveCheck())
        .add(checkFactory.createAtLeastNLayersMustBeSelectedCheck(0));
    }
    
}//end class