读取postgis中数据写入shapefile-1

数据库中表:Door、State

Door表中数据

State表中数据

读取数据库代码:主要读取Door和State数据,部分代码重复,可以精简

package com.ubiloc.mj;
import java.sql.Connection;  
import java.sql.DriverManager;  
import java.sql.ResultSet;  
import java.sql.SQLException;  
import java.sql.Statement;  
import java.util.ArrayList;
import java.util.Vector;

import org.geotools.geometry.jts.JTSFactoryFinder;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.LineString;

import com.vividsolutions.jts.geom.GeometryFactory;

import com.vividsolutions.jts.geom.Point;
  


public class Postgre {  
	GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory( null );
    private Connection conn;  
    static String url = "jdbc:postgresql://127.0.0.1:5432/postgis";

    static String usr = "postgres";

    static String psd = "ubiloc";

    public void init() throws Exception {  
        Class.forName("org.postgresql.Driver");  
        conn = DriverManager.getConnection(url, usr, psd);  
    }  
      
    public void destroy() throws SQLException {  
        if (conn != null) {  
            conn.close();  
        }  
    }  

    public ArrayList<Door> getDoor() throws SQLException, JSONException{
    	Statement stmt = conn.createStatement();  
    	  String sql = "SELECT " +
    	  		"doorid, " +
    	  		"lspaceid, " +
    	  		"rspaceid, " +
    	  		"curstate, " +
    	  		"ST_AsGeoJson(geom)" +
    	  		"FROM door;";  
          ResultSet rs = stmt.executeQuery(sql);  
          ArrayList<Door> door=new ArrayList<Door>();
          while (rs.next()) {
        	  Door d=new Door();
              int doorid = rs.getInt(1);
              int lspaceid=rs.getInt(2);
              int rspaceid=rs.getInt(3);
              String curstate=rs.getString(4);
              boolean currentState;
           
              if(curstate.equals("open")) {currentState=true;}
                 else{currentState=false;}
              d.setDoorID(doorid);
              d.setLeftSpaceID(lspaceid);
              d.setRightSpaceID(rspaceid);
              d.setCurrentState(currentState);
             /* System.out.println("doorid:"+doorid+"--lspaceid:"+lspaceid+"--rspaceid:"+rspaceid+"--curstate:"+curstate);*/
              String line=rs.getString(5);
          	JSONObject jsonObject = new JSONObject(line);
         	String str1 = jsonObject.getString("type");

         	
         	JSONArray str4=jsonObject.getJSONArray("coordinates");

         	JSONArray str5=str4.getJSONArray(0);
         	
         	Vector<Point> v=new Vector<Point>();
         	for(int i=0;i<str5.length();i++){
         	JSONArray str6=str5.getJSONArray(i);
         	double x=str6.getDouble(0);
         	double y=str6.getDouble(1);
         	Coordinate coo=new Coordinate(x, y);
    		Point p = geometryFactory.createPoint(coo); 
    		v.addElement(p);
         	}
         	Coordinate[] coords2  =
    			new Coordinate[] {new Coordinate(v.elementAt(0).getX(),v.elementAt(0).getY()), 
         			new Coordinate(v.elementAt(1).getX(),v.elementAt(1).getY()) };
    		LineString line2 = geometryFactory.createLineString(coords2);
    		d.setGeo(line2);
    		door.add(d);
          }
		return door;  
    }
    
    public ArrayList<State> getStateByID(int id) throws SQLException, JSONException{
    	Statement stmt = conn.createStatement();  
    	  String sql = "SELECT id, ST_AsGeoJson(geom) " +
    	  		"FROM state " +
    	  		"where id="+id+";";  
          ResultSet rs = stmt.executeQuery(sql);  
          ArrayList<State> state=new ArrayList<State>();
          while (rs.next()) {
        	 State s=new State();
          int stateid = rs.getInt(1);
          s.setID(stateid);     
        String point=rs.getString(2);
        JSONObject jsonObject = new JSONObject(point);
       	String str1 = jsonObject.getString("type");
   	
       	JSONArray str2=jsonObject.getJSONArray("coordinates");
     	Vector<Point> v=new Vector<Point>();
     	for(int i=0;i<str2.length();i++){
     	
     	double x=str2.getDouble(0);
     	double y=str2.getDouble(1);
     	Coordinate coo=new Coordinate(x, y);
		Point p = geometryFactory.createPoint(coo); 
		s.setPgeo(p);
		v.addElement(p);
		}   
     	state.add(s);
          }
		return state;

    }

}  


转载自:https://blog.csdn.net/cehui115081/article/details/18730621

You may also like...