How to Load a Shapefile in to Oracle Spatial

2009 June 14
by Adam Estrada

Please note that you cannot just insert a Shapefile in to Oracle and expect the geometries to be stored and indexed properly! Therefore, you’ve got to use a set of “special” tools to insert the data in to Oracle Spatial. Oracle does provide you with 2 very easy to use utilities that can be downloaded for free. The first is called shp2sdo which is a command line tool that basically converts your Shapefile in to a SQL Script that will be used insert your data in to Oracle. We’ve seen similar command line utilities used with other applications like PostGIS. The usage for shp2sdo is as follows.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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

This application creates the following files.

  • SQL Script which is used to define the schema for each geometry
  • Control (CTL) file that defines the data to be loaded
  • Data (DAT) file which holds your spatial data.

Now that your data are converted to a SQL Script you need to load it in to the database, right? Right…so you’ll need to use SQLPlus to do this! If you use Oracle and don’t know what SQLPlus is, you’re in for a real treat! Command line goodness…

Anyway, using SQLPlus you ‘ll need to run the output SQL file from shp2sdo. This will create the schema that will hold your data in the database. I usually just create a DOS batch script that will launch SQLPlus and load the data in a single step.

1
2
sqlplus username/password@10.11.18.41:1521/mydb
@"C:\Agriculture\Data\AnimalBusinesses.SQL"

Now your schema is created and you’re good to go with loading the actual data part of the dataset. To do this, you’ll need to use another command line tool called SQL*Loader. The command line options for using SQL*Loader are pretty extensive but I only use (or need to use) the bare bones functionality. See the command line options below.

1
sqlldr username/password@10.11.18.41:1521/mydb C:\pathtofile\file.ctl

There you have it! The only thing you’re missing is the index which in Oracle is really easy to create.

1
2
CREATE INDEX MyIndex_idx ON MyTable (geom)
INDEXTYPE IS MDSYS.SPATIAL_INDEX;
Bookmark and Share
No comments yet

Leave a Reply

Note: You can use basic XHTML in your comments. Your email address will never be published.

Subscribe to this comment feed via RSS

Spam protection by WP Captcha-Free