2009年4月24日星期五

PostgreSQL+PostGIS的使用 5

五、 PostGIS示例

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

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

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

这里会获取所有风相关的信息,并且附加了以JSON格式返回的几何信息,这有助于我们在Flex中进行解析。如下图是关于风的查询结果:

下面我们希望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进行运算后返回的结果:

在这里,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 条评论:

匿名 说...

好,有深度!
我的博客上引用了你好多文章,在此谢过了!
cnzzb.cnblogs.com