Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- OpenJump, BeanShell, MakeNewLayerFromDAT
- */
- public class Point {
- public double y = 0;
- public double x = 0;
- public double m = 0;
- public void Point(double y, double x/*, double m*/) throws IOException {
- this.y=y;
- this.x=x;
- /*m.isNull() ? this.m=0 : this.m=m;*/
- }
- }
- public class DAT {
- public String filename = "";
- public String header = "";
- public HashMap tables = new HashMap();
- public ArrayList wholeFile = new ArrayList();
- public HashMap points = new HashMap();
- public void DAT(String filename) throws IOException {
- this.filename = filename;
- }
- public void openToHashMap() throws IOException {
- try {
- // https://techknowstudy.wordpress.com/2015/03/10/reading-a-file-through-bean-shell/
- fileReader=new FileReader(filename);
- bufferReader= new BufferedReader(fileReader);
- linenumber=0;
- while( (line=bufferReader.readLine()) != null ) {
- //if(line.equals("")) { continue; }
- line = line.trim().replaceAll("\\*$","");
- if ( ++linenumber == 1 ) {
- header = line;
- continue;
- }
- if(line.startsWith("T_")) {
- table = line;
- tables.put(table,new ArrayList());
- continue;
- } else {/*
- String[] words = line.split("\\*");
- for(i=0;i<words.length;i++){
- print(words[i]+"\t");
- }*/
- tables{table}.add(line);
- }
- };
- fileReader.close();
- bufferReader.close();
- for ( key : this.tables.keySet() ){ this.tables{key}.trimToSize(); }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public void openToArrayList() throws IOException {
- try {
- fileReader=new FileReader(filename);
- bufferReader= new BufferedReader(fileReader);
- linenumber=0;
- while( (line=bufferReader.readLine()) != null ) {
- line = line.trim().replaceAll("\\*$","");
- if ( ++linenumber == 1 ) {
- header = line;
- continue;
- }
- wholeFile.add(line);
- };
- fileReader.close();
- bufferReader.close();
- wholeFile.trimToSize();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public void setPoints() {
- for ( row : this.tables{"T_PONT"} ){
- cols = row.split("\\*",-1);
- points.put(cols[0],new Point(Double.parseDouble(cols[1]),Double.parseDouble(cols[2])/*,Double.parseDouble(cols[3])*/));
- }
- }
- } // end of DAT class.
- /*
- {
- dat = new DAT("q:\\OpenJUMP\\beanshell\\dat_ck_proba.dat");
- print ("Fájlnév: "+dat.filename );
- elotte=Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
- dat.openToHashMap();
- //dat.openToArrayList();
- utanna=Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
- print (elotte/1024/1024+" "+utanna/1024/1024+" : "+(utanna-elotte)/1024/1024+" [Mb]");
- print ( dat.header );
- for ( t : dat.tables.keySet() ) {
- print(t+": "+dat.tables{t}.size());
- }
- dat.setPoints();
- for ( key : dat.points.keySet() ) {
- print(key+": "+dat.points{key}.y+" "+dat.points{key}.x);
- }
- }*/
- {
- dat = new DAT("q:\\OpenJUMP\\beanshell\\dat_tug.dat");
- dat.openToHashMap();
- dat.setPoints();
- Random rand = new Random();
- randomString(int length) {
- StringBuffer sb = new StringBuffer();
- for (int i = 0 ; i < length ; i++) sb.append((char)(64 + 27.0*Math.random()));
- return sb.toString();
- }
- schema = new FeatureSchema();
- schema.addAttribute("id",AttributeType.BIGINT);
- schema.addAttribute("name",AttributeType.STRING);
- schema.addAttribute("geometry",AttributeType.GEOMETRY);
- print("Attributes of schema:");
- for ( name : schema.attributeNames ) {
- print(" - "+name+" ["+schema.getAttributeType(name)+"]");
- }
- dataset = new FeatureDataset(schema);
- for ( key : dat.points.keySet() ) {
- coord = new Coordinate(dat.points{key}.x,dat.points{key}.y
- );
- geom = new GeometryFactory().createPoint(coord);
- geom.setSRID(23700);
- bf = new BasicFeature(schema);
- bf.setGeometry(geom);
- //bf.setUserData("id", rand.nextInt(100)); // don't work...
- //bf.setUserData("name", randomString(5)); // don't work...
- bf.setAttribute("id", key);
- dataset.add(bf);
- }
- print("Dataset size: "+dataset.size());
- for ( f : dataset.getFeatures() ) {
- print(" - "+f.getGeometry()+"\tid="+f.getAttribute(0)+"\tname="+f.getAttribute(1));
- }
- wc.layerManager.addLayer("DEMO","one point",dataset);
- print("˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙");
- FeatureCollection points = FeatureDatasetFactory.createFromGeometry(geoms,schema);
- print("dataset size: "+points.size()+" [...]");
- wc.layerManager.addLayer("DEMO","more points",points);
- print("˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙˙");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement