sinoly

             :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            29 隨筆 :: 76 文章 :: 189 評(píng)論 :: 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;?? // 數(shù)據(jù)容器
          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( " 系統(tǒng)連接到位于: " + URL + " 的空間數(shù)據(jù)庫(kù) " + database + " 成功! " );
          38 ????
          39 ???}
          else {
          40 ????System.out.println( " 系統(tǒng)連接到位于: " + URL + " 的空間數(shù)據(jù)庫(kù) " + database + " 失敗!請(qǐng)檢查相關(guān)參數(shù) " );
          41 ???}

          42 ??}
          ? catch ?(IOException?e)? {
          43 ???e.printStackTrace();
          44 ???System.out.println( " 系統(tǒng)連接到位于: " + URL + " 的空間數(shù)據(jù)庫(kù) " + database + " 失敗!請(qǐng)檢查相關(guān)參數(shù) " );
          45 ??}

          46 ?}

          47 ?
          48 ? public ? static ? void ?main(String[]?args)? throws ?IOException {
          49 ??ConnPostGis( "" , " localhost " , 5432 , " navigation " , " root " , " 123 " );
          50 ?? /* 讀取指定類(lèi)型名的地理特征? */
          51 ??FeatureSource?fsBC? = ?pgDatastore.getFeatureSource( " roads " );
          52 ??System.out.println(fsBC.getFeatures().size());
          53 ?? /* 得到空間數(shù)據(jù)庫(kù)中所有特征表的表名 */
          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



          ???? 進(jìn)行Geotools的開(kāi)發(fā)工作,肯定會(huì)參考GeoTools的官方參考文檔。不過(guò)我想很多人估計(jì)都遇到了和我一樣的問(wèn)題,就是官方參考指南中幾乎沒(méi)有可以編譯通過(guò)的代碼,也就無(wú)法談及用這些代碼進(jìn)行學(xué)習(xí)了。昨天抽空將Geotools指南中連接到POSTGIS的代碼進(jìn)行了重新編譯,才發(fā)現(xiàn)指南中的代碼是在geotools 2.1.4中編譯的。。。狂汗ing。。我居然一直認(rèn)為是我的人品問(wèn)題,才導(dǎo)致這些代碼在我機(jī)器上就無(wú)法編譯成功滴。。看來(lái)說(shuō)明了一個(gè)問(wèn)題:4475_200361112122146460.gif.。。。
          ????? 同時(shí)在編譯過(guò)程中還翻閱了2.3的API,也在其中發(fā)現(xiàn)了幾個(gè)對(duì)原有方法進(jìn)行功能修改或者干脆廢棄的方法,在此列出:
          ??????1、如果你和我一樣使用了POSTGRESQL 8.4,sorry,麻煩你不要用其自帶的任何JDBC驅(qū)動(dòng),直接使用geotools中帶的數(shù)據(jù)庫(kù)驅(qū)動(dòng)吧。不要問(wèn)我為什么,我也不知道,我唯一知道的就是只有這樣程序才能真正的連接到postgis。
          ??? 2、gt2-main.jar;gt2-postgis.jar;JTS-1.4.jar;geoapi-1.1.0alpha.jar;vecmath-1.2.1.jar。你需要確定這幾個(gè)文件存在與你的項(xiàng)目構(gòu)建之中。
          ?? 3、原有取得指定圖層Feature總數(shù)的方法是FeatureSource.getCount(Query.ALL)),這個(gè)方法已經(jīng)不能使用了,可以更換成getFeatures().size()方法來(lái)取得Feature的總數(shù)。(ps:雖說(shuō)取得總數(shù)的方法用的不多,不過(guò)我覺(jué)得用這個(gè)方法來(lái)測(cè)試是否能連接到postgis還是很方便的。)
          ?? 4、應(yīng)用中最好能自己重寫(xiě)postgis的連接池(可不是上面例子中的這種簡(jiǎn)單玩意,嘿嘿),一般關(guān)系數(shù)據(jù)庫(kù)額連接池有很多中解決方式,但postgis的數(shù)據(jù)庫(kù)連接池目前我還不知道有什么好的解決方式,之所以要自己寫(xiě)連接池的目的就是為了更好的控制postgis的空間索引關(guān)系。



          posted on 2007-01-25 09:55 sinoly 閱讀(7208) 評(píng)論(9)  編輯  收藏 所屬分類(lèi): POSTGISGEOTOOLS

          評(píng)論

          # re: 使用GeoTools 2.3M連接到POSTGIS數(shù)據(jù)庫(kù)[未登錄](méi) 2007-01-25 10:07 sinoly
          如果有兄弟對(duì)開(kāi)源GIS平臺(tái)開(kāi)發(fā)感興趣的,考慮一下,一起搞搞:)
            回復(fù)  更多評(píng)論
            

          # re: 使用GeoTools 2.3M連接到POSTGIS數(shù)據(jù)庫(kù) 2007-01-25 12:49 Flyingis
          我有興趣,但現(xiàn)在自由的時(shí)間少了,大家可以一起探討。
          有沒(méi)有興趣加入WebGIS開(kāi)發(fā)設(shè)計(jì)組,把3S相關(guān)的文章整理在一起?  回復(fù)  更多評(píng)論
            

          # re: 使用GeoTools 2.3M連接到POSTGIS數(shù)據(jù)庫(kù)[未登錄](méi) 2007-05-22 09:16 過(guò)客
          有探討的群沒(méi)?我在學(xué)習(xí),想&大家一塊探討。有的話加我啊
          373572418  回復(fù)  更多評(píng)論
            

          # re: 使用GeoTools 2.3M連接到POSTGIS數(shù)據(jù)庫(kù) 2007-12-29 16:17 前進(jìn)
          我了很想和大家一起探討,79666049  回復(fù)  更多評(píng)論
            

          # re: 使用GeoTools 2.3M連接到POSTGIS數(shù)據(jù)庫(kù) 2008-01-02 11:32 前進(jìn)
          為了方便大家交流,請(qǐng)加入我的開(kāi)源GIS項(xiàng)目分析的QQ群40087303,現(xiàn)在已經(jīng)有很多碩士和博士,有興趣的朋友快加入吧,我們一起為提高中國(guó)開(kāi)源實(shí)力而努力!現(xiàn)階段分析的項(xiàng)目是SharpMap、Geoserver、Openlayers、GRASS、PostgerSQL、Geotools等
          我的QQ號(hào):79666049


          群號(hào):40087303
          群名:開(kāi)源GIS項(xiàng)目分析
            回復(fù)  更多評(píng)論
            

          # re: 使用GeoTools 2.3M連接到POSTGIS數(shù)據(jù)庫(kù) 2008-01-02 17:13 kenzhang
          最近一直在學(xué)openGis,博主的文章也一直在看,收獲頗多,感謝!  回復(fù)  更多評(píng)論
            

          # re: 使用GeoTools 2.3M連接到POSTGIS數(shù)據(jù)庫(kù) 2008-07-15 10:10 ads
          請(qǐng)問(wèn)一下hibernate hql 支持 postgis函數(shù)嗎???  回復(fù)  更多評(píng)論
            

          # re: 使用GeoTools 2.3M連接到POSTGIS數(shù)據(jù)庫(kù) 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) {
          }
          }
          }  回復(fù)  更多評(píng)論
            

          # re: 使用GeoTools 2.3M連接到POSTGIS數(shù)據(jù)庫(kù) 2008-12-08 11:02 songhf
          幫忙看看這個(gè)問(wèn)題:
          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) {
          }
          }
          }  回復(fù)  更多評(píng)論
            

          主站蜘蛛池模板: 肥乡县| 扎兰屯市| 泸水县| 潍坊市| 桐庐县| 大港区| 廉江市| 景德镇市| 和田市| 栾川县| 察雅县| 利川市| 宁晋县| 句容市| 南投县| 大庆市| 田林县| 广南县| 且末县| 容城县| 镇赉县| 万载县| 寿宁县| 黑河市| 平湖市| 娱乐| 昌江| 伊吾县| 夏河县| 修文县| 桑日县| 微山县| 乐东| 宣恩县| 耿马| 新干县| 策勒县| 铁岭市| 双辽市| 全南县| 天等县|