使用ESRI shapefile

轉自: http://book.51cto.com/art/200910/157532.htm

             http://www.gispark.com/html/jichu/2008/0326/2240.html

             http://www.gispark.com/html/jichu/2010/1008/3133.html



使用ESRI shapefile(1)

ESRI shapefile格式是進行地理數據傳輸的流行格式。在第5章中,我們曾用了一個簡單的命令行工具對shapefile進行轉換並把它們加載到空間表中。現在我們要展示的是怎樣在Java中對其進行讀寫。爲此,我們將要用到oracle.spatial.util包中的一些類。

如果你僅需把ESRI shapefile加載到數據庫表中,那麼oracle.spatial.util就能夠滿足你的需求。調用SampleShapefileToJGeomFeature類並向它傳遞合適的參數。如果你不瞭解這些參數,可以不包含任何參數,該類將告訴你這些參數的詳情。下面是把shp_cities shapefile內容加載到us_cities表中的例子:

  1. C:\>java oracle.spatial.util.SampleShapefile ToJGeomFeature -h 127.0.0.1 -p 1521  
  2. -s orcl111 -u spatial -d spatial -t us_cities -f shp_cities -r 8307  
  3. host: 127.0.0.1  
  4. port: 1521  
  5. sid: orcl111  
  6. db_username: spatial  
  7. db_password: spatial  
  8. db_tablename: us_cities  
  9. shapefile_name: shp_cities  
  10. SRID: 8307  
  11. Connecting to Oracle10g using...  
  12. 127.0.0.1,1521,orcl111, spatial, spatial, us_cities, shp_cities, null, 8307  
  13. Dropping old table...  
  14. Creating new table...  
  15. Converting record #10 of 195  
  16. Converting record #20 of 195  
  17. Converting record #30 of 195  
  18. Converting record #40 of 195  
  19. Converting record #50 of 195  
  20. Converting record #60 of 195  
  21. Converting record #70 of 195  
  22. Converting record #80 of 195  
  23. Converting record #90 of 195  
  24. Converting record #100 of 195  
  25. Converting record #110 of 195  
  26. Converting record #120 of 195  
  27. Converting record #130 of 195  
  28. Converting record #140 of 195  
  29. Converting record #150 of 195  
  30. Converting record #160 of 195  
  31. Converting record #170 of 195  
  32. Converting record #180 of 195  
  33. Converting record #190 of 195  
  34. 195 record(s) converted.  
  35. Done. 

該類將創建表、加載該表並在USER_SDO_GEOM_METADATA中插入合適的元數據。

1. 對shapefile的簡單說明

shapefile名字會讓人產生誤解。每個shapefile至少由3個文件構成,它們的文件名都相同,只是擴展名互不相同。例如,在前面例子中的shp_cities shapefile實際上是下面文件的集合:

shp_cities.shp:這個文件中保存了對實際幾何體的定義。

shp_cities.shx:在形狀上的空間索引。

shp_cities.dbf:一個dBASE Ⅳ文件,包含每個幾何體的屬性。

有些shapefile中也帶有其他一些文件,如shp_cities.prj或shp_cities.sbn。這些都會被Oracle Spatial Java 類所忽略。

在爲shapefile命名的時候不要包含.shp擴展名。所有組成同一個shapefile的文件都應該被存儲在同一個目錄下。

2. 在程序中加載shapefile

oracle.spatial.util包中包含了3個可以用來在你的程序中讀取和加載shapefile的類。表7-11是對它們的總結。

表7-11  Shapefile處理類

   

   

ShapefileReaderJGeom

提供函數,用來從shapefile中讀取形狀

(幾何體)並把它們轉換成JGeometry對象

DBFReaderJGeom

提供函數,用來從DBF文件中讀取

屬性並獲取該屬性的名稱和類型

ShapefileFeatureJGeom

使用兩個讀取類提供高端函數,用來

創建數據庫表並從shapefile中對其進行加載

下面的例子展示了怎樣用shapefile處理類把shapefile加載器簡單地合併到你的應用中。首先是打開輸入文件並設置輔助類。要注意的是,shapeFileName中包含的是沒有擴展名的shapefile名稱。

  1. // Open SHP and DBF files  
  2. ShapefileReaderJGeom shpr = new ShapefileReaderJGeom(shapeFileName);  
  3. DBFReaderJGeom dbfr = new DBFReaderJGeom(shapeFileName);  
  4. ShapefileFeatureJGeom sf = new ShapefileFeatureJGeom();

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章