sinoly

             :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            29 隨筆 :: 76 文章 :: 189 評(píng)論 :: 0 Trackbacks
          ????? 唉,這幾天撒事情都沒有做,就搞定了GeoTools的一些bug的刪除。2.3版本問題比較多,上次在GT上和Richard聊天,他也對(duì)GeoTools目前的狀況表示很是不滿,但是為了PostGis的推廣更順利(畢竟,大多數(shù)使用Postgis的兄臺(tái)還是在geotools平臺(tái)上),很多時(shí)候很多事情都不是我們能決定的。。。所以只能自己動(dòng)手去將GeoTools在文檔中沒有說清楚的,或者無法編譯的程序根據(jù)新的API從新做一次。
          ???? 這幾天搞定了GeoTools的PostgisDataStore操作。包含連接、讀取、新建、插入等等。其實(shí)很多功能比如新建和插入都可以用SQL語句去完成,不過這樣做對(duì)postgis即將推出的空間索引還是有很大影響的。所以我們沒有使用SQL去完成這些工作。
          ???
          /*
          ?*?POSTGEO?
          ?
          */

          package?com.geotools.test;

          /**
          ?*?
          ?*?CopyRight?(C)?All?rights?reserved.
          ?*?<p>
          ?*?
          ?*?WuHan?Inpoint?Information?Technology?Development,Inc.
          ?*?<p>
          ?*?
          ?*?Author?sinoly
          ?*?<p>
          ?*?Project?Name:?PostGeo
          ?*?
          ?*?
          @version?1.0?2006-11-13
          ?*?
          ?*?<p>
          ?*?Base?on?:?JDK1.5
          ?*?<p>
          ?*?
          ?
          */

          import?java.io.IOException;
          import?java.util.HashMap;
          import?java.util.Map;
          import?java.util.NoSuchElementException;

          import?org.apache.log4j.Logger;
          import?org.geotools.data.FeatureReader;
          import?org.geotools.data.FeatureResults;
          import?org.geotools.data.FeatureSource;
          import?org.geotools.data.FeatureStore;
          import?org.geotools.data.FeatureWriter;
          import?org.geotools.data.postgis.PostgisDataStore;
          import?org.geotools.data.postgis.PostgisDataStoreFactory;
          import?org.geotools.factory.FactoryRegistryException;
          import?org.geotools.feature.AttributeType;
          import?org.geotools.feature.AttributeTypeFactory;
          import?org.geotools.feature.Feature;
          import?org.geotools.feature.FeatureType;
          import?org.geotools.feature.FeatureTypeFactory;
          import?org.geotools.feature.IllegalAttributeException;
          import?org.geotools.feature.SchemaException;
          import?org.geotools.geometry.Geometry;

          import?com.vividsolutions.jts.geom.LineString;
          import?com.vividsolutions.jts.io.ParseException;
          import?com.vividsolutions.jts.io.WKTReader;

          public?class?GetPostgisData?{
          ????
          private?static?final?Logger?LOGGER?=?Logger.getLogger("org.geotools.postgis");

          ????
          static?PostgisDataStore?pgDatastore;
          ????
          static?PostgisDataStoreFactory?factory=new?PostgisDataStoreFactory();
          ????
          static?FeatureSource?fsBC;
          ????@SuppressWarnings(
          "unchecked")
          ????
          private?static?void?ConnPostGis(String?dbtype,String?URL,int?port,String?database,
          ????????String?user,String?password)
          {
          ????????Map?params?
          =?new?HashMap();
          ????????params.put(
          "dbtype",?"postgis");
          ????????params.put(
          "host",?URL);
          ????????params.put(
          "port",?new?Integer(port));
          ????????params.put(
          "database",?database);
          ????????params.put(
          "user",?user);
          ????????params.put(
          "passwd",?password);????????
          ????????
          try?{
          ????????????pgDatastore
          =(PostgisDataStore)?factory.createDataStore(?params?);
          ????????????
          if(pgDatastore!=null){
          ????????????????System.out.println(
          "系統(tǒng)連接到位于:"+URL+"的空間數(shù)據(jù)庫"+database+"成功!");
          ????????????}
          else{
          ????????????????System.out.println(
          "系統(tǒng)連接到位于:"+URL+"的空間數(shù)據(jù)庫"+database+"失敗!請(qǐng)檢查相關(guān)參數(shù)");
          ????????????}

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

          ????}


          ????
          //讀取指定類型名的地理特征?
          ????public?static?void?getFeatureSource(String?sourceName){
          ????????
          try?{
          ????????????fsBC?
          =?pgDatastore.getFeatureSource(sourceName);
          ????????????
          //System.out.println(fsBC.getFeatures().size());
          ????????}
          ?catch?(IOException?e)?{
          ????????????e.printStackTrace();
          ????????}
          ????????
          ????}

          ????
          //取得POSTGIS中所有的地理圖層
          ????public?static?void?getAllLayers(){
          ????????
          try?{
          ????????????String[]?typeName?
          =?pgDatastore.getTypeNames();
          ????????????
          for(int?i=0;i<typeName.length;i++){
          ????????????????System.out.println(typeName[i]);
          ????????????}

          ????????}
          ?catch?(IOException?e)?{
          ????????????e.printStackTrace();
          ????????}

          ????}

          ????
          //獲取圖層地理元素屬性(Feature?Attribute)
          ????public?static?void?getAttribute(){
          ????????FeatureType?ftBC
          =fsBC.getSchema();
          ????????System.out.println(ftBC.getAttributeCount());
          ????????
          for?(int?i?=?0;?i?<?ftBC.getAttributeCount();?i++)?{
          ????????????AttributeType?at?
          =?ftBC.getAttributeType(?i?);
          ????????????
          //判斷屬性類型是否為可分配的幾何對(duì)象
          ????????????if?(!Geometry.class.isAssignableFrom(at.getType()))
          ????????????????System.out.print(at.getType()?
          +?"\t");
          ????????}

          ????????System.out.println();
          ????????
          for?(int?i?=?0;?i?<?ftBC.getAttributeCount();?i++)?{
          ????????????AttributeType?at?
          =?ftBC.getAttributeType(?i?);
          ????????????
          if?(!Geometry.class.isAssignableFrom(at.getType()))
          ????????????????System.out.print(at.getName()?
          +?"\t");
          ????????}

          ????}

          ????
          ????
          //從數(shù)據(jù)容器中讀取所有的特征屬性?
          ????@SuppressWarnings("deprecation")
          ????
          public?static?void?PostGisReading(){
          ????????
          try?{
          ????????????FeatureResults?fsRU?
          =?fsBC.getFeatures();
          ????????????FeatureReader?reader?
          =?fsRU.reader();
          ????????????
          while?(reader.hasNext())?{
          ????????????????Feature?feature;
          ????????????????
          try?{
          ????????????????????feature?
          =?reader.next();
          ????????????????????System.out.print(feature.getID()?
          +?"\t");
          ????????????????????
          for?(int?i?=?0;?i?<?feature.getNumberOfAttributes();?i++)?{
          ????????????????????????Object?attribute?
          =?feature.getAttribute(?i?);
          ????????????????????????
          if?(!(attribute?instanceof?Geometry))
          ????????????????????????????System.out.print(attribute?
          +?"\t");
          ????????????????????}

          ????????????????????System.out.println();
          ????????????????}
          ?catch?(NoSuchElementException?e)?{
          ????????????????????e.printStackTrace();
          ????????????????}
          ?catch?(IllegalAttributeException?e)?{
          ????????????????????e.printStackTrace();
          ????????????????}

          ????????????}

          ????????????reader.close();
          ????????}
          ?catch?(IOException?e1)?{
          ????????????e1.printStackTrace();
          ????????}

          ????}

          ????
          ????
          //添加特征值到新的特征對(duì)象中。等同于新建一個(gè)postgis數(shù)據(jù)表并向其中插入數(shù)據(jù)
          ????@SuppressWarnings("deprecation")
          ????
          public?static?void?createFeatures(){
          ????????
          try?{
          ????????????AttributeType?geom?
          =?AttributeTypeFactory.newAttributeType("the_geom",LineString.class);
          ????????????AttributeType?name?
          =?AttributeTypeFactory.newAttributeType("name",String.class);
          ????????????FeatureType?ftRoad?
          =?FeatureTypeFactory.newFeatureType
          ????????????????????????????????(
          new?AttributeType[]?{geom,name},?"tem_road");
          ????????????WKTReader?wktReader?
          =?new?WKTReader();
          ????????????
          try?{
          ????????????????LineString?geometry?
          =?(LineString)?wktReader.read("LINESTRING?(0?0,?10?10)");
          ????????????????String?roadName
          ="武絡(luò)路";
          ????????????????pgDatastore.createSchema(ftRoad);
          ????????????????FeatureWriter?aWriter?
          =?pgDatastore.getFeatureWriter("tem_road",
          ????????????????????????((FeatureStore)?pgDatastore.getFeatureSource(
          "tem_road")).getTransaction());
          ????????????????
          /**如有批量導(dǎo)入數(shù)據(jù)要求,可使用?org.geotools.data.FeatureStore?*/
          ????????????????Feature?aNewFeature?
          =?aWriter.next();
          ????????????????aNewFeature.setAttribute(
          "the_geom",geometry);
          ????????????????aNewFeature.setAttribute(
          "name",?roadName);
          ????????????????aWriter.write();
          ????????????????aWriter.close();
          ????????????}
          ?catch?(ParseException?e)?{
          ????????????????e.printStackTrace();
          ????????????}
          ?catch?(IllegalAttributeException?e)?{
          ????????????????e.printStackTrace();
          ????????????}
          ?catch?(IOException?e)?{
          ????????????????e.printStackTrace();
          ????????????}

          ????????}
          ?catch?(FactoryRegistryException?e)?{
          ????????????e.printStackTrace();
          ????????}
          ?catch?(SchemaException?e)?{
          ????????????e.printStackTrace();
          ????????}

          ????}

          ????
          ????
          //添加Feature到已知的圖層之中
          ????public?static?void?insertFeatures(String?featurename){
          ????????WKTReader?wktReader?
          =?new?WKTReader();
          ????????
          try?{
          ????????????LineString?geometry?
          =?(LineString)?wktReader.read("LINESTRING?(10?10,?20?20)");
          ????????????String?roadName
          ="珞瑜路";
          ????????????FeatureSource?source?
          =?pgDatastore.getFeatureSource(featurename);
          ????????????FeatureWriter?aWriter?
          =?pgDatastore.getFeatureWriterAppend(featurename,((FeatureStore)?source).getTransaction());
          ????????????
          /**如有批量導(dǎo)入數(shù)據(jù)要求,可使用?org.geotools.data.FeatureStore?*/
          ????????????Feature?feature?
          =?aWriter.next();
          ????????????
          try?{
          ????????????????feature.setAttribute(
          "the_geom",geometry);
          ????????????????feature.setAttribute(
          "name",?roadName);
          ????????????}
          ?catch?(IllegalAttributeException?e)?{
          ????????????????
          //?TODO?自動(dòng)生成?catch?塊
          ????????????????e.printStackTrace();
          ????????????}

          ????????????aWriter.write();
          ????????????aWriter.close();
          ????????}
          ?catch?(ParseException?e1)?{
          ????????????
          //?TODO?自動(dòng)生成?catch?塊
          ????????????e1.printStackTrace();
          ????????}
          ?catch?(IOException?e)?{
          ????????????
          //?TODO?自動(dòng)生成?catch?塊
          ????????????e.printStackTrace();
          ????????}


          ????}

          ????
          ????
          public?static?void?main(String[]?args)?throws?IOException{
          ????????ConnPostGis(
          "","localhost",5432,"navigation","root","to0124@c");
          ????????
          /*讀取空間庫中所有圖層*/
          ????????getAllLayers();
          ????????
          /*讀取roads圖層的空間庫,取得FeatureSource對(duì)象,
          ?????????*?getAttribute()方法用于讀取此圖層所定義的所有的屬性
          ?????????*?并通過PostGisReading()方法讀取此圖層中所有信息
          */

          ????????getFeatureSource(
          "roads");
          ????????getAttribute();
          ????????PostGisReading();
          ????????
          /*在空間庫中新建一個(gè)schema并向表中插入數(shù)據(jù)*/
          ????????createFeatures();
          ????????
          /*向tem_road圖層的空間庫中插入一條新的記錄*/
          ????????insertFeatures(
          "tem_road");
          ????????
          /*修改空間庫記錄*/
          ????}

          }


          ????? 目前對(duì)GeoTools真的有些失望。。。不過作為一個(gè)開源中間件,它做到目前這個(gè)地位真的除了它推出比較早以外,還得感謝開源社區(qū)的幫助,2006年10月份推出的2.2版本其中對(duì)WMS中圖層的渲染效率的大幅提升就是得益與Richard兄臺(tái)的絕妙建議。自己也希望能向Richard學(xué)習(xí),一個(gè)43歲的丹麥程序員,真的讓人真的體會(huì)到什么叫“Open Mind”。。。過年期間的事情也有了眉目,著力修正shp2pqsql的問題。目前隨post安裝版推出的這個(gè)程序還是有些bug,在中文編碼以及容錯(cuò)方面都需要提高。


          posted on 2007-01-31 22:58 sinoly 閱讀(11576) 評(píng)論(16)  編輯  收藏 所屬分類: GEOTOOLS

          評(píng)論

          # re: 終于搞定GeoTools對(duì)PostGis的操作 2007-01-31 23:07 sinoly
          忘記提醒了,操作過程中由于2.3版本還有一個(gè)重大bug暫時(shí)沒有修改,所以為了createFeatures()的執(zhí)行,將gt2-postgis-2.3.jar換成了2.2.1版本中對(duì)應(yīng)的文件。
          不知道為什么SF上下2.3的源代碼怎么都無法完成。。。繼續(xù)等待。。。到時(shí)候看看源碼中是否可以更正這個(gè)問題  回復(fù)  更多評(píng)論
            

          # re: 終于搞定GeoTools對(duì)PostGis的操作 2007-02-01 00:24 施偉
          GIS不熟。。。
          提個(gè)小建議,把類似
          for (int i = 0; i < feature.getNumberOfAttributes(); i++)
          的代碼改為用迭代器,或者改為
          for (int i = 0, int x = feature.getNumberOfAttributes(); i < x; i++)
          效率應(yīng)該會(huì)好些。。。
          至于原因嘛 因?yàn)閒eature.getNumberOfAttributes()這種東西值是會(huì)變的,如果你在循環(huán)里操作了featrue那就很危險(xiǎn)了。。。
          還有就是每次循環(huán)都會(huì)計(jì)算feature.getNumberOfAttributes()的值并新開一個(gè)空間來存,很不劃算的說。。。  回復(fù)  更多評(píng)論
            

          # re: 終于搞定GeoTools對(duì)PostGis的操作 2007-02-01 00:36 sinoly
          呵呵,收錄老弟的意見。。。  回復(fù)  更多評(píng)論
            

          # re: 終于搞定GeoTools對(duì)PostGis的操作 2007-02-01 00:42 一生
          也準(zhǔn)備學(xué)習(xí)GT  回復(fù)  更多評(píng)論
            

          # re: 終于搞定GeoTools對(duì)PostGis的操作 2007-02-01 11:55 sinoly
          呵呵,“一生”兄弟,有空一起研究:)
            回復(fù)  更多評(píng)論
            

          # re: 終于搞定GeoTools對(duì)PostGis的操作 2007-02-01 14:08 梅穎
          嗯,我也關(guān)注一哈啊,呵呵  回復(fù)  更多評(píng)論
            

          # re: 終于搞定GeoTools對(duì)PostGis的操作 2007-02-09 09:20 maxcm
          你好,我是個(gè)學(xué)geotools的新手,現(xiàn)在公司要做一個(gè)項(xiàng)目,需要用到開源的項(xiàng)目,現(xiàn)在苦于不知道該怎么去學(xué),入門比較難。想請(qǐng)大哥能指點(diǎn)一下。。謝謝了
            回復(fù)  更多評(píng)論
            

          # re: 終于搞定GeoTools對(duì)PostGis的操作 2007-02-09 09:33 maxcm
          還有,那個(gè)geoserver和geotools有什么聯(lián)系,資料太少了,而且過了年以后,項(xiàng)目就要啟動(dòng)了,真是急死人了!
          希望大哥能幫幫我,不勝感激!!!!!  回復(fù)  更多評(píng)論
            

          # re: 終于搞定GeoTools對(duì)PostGis的操作 2007-02-09 09:37 sinoly
          @maxcm
          呵呵,我是摸索著前進(jìn)。但愿發(fā)布的這些學(xué)習(xí)歷程能對(duì)你有幫助
          學(xué)習(xí)嗎,呵呵,geoserver是Geotools的一個(gè)相當(dāng)好的實(shí)現(xiàn)。。。當(dāng)然,geotools里面還包含了其他很多的技術(shù)
          geoserver的幾何對(duì)象操作、空間數(shù)據(jù)運(yùn)算都是基于JTS的。。。
          。。。 。。。
          加我msn好了。。。sinoly(at)msn.com  回復(fù)  更多評(píng)論
            

          # re: 終于搞定GeoTools對(duì)PostGis的操作 2007-02-09 11:39 梅穎
          我能加不呵呵  回復(fù)  更多評(píng)論
            

          # re: 終于搞定GeoTools對(duì)PostGis的操作 2007-02-09 16:50 sinoly
          貌似你加了很久了滴說  回復(fù)  更多評(píng)論
            

          # re: 終于搞定GeoTools對(duì)PostGis的操作 2007-03-17 22:37 cyqian
          求助
          FeatureSource fsBC=pgDataStore.getFeatureSource(featureName);

          我加入了上面getFeatureSource這句之后就有問題,不了解哎~~

          下面是警告的一部分:

          2007-3-17 22:19:38 FactoryRegistry scanForPlugins
          警告: Can't load a service for category "CRSAuthorityFactory". Cause is "NoClassDefFoundError: javax/units/ConversionException".
          2007-3-17 22:19:38 FactoryRegistry scanForPlugins
          警告: Can't load a service for category "CRSAuthorityFactory". Cause is "NoClassDefFoundError: javax/units/ConversionException".
          2007-3-17 22:19:38 FactoryRegistry scanForPlugins
          警告: Can't load a service for category "CRSAuthorityFactory". Cause is "NoClassDefFoundError: javax/units/ConversionException".
          Exception in thread "main" java.lang.StackOverflowError
          at java.util.HashMap.get(HashMap.java:343)
          at java.awt.RenderingHints.get(RenderingHints.java:461)
          at org.geotools.factory.FactoryRegistry.getServiceProvider(FactoryRegistry.java:223)
          at org.geotools.factory.FactoryCreator.getServiceProvider(FactoryCreator.java:115)
          at org.geotools.referencing.FactoryFinder.getAuthorityFactory(FactoryFinder.java:235)
          at org.geotools.referencing.FactoryFinder.getCRSAuthorityFactory(FactoryFinder.java:496)
          at org.geotools.referencing.factory.epsg.LongitudeFirstFactory.createBackingStore(LongitudeFirstFactory.java:180)
          at org.geotools.referencing.factory.DeferredAuthorityFactory.getBackingStore(DeferredAuthorityFactory.java:136)
          at org.geotools.referencing.factory.BufferedAuthorityFactory.isAvailable(BufferedAuthorityFactory.java:206)
          at org.geotools.referencing.factory.DeferredAuthorityFactory.isAvailable(DeferredAuthorityFactory.java:124)
          at org.geotools.factory.FactoryRegistry$DefaultFilter.filter(FactoryRegistry.java:76)
          at javax.imageio.spi.FilterIterator.advance(ServiceRegistry.java:793)
          at javax.imageio.spi.FilterIterator.<init>(ServiceRegistry.java:787)  回復(fù)  更多評(píng)論
            

          # re: 終于搞定GeoTools對(duì)PostGis的操作 2007-03-18 19:59 sinoly
          @cyqian
          你是不是引入了gt2-postgis-2.3.jar?
          這個(gè)包需要替換到2.2版本。


          ps:你這里貌似還不是這個(gè)問題。。。如果你放心可以把你的代碼發(fā)給我。。呵呵。。我們一起看看
            回復(fù)  更多評(píng)論
            

          # re: 終于搞定GeoTools對(duì)PostGis的操作 2007-04-08 21:02 gaoang
          盡管已讀過英文版的,還是謝謝分享 :)  回復(fù)  更多評(píng)論
            

          # re: 終于搞定GeoTools對(duì)PostGis的操作 2007-10-23 09:52 mreay
          正在了解這方面的內(nèi)容,學(xué)習(xí)中
          加你MSN中!  回復(fù)  更多評(píng)論
            

          # re: 終于搞定GeoTools對(duì)PostGis的操作 2008-11-12 10:40 大智
          我想請(qǐng)教一個(gè)問題:

          我在java程序中應(yīng)該如何調(diào)用postgis中的那個(gè)空間分析的函數(shù)??像distance,buffer等等, 是需要postgis的庫嗎?好像下載的庫里面找不到  回復(fù)  更多評(píng)論
            

          主站蜘蛛池模板: 易门县| 清丰县| 巩义市| 和田市| 屯昌县| 游戏| 乌兰察布市| 灵寿县| 枞阳县| 阿勒泰市| 玉林市| 广州市| 菏泽市| 怀安县| 莲花县| 永州市| 望奎县| 密山市| 鸡泽县| 威远县| 罗源县| 噶尔县| 奉新县| 读书| 孝昌县| 莫力| 获嘉县| 镇坪县| 蓬溪县| 石河子市| 余庆县| 咸丰县| 桃江县| 宿迁市| 自治县| 舟曲县| 长海县| 石家庄市| 宿迁市| 铅山县| 分宜县|