sinoly

   :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  29 随笔 :: 76 文章 :: 189 评论 :: 0 Trackbacks

 

 1 package  com.geotools.test;
 2
 3 /**
 4  * 
 5  * CopyRight (C) All rights reserved.
 6  * <p> * WuHan Inpoint Information Technology Development,Inc.
 7  * <p> * Author sinoly <p>  Project Name: PostGeo
 8  * <p>
 9  * Base on : JDK1.5
10  * <p>
11  * 
12   */
 
13 import  java.io.IOException;
14 import  java.util.HashMap;
15 import  java.util.Map;
16
17 import  org.geotools.data.DataStore;
18 import  org.geotools.data.DataStoreFinder;
19 import  org.geotools.data.FeatureSource;
20
21 public   class  GetPostgisData  {
22  
23   static  DataStore pgDatastore;   // 数据容器
24  
25  @SuppressWarnings( " unchecked " )
26   private   static   void  ConnPostGis(String dbtype,String URL, int  port,String database,String user,String password) {
27   Map params  =   new  HashMap();
28   params.put( " dbtype " " postgis " );
29   params.put( " host " , URL);
30   params.put( " port " new  Integer(port));
31   params.put( " database " , database);
32   params.put( " user " , user);
33   params.put( " passwd " , password);  
34    try   {
35    pgDatastore  =  DataStoreFinder.getDataStore(params);
36     if (pgDatastore != null ) {
37     System.out.println( " 系统连接到位于: " + URL + " 的空间数据库 " + database + " 成功! " );
38     
39    }
else {
40     System.out.println( " 系统连接到位于: " + URL + " 的空间数据库 " + database + " 失败!请检查相关参数 " );
41    }

42   }
  catch  (IOException e)  {
43    e.printStackTrace();
44    System.out.println( " 系统连接到位于: " + URL + " 的空间数据库 " + database + " 失败!请检查相关参数 " );
45   }

46  }

47  
48   public   static   void  main(String[] args)  throws  IOException {
49   ConnPostGis( "" , " localhost " , 5432 , " navigation " , " root " , " 123 " );
50    /* 读取指定类型名的地理特征  */
51   FeatureSource fsBC  =  pgDatastore.getFeatureSource( " roads " );
52   System.out.println(fsBC.getFeatures().size());
53    /* 得到空间数据库中所有特征表的表名 */
54   String[] typeName = pgDatastore.getTypeNames();
55    for ( int  i = 0 ;i < typeName.length;i ++ ) {
56    System.out.println(typeName[i]);
57   }

58  }

59 }

60
61



     进行Geotools的开发工作,肯定会参考GeoTools的官方参考文档。不过我想很多人估计都遇到了和我一样的问题,就是官方参考指南中几乎没有可以编译通过的代码,也就无法谈及用这些代码进行学习了。昨天抽空将Geotools指南中连接到POSTGIS的代码进行了重新编译,才发现指南中的代码是在geotools 2.1.4中编译的。。。狂汗ing。。我居然一直认为是我的人品问题,才导致这些代码在我机器上就无法编译成功滴。。看来说明了一个问题:4475_200361112122146460.gif.。。。
      同时在编译过程中还翻阅了2.3的API,也在其中发现了几个对原有方法进行功能修改或者干脆废弃的方法,在此列出:
      1、如果你和我一样使用了POSTGRESQL 8.4,sorry,麻烦你不要用其自带的任何JDBC驱动,直接使用geotools中带的数据库驱动吧。不要问我为什么,我也不知道,我唯一知道的就是只有这样程序才能真正的连接到postgis。
    2、gt2-main.jar;gt2-postgis.jar;JTS-1.4.jar;geoapi-1.1.0alpha.jar;vecmath-1.2.1.jar。你需要确定这几个文件存在与你的项目构建之中。
   3、原有取得指定图层Feature总数的方法是FeatureSource.getCount(Query.ALL)),这个方法已经不能使用了,可以更换成getFeatures().size()方法来取得Feature的总数。(ps:虽说取得总数的方法用的不多,不过我觉得用这个方法来测试是否能连接到postgis还是很方便的。)
   4、应用中最好能自己重写postgis的连接池(可不是上面例子中的这种简单玩意,嘿嘿),一般关系数据库额连接池有很多中解决方式,但postgis的数据库连接池目前我还不知道有什么好的解决方式,之所以要自己写连接池的目的就是为了更好的控制postgis的空间索引关系。



posted on 2007-01-25 09:55 sinoly 阅读(7130) 评论(9)  编辑  收藏 所属分类: POSTGISGEOTOOLS

评论

# re: 使用GeoTools 2.3M连接到POSTGIS数据库[未登录] 2007-01-25 10:07 sinoly
如果有兄弟对开源GIS平台开发感兴趣的,考虑一下,一起搞搞:)
  回复  更多评论
  

# re: 使用GeoTools 2.3M连接到POSTGIS数据库 2007-01-25 12:49 Flyingis
我有兴趣,但现在自由的时间少了,大家可以一起探讨。
有没有兴趣加入WebGIS开发设计组,把3S相关的文章整理在一起?  回复  更多评论
  

