here it is,
but I just recognize that it writes a new layer - I am not that sure if that is wished

stefan

Sunburned Surveyor wrote:
Stefan,

Can I get the source code for your plug-in. I think it will be rather
simple to modify it to add a UUID as an attribute to each feature in a
layer.

Thanks,

SS

/*****************************************************
 * created:  		07.06.2005
 * last modified:  	21.11.2005
 * 					24.11.2005 feature cloning added
 * @author sstein
 * 
 * description:
 * 
 *****************************************************/

package ch.unizh.geo.degen.settlementtypes;


import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

import javax.swing.JComboBox;

import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jump.feature.AttributeType;
import com.vividsolutions.jump.feature.BasicFeature;
import com.vividsolutions.jump.feature.Feature;
import com.vividsolutions.jump.feature.FeatureCollection;
import com.vividsolutions.jump.feature.FeatureDataset;
import com.vividsolutions.jump.feature.FeatureSchema;
import com.vividsolutions.jump.task.TaskMonitor;
import com.vividsolutions.jump.workbench.WorkbenchContext;
import com.vividsolutions.jump.workbench.model.Layer;
import com.vividsolutions.jump.workbench.model.StandardCategoryNames;
import com.vividsolutions.jump.workbench.plugin.AbstractPlugIn;
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.ThreadedPlugIn;
import com.vividsolutions.jump.workbench.ui.GUIUtil;
import com.vividsolutions.jump.workbench.ui.MultiInputDialog;
import com.vividsolutions.jump.workbench.ui.plugin.FeatureInstaller;
import com.vividsolutions.jump.workbench.ui.zoom.ZoomToSelectedItemsPlugIn;

/**
 * @description:
 * 		simplifies the outline of a builing
 *      a new layer with the result is created, with an additional Attribute
 * 					
 * @author sstein
 *
 **/
public class AddIDPlugIn extends AbstractPlugIn implements ThreadedPlugIn{

    private ZoomToSelectedItemsPlugIn myZoom = new ZoomToSelectedItemsPlugIn();
    private final String newAttributString = "BuildID";
    private final String LAYEROBJECTS = "Layer Objects";
    private MultiInputDialog dialog;
    private FeatureCollection lobjects = null;

    public void initialize(PlugInContext context) throws Exception {
        FeatureInstaller featureInstaller = new FeatureInstaller(context.getWorkbenchContext());
    	featureInstaller.addMainMenuItem(
    	        this,								//exe
                new String[] {"Generalisation", "Settlement"}, 	//menu path
                this.getName(), //name methode .getName recieved by AbstractPlugIn 
                false,			//checkbox
                null,			//icon
                createEnableCheck(context.getWorkbenchContext())); //enable check        
    }
    
    public static MultiEnableCheck createEnableCheck(WorkbenchContext workbenchContext) {
        EnableCheckFactory checkFactory = new EnableCheckFactory(workbenchContext);

        return new MultiEnableCheck()
                        .add(checkFactory.createWindowWithLayerNamePanelMustBeActiveCheck());
    }
    
	public boolean execute(PlugInContext context) throws Exception{
        //Unlike ValidatePlugIn, here we always call #initDialog because we want
        //to update the layer comboboxes.
        initDialog(context);
        dialog.setVisible(true);
        if (!dialog.wasOKPressed()) {
            return false;
        }
        else{            
        	this.lobjects = dialog.getLayer(this.LAYEROBJECTS).getFeatureCollectionWrapper();    	
        }
        return true;	  
	}
	
    private void initDialog(PlugInContext context) {
	    	
	        dialog = new MultiInputDialog(context.getWorkbenchFrame(), "add object ids", true);
	        //dialog.setSideBarImage(IconLoader.icon("Overlay.gif"));
	        dialog.setSideBarDescription(
	            "load layer objects and add a new field with a unique id per layer");
	        try {
	        	JComboBox addLayerComboBoxBuild = dialog.addLayerComboBox(this.LAYEROBJECTS, context.getCandidateLayer(0), null, context.getLayerManager());
	        }
	        catch (IndexOutOfBoundsException e) {}
		    //dialog.addDoubleField(T1, 20.0, 4);
	        GUIUtil.centreOnWindow(dialog);
	    }	

    public void run(TaskMonitor monitor, PlugInContext context) throws Exception{
        
    	    //this.zoom2Feature(context);	    
    	    FeatureCollection fc = this.addID(context, monitor);
    	    context.addLayer(StandardCategoryNames.WORKING, "objects with id", fc);
    	    System.gc();    		
    	}
	
	/**
	 * centers the selected feature
	 * @param context
	 * @throws Exception
	 */
	private void zoom2Feature(PlugInContext context) throws Exception{
		    
	    this.myZoom.execute(context);	    
	}
	
	protected Layer layer(PlugInContext context) {
		return (Layer) context.getLayerViewPanel().getSelectionManager()
				.getLayersWithSelectedItems().iterator().next();
	}
	

	private FeatureCollection addID(PlugInContext context, TaskMonitor monitor) throws Exception{
	    
	    System.gc(); //flush garbage collector
	    // --------------------------	    
	    //-- get selected items
	    final Collection features = this.lobjects.getFeatures();
	        
	    int count=0; int noItems = features.size(); Geometry resultgeom = null;	    
	    //FeatureDataset problematicFeatures = null;
	    FeatureDataset resultFeatures = null;
	    ArrayList problematicEdges = new ArrayList();
       	//List resultList = new ArrayList();
       	FeatureSchema fs = new FeatureSchema();
	    //--get single object in selection to analyse
      	for (Iterator iter = features.iterator(); iter.hasNext();) {
      		count++;
      		Feature ft = (Feature)iter.next();
      		//[sstein - 23.11.2005] line added to avoid that original features get changed
      		Feature f= (Feature)ft.clone();      		      		
      		//-- set schema by using the first selected item
      		//check if attribute name exists
      		boolean attributeExists = false;
      		if (count == 1){      			
      			//-- not sure to do that, since feature schemas of selected objects might be different 
      			fs = copyFeatureSchema(f.getSchema());
       			attributeExists = fs.hasAttribute(this.newAttributString);          			
       			if (attributeExists == false){	      		      			
	      			fs.addAttribute(this.newAttributString, AttributeType.INTEGER);
	      			//problematicFeatures = new FeatureDataset(fs);
       			}
      			resultFeatures = new FeatureDataset(fs);
      		}
      		//--create new Feature with one new attribute and copy attributvalues
      		Feature fnew = new BasicFeature(fs);
      		Object[] attribs = f.getAttributes();
      		if (attributeExists == false){
	      		Object[] attribsnew = new Object[attribs.length+1];
	      		for (int i = 0; i < attribs.length; i++) {
					attribsnew[i] = attribs[i];
				}
	      		attribsnew[attribs.length]= "init";
	      		fnew.setAttributes(attribsnew);
	      	}
      		else{
      			fnew.setAttributes(attribs);
      		}
            fnew.setAttribute(this.newAttributString, new Integer(count));
       	    resultFeatures.add(fnew);
		    String mytext = "item: " + count + " / " + noItems;
		    monitor.report(mytext);	       	
      	}//  end loop over layer items
	    //context.addLayer(StandardCategoryNames.WORKING, "simplified buildings", resultFeatures);	    
        return resultFeatures;        
	}
    
	private FeatureSchema copyFeatureSchema(FeatureSchema oldSchema){
		FeatureSchema fs = new FeatureSchema();
		for (int i = 0; i < oldSchema.getAttributeCount(); i++) {
			AttributeType at = oldSchema.getAttributeType(i);
			String aname = oldSchema.getAttributeName(i);
			fs.addAttribute(aname,at);
			fs.setCoordinateSystem(oldSchema.getCoordinateSystem());
		}		
		return fs;
	}
  
}
_______________________________________________
jump-users mailing list
[email protected]
http://lists.refractions.net/mailman/listinfo/jump-users

Reply via email to