Advertisement
kijato

OpenJump, BeanShell, MakeNewLayerFromDAT

Jun 3rd, 2019
415
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.60 KB | None | 0 0
  1. /*
  2.     OpenJump, BeanShell, MakeNewLayerFromDAT
  3. */
  4. public class Point {
  5.     public double y = 0;
  6.     public double x = 0;
  7.     public double m = 0;
  8.     public void Point(double y, double x/*, double m*/) throws IOException {
  9.         this.y=y;
  10.         this.x=x;
  11.         /*m.isNull() ? this.m=0 : this.m=m;*/
  12.     }
  13. }
  14.  
  15. public class DAT {
  16.   public String filename = "";
  17.   public String header = "";
  18.   public HashMap tables = new HashMap();
  19.   public ArrayList wholeFile = new ArrayList();
  20.   public HashMap points = new HashMap();
  21.    
  22.   public void DAT(String filename) throws IOException {
  23.       this.filename = filename;
  24.   }
  25.  
  26.   public void openToHashMap() throws IOException {
  27.       try {
  28.         // https://techknowstudy.wordpress.com/2015/03/10/reading-a-file-through-bean-shell/
  29.         fileReader=new FileReader(filename);
  30.         bufferReader= new BufferedReader(fileReader);
  31.         linenumber=0;
  32.         while( (line=bufferReader.readLine()) != null ) {
  33.             //if(line.equals("")) { continue; }
  34.             line  = line.trim().replaceAll("\\*$","");
  35.             if ( ++linenumber == 1 ) {
  36.                header = line;
  37.                continue;
  38.             }
  39.             if(line.startsWith("T_")) {
  40.                 table = line;
  41.                 tables.put(table,new ArrayList());
  42.                 continue;
  43.             } else {/*
  44.                 String[] words = line.split("\\*");
  45.                 for(i=0;i<words.length;i++){
  46.                     print(words[i]+"\t");
  47.                 }*/
  48.                 tables{table}.add(line);
  49.             }
  50.         };
  51.         fileReader.close();
  52.         bufferReader.close();
  53.         for ( key : this.tables.keySet() ){ this.tables{key}.trimToSize(); }
  54.     } catch (IOException e) {
  55.         e.printStackTrace();
  56.     }
  57.  
  58.   }
  59.  
  60.   public void openToArrayList() throws IOException {
  61.     try {
  62.         fileReader=new FileReader(filename);
  63.         bufferReader= new BufferedReader(fileReader);
  64.         linenumber=0;
  65.         while( (line=bufferReader.readLine()) != null ) {
  66.             line  = line.trim().replaceAll("\\*$","");
  67.             if ( ++linenumber == 1 ) {
  68.                header = line;
  69.                continue;
  70.             }
  71.             wholeFile.add(line);
  72.         };
  73.         fileReader.close();
  74.         bufferReader.close();
  75.         wholeFile.trimToSize();
  76.     } catch (IOException e) {
  77.         e.printStackTrace();
  78.     }
  79.   }
  80.  
  81.   public void setPoints() {
  82.     for ( row : this.tables{"T_PONT"} ){
  83.         cols = row.split("\\*",-1);
  84.         points.put(cols[0],new Point(Double.parseDouble(cols[1]),Double.parseDouble(cols[2])/*,Double.parseDouble(cols[3])*/));
  85.     }
  86.   }
  87.  
  88. } // end of DAT class.
  89. /*
  90. {
  91.  
  92.     dat = new DAT("q:\\OpenJUMP\\beanshell\\dat_ck_proba.dat");
  93.     print ("Fájlnév: "+dat.filename );
  94.     elotte=Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
  95.     dat.openToHashMap();
  96.     //dat.openToArrayList();
  97.     utanna=Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
  98.     print (elotte/1024/1024+" "+utanna/1024/1024+" : "+(utanna-elotte)/1024/1024+" [Mb]");
  99.  
  100.     print ( dat.header );
  101.     for ( t : dat.tables.keySet() ) {
  102.         print(t+": "+dat.tables{t}.size());
  103.     }
  104.  
  105.     dat.setPoints();
  106.     for ( key : dat.points.keySet() ) {
  107.         print(key+": "+dat.points{key}.y+" "+dat.points{key}.x);
  108.     }
  109.  
  110. }*/
  111.  
  112. {
  113.  
  114. dat = new DAT("q:\\OpenJUMP\\beanshell\\dat_tug.dat");
  115. dat.openToHashMap();
  116. dat.setPoints();
  117.  
  118.   Random rand = new Random();
  119.  
  120.   randomString(int length) {
  121.       StringBuffer sb = new StringBuffer();
  122.       for (int i = 0 ; i < length ; i++) sb.append((char)(64 + 27.0*Math.random()));
  123.       return sb.toString();
  124.   }
  125.  
  126.  
  127.   schema = new FeatureSchema();
  128.   schema.addAttribute("id",AttributeType.BIGINT);
  129.   schema.addAttribute("name",AttributeType.STRING);
  130.   schema.addAttribute("geometry",AttributeType.GEOMETRY);
  131.   print("Attributes of schema:");
  132.   for ( name : schema.attributeNames ) {
  133.     print("  - "+name+" ["+schema.getAttributeType(name)+"]");
  134.   }
  135.  
  136.   dataset = new FeatureDataset(schema);
  137.    for ( key : dat.points.keySet() ) {
  138.     coord = new Coordinate(dat.points{key}.x,dat.points{key}.y
  139. );
  140.     geom = new GeometryFactory().createPoint(coord);
  141.     geom.setSRID(23700);
  142.     bf = new BasicFeature(schema);
  143.     bf.setGeometry(geom);
  144.     //bf.setUserData("id", rand.nextInt(100)); // don't work...
  145.     //bf.setUserData("name", randomString(5)); // don't work...
  146.     bf.setAttribute("id", key);
  147.     dataset.add(bf);
  148.   }
  149.   print("Dataset size: "+dataset.size());
  150.   for ( f : dataset.getFeatures() ) {
  151.     print("  - "+f.getGeometry()+"\tid="+f.getAttribute(0)+"\tname="+f.getAttribute(1));
  152.   }
  153.   wc.layerManager.addLayer("DEMO","one point",dataset);
  154.   print("˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙");
  155.  
  156.  
  157.   FeatureCollection points = FeatureDatasetFactory.createFromGeometry(geoms,schema);   
  158.   print("dataset size: "+points.size()+" [...]");
  159.   wc.layerManager.addLayer("DEMO","more points",points);
  160.   print("˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙");
  161.  
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement