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中編譯的。。??窈筰ng。。我居然一直認為是我的人品問題,才導致這些代碼在我機器上就無法編譯成功滴。??磥碚f明了一個問題: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 閱讀(7205) 評論(9)  編輯  收藏 所屬分類: POSTGIS 、GEOTOOLS

          評論

          # 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) {
          }
          }
          }  回復  更多評論
            

          主站蜘蛛池模板: 诸暨市| 惠安县| 宁明县| 鞍山市| 玉环县| 库车县| 华容县| 比如县| 光泽县| 黔西县| 河北区| 文成县| 宝应县| 海盐县| 阿拉善右旗| 舟曲县| 乌拉特前旗| 阿坝| 西藏| 石河子市| 藁城市| 乌拉特后旗| 吴桥县| 定兴县| 普定县| 商河县| 临城县| 蕉岭县| 黄梅县| 新河县| 沙坪坝区| 田阳县| 资溪县| 江安县| 涞源县| 周宁县| 镇雄县| 蛟河市| 正蓝旗| 当涂县| 南乐县|