Shape file 导入到Oracle Spatial 数据库

最近项目涉及该内容,但是网上搜索的很多oracle的相关链接都是以前的url,现在无法访问。

虽然shp2sdo.exe在CSDN上也有下载,但总归是需要积分的,另外,在找资源时,又发现了以下相关内容,也在此汇总,以便有需要或有兴趣的看官一用。


ps1:  JAVA SHAPEFILE CONVERTER,也是用于将esri的shp文件转换为oracle的数据库表的工具,原文介绍如下:

DESCRIPTION
The Java Shapefile Converter transforms an ESRI Shapefile into an Oracle database table
for use with Oracle Spatial and Locator. The Shapefile Converter uses the Oracle Spatial
Java-based Shapefile Adapter and SampleShapefileToJGeomFeature classes to load a
Shapefile directly into a database table, with the Oracle-equivalent .dbf data types for the
attribute columns and the SDO_GEOMETRY data type for the geometry column. The
Shapefile Adapter can also be used to create your own applications and interfaces that
transform Shapefiles to SDO_GEOMETRY or JGeometry data types (see the Oracle
Spatial Java API for more information). To simply load a Shapefile into the database, use
the SampleShapefileToJGeomFeature class as illustrated below.
文档传送门>>
文中涉及的;%ORACLE_HOME%\md\jlib\sdoutl.jar;%ORACLE_HOME%\md\jlib\sdoapi.jar  是安装什么版本的oracle附带的lib,烦请有知道的看官留言,在此不胜感激!

ps2:Oracle Spatial 空间信息管理  这本书貌似讲的很详细,CSDN有下载,上下两部分需要10分,但也有部分章节试读的;

试读传送门>>


回归正题,shp2sdo.exe 目前下载方法如下:

1、进入Oracle的搜索页面:https://search.oracle.com/search/

2、搜索shp2sdo,下面列出结果:

Shape file 导入到Oracle Spatial 数据库_第1张图片

第一个using-shp2sdo是使用说明,内容如下:

Example usage of the shape to sdo converter for Oracle9i Spatial and higher versions -

on Windows NT:

shp2sdo.exe states states -g geom -d -x (-180,180) -y (-90,90) -s 8307 -t 0.5 -v

on Sun Sparc Solaris or Linux:

shp2sdo.exe states states -g geom -d -x \(-180,180\) -y \(-90,90\) -s 8307 -t 0.5 -v

In the previous command:

states - the shapefile to convert (expects .dbf, .shp, and .shx files)

states - the name of the table to create and use in Oracle

-g  geom - geom is the name of the column to load for the geometry object

-d     - put the data in the control file generated by the tool

-x     - the bounds of the first dimension in the coordinate system

-y     - the bounds of the second dimension in the coordinate system

-s     - the SRID (spatial reference system ID)

-t     - the tolerance

-v     - verbose output


If the target database is Oracle8i, then use the -8 option on the 
command line.


Type shp2sdo.exe -h for help:

shp2sdo.exe -h

shp2sdo - Shapefile(r) To Oracle Spatial Converter
Version 2.14 09-Jan-2004
Copyright 1997,2004 Oracle Corporation
For use with Oracle Spatial.

USAGE: shp2sdo [-o]   -g 
               -i  -n  -p -d
               -x (xmin,xmax) -y (ymin,ymax) -s 
  or
       shp2sdo -r   -c  -n  -a -d
               -x (xmin,xmax) -y (ymin,ymax)
    shapefile           - name of input shape file
                          (Do not include suffix .shp .dbf or .shx)
    tablename           - spatial table name
                          if not specified: same as input file name
  Generic options:
    -o                  - Convert to object/relational format (default)
    -r                  - Convert to the relational format
    -d                  - store data in the control file
                          if not specified: keep data in separate files
    -x                  - bounds for the X dimension
    -y                  - bounds for the Y dimension
    -v                  - verbose output
    -h or -?            - print this message
  Options valid for the object model only:
    -g geometry column  - Name of the column used for the SDO_GEOMETRY object
                          if not specified: GEOM
    -i id_column        - Name of the column used for numbering the geometries
                          if not specified, no key column will be generated
                          if specified without name, use ID
    -n start_id         - Start number for IDs
                          if not specified, start at 1
    -p                  - Store points in the SDO_ORDINATES array
                          if not specified, store in SDO_POINT
    -s                  - Load SRID field in geometry and metadata
                          if not specified, SRID field is NULL
    -t                  - Load tolerance fields (x and y) in metadata
                          if not specified, tolerance fields are 0.00000005
    -8                  - Write control file in 8i format
                          if not specified, file written in 9i format
    -f                  - Write geometry data with 10 digits of precision
                          if not specified, 6 digits of precision is used
  Options valid for the relational model only:
    -c ordcount         - Number of ordinates in _SDOGOEM table
                          if not specified: 16 ordinates
    -n start_gid        - Start number for GIDs
                          if not specified, start at 1
    -a                  - attributes go in _SDOGEOM table
                          if not specified, attributes are in separate table