# re: 使用GeoTools 2.3M连接到POSTGIS数据库[未登录] 2007-05-22 09:16 过客
有探讨的群没?我在学习,想&大家一块探讨。有的话加我啊
373572418  回复  更多评论
  

# re: 使用GeoTools 2.3M连接到POSTGIS数据库 2007-12-29 16:17 前进
我了很想和大家一起探讨,79666049  回复  更多评论
  

# re: 使用GeoTools 2.3M连接到POSTGIS数据库 2008-01-02 11:32 前进
为了方便大家交流,请加入我的开源GIS项目分析的QQ群40087303,现在已经有很多硕士和博士,有兴趣的朋友快加入吧,我们一起为提高中国开源实力而努力!现阶段分析的项目是SharpMap、Geoserver、Openlayers、GRASS、PostgerSQL、Geotools等
我的QQ号:79666049


群号:40087303
群名:开源GIS项目分析
  回复  更多评论
  

# re: 使用GeoTools 2.3M连接到POSTGIS数据库 2008-01-02 17:13 kenzhang
最近一直在学openGis,博主的文章也一直在看,收获颇多,感谢!  回复  更多评论
  

# re: 使用GeoTools 2.3M连接到POSTGIS数据库 2008-07-15 10:10 ads
请问一下hibernate hql 支持 postgis函数吗???  回复  更多评论
  

# re: 使用GeoTools 2.3M连接到POSTGIS数据库 2008-12-08 11:01 songhf
for (int i = 0; i < shapes.length; i++) {
filter = new SeShapeFilter(tableName, spatialCol, shapes[i], SeFilter.METHOD_ENVP);
filters[i] = filter;
}
try {
String[] cols = new String[tableDef.length];
for (int i = 0; i < tableDef.length; i++)
cols[i] = tableDef[i].getName();
SeSqlConstruct sqlCons = new SeSqlConstruct(tableName);
SeQuery spatialQuery =session.createSeQuery(cols, sqlCons);
System.out.println("run1");
spatialQuery.prepareQuery();
System.out.println("run2");
spatialQuery.setSpatialConstraints(SeQuery.SE_OPTIMIZE, false,
filters);
spatialQuery.execute();
SeRow row = spatialQuery.fetch();
SeColumnDefinition colDef = new SeColumnDefinition();
int numCols = 0;
try {
numCols = row.getNumColumns();
} catch (NullPointerException ne) {

System.out.println("\n No shape retrieved");
spatialQuery.close();
return;
}
while (row != null) {
for (int i = 0; i < numCols; i++) {
colDef = row.getColumnDef(i);
int type = colDef.getType();
if (row.getIndicator((short) i) != SeRow.SE_IS_NULL_VALUE) {
switch (type) {
case SeColumnDefinition.TYPE_INT16:
System.out.println("\t" + colDef.getName() + " : "
+ row.getShort(i));
break;
case SeColumnDefinition.TYPE_DATE:
System.out.println("\t" + colDef.getName() + " : "
+ (row.getTime(i)).getTime());
break;
case SeColumnDefinition.TYPE_INT32:
System.out.println("\n\t" + colDef.getName()
+ " : " + row.getInteger(i));
break;
case SeColumnDefinition.TYPE_FLOAT32:
System.out.println("\t" + colDef.getName() + " : "
+ row.getFloat(i));
break;
case SeColumnDefinition.TYPE_FLOAT64:
System.out.println("\t" + colDef.getName() + " : "
+ row.getDouble(i));
break;
case SeColumnDefinition.TYPE_STRING:
System.out.println("\t" + colDef.getName() + " : "
+ row.getString(i));
break;
case SeColumnDefinition.TYPE_SHAPE:
SeShape spVal = (SeShape) row.getShape(i);
SeExtent sExt=spVal.getExtent();
double dCenterX=(sExt.getMaxX()+sExt.getMinX())/2;
double dCenterY=(sExt.getMaxY()+sExt.getMinY())/2;
break;
case SeColumnDefinition.TYPE_NSTRING:
System.out.println("\t" + colDef.getName() + " : "
+ row.getString(i));
break;
default:
System.out.println("Unknown Type");

System.out.println("\t" + colDef.getName() + " : ");
break;
}
}
}
row = spatialQuery.fetch();
}
spatialQuery.close();
SeState s=null;
session.close(s);
} catch (SeException sexp) {
System.out.println("SeException : "
+ sexp.getSeError().getErrDesc());
}
} catch (Exception e) {
}
}
}  回复  更多评论
  

# re: 使用GeoTools 2.3M连接到POSTGIS数据库 2008-12-08 11:02 songhf
帮忙看看这个问题:
SeException : THIS CONNECTION IS LOCKED TO A DIFFERENT THREAD.
package com.easymap.demo;
import com.esri.sde.sdk.client.*;
import com.esri.sde.sdk.sg.ENVELOPE;
import org.geotools.arcsde.pool.*;
import org.geotools.data.*;
import java.io.IOException;
import java.util.*;
public class ReadSde {
private static SeLayer layer = null;
private static SeTable table = null;
public static void main(String args[]) {
HashMap m = new HashMap();
m.put(ArcSDEConnectionConfig.DBTYPE_PARAM,
ArcSDEConnectionConfig.DBTYPE_PARAM_VALUE);
m.put(ArcSDEConnectionConfig.SERVER_NAME_PARAM, "192.168.10.90");
m.put(ArcSDEConnectionConfig.PORT_NUMBER_PARAM, "5151");
m.put(ArcSDEConnectionConfig.INSTANCE_NAME_PARAM, "orasrv");
m.put(ArcSDEConnectionConfig.USER_NAME_PARAM, "cwgtgis");
m.put(ArcSDEConnectionConfig.PASSWORD_PARAM, "cwgtgis");
ArcSDEConnectionConfig config = new ArcSDEConnectionConfig(m);
SessionPoolFactory sessionFactory = SessionPoolFactory.getInstance();
String tableName = "CWGTGIS.JSYD_PG_500";
String spatialCol = "";
try {
SessionPool sessionPool = sessionFactory.createPool(config);
ISession session = sessionPool.getSession();
SeColumnDefinition[] tableDef = session
.describe(tableName);
for (int i = 0; i < tableDef.length; i++) {
System.out.println(tableDef[i].getName());
if (tableDef[i].getType() == SeColumnDefinition.TYPE_SHAPE) {
spatialCol = tableDef[i].getName();
break;
}
}
layer = session.createSeLayer(tableName,spatialCol);
SeShape shape = new SeShape(layer.getCoordRef());
double minX = 502151.15789, minY = 298837.65948, maxX = 508619.77825, maxY = 304522.20465;
SeExtent extent = new SeExtent(minX, minY, maxX, maxY);
shape.generateRectangle(extent);
SeShape[] shapes = new SeShape[1];
shapes[0] = shape;
SeShapeFilter filters[] = new SeShapeFilter[shapes.length];
SeShapeFilter filter = null;
for (int i = 0; i < shapes.length; i++) {
filter = new SeShapeFilter(tableName, spatialCol, shapes[i], SeFilter.METHOD_ENVP);
filters[i] = filter;
}
try {
String[] cols = new String[tableDef.length];
for (int i = 0; i < tableDef.length; i++)
cols[i] = tableDef[i].getName();
SeSqlConstruct sqlCons = new SeSqlConstruct(tableName);
SeQuery spatialQuery =session.createSeQuery(cols, sqlCons);
System.out.println("run1");
spatialQuery.prepareQuery();
System.out.println("run2");
spatialQuery.setSpatialConstraints(SeQuery.SE_OPTIMIZE, false,
filters);
spatialQuery.execute();
SeRow row = spatialQuery.fetch();
SeColumnDefinition colDef = new SeColumnDefinition();
int numCols = 0;
try {
numCols = row.getNumColumns();
} catch (NullPointerException ne) {

System.out.println("\n No shape retrieved");
spatialQuery.close();
return;
}
while (row != null) {
for (int i = 0; i < numCols; i++) {
colDef = row.getColumnDef(i);
int type = colDef.getType();
if (row.getIndicator((short) i) != SeRow.SE_IS_NULL_VALUE) {
switch (type) {
case SeColumnDefinition.TYPE_INT16:
System.out.println("\t" + colDef.getName() + " : "
+ row.getShort(i));
break;
case SeColumnDefinition.TYPE_DATE:
System.out.println("\t" + colDef.getName() + " : "
+ (row.getTime(i)).getTime());
break;
case SeColumnDefinition.TYPE_INT32:
System.out.println("\n\t" + colDef.getName()
+ " : " + row.getInteger(i));
break;
case SeColumnDefinition.TYPE_FLOAT32:
System.out.println("\t" + colDef.getName() + " : "
+ row.getFloat(i));
break;
case SeColumnDefinition.TYPE_FLOAT64:
System.out.println("\t" + colDef.getName() + " : "
+ row.getDouble(i));
break;
case SeColumnDefinition.TYPE_STRING:
System.out.println("\t" + colDef.getName() + " : "
+ row.getString(i));
break;
case SeColumnDefinition.TYPE_SHAPE:
SeShape spVal = (SeShape) row.getShape(i);
SeExtent sExt=spVal.getExtent();
double dCenterX=(sExt.getMaxX()+sExt.getMinX())/2;
double dCenterY=(sExt.getMaxY()+sExt.getMinY())/2;
break;
case SeColumnDefinition.TYPE_NSTRING:
System.out.println("\t" + colDef.getName() + " : "
+ row.getString(i));
break;
default:
System.out.println("Unknown Type");
System.out.println("\t" + colDef.getName() + " : ");
break;
}
}
}
row = spatialQuery.fetch();
}
spatialQuery.close();
SeState s=null;
session.close(s);
} catch (SeException sexp) {
System.out.println("SeException : "
+ sexp.getSeError().getErrDesc());
}
} catch (Exception e) {
}
}
}  回复  更多评论
  


只有注册用户登录后才能发表评论。


网站导航: