如何将shapefile文件导入到oracle数据库(转载)

采用开源的Geoserver、Geotools、Mapbuilder来开发,数据库使用oracle spataral,刚开始是一
直在看别人已经做好的一个半成品系统,自己模仿着来改,GIS采用的数据格式比较特殊,不能使用一般的导表方式来导,而且地图文
件的格式也比较特殊,一般是shp、或者是tab,这里主要是用的shp文件,因此需要将shp文件进行处理,转换成oracle数据格式,其实
也没什么难的,只是如果不熟悉的话,可能也需要浪费一点时间来看一下。
首先我们要把shp文件转换分割成oracle可以导入的数据库文件,这个转换可以使用oracle提供的一个现成的工具shp2sdo(很小),可
以到官方网站去找,下载后把此文件复制到PATH变量包含的目录下,如我的oracle客户
端安装后自动注册的环境变量是 path C:/Oracle/product/10.1.0/Client_1/bin;,我们可以把此文件拷贝到该目录下,然后在dos下运行该
工具,定位到我们的shp文件的位置,例如我们的shp文件名称是state.shp(shapefile包括至少三个文件state.shp state.dbf state.idx)
在D:/data/目录下,我们在dos命令窗口下就应该这样操作
shp2sdo state statearea -i gid -s 8307 -g -d

shp2sdo shapefile tablename -g geom -d -s 8307 -t 0.5 -v,
其中state即为本地的shp文件名(不加shp后缀),statearea为生成的文件名(同时也是将来导进数据库的表名),-d代表含义是将分
解后的 ctl文件(控制文件)和data文件(数据存储文件)分别生成,如果没有该选项,则不会有单独的data文件生成,数据存储和控制
都在ctl一个文件中, 经常用到的还有选项 -i id_colum指定id序列列,默认是id,即作为生成数据的唯一性标志,此列是不可能重复的,一
般作为索引列,-s **指定生成srid,默认是null,目前一般是8307吧,-g geometry column指定sdo_geometry,默认是GEOM,此选项
一般使用默认即可,命令执行后,会生成三个文件,statearea.sql、 statearea.ctl、stateare.data。
分解完成后就是导入,这里仍然使用命令行的方式
D:/data/>sqlplus pgg/pgg@orcl
SQL>@statearea.sql
SQL>quit
导入ctl文件
D:/data/>sqlldr pgg/pgg@orcl statearea
建立空间索引
D:/data/>sqlplus pgg/pgg@orcl
SQL>CREATE Index STATEAREA_idx ON STATEAREA (GEOM) INDEXTYPE is MDSYS.SPATIAL_INDEX;
至此空间数据导入完毕。
另外还需要建立一个视图,具体作用说不大清楚,但在启动程序发布地图的时候会用到,如果没有此视图文件会出现图层无法map的错
误,建立过程如下:
SQL> CREATE OR REPLACE VIEW CS_SRS AS
2 SELECT "CS_NAME","SRID","AUTH_SRID","AUTH_NAME","WKTEXT","CS_BOUNDS"
3 FROM MDSYS.CS_SRS
4 /
另外如果自己手工新建图层的话,过程如下:
DROP TABLE BZXX;
CREATE TABLE BZXX(
GID VARCHAR2(32) NOT NULL,
NAME VARCHAR2(20),
BZNR VARCHAR2(400),
BEIZ VARCHAR2(600),
COOR MDSYS.SDO_GEOMETRY
);
ALTER TABLE BZXX
ADD CONSTRAINT PK_BZXX PRIMARY KEY (GID);
DELETE FROM USER_SDO_GEOM_METADATA
WHERE TABLE_NAME = ‘BZXX’ AND COLUMN_NAME = ‘COOR’ ;
INSERT INTO USER_SDO_GEOM_METADATA (TABLE_NAME, COLUMN_NAME, DIMINFO, SRID)
VALUES (‘BZXX’, ‘COOR’,
MDSYS.SDO_DIM_ARRAY
(MDSYS.SDO_DIM_ELEMENT(‘X’, -180.000000000, 180.000000000, 0.500000000),
MDSYS.SDO_DIM_ELEMENT(‘Y’, -90.000000000, 90.000000000, 0.500000000)
),
8307);
COMMIT;
CREATE INDEX BZXX_GEOM_INDEX ON BZXX(COOR)
INDEXTYPE IS MDSYS.SPATIAL_INDEX
PARAMETERS(‘SDO_INDX_DIMS=2, LAYER_GTYPE=POINT’);

 

附注:各参数的含义

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

【转】

Step 1
Download tool Oracle Shapefile Converter at http://www.oracle.com/technology/software/products/spatial/index.html and extract it into any directory.
Open the directory correspond with your operating system (ex: directory shp2sdo_nt for MS Windows), called CONVERT_HOME.
Call the directory that contains your shapefiles is SHAPE_HOME.
Call the directory that installs Oracle 10g is ORACLE_HOME.

Step 2
1. Open console, run the following commands:
> cd $SHAPE_HOME
> set path="$CONVERT_HOME"

2. Run shp2sdo converter:

  • on Windows NT
shp2sdo.exe  <table_name> -g <geom_name> -d -x (<minx>,<maxx>) -y (<miny>,<maxy>) -s <SRS> -t <tolerance> -v
  • on Sun Sparc Solaris or Linux
shp2sdo.exe  <table_name> -g <geom_name> -d -x /(<minx>,<maxx>/) -y /(<miny>,<maxy>/) -s <SRS> -t <tolerance> -v

Ex:
shp2sdo.exe tuyendg spa_tuyendg -g geom -d -x (575977.125,577368.125) -y (1195219.125,1196522.0) -s 32648 -t 0.5 -v

Note: Type  shp2sdo.exe -h for help.


Step 3
After running shp2sdo converter, it creates 2 files:
  • *.sql : to create oracle table.
  • *.ctrl : to store data.
1. Open iSQL*Plus with database that you want to import, execute the content in *.sql (ex: @spa_tuyendg.sql).

2. Open file *.ctrl, insert line " CHARACTERSET UTF8" after line "LOAD DATA" and save it.

3. Open console, use Oracle SQL*Loader by running the following commands:
> set path="$ORACLE_HOME/product/10.1.0/db_1/BIN"
> sqlldr / 

Ex:  sqlldr SYSTEM/oracle spa_tuyendg


Step 4
Open iSQL*Plus, execute the following commands:
CREATE INDEX  ON <table_name>(<geom_name> ) INDEXTYPE IS MDSYS.SPATIAL_INDEX;
EXECUTE SDO_MIGRATE.TO_CURRENT('
<table_name>','<geom_name>');

Ex:
CREATE INDEX spa_tuyendg_spatial_idx ON spa_tuyendg(geom) INDEXTYPE IS MDSYS.SPATIAL_INDEX;
EXECUTE SDO_MIGRATE.TO_CURRENT('SPA_TUYENDG','GEOM');

你可能感兴趣的:(open,source,gis,oracle,数据库,table,attributes,file,path)