[Tinyos-contrib-commits] CVS: tinyos-1.x/contrib/eyes/tools/java/de/tub/eyes/gis GIS.java, NONE, 1.1 GIS.properties, NONE, 1.1 Jdbc.java, NONE, 1.1 MapXY.java, NONE, 1.1 Plugin.java, NONE, 1.1 Textfile.java, NONE, 1.1

Till Wimmer twimmer at users.sourceforge.net
Tue Apr 4 14:46:27 PDT 2006


Update of /cvsroot/tinyos/tinyos-1.x/contrib/eyes/tools/java/de/tub/eyes/gis
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv22256/gis

Added Files:
	GIS.java GIS.properties Jdbc.java MapXY.java Plugin.java 
	Textfile.java 
Log Message:
Classes for geographic information

--- NEW FILE: GIS.java ---
/*
 * GIS.java
 *
 * Created on March 24, 2006, 5:49 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package de.tub.eyes.gis;

import java.sql.*;
import java.util.*;
import java.io.*;


/**
 *
 * @author Till Wimmer
 */
public class GIS {
    private Connection db = null;
    private static TreeMap nodes = new TreeMap();
    private static Properties config = null;
    private static String propFile = null;
    private Plugin plugin = null;
    
    static final String [] GIS_TYPES = {"jdbc", "textfile"};
    
    /** Creates a new instance of GIS */
    public GIS() {
    }
    
    public GIS(String propFile)  {
        this.propFile = propFile;
    }
    
    public boolean init(String className) {
        if (!readProps())
            return false;
        
        if (className.equals("jdbc")) {
            plugin = new Jdbc(config);
            return plugin.init();
        }
        
        if (className.equals("textfile")) {
            plugin = new Textfile(config);
            return plugin.init();
        }
        
        return false;
    }
        
    public boolean readDB() {
        if (plugin == null)
            return false;
        
        return plugin.readDB(nodes);        
    }
    
    public static int [] getFloorXY(int ID) {
        System.out.print("getFloorXY: ");
        if (nodes == null) {
            System.out.println("nodes = null!");
        
            return null;
        }
        
        MapXY node = null;
        if ( (node=(MapXY)nodes.get(new Integer(ID))) == null ) {
            System.out.println("ID not found.");
            
            return null;
        }
        else {
            System.out.println("ID "+ID+": map = "+node.map+" x = "+node.x+" y = "+node.y);            
            int [] coord = { node.map, node.x, node.y };
            return coord;
        }
    }
        
    public void setPropFile(String propFile) {
        this.propFile = propFile;
    }
    
    private boolean readProps() {
        try {
            config = new Properties();
            if (propFile == null)
                config.load( new FileInputStream("GIS.properties"));
            else
                config.load(new FileInputStream(propFile));
        }
        catch (IOException e) {
            System.err.println(e);
            return false;
        }
        
        return true;
    }    
}

--- NEW FILE: GIS.properties ---
# Sample ResourceBundle properties file
#jdbc_url=jdbc:postgresql://192.168.168.5/mydb
jdbc_url=jdbc:postgresql://localhost/postgres

jdbc_pwd=hos7sa
jdbc_usr=develop
jdbc_cntMaps=3
jdbc_mapType_0=0
jdbc_zMin_0=8.0
jdbc_zMax_0=10.0
jdbc_mapType_1=0
jdbc_zMin_1=11.0
jdbc_zMax_1=13.0
jdbc_mapType_2=0
jdbc_zMin_2=15.0
jdbc_zMax_2=17.0

jdbc_scale_0=46.25
jdbc_scale_1=46.25
jdbc_scale_2=46.25

text_tableFile=
text_cntMaps=3
text_mapType_0=0
text_mapType_1=0
text_mapType_2=0
--- NEW FILE: Jdbc.java ---
/*
 * jdbc.java
 *
 * Created on April 3, 2006, 4:50 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package de.tub.eyes.gis;

import java.sql.*;
import java.util.*;

/**
 *
 * @author develop
 */
public class Jdbc implements Plugin {
    private Connection db = null;
    private Properties config = null;
    
    /** Creates a new instance of jdbc */
    public Jdbc(Properties config) {
        this.config = config;
    }
    
    public boolean init() {
        try {
            Class.forName("org.postgresql.Driver");
        
        } catch (Exception e) {
            System.err.println("Error opening PostgreSQL JDBC driver");
            return false;
        }
        
        String url, usr, pwd;

        url = config.getProperty("jdbc_url");
        usr = config.getProperty("jdbc_usr");
        pwd = config.getProperty("jdbc_pwd");
        
        try {
            db = DriverManager.getConnection(url, usr, pwd);
        } catch (Exception e) {
            System.err.println(e.getLocalizedMessage());
            return false;
        }
        return true;
    }
        
