使用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();

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