Advertisement
kijato

OpenJump, Beanshell - setGeometryFromYX

May 21st, 2020
1,422
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.65 KB | None | 0 0
  1. /////////////////////////////////////////////////////
  2. // This script adds a new layer from a selected one//
  3. // The new layer has two new attributes            //
  4. // containing X and Y of a coordinate              //
  5. // Michael Michaud 2009-03-02                      //
  6. /////////////////////////////////////////////////////
  7.  
  8. //Begin Import libraries
  9.  
  10. import com.vividsolutions.jump.feature.*;
  11. import com.vividsolutions.jump.workbench.model.StandardCategoryNames;
  12. import com.vividsolutions.jump.workbench.ui.MultiInputDialog;
  13. //End Import Libraries
  14.  
  15. //Begin Dialog
  16. dialog = new MultiInputDialog(wc.workbench.frame, "", true);
  17. dialog.addLabel("<html>This script creates a  new layer, from a selected point layer.<br/>The new layer has two new attributes containing coordinates X and Y of the points</html>");
  18. //End Dialog
  19.  
  20. //Begin Choices YES/NO
  21. dialog.setVisible(true);
  22. if (!dialog.wasOKPressed()) return;
  23. //End Choices YES/NO
  24.  
  25. //Begin script
  26. ll = wc.layerNamePanel.selectedLayers;
  27. if (ll.length != 1) wc.workbench.frame.warnUser("Exactly one layer must be selected");
  28. else {
  29. fc = ll[0].featureCollectionWrapper;
  30. fs = fc.featureSchema.clone();
  31. fs.addAttribute("X", AttributeType.DOUBLE);
  32. fs.addAttribute("Y", AttributeType.DOUBLE);
  33. resultFC = new FeatureDataset(fs);
  34. for (f : fc.features) {
  35.    nf = new BasicFeature(fs);
  36.    for (int i = 0 ; i < fs.attributeCount-2 ; i++){
  37.        nf.setAttribute(i, f.getAttribute(i));
  38.    }
  39.    nf.setAttribute("X", f.geometry.coordinate.x);
  40.    nf.setAttribute("Y", f.geometry.coordinate.y);
  41.    resultFC.add(nf);
  42. }
  43. wc.layerManager.addLayer(StandardCategoryNames.RESULT, ll[0].name+"_XY", resultFC);
  44. }
  45.  
  46. //End script
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement