Migrating Postgres / Postgis Tables to Oracle Spatial

August 19th, 2008 rupert Comments off


Step 1: Installing and configuring

1. On Windows, make sure that Windows > Settings > Control Panel > Regional Settings
- Chinese (PRC)

Picture 1.png

2. Install Oracle10g
- make sure you have spatial installed

SQL> SELECT COMP_NAME, STATUS
FROM DBA_REGISTRY
WHERE COMP_NAME = 'Spatial';

3. Create a multilingual database using dbca. Make sure setting is set to UTF8. I installed the sample schema for learning purposes. Read this previous post for more information.

4. We can add a user for our spatial database or just enable scott/tiger from the Enterprise Manager.

Picture 15.png

On Administration > Users > “Search for SCOTT” > Hit EDIT > UNLOCK the status.

Picture 16.png


Step 2: Get Oracle Client Tools


1. On the same WindowsXPSP3 machine that I installed Oracle. There is already SQL*Plus. However I always go for the native which is CLI based. So on a command prompt, sqlplus still works. Also note that I can toggle to my previous commands using “Arrow Up”. If we have the Oracle Database installed on a different machine, we need to download the ff:

* Oracle SQL Developer (Java GUI-Based)

http://www.oracle.com/technology/software/products/sql/index.html

* Oracle 10g Client for Windows (10201_client_win32 1.zip 453MB)

* Oracle 10g Client for MacOS (Oracle_10204Client_MAC_X86.zip 189MB) – rarely used.

* Oracle Instant Client for MacOS (instantclient-basic-macosx-10.2.0.4.0.zip 32MB) – need to try this.


Step 3: Creating the Table


1. POI Table

CREATE TABLE poi
(
  poi_id number(10),
  cn_name varchar2(255),
  py_name varchar2(255),
  en_name varchar2(255),
  en_visname varchar2(255),
  cn_fullpoiadd varchar2(255),
  en_fullpoiadd varchar2(255),
  py_fullpoiadd varchar2(255),
  cn_rdname varchar2(255),
  py_rdname varchar2(255),
  cn_address_no varchar2(255),
  py_address_no varchar2(255),
  cn_address_other varchar2(255),
  py_address_other varchar2(255),
  postal varchar2(20),
  tel_no varchar2(255),
  fax_no varchar2(255),
  email varchar2(255),
  web_url varchar2(255),
  operating_hours  varchar2(4000),
  cards_accepted  varchar2(4000),
  cust_capacity  varchar2(4000),
  park_space  varchar2(4000),
  longitude number(20,8),
  latitude number(20,8),
  CONSTRAINT "poi_pkey" PRIMARY KEY (poi_id)
);


Step 4: Exporting to TextFile with “|”


Export the table into a textfile. Run the SQL statement below on Navicat. Afterwards, run the “Export Wizard” and specify “|” as the delimiter.

SELECT
  poi_id,
  cn_name,
  py_name,
  en_name,
  en_visname,
  cn_fullpoiadd,
  en_fullpoiadd,
  py_fullpoiadd,
  cn_rdname,
  py_rdname,
  cn_address_no,
  py_address_no,
  cn_address_other,
  py_address_other,
  postal,
  tel_no,
  fax_no,
  email,
  web_url,
  operating_hours,
  cards_accepted,
  cust_capacity,
  park_space,
  longitude,
  latitude
FROM poi

Find out how many lines were exported using “wc -l file-name”. If there are more lines than actual records then most likely there are ‘\n’ (newlines / carriage returns) on the exported file.


Step 5: Use SQLLOADER to bulkload the data

SQLLDR scott/tiger CONTROL=poi_full.ctl DATA=data_navicat_cn_all2.dat

Since we have point data in longitude, latitude columns. It is very easy to populate the SDO_GEOMETRY with these columns.


Step 6: Creating Point Geometries from Long/Lat Columns

CREATE TABLE poi_app AS SELECT * FROM poi WHERE latitude > 0 AND longitude > 0;
 
ALTER TABLE poi_app ADD the_geom SDO_GEOMETRY;
 
UPDATE poi_app 
SET the_geom = SDO_GEOMETRY( 2001, NULL, SDO_POINT_TYPE(longitude, latitude, NULL), NULL, NULL);
 
INSERT INTO USER_SDO_GEOM_METADATA VALUES
(
	'poi_app', -- TABLE_NAME
	'the_geom', -- COLUMN_NAME
	SDO_DIM_ARRAY -- DIMINFO attribute for storing dimension bounds, tolerance
	(
		SDO_DIM_ELEMENT
		(
			'LONGITUDE', -- DIMENSION NAME for first dimension
			-180, -- SDO_LB for the dimension
			180, -- SDO_UB for the dimension
			0.5 -- Tolerance of 0.5 meters
		),
		SDO_DIM_ELEMENT
		(
			'LATITUDE', -- DIMENSION NAME for second dimension
			-90, -- SDO_LB for the dimension
			90, -- SDO_UB for the dimension
			0.5 -- Tolerance of 0.5 meters
		)
	),
	4326 -- SRID value for specifying a geodetic coordinate system
);
 
CREATE INDEX idx_poi_app_the_geom ON poi_app(the_geom) INDEXTYPE IS MDSYS.SPATIAL_INDEX;

Once the index is created, Oracle creates a SEQUENCE(MDRS_C796$) and a TABLE(MDRT_C796$). Please do not delete this table. I accidentally deleted this table and I have to recreate the indexes–well it was not that easy. I have to DROP INDEX, insert it into USER_SDO_GEOM_METADATA, then CREATE INDEX.

Note that it is important to include the geometry to be indexed in the USER_SDO_GEOM_METADATA table. This should happen before creating the index or receive an error like…

Picture 2.png


Step 7: Creating Geometries from ESRI Shapefiles


* PGSQL2SHP – Use this to convert from a postgres table to an ESRI Shapefile. Note that if you have mixed geometries in a single column, then you need output different shapefiles for each geometry. For example, I have a “geo_entities” table which contains polygons, multipolygons, and multilinestrings. I was able to come up with three (3) different shapefiles for each geometry. Then afterwards load the individual shapefiles using SDO2SHP below.

pgsql2shp -f geo_entities_2006 -h 127.0.0.1 -u lbs -P ******* -g the_geom beijing_app "SELECT gid,cn_name,py_name,en_name,entity_type,geom_type,meta_name,cn_district,the_geom FROM geo_entities WHERE GeometryType(the_geom) = 'MULTILINESTRING'"

* Ensure that you download SDO2SHP

shp2sdo shp\geo_entities_2007 geo_entities_2007 -g the_geom -x (-180,180) -y (-90,90) -s 4326 
-t 0.5 -v

The output of SHP2SDO are three (3) files:

a. CTL – control file containing “LOAD DATA…”
b. SQL – contains “CREATE TABLE…”
c. DAT – Data with “#”

Categories: oracle Tags:

Oracle: Creating a MultiLingual Database

August 19th, 2008 rupert Comments off

Notes on to create a multilingual database in UTF8 in Oracle10g.

1. Run the Oracle Database Configuration Assistant (DBCA)

Picture 2.png

2. Choose Create Database

Picture 3.png

3. Choose “General Purpose”.

4. Then specify the GLOBAL DATABASE NAME or ORACLE SID (orcl4)

Picture 5.png

5. Click Next on Management Options.

6. On “Database Credentials”, use the same passwords so we would not forget them for now…

Picture 7.png

7. On “Storage Options”. Default options is FILE STORAGE. Next.

8. On “Database File Locations”, hit Next as well.

9. On “Recover Options”, same goes here.. Next.

10. On “Database Content”, install the sample schemas for practice.
Picture 11.png

11. On “Initialization Parameters”, click on the “Character Sets” tab. Note that we chose the 2nd option which is “Use Unicode” (ALT32UTF8) in order to support chinese. On the National Character Set, please choose “UTF8″ (the 2nd option in the drop-down list”) also.

Picture 12.png

12. Hit Next until we create the database.

13. To check you should have the Oracle Services installed in your Services.

Picture 14.png

Categories: oracle Tags:

Using GDALWARP to reproject raster that will fit with Google Satellite

July 29th, 2008 rupert Comments off

Just a couple of notes to onself using gdal: Use gdalwarp to reproject your GeoTIFF files! I wanted to use my own satellite images acquired from GeoEye, however, on some areas I wanted to use google sat images as well since I don’t have the coverage. In order to do so, I need to reproject the sat images to 900913. Note you need to specify this in your epsg file in my previous post.

rupert:beijing_900913_satellite rupert$ gdalinfo Mosaic_RGB.tif
Driver: GTiff/GeoTIFF
Files: Mosaic_RGB.tif
Size is 4248, 4553
Coordinate System is:
GEOGCS["WGS 84",
    DATUM["WGS_1984",
        SPHEROID["WGS 84",6378137,298.2572235630016,
            AUTHORITY["EPSG","7030"]],
        AUTHORITY["EPSG","6326"]],
    PRIMEM["Greenwich",0],
    UNIT["degree",0.0174532925199433],
    AUTHORITY["EPSG","4326"]]
Origin = (116.291476140000000,40.025198500000002)
Pixel Size = (0.000046860000000,-0.000035970000000)
Metadata:
  AREA_OR_POINT=Area
  TIFFTAG_XRESOLUTION=100
  TIFFTAG_YRESOLUTION=100
Image Structure Metadata:
  INTERLEAVE=BAND