After running the shp2sdo converter the next step is to create the Oracle table
and load the user_sdo_geom_metadata table.  The file used to do this is generated
by the converter.  In the example above, it would be called states.sql.  Log into
SQL*Plus as the user who will own the layer, and type: 

@states.sql.

After this, load the data using sql*loader.  In the above example, assuming
user scott with password tiger, do:

sqlldr scott/tiger states

After the layer is loaded, the final requirement (for polygon layers only)
is to migrate the layer to the 8.1.6 and higher format (fixes the SDO_GTYPES 
and etypes as well as all polygon rotation and ordering requirements).  
After logging into SQL*Plus:

If you are using Oracle8i, type:

EXECUTE SDO_MIGRATE.FROM_815_TO_81X('STATES');

If you are using Oracle9i or higher, type:

EXECUTE SDO_MIGRATE.TO_CURRENT('STATES','GEOM');

像第二个结果那样,前面带压缩包图样的就是程序的下载链接。


如果以后 下载链接失效了,看官也可以仿照这样的步骤尝试找到自己想要的内容!


Ok,终于开始导入数据操作了:

首先,我参考的内容是这两个帖子:

如何将shp数据导入Oracle Spatial

如何将ESRI的Shapefile导入到Oracle数据库中(转)


1、根据平台将shp2sdo.exe解压到ORACLE_HOME/Bin下(也就是和sqlplus.exe同目录);

2、选择无中文的目录存放shp文件,例如:D:/app/shpfile/

示例shp文件为:test.shp,目标表为testtable 表中 唯一索引id 为objectid, 几何对象字段为shape。

3、在命令行内,转到shp文件所在目录,然后执行shp2sdo命令,如下:

cd d:/app/shpfile
d:
shp2sdo test testtable -g shape -d -s 4326 -t 0.5 -i objectid

-g 用于指定geometry对象的列名(默认是geom),

-i 用于指定唯一索引(默认是id)

其他参数说明参见上文。

执行命令后,会在同目录生成两个文件,分别是testtable.ctl 和 testtable.sql 

4、如果新建表的话,可以继续进行如下操作

sqlplus sde/sde@orcl
SQL>@testtable.sql
SQL>quit 

然后导入ctl文件

sqlldr sde/sde@orcl testtable

control = D:/app/shpfile/testtable.ctl

建立空间索引

sqlplus sde/sde@orcl
SQL>create index your_index_name ON testtable(shape) INDEXTYPE is MDSYS.SPATIAL_INDEX;

注意表名中间的字段是你表中定义的geometry对象的列名,这里示例中使用的是shape。

然后进入sqlplus,执行下面的语句,

SQL> EXECUTE SDO_MIGRATE.TO_CURRENT('testtable','shape');

上面是新增空间数据表的操作,但是我遇到的情况是向已有的数据表中插入记录,表结构和shp中数据结构还不一致,于是我的方法如下:

执行到上述第三步生成ctl和sql文件,这时,我们使用文本编辑器打开ctl文件,可以看到shp中的数据记录,
像我在实例中需要导入的是新增的坐标点,因此导出的ctl数据段格式如下:

BEGINDATA
 1|0|testDep1| 2001|4326|112.97|37.62|
 2|0|testDep2| 2001|4326|119.15|32.67|

然后我根据我的数据表格式拼写sql:

INSERT INTO  testtable (objectid, fid_1, name, shape)  VALUES (sde.version_user_ddl.next_row_id('SDE',98), 'testDep1_fid_001','testDep1', sde.st_geometry('point (112.97 37.62)',2));

其中next_row_id('SDE',98) 里面的98是sequence的名称。

使用这种方式,我顺利将shp中的空间数据导入到已有的数据表中。

sde.st_geometry 类型说明:传送门>> ————里面有介绍sde.st_geometry的使用方法,以及其他sde的数据类型的使用方法。


看在我辛勤编写的份上,转载请注明出处!

http://blog.csdn.net/shangyue1110/article/details/41355887






你可能感兴趣的:(Oracle)