13432878738 发表于 2016-11-20 06:19:27

PostgreSQL+PostGIS的使用 5

  五、 PostGIS示例

下面我们通过一个简单的Flex应用示例来看一下PostGIS的用法:

假想现在发生了恐怖袭击,导致在一些城市有污染物出现,现在我们要根据污染物和当地风力、风向情况,计算污染扩散范围,针对这些区域及时进行警报和疏散。

首先我们希望获得所有发生污染的城市的当前风速、风向等信息,在我们的PostGIS数据库中有一个空间表保存着这些信息,我们构造这样的SQL语句进行查询:
select *,ST_AsGeoJson(shape) from sde.wind

这里会获取所有风相关的信息,并且附加了以JSON格式返回的几何信息,这有助于我们在Flex中进行解析。如下图是关于风的查询结果:
http://2.bp.blogspot.com/__ZvvWze6o0c/SfFiP8rfycI/AAAAAAAACX8/Ro-ky9fIdiQ/s400/2009-4-24+14-20-57.png
下面我们希望PostGIS帮助我们实现一些空间分析。我们以污染发生的城市为起点,当地风向为主方向,构造一个30度开角的范围;这个范围将是污染扩散的主要方向,扩散的范围主要和风的强度有关;在构造这个区域以后,为了保险起见,我们在对其进行一定范围的缓冲,最后得到每个污染源可能扩散的范围。我们构造的SQL语句如下:
select *,ST_AsGeoJson(ST_Buffer(   ST_PolygonFromText(    'POLYGON(('    ||ST_X(shape)||' '||ST_Y(shape)||','    ||ST_X(shape)+velocity*cos((direction+15)*PI()/180)/20||' '||ST_Y(shape)+velocity*sin((direction+15)*PI()/180)/20||','    ||ST_X(shape)+velocity*cos((direction-15)*PI()/180)/20||' '||ST_Y(shape)+velocity*sin((direction-15)*PI()/180)/20||','    ||ST_X(shape)||' '||ST_Y(shape)||'))'   )   , velocity/50) ) from sde.wind

下面是PostGIS进行运算后返回的结果:
http://4.bp.blogspot.com/__ZvvWze6o0c/SfFiQRxnjXI/AAAAAAAACYE/Zwfw4v3FO0s/s400/2009-4-24+14-21-12.png
在这里,Flex应用与服务器的交互通过BlazeDS进行,下面是本示例在服务器端的Java代码:

package wuyf;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;

public class Wind
{
private Connection conn = null;

public Connection getConn()
{
if (conn==null)
{
   try
   {
    Class.forName("org.postgresql.Driver");
    String url = "jdbc:postgresql://localhost:5432/sde" ;
    conn = DriverManager.getConnection(url, "sde" , "pwd" );
    conn.setAutoCommit(false);
   }
   catch(Exception e)
   {
    System.err.print(e);
   }
}

return conn;
}

public ArrayList> getWinds()
{
ArrayList> result = new ArrayList>();

if ( this.getConn()==null )
   return result;

try
{
   String sql = "select *,ST_AsGeoJson(shape) from sde.wind";

   Statement st = this.getConn().createStatement();   
   st.setFetchSize(0);   
   ResultSet rs = st.executeQuery(sql);
   while (rs.next())
   {
    HashMap map = new HashMap();
    map.put("shape", rs.getString("ST_AsGeoJson"));
    map.put("velocity", rs.getString("velocity"));
    map.put("direction", rs.getString("direction"));
    result.add(map);
   }
   rs.close();
   st.close();
}
catch(Exception e)
{
   System.err.print(e);
}

return result;
}


public ArrayList> getEffectZones()
{
ArrayList> result = new ArrayList>();

if ( this.getConn()==null )
   return result;

try
{
   String sql = "select *,ST_AsGeoJson(";
   sql+=   "ST_Buffer(";
   sql+=    "ST_PolygonFromText(";
   sql+=   "'POLYGON(('";
   sql+=   "||ST_X(shape)||' '||ST_Y(shape)||','";
   sql+=   "||ST_X(shape)+velocity*cos((direction+15)*PI()/180)/20||' '||ST_Y(shape)+velocity*sin((direction+15)*PI()/180)/20||','";
   sql+=   "||ST_X(shape)+velocity*cos((direction-15)*PI()/180)/20||' '||ST_Y(shape)+velocity*sin((direction-15)*PI()/180)/20||','";
   sql+=   "||ST_X(shape)||' '||ST_Y(shape)||'))'";
   sql+=    ")";
   sql+=    ", velocity/50";
   sql+=   ")";
   sql+=") ";
   sql+="from sde.wind";

   Statement st = this.getConn().createStatement();   
   st.setFetchSize(0);   
   ResultSet rs = st.executeQuery(sql);
   while (rs.next())
   {
    HashMap map = new HashMap();
    map.put("shape", rs.getString("ST_AsGeoJson"));
    map.put("velocity", rs.getString("velocity"));
    map.put("direction", rs.getString("direction"));
    result.add(map);
   }
   rs.close();
   st.close();
}
catch(Exception e)
{
   System.err.print(e);
}

return result;
}

}
页: [1]
查看完整版本: PostgreSQL+PostGIS的使用 5