Corner Coordinates:
Upper Left  ( 116.2914761,  40.0251985) (116d17'29.31"E, 40d 1'30.71"N)
Lower Left  ( 116.2914761,  39.8614271) (116d17'29.31"E, 39d51'41.14"N)
Upper Right ( 116.4905374,  40.0251985) (116d29'25.93"E, 40d 1'30.71"N)
Lower Right ( 116.4905374,  39.8614271) (116d29'25.93"E, 39d51'41.14"N)
Center      ( 116.3910068,  39.9433128) (116d23'27.62"E, 39d56'35.93"N)
Band 1 Block=4248x1 Type=Byte, ColorInterp=Red
Band 2 Block=4248x1 Type=Byte, ColorInterp=Green
Band 3 Block=4248x1 Type=Byte, ColorInterp=Blue
rupert:beijing_900913_satellite rupert$ gdalwarp -s_srs epsg:4326 -t_srs epsg:900913 Mosaic_RGB.tif sat_4m_rgb.tif
Creating output file that is 4245P x 4556L.
Processing input file Mosaic_RGB.tif.
0...10...20...30...40...50...60...70...80...90...100 - done.
rupert:beijing_900913_satellite rupert$ gdalinfo sat_4m_rgb.tif
Driver: GTiff/GeoTIFF
Files: sat_4m_rgb.tif
Size is 4245, 4556
Coordinate System is:
PROJCS["Google Maps Global Mercator",
    GEOGCS["WGS 84",
        DATUM["WGS_1984",
            SPHEROID["WGS 84",6378137,298.2572235630016,
                AUTHORITY["EPSG","7030"]],
            AUTHORITY["EPSG","6326"]],
        PRIMEM["Greenwich",0],
        UNIT["degree",0.0174532925199433],
        AUTHORITY["EPSG","4326"]],
    PROJECTION["Mercator_1SP"],
    PARAMETER["central_meridian",0],
    PARAMETER["scale_factor",1],
    PARAMETER["false_easting",0],
    PARAMETER["false_northing",0],
    UNIT["metre",1,
        AUTHORITY["EPSG","9001"]]]
Origin = (12945507.907502911984921,4869604.732793668285012)
Pixel Size = (5.219801430503303,-5.219801430503303)
Metadata:
  AREA_OR_POINT=Area
Image Structure Metadata:
  INTERLEAVE=PIXEL
Corner Coordinates:
Upper Left  (12945507.908, 4869604.733) (116d17'29.31"E, 40d12'53.10"N)
Lower Left  (12945507.908, 4845823.317) (116d17'29.31"E, 40d 3'2.78"N)
Upper Right (12967665.965, 4869604.733) (116d29'25.89"E, 40d12'53.10"N)
Lower Right (12967665.965, 4845823.317) (116d29'25.89"E, 40d 3'2.78"N)
Center      (12956586.936, 4857714.025) (116d23'27.60"E, 40d 7'58.12"N)
Band 1 Block=4245x1 Type=Byte, ColorInterp=Red
Band 2 Block=4245x1 Type=Byte, ColorInterp=Green
Band 3 Block=4245x1 Type=Byte, ColorInterp=Blue

MacOSX Utilities: iTerm + MarsEdit

July 29th, 2008 rupert Comments off

Just a quick note before I go home tonight…

1. Replace your current terminal with iTerm.

2. MarsEdit rocks! Ive been posting lazy and all but with this blog client at hand.. blog posting is easier. Way way better compared to ecto!! (IMHO).

Categories: mac, osx Tags: ,

Installing Chinese Font for Mapserver

July 28th, 2008 rupert Comments off

We wanted to use a different font for mapserver which is Microsoft Yahei instead of zysong. Apparently, mapserver have some issues with path problems or with the ttf having more than one word as a font name.

Thanks to Martin Hosken’s perl modules for fonts, we were able to rename the Microsoft Yahei TTF to msyh.ttf.

1. Download: (a) Font-TTF-0.45 (b) Font-TTF-Scripts-0.11.1

2. Unzip

#perl Makefile.pl
#make
#make install

3. ttfname

rupert:Desktop rupert$ ttfname
Usage:
ttfname [-f "full_name"] -n "name" [-t num] [-q] infile.ttf outfile.ttf 
Renames the TTF with the given name and outputs the newly named font to out.ttf.
Options:
      -f "name"   specifies new full name (optional) as opposed to the
                  default calculated form.
      -l lang     language number to use (default all langs)
                  if specified name entries will be added for all platforms and
                      encodings covered by the cmap if not already there
      -n "name"   specifies new font family name (not optional)
      -q          disable signon message
      -s filename overrides -n and gets string from file. Useful for -t
      -t num      overrides the normal naming areas to change another
                  string -f becomes inactive.
#ttfname -f "Microsoft YaHei" -n "msyh" Microsoft\ YaHei.ttf msyh.ttf

Picture 1.png

Categories: china Tags: ,