    public boolean readDB(TreeMap nodes) {
        if (db == null)
            return false;
        
        nodes.clear();
        
        int cntMaps = Integer.parseInt(config.getProperty("jdbc_cntMaps", "0"));
        //int cntMaps = Integer.parseInt(config.getProperty("map_cntImgs", "0"));        
        //if (cntMaps == 0 || cntMaps == 0)
        //    return false;
        
        float [] floorMin = new float [cntMaps];
        float [] floorMax = new float[cntMaps];
        float [] scale = new float[cntMaps];
        int [] type = new int[cntMaps];
        
        for (int i=0; i<cntMaps; i++) {
            String name = "jdbc_zMin_" + i;
            floorMin[i]= Float.parseFloat(config.getProperty(name));
            name = "jdbc_zMax_" + i;
            floorMax[i]= Float.parseFloat(config.getProperty(name));           
        }
                 
        for (int i=0; i<cntMaps; i++) {
            String name = "jdbc_scale_" + i;
            scale[i]= Float.parseFloat(config.getProperty(name));            
        }
        
        for (int i=0; i<cntMaps; i++) {
            String name = "jdbc_mapType_" + i;
            type[i]= Integer.parseInt(config.getProperty(name, "0"));            
        }       
        
        Statement  st=null;
        
        try {
            st = db.createStatement();
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        ResultSet rs=null;
        
        for (int map=0; map < cntMaps; map++) {
            String statement;
            switch(type[map]) {
                
                case 0:
                    statement = "SELECT * from node_xyz WHERE location_z >= " + floorMin[map]
                            + " AND location_z <= " + floorMax[map];
                    break;
                case 1:
                    statement = "SELECT * from node_xyz WHERE location_x >= " + floorMin[map]
                            + " AND location_x <= " + floorMax[map];
                    break;
                case 2:
                    statement = "SELECT * from node_xyz WHERE location_y >= " + floorMin[map]
                            + " AND location_y <= " + floorMax[map];
                    break;
                default:
                    continue;
            }
        
            try {
                rs = st.executeQuery(statement);
            } catch (Exception e) { continue; }
        
            if (rs == null)
                continue;
            try {
                while (rs.next()) {
                    MapXY node = new MapXY();
                    node.map = map;
                    
                    Integer ID = new Integer(rs.getInt("node_id"));
                    switch (type[map]) {
                        
                        case 0:
                            node.x = (int)(rs.getFloat("location_x")*scale[map]);
                            node.y = (int)(rs.getFloat("location_y")*scale[map]);                            
                            System.out.println("x="+node.x+" y="+node.y+" map="+map+" ID="+ID);
                            break;
                        case 1:
                            node.x = (int)(rs.getFloat("location_y")*scale[map]);
                            node.y = (int)(rs.getFloat("location_z")*scale[map]);                            
                            System.out.println("y="+node.x+" z="+node.y+" map="+map+" ID="+ID);
                            break;
                        case 2:
                            node.x = (int)(rs.getFloat("location_z")*scale[map]);
                            node.y = (int)(rs.getFloat("location_x")*scale[map]);                            
                            System.out.println("z="+node.x+" x="+node.y+" map="+map+" ID="+ID);
                            break;
                        default:
                            continue;
                    }
                    //nodes.setSize(ID);
                    nodes.put(ID, node);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            try { rs.close(); } catch (Exception e) {
                e.printStackTrace();
            }
        }
             
        try { st.close(); } catch (Exception e) {        
            e.printStackTrace();
        }
        
        return true;
    }
}

--- NEW FILE: MapXY.java ---
/*
 * mapXY.java
 *
 * Created on April 3, 2006, 5:48 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package de.tub.eyes.gis;

/**
 *
 * @author Till Wimmer
 */
public class MapXY {
    public int map;
    public int x;
    public int y;    
    
    /** Creates a new instance of mapXY */
    public MapXY() {
    }        
        
    public MapXY (int floor, int y, int z) {
        this.map = map;
        this.x = x;
        this.y = y;    
    }    
}

--- NEW FILE: Plugin.java ---
/*
 * plugin.java
 *
 * Created on April 3, 2006, 5:32 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package de.tub.eyes.gis;

import java.util.Properties;


/**
 *
 * @author Till Wimmer
 */
public interface Plugin {
    public boolean init();
    public boolean readDB(java.util.TreeMap nodes);
}

--- NEW FILE: Textfile.java ---
/*
 * textfile.java
 *
 * Created on April 3, 2006, 4:50 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package de.tub.eyes.gis;

import java.io.*;
import java.util.Properties;
import java.util.TreeMap;
import java.util.regex.Pattern;

/**
 *
 * @author develop
 */
public class Textfile implements Plugin {
    private Properties config;
    
    /** Creates a new instance of textfile */
    public Textfile(Properties config) {
        this.config = config;
    }
    
    public boolean init() {
        return true;
    }

    public boolean readDB(TreeMap nodes) {
        String line;
        String fname;        

        if ( (fname = config.getProperty("textfile_TableFile")) == null 
                || fname.equals("") )
            return false;

        File fh = new File(fname);
        FileReader fr = null;
        try {
            fr = new FileReader(fh);
        }
        catch (FileNotFoundException e) {
            System.err.println("File " + fname + ":" + e);
            return false;
        }
        
        nodes.clear();
                
        BufferedReader in = new BufferedReader(fr);
        
        try {
            while ((line = in.readLine()) != null) {
                MapXY node = new MapXY();
                String [] data  = Pattern.compile("\t").split(line);
                if (data.length < 4) {
                    System.err.println ("readTable(): XY Parse error");
                    continue;
                }
                Integer id = new Integer(data[0]);
                node.x = Integer.parseInt(data[1]);
                node.y = Integer.parseInt(data[2]);
                node.map = Integer.parseInt(data[3]);
                
                //nodeMap.put(new Integer(nodeData.ID), nodeData);
                nodes.put(id, node);
            }
        }
        catch (IOException e) {
            System.err.println(e.getMessage());
            return false;
        }
        
        return true;
    }    
}



More information about the Tinyos-contrib-commits mailing list