Advertisement
kijato

OpenJump, BeanShell, MakeNewLayerFromDAT (teszt verzió)

Jan 30th, 2020
438
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.37 KB | None | 0 0
  1. /*
  2.     OpenJump, BeanShell, MakeNewLayerFromDAT (teszt verzió)
  3. */
  4.  
  5. /*
  6.     public HashMap<Integer, LinkedHashMap<Integer, ArrayList<Integer>>> polySkeleton = new
  7.            HashMap<Integer, // felulet_id
  8.                             LinkedHashMap<Integer, // felulet_sub_id
  9.                                                    ArrayList<Integer> // pont_id
  10.                                          >
  11.                   >();
  12. */
  13.  
  14. import java.io.*;
  15. import java.nio.file.*;
  16. import java.util.*;
  17.  
  18. public class Point {
  19.     public double y = 0;
  20.     public double x = 0;
  21.     public double m = 0;
  22.     public void Point(double y, double x) throws IOException {
  23.         Point(y,x,0);
  24.     }
  25.     public void Point(double y, double x, double m) throws IOException {
  26.         this.y=y;
  27.         this.x=x;
  28.         this.m=m;
  29.     }
  30. }
  31.  
  32. { // Geomteriai elemek osztályai a DAT-ban
  33.     public class Pont {
  34.         public int pont_id = 0;
  35.         public double pont_x = 0; // A pont EOV x koordinátája
  36.         public double pont_y = 0; // A pont EOV y koordinátája
  37.         public double pont_H = 0; // A pont EOMA magassága
  38.         public int kozephiba_oszt_V = 0;
  39.         public int kozephiba_oszt_M = 0;
  40.         public void Pont(double x, double y, double h) throws IOException {
  41.             this.pont_x=x;
  42.             this.pont_y=y;
  43.             this.pont_H=h;
  44.         }
  45.     }
  46.     public class HatarVonal {
  47.         public int hatarvonal_id = 0;
  48.         public int hvsub_id = 0; // Alazonosító sorszám az egy határvonalat alkotó pontpárok (szakaszok) megkülönböztetésére
  49.         public int pont_id_1 = 0;
  50.         public int pont_id_2 = 0;
  51.         public int osszekot_mod = 0;
  52.         public int osszekot_id = 0;
  53.     }
  54.     public class Hatar {
  55.         public int hatar_id = 0;
  56.         public int hsub_id = 0; // Alazonosító sorszám az egy határt alkotó határvonalak megkülönböztetésére
  57.         public int hatarvonal_id = 0;
  58.         public char irany_valt = "";
  59.     }
  60.     public class Felulet {
  61.         public int felulet_id = 0;
  62.         public int subfel_id = 0; // Alazonosító sorszám a felületet alkotó valamely külső határ (és annak belső határai) megkülönböztetésére az ugyanazt a felületet alkotó többi külső határtól (és azok külső határaitól)
  63.         public int hatar_id  = 0;
  64.         public char hatar_valt = "";
  65.     }
  66. }
  67.  
  68. { /* Minta a feluletek felépítéséhez
  69. Foktő (1342) BA02
  70. felulet_id=10370 + hatar_valt='-' (ismeretlen információt hordoz...)
  71.     subfel_id=1
  72.         hatar_id=7610 + hatar_valt='+' (külső határ)
  73.             hsub_id irany_valt  h.hatarvonal_id hvsub_id    hv.p1_id    hv.p2_id    p.id
  74.             1       -           26354           1           207         1           1
  75.             2       -           26355           1           343         207         207
  76.             3       -           26353           1           1042        343         343
  77.             4       -           26356           1           1773        1042        1042
  78.             5       -           26338           1           1962        1773        1773
  79.         hatar_id=7986 + hatar_valt='-' (belső határ)
  80.             hsub_id irany_valt  h.hatarvonal_id hvsub_id    v.p1_id
  81.             1       +           3204            1           211         218         211
  82.             2       +           3202            1           218         224         218
  83.             3       +           24584           1           224         226         224
  84.             4       +           24585           1           226         228         226
  85.             5       -           26740           1           282         228         228
  86.     subfel_id_2
  87.         hatar_id=6888 + hatar_valt='+' (külső határ)
  88.             hsub_id irany_valt  h.hatarvonal_id hvsub_id    v.p1_id
  89.             1       -           11362           1           24726       24725       24725
  90.             2       -           5880            1           24741       24726       24726
  91.             3       -           5879            1           24744       24741       24741
  92.             4       -           5882            1           24751       24744       24744
  93.             5       +           5883            1           24751       24756       24751
  94. */ }
  95.  
  96. public class DAT {
  97.  
  98.   public String filename = "";
  99.   public String header = "";
  100.  
  101.   public HashMap tables = new HashMap();
  102.  
  103.   public ArrayList wholeFileInArrayList = new ArrayList();
  104.  
  105.   public byte[] wholeFileInBytes;
  106.   public HashMap byteIndex = new HashMap();
  107.  
  108.   public HashMap points = new HashMap();
  109.  
  110.   public void DAT(String filename) throws IOException {
  111.       this.filename = filename;
  112.   }
  113.  
  114.   public void openToHashMap() throws IOException {
  115.       try {
  116.         // https://techknowstudy.wordpress.com/2015/03/10/reading-a-file-through-bean-shell/
  117.         fileReader=new FileReader(filename);
  118.         bufferReader= new BufferedReader(fileReader);
  119.         linenumber=0;
  120.         while( (line=bufferReader.readLine()) != null ) {
  121.             //if(line.equals("")) { continue; }
  122.             line  = line.trim().replaceAll("\\*$","");
  123.             if ( ++linenumber == 1 ) {
  124.                header = line;
  125.                continue;
  126.             }
  127.             if(line.startsWith("T_")) {
  128.                 table = line;
  129.                 tables.put(table,new ArrayList());
  130.                 continue;
  131.             } else {/*
  132.                 String[] words = line.split("\\*");
  133.                 for(i=0;i<words.length;i++){
  134.                     print(words[i]+"\t");
  135.                 }*/
  136.                 tables{table}.add(line);
  137.             }
  138.         };
  139.         fileReader.close();
  140.         bufferReader.close();
  141.         for ( key : this.tables.keySet() ){ this.tables{key}.trimToSize(); }
  142.     } catch (IOException e) {
  143.         e.printStackTrace();
  144.     }
  145.  
  146.   }
  147.  
  148.   public void openToArrayList() throws IOException {
  149.     try {
  150.         fileReader=new FileReader(filename);
  151.         bufferReader= new BufferedReader(fileReader);
  152.         linenumber=0;
  153.         while( (line=bufferReader.readLine()) != null ) {
  154.             line  = line.trim().replaceAll("\\*$","");
  155.             if ( ++linenumber == 1 ) {
  156.                header = line;
  157.                continue;
  158.             }
  159.             wholeFileInArrayList.add(line);
  160.         };
  161.         fileReader.close();
  162.         bufferReader.close();
  163.         wholeFileInArrayList.trimToSize();
  164.     } catch (IOException e) {
  165.         e.printStackTrace();
  166.     }
  167.   }
  168.  
  169.   public ArrayList openToArrayList(String table) throws IOException {
  170.     sorok = new ArrayList();
  171.     check=0;
  172.     try {
  173.         fileReader=new FileReader(filename);
  174.         bufferReader= new BufferedReader(fileReader);
  175.         while( (line=bufferReader.readLine()) != null ) {
  176.             line  = line.trim().replaceAll("\\*$","");
  177.             if ( line.equals(table) ) {
  178.                check=1;
  179.                continue;
  180.             }
  181.             if( line.startsWith("T_") && line.indexOf('*')==-1 ) {
  182.                check=0;
  183.                break;
  184.             }
  185.             if( check==1 ) {
  186.                sorok.add(line);
  187.             }
  188.         };
  189.         fileReader.close();
  190.         bufferReader.close();
  191.     } catch (IOException e) {
  192.         e.printStackTrace();
  193.     }
  194.     return sorok;
  195.   }
  196.  
  197.   public byte[] openToByteArray() throws IOException {
  198.         try {
  199.             file = new File(filename);
  200.             wholeFileInBytes = new byte[file.length()];
  201.             inputStream = new FileInputStream(file);
  202.             inputStream.read(wholeFileInBytes);
  203.             //wholeFileInBytes = Files.readAllBytes(Paths.get(filename));
  204.         } catch (Exception e) {
  205.             System.out.println(e);
  206.         }
  207.        
  208.         HashMap indexOfWholeFileInBytes = new HashMap();
  209.  
  210.         table="";
  211.         lastEOL = 0;
  212.         for ( int i = 0; i < wholeFileInBytes.length; i++ ) {
  213.            
  214.             if ( wholeFileInBytes[i]==0x0A ) {
  215.                 sor = ( new String ( Arrays.copyOfRange(wholeFileInBytes, lastEOL, i-1) ) ) .trim().replaceAll("\\*$","") ;
  216.  
  217.                 if ( lastEOL==0 ) {
  218.                     header = sor;
  219.                     table = "Fejléc";
  220.                 }
  221.  
  222.                 if ( sor.startsWith("T_") && sor.indexOf('*')==-1 ) {
  223.                     table = sor;
  224.                     indexOfWholeFileInBytes.put(table,new ArrayList());
  225.  
  226.                 } else {
  227.                     //indexOfWholeFileInBytes{table}.add(lastEOL);
  228.                     //indexOfWholeFileInBytes{table}.add(i-1);
  229.                 }
  230.                
  231.                 print ( table + " ¨"+sor+"¨" );
  232.                
  233.                
  234.                 //eoln.add(i);
  235.                
  236.                 lastEOL=i+1;
  237.             }
  238.            
  239.         }
  240.        
  241.         print (this.header);
  242.         print ( indexOfWholeFileInBytes );
  243.        
  244.        
  245.        
  246.         return wholeFileInBytes;
  247.     }
  248.  
  249.  
  250.   public void setPoints() {
  251.     for ( row : this.tables{"T_PONT"} ){
  252.     //for ( row : openToArrayList("T_PONT") ){
  253.         cols = row.split("\\*",-1);
  254.         //points.put(cols[0],new Point(Double.parseDouble(cols[1]),Double.parseDouble(cols[2])/*,Double.parseDouble(cols[3])*/));
  255.         if(cols[3].equals("")) {
  256.             points.put(cols[0],new Coordinate(Double.parseDouble(cols[2]),Double.parseDouble(cols[1]),0));
  257.         } else {
  258.             points.put(cols[0],new Coordinate(Double.parseDouble(cols[2]),Double.parseDouble(cols[1]),Double.parseDouble(cols[3])));
  259.         }
  260.     }
  261.   }
  262.  
  263. } // end of DAT class.
  264. /*
  265. {
  266.  
  267.     dat = new DAT("q:\\OpenJUMP\\beanshell\\dat_ck_proba.dat");
  268.     print ("Fájlnév: "+dat.filename );
  269.     elotte=Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
  270.     dat.openToHashMap();
  271.     //dat.openToArrayList();
  272.     utanna=Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
  273.     print (elotte/1024/1024+" "+utanna/1024/1024+" : "+(utanna-elotte)/1024/1024+" [Mb]");
  274.  
  275.     print ( dat.header );
  276.     for ( t : dat.tables.keySet() ) {
  277.         print(t+": "+dat.tables{t}.size());
  278.     }
  279.  
  280.     dat.setPoints();
  281.     for ( key : dat.points.keySet() ) {
  282.         print(key+": "+dat.points{key}.y+" "+dat.points{key}.x);
  283.     }
  284.  
  285. }*/
  286.  
  287. {
  288.  
  289. s1 = System.currentTimeMillis();
  290.  
  291.     filename = "c:\\Temp\\terkepek\\ck_proba.dat";
  292.     filename = "q:\\OpenJUMP\\beanshell\\dat\\dat_kmet.dat";
  293.     filename = "q:\\OpenJUMP\\beanshell\\dat\\dat_ck_proba.dat";
  294.  
  295.     dat = new DAT(filename);
  296.    
  297.     dat.openToHashMap();
  298.     dat.setPoints();
  299.    
  300.     b = dat.openToByteArray();
  301.  
  302. s2 = System.currentTimeMillis();
  303. print("eltelt idő: "+(s2-s1)+" ms\n");
  304.  
  305.   Random rand = new Random();
  306.  
  307.   randomString(int length) {
  308.       StringBuffer sb = new StringBuffer();
  309.       for (int i = 0 ; i < length ; i++) sb.append((char)(64 + 27.0*Math.random()));
  310.       return sb.toString();
  311.   }
  312.  
  313.   schema = new FeatureSchema();
  314.   schema.addAttribute("id",AttributeType.BIGINT);
  315.   //schema.addAttribute("name",AttributeType.STRING);
  316.   schema.addAttribute("geometry",AttributeType.GEOMETRY);
  317.   print("Attributes of schema:");
  318.   for ( name : schema.attributeNames ) {
  319.     print("  - "+name+" ["+schema.getAttributeType(name)+"]");
  320.   }
  321.  
  322.   dataset = new FeatureDataset(schema);
  323.     for ( key : dat.points.keySet() ) {
  324.       //coord = new Coordinate(dat.points{key}.y,dat.points{key}.x);
  325.       //geom = new GeometryFactory().createPoint(coord);
  326.       geom = new GeometryFactory().createPoint(dat.points{key});
  327.       geom.setSRID(23700);
  328.       bf = new BasicFeature(schema);
  329.       bf.setGeometry(geom);
  330.       //bf.setUserData("id", rand.nextInt(100)); // don't work...
  331.       //bf.setUserData("name", randomString(5)); // don't work...
  332.       bf.setAttribute("id", key);
  333.       //bf.setAttribute("name", randomString(5));
  334.       dataset.add(bf);
  335.     }
  336.   print("Dataset size: "+dataset.size());
  337.   for ( f : dataset.getFeatures() ) {
  338.     //print("  - "+f.getGeometry()+"\tid="+f.getAttribute(0)+"\tname="+f.getAttribute(1));
  339.   }
  340.   wc.layerManager.addLayer("DEMO","points",dataset);
  341.  
  342.  
  343.   /*
  344.   FeatureCollection points = FeatureDatasetFactory.createFromGeometry(geoms,schema);   
  345.   print("dataset size: "+points.size()+" [...]");
  346.   wc.layerManager.addLayer("DEMO","more points",points);
  347.   print("˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙");
  348.   */
  349.  
  350.  
  351. s3 = System.currentTimeMillis();
  352. print("eltelt idő: "+(s3-s2)+" ms\n");
  353. print("+--------------------+\n"+"|   END OF PROCESS   |\n"+"+--------------------+\n");
  354.  
  355. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement