轉自: 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表中的例子:
- C:\>java oracle.spatial.util.SampleShapefile ToJGeomFeature -h 127.0.0.1 -p 1521
- -s orcl111 -u spatial -d spatial -t us_cities -f shp_cities -r 8307
- host: 127.0.0.1
- port: 1521
- sid: orcl111
- db_username: spatial
- db_password: spatial
- db_tablename: us_cities
- shapefile_name: shp_cities
- SRID: 8307
- Connecting to Oracle10g using...
- 127.0.0.1,1521,orcl111, spatial, spatial, us_cities, shp_cities, null, 8307
- Dropping old table...
- Creating new table...
- Converting record #10 of 195
- Converting record #20 of 195
- Converting record #30 of 195
- Converting record #40 of 195
- Converting record #50 of 195
- Converting record #60 of 195
- Converting record #70 of 195
- Converting record #80 of 195
- Converting record #90 of 195
- Converting record #100 of 195
- Converting record #110 of 195
- Converting record #120 of 195
- Converting record #130 of 195
- Converting record #140 of 195
- Converting record #150 of 195
- Converting record #160 of 195
- Converting record #170 of 195
- Converting record #180 of 195
- Converting record #190 of 195
- 195 record(s) converted.
- 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名稱。
- // Open SHP and DBF files
- ShapefileReaderJGeom shpr = new ShapefileReaderJGeom(shapeFileName);
- DBFReaderJGeom dbfr = new DBFReaderJGeom(shapeFileName);
- ShapefileFeatureJGeom sf = new ShapefileFeatureJGeom();