Archive for December, 2008

ArcSDE – Postgre – Postgis

December 30, 2008

I was assigned to discover how ArcSDE and Postgre can connect. Here is my finding.

Before you read this article, you must be equipped with some knowledge about Postgre so you will have a better understanding. You can read “PostgreSQL 8 for WINDOWS” by Richard Blum. Just read the first 4 chapters will do !

This article is about connecting your Postgre to ArcSDE. There are a few information that you will find in this article :
1. The installation step by step.
2. Creating a Postgre database
3. Connecting and registering your database to ArcSDE
4. Scenario : Create table manually, fill in some data and register it with ArcSDE
5. User privilege
6. A little about ArcSDE

First of all, you have to remember that :
1. ArcSDE fpr Postgre only available at 9.3
2. NO OLDER client can access ArcSDE for Postgre (ie: Desktop 9.2 can’t access ArcSDE 9.3)
3. Remember that ArcSDE only support : Postgre 8.3.0 and Postgis 1.3.2
4. This guide is tested in Windows XP operating system (this might work on Windows Server 2000 and 2003)
5. ArcSDE runs on : Window Server 2000 and 2003, Linux SUSE 10 and RedHat

The installation steps
This is the order for the installations :
1. Postgre
2. Postgis
3. ArcSDE installation and
4. ArcSDE postinstallation (optional)
The installation of Postgre is prety much straight forward. Just follow the wizard and you are done with it. Here are the screen shots for the Postgre installation
Postgre installation
Remark :
1.jpg and 2.jpg: accept the default
3.jpg : You can enable the Application Stack Builder if you don’t have Postgis 1.3.2 installation file. If you have Postgis then you can disable it
4.jpg : it’s the Postgre’s service name that will run on your Windows. Just accept the default values
5.jpg : it’s the superuser account. The default for “Superuser name” is usually “postgres”. Leave it as it is. Give password that is easy to remember.
6.jpg, 7.jpg and 8.jpg : Accept the default until finish
After finish installing Postgre, you can install Postgis. You don’t have to install Postgis if you don’t intend to use GEOMETRY spatial data type in your database.
Postgis installation
Remark :
Before you start Postgis installation, if you want to set the tablespace to the location that you prefer, you must set the “default_tablespace” in PGAdmin (you can find it in PGAdmin in menu Tools –> Server Configuration –> postgresql.conf). Restart the computer to get the changes saved into your computer.
5.jpg : Specify the database name. You will need this name later in ArcSDE post-installation iff you want to use GEOMETRY to store your spatial data
The installation of Postgis will create a template Postgis database called “template_postgis” and a database of your own (by default, the name is usually “postgis”). For the last database mentioned, postgis, you can change the database name if you want to.
Next is installing ArcSDE :
The most important part is the post installation.
5.jpg : specify the “Superuser name” of your Postgre database
6.jpg : specify the Postgis database that you have created in the previous step.
If you accidentally type in a wrong database name you can fix it by executing command in postgre “psql –d yourdatabase -f lwpostgis.sql”. The script is provided with ArcSDE installation dvd.
10.jpg : name the service to your desire. It has to be meaningfull and easy to remember
The post installation will create a Login Role called “sde”. See “after installing sde.jpg”. Compare it with “before installing sde.jpg”.
If you follow the installation steps described here, than you are DONE. You can use your database with ArcSDE. HOWEVER… the owner of your data is “sde”. If you want the data to be owned by a certain user than you can create your own Login Role.
Important Note : to be a data owner, the “SCHEMA NAME” and “LOGIN ROLE” must be THE SAME ! Otherwise you will get error “Database user name and current user schema do not match”

Creating a Postgre database (provided that you already have Postgis installed)
You can create a database manually by using script SQL via PGAdmin or “psql to Postgres” :
1. You must CREATE a folder in “postgres installation directory\8.3\data\foldername” manually from Windows Explorer. Replace “foldername” with name that represents your tablespace. This name will be used for variable “TABLESPACE” when you create new database. If you don’t want to, the default TABLESPACE will be pg_default and you don’t need to include the “TABLESPACE” word when you create new database.
2. Execute script below :
CREATE DATABASE [dbname] //dbname = database name
WITH OWNER=[yourloginrole] //yourloginrole = specify the owner name/login role
TEMPLATE=template_postgis //this is the template database. By default it is “template_postgis”
TABLESPACE=[foldername] //[Optional]. foldername = the folder that you created in the first step. If you don’t specify (ie: not writing the ..
//..TABLESPACE word), the default value is pg_default”
Or
postgres=# CREATE DATABASE dbname template=template_postgis tablespace=myspatialdb;
From the script above, you can see that I use the Postgis template to create my database. The database created will have ability to store data in Postgis format (GEOMETRY).
3. Execute the script below to create role and schema.
create role myself login password ‘myself’ noinherit createdb;
create schema myself authorization myself
grant USAGE on schema myself to public;
GRANT select, insert, update, delete on public.geometry_columns to myself; //execute this line if your data is stored in GEOMETRY because the data..
//..is stored in Public schema
4. Run ArcCatalog. Double click “Add Spatial Database Connection”. Fill in the textbox correctly then click Connect. See “connecting via catalog.jpg”. You can start working now by creating Feature Class (FC) or Feature Dataset (FD). Right click your newly created FD/FC then click “Register with Geodatabase” and “Register as Version” (optional).
5. Edit your data in ArcMap. Dont forget to turn off the Version editing if you don’t want to have versioned editing by UN-CHECKING the “Edit a version of a database…”. See “edit option.jpg”. By default it is CHECKED. If you edit your FC straight away you will get error message like this “no sufficient privilege or feature class not registered as version”. If you want to have version edit, then leave the default setting (CHECKED) but you must do “Register as Version” in ArcCatalog first.
6. Finish. You are ready to work now !

Connecting and registering your database to ArcSDE
You can connect your EXISTING table to ArcSDE manually by registering it using command supplied with ArcSDE. Once it is registered with ArcSDE then it will have full geodatabase functionality. You can execute this command through command prompt. The command is :
sdelayer -o register -l tester,shape -e a -C objectid,SDE -R 2 -t ST_GEOMETRY -i 5158 -D final2 -u final2 -p final2
Remark :
-o register : the operation type is “register”
-l tester,shape : the table name is “tester” and the spatial column in this table is “shape”. Spatial column has data type of GEOMETRY or ST_GEOMETRY.
-e a : “a” indicates that the data represents Area. For point type “p” and for line type “l”.
-C objectid,SDE : “objectid” is column that serves as id (index) and this is maintained by user called “SDE”. It is recommended that the index is maintained by user “SDE”
-R 2 : “2” is the spatial reference number of your data. If you do not know, you can find out by creating a dummy feature class (and don’t forget to specify the spatial reference) in ArcCatalog then find it out in sde.sde_geometry_columns in column “srid”
-t : “ST_GEOMETRY” is the spatial type. You can choose Postgis spatial type by typing “GEOMETRY”
-i : “5158” is the service number of your ArcSDE service
-D : “final2” is the name of your database
-u : “final2” is the username
-p : “final2” is the password
(for the sake of testing, I put in the same name for database name, username and password). The “sdelayer -o register..” above will create 2 indexes in your table. There will be codes added to your table like the following :
CREATE INDEX a1_ix1
ON schema.table
USING gist
(shape);

CREATE UNIQUE INDEX r39_sde_rowid_uk
ON schema.table
USING btree
(objectid)
WITH (FILLFACTOR=75);
To test the connection, open ArcCatalog then double click “Add Spatial Database Connection” and connect using your Login Role that you created in Postgre.

Scenario : Create table manually, fill in some data and register it with ArcSDE
1. Creating table
create table sde.blocksss_st(objectid integer not null, block varchar(24), shape st_geometry);
ALTER TABLE sde.blocksss_st OWNER TO sde;
GRANT ALL ON TABLE sde.blocks_st TO sde;
2. Fill in some data
insert into sde.blocksss_st values (1, ‘block’, st_geometry(‘linestring(487998.66386880 3766968.84310695, 490186.81623251 3766968.84310695)’,1));
To view the data that is just inserted run :
select * from sde.blocks_st;
Remark : Number “1” in the end of the command above is the SRID number. Please refer to Connecting and registering your database to ArcSDE above.
3. Register it with ArcSDE
sdelayer -o register -l sde.blocks_st,shape -C objectid,SDE -e l -t ST_GEOMETRY -i 5154 -D postgis -u sde -p sde
To check the data yo can run :
select st_astext(shape) from sde.blocks_st;

User privilege
There are two types of user, EDITOR and VIEWER. You can grant privileges to both types using ArcCatalog or PGAdmin. I recommend that you use ArcCatalog because it is easier as you don’t have to find the corresponding A and D tables if the FC is involved in Version editing.
1. EDITOR : has privileges Select, Insert, Delete and Update
Create new Login Role using PGAdmin tool. Open ArcCatalog, right click the FC that you want it to be granted to. Check all the available privileges. See “grant1.jpg” Select the privileges. See “grant2.jpg”. You can check the table in PGAdmin.
This grant process will add the following code to your FC (table) in your table :
GRANT SELECT, UPDATE, INSERT, DELETE ON TABLE schema.table TO loginrole;
2. VIEWER : has Select privilege
Create new Login Role using PGAdmin tool. Open ArcCatalog, right click the FC that you want it to be granted to. Check the SELECT privilege
This grant process will add the following code to your FC (table) in your table :
GRANT SELECT ON TABLE schema.table TO loginrole;
You can also group your user. This is how to do it :
1. Right click GROUP ROLES  Add New. Give the group a name. No password is required
2. Double click the table name. This will open the table properties. Go to “Privileges” tab. See “grant group 1.jpg”
3. Double click SCHEMA where the FC exist. Grant USAGE for the group role. See “grant group 2.jpg”
4. Create a new Login Role. Check the option “inherits rights from from parent roles”. See “grant group user 1.jpg”. In tab “Role membership”, add the Group Role to the textbox on the right by clicking the “>>” button. See “grant group user 2.jpg”
5. Grant SELECT for group roles to the desired table. See “grant select group.jpg”
Any Login Role that is a member of Group Role will automatically inherit the GROUP ROLE privilege.
Remark : you CAN’T GRANT SELECT to Group Roles via ArcCatalog but you CAN GRANT SELECT to Login Roles via ArcCatalog. See point #2 about VIEWER above.

A little about ArcSDE
ArcSDE stands for Arc-Spatial Database Engine. It has ability to make your database spatially enabled. ArcSDE adds the following capabilities to geodatabases:
• It defines how spatial data is stored and managed in a DBMS
• ArcSDE enables support for version management of geodatabases. This enables use by multiple, simultaneous users and editors, supports distributed geodatabases, and enables historical archiving of all updates.
• ArcSDE supports spatial types for SQL in all DBMS’s using the ISO and OGC SQL Multi-media standard for SQL.

the end.
Note: i havnt uploaded the screen captures yet.

Advertisements

indosat im2 downloadspeed

December 30, 2008

Sabtu pagi kemarin tgl 27 Dec saya ngenet pakai indosatm2. iseng2 donload program yg ukurannya 17MB-an.Ternyata lumayan cepet lho..download speednya sampai 26Kbps

speed donlod

Weleh2, g nyangka., biasanya indosatm2 kan lemottt (bisa nyambung aja udah untung bangettt). Speed ini g ada apa2nya sama yg di kantor yg bisa mencapai 26oKbps. Mungkin kejadian download cepet indosatm2 ini hanya once in a lifetime.. hehehe.

Kmrn aja mau ngenet g nyambung2. Dicobain dr pagi sampai siang tetep ae “PPP protocol error”. Baru deh sorenya bisa. Meskipun rada lemottt tapi ok lah. What do u expect with 100rb per-month ??? Murah kok minta cepet ! 🙂

Kayaknya iklan indosatm2 yg ngenet cepet itu boong deh. Coba test speednya di http://www.cbn.net -> Support -> Speed test. Dijamin g sukses ngelewatin testnya ALIAS gatot, GAgal TOTal !

What do you think guys ? share your experience by putting comment in this post.

[ngenetnya pakai hp 3G. jadi mungkin speednya agak kurang maksimal]

SQL Server 2005 Trigger

December 15, 2008

this article is about sql server 2005 trigger, update ,insert ,delete, and logon trigger. Although not much, hope this can shed some light. BTW, i have not tested the coding especially about the logon trigger cos i haven’t got Ms SQL Server SP2. 

Here is my compilation about trigger. Happy reading… 🙂

Trigger adalah fitur di MsSQL Server 2005 yang merespon terhadap event yang terjadi di dalam suatu database. Logon Trigger mulai diperkenalkan di MsSql Server 2005 SP 2. Di Oracle, trigger ini sudah ada sejak lama.

Tabel yang menyimpan informasi tentang siapa saja yg logon adalah sys.dm_exec_sessions. Atau dapat juga dilihat dengan menjalankan kode sql “EXEC sp_who” atau “EXEC sp_who2”. Saya sendiri masih belum mudheng yang mana – yang mana.. J

Oya, utk instalasi MSSQLServer 2005 ini butuh Microsoft Windows Installer 3.1. Download dulu programnya.Kecil kok, sekitar 2.5Mb.

FYI, MsSQL Server 2000 belum mendukung Logon Trigger

1. Membuat trigger saat update, insert :

CREATE TRIGGER DataTable_Audit on DataTable

FOR INSERT, UPDATE  saat update dan insert
AS IF UPDATE(MonitoredColumn)
INSERT INTO myAuditTable (USERLOGIN, ChangeDT)
VALUES (SYSTEM_USER, getdate()) — login si user dimasukkan ke tabel
GO

Untuk men-test script di atas, gunakan script di bawah ini :

DECLARE @ID int
INSERT INTO DataTable (UnMonitoriedColumn, MonitoredColumn)
VALUES (‘ABC’, ‘DEF’)
SELECT @ID = SCOPE_IDENTITY()
UPDATE DataTable
SET MonitoredColumn = ‘GHI’
WHERE [Id] = @ID
UPDATE DataTable
SET UnMonitoriedColumn = ‘JKL’
WHERE [Id] = @ID
SELECT * FROM myAuditTable
GO

 Hasilnya adalah :

 AuditEventID USERLOGIN ChangeDT

———— ——————– ——————————————————
1 NSandrew 2005-07-18 03:13:20.403
2 NSandrew 2005-07-18 03:13:20.403
(2 row(s) affected)

Statement update yg kedua TIDAK mengubah isi dari MonitoredCloumn, jadi hanya 2 event saja yang tercatat yaitu INSERT dan UPDATE untuk MonitoredColoumn.

Untuk memudahkan pemahaman, ada versi sederhana seperti di bawah:

USE db1;

GO

 CREATE TRIGGER new_user ON nama_tabel — ini koding utk membuat trigger

AFTER INSERT

AS

  PRINT ‘New users added.’;

GO

 SELECT * FROM sys.triggers — melihat trigger yang ada di tabel tersebut

GO

Hasilnya adalah :

name         object_id   parent_id   type type_desc  

———— ———– ———– —- ————

dml_message  690101499   674101442   TR   SQL_TRIGGER

new_user     706101556   674101442   TR   SQL_TRIGGER

2. Membuat trigger saat Logon event terjadi (MsSQL Server 05 SP 2)

 Ketika ada event logon, maka informasi user tersebut akan dimasukkan ke table lain.

Gunakan koding di bawah

 USE master;

GO
CREATE LOGIN login_test WITH PASSWORD = '3KHJ6dhx(0xVYsdf' MUST_CHANGE,
    CHECK_EXPIRATION = ON;
GO
GRANT VIEW SERVER STATE TO login_test;
GO
CREATE TRIGGER connection_limit_trigger
ON ALL SERVER WITH EXECUTE AS 'login_test'
FOR LOGON
AS
BEGIN
IF ORIGINAL_LOGIN()= 'login_test' AND
    (SELECT COUNT(*) FROM sys.dm_exec_sessions
            WHERE is_user_process = 1 AND
                original_login_name = 'login_test') > 3
    ROLLBACK;
END;
Koding di atas akan menolak login utk suatu user tertentu jika user tersebut sudah membuat login sebanyak 3 kali.

Atau bisa juga dengan koding di bawah. Koding ini akan mencatat informasi dari user yang masuk ke database ke dalam suatu tabel.

 CREATE TRIGGER [connection_limit_triggerDDLTRIGGER]

ON ALL SERVER WITH EXECUTE AS ‘login_testDDLTRIGGER’

FOR LOGON

AS

BEGIN

IF ORIGINAL_LOGIN()= ‘login_testDDLTRIGGER’ AND

    (SELECT COUNT(*) FROM sys.dm_exec_sessions

            WHERE is_user_process = 1 AND

                original_login_name = ‘login_testDDLTRIGGER’) > 1

    BEGIN

      DECLARE @data XML

      SET @data = EVENTDATA()

      INSERT ray_ddl_log (PostTime, DB_User, Event, TSQL)

       VALUES

       (GETDATE(),

       CONVERT(nvarchar(100), CURRENT_USER),

       @data.value(‘(/EVENT_INSTANCE/EventType)[1]’, ‘nvarchar(100)’),

       @data.value(‘(/EVENT_INSTANCE/TSQLCommand)[1]’, ‘nvarchar(2000)’) )

     END

END;

GO

3. SQLExpress tidak bisa menangani event logon. Maka koding di bawah dapat digunakan:

CREATE TRIGGER trgLogDDLEvent ON DATABASE 

    FOR DDL_DATABASE_LEVEL_EVENTS
AS
    DECLARE @data XML
    SET @data EVENTDATA()
    IF @data.value('(/EVENT_INSTANCE/EventType)[1]''nvarchar(100)')
        <> 'CREATE_STATISTICS' 
        INSERT  INTO DDLChangeLog
                (
                  EventType,
                  ObjectName,
                  ObjectType,
                  tsql
                )
        VALUES  (
                   @data.value('(/EVENT_INSTANCE/EventType)[1]',
                              'nvarchar(100)'),
                  @data.value('(/EVENT_INSTANCE/ObjectName)[1]',
                              'nvarchar(100)'),
                  @data.value('(/EVENT_INSTANCE/ObjectType)[1]',
                              'nvarchar(100)'),
                  @data.value('(/EVENT_INSTANCE/TSQLCommand)[1]',
                              'nvarchar(max)')
                ) ;
GO

4. Bagaimana cara melihat daftar event (event list) dari trigger yang sudah ada (existing trigger) dengan menggunakan sys.trigger_events ?

Kamu bisa menggabungkan sys.trigger_events dan sys.trigger. Kodingnya adalah sebagai berikut :

 USE db1; — ini adalah nama database

GO

 SELECT t.name, e.type, e.type_desc

FROM sys.trigger_events AS e, sys.triggers AS t

WHERE e.object_id = t.object_id

GO

Hasilnya adalah :

name           type   type_desc

————– —— ———

dml_message    1      INSERT

dml_message    2      UPDATE

dml_message    3      DELETE

new_user       1      INSERT

(4 row(s) affected)

5. Menyimpan event ALTER TABLE ke dalam suatu table. 

Buat dulu table yang akan menyimpan informasi user nya dengan koding berikut :

USE Northwind; 
GO 
CREATE TABLE EvtLog 

   
PostTime DATETIME
   
LoginName NVARCHAR(100), 
   
EventType NVARCHAR(100), 
   
TSQLCommand NVARCHAR(2000)  

GO 

Buat DDL Trigger nya :

CREATE TRIGGER trPreventTblChange  
ON DATABASE  
FOR 
ALTER_TABLE 
AS 
DECLARE 
@Data XML 
SET @Data EventData() 
INSERT EvtLog (PostTimeLoginNameEventTypeTSQLCommand)  
VALUES  
   
(GETDATE(),  
   
CONVERT(NVARCHAR(100), CURRENT_USER),  
   
@Data.value('(/EVENT_INSTANCE/EventType)[1]''nvarchar(100)'),  
   
@Data.value('(/EVENT_INSTANCE/TSQLCommand)[1]''nvarchar(2000)') ) ; 
GO

Lalu tes :

USE Northwind 
GO 

ALTER TABLE Region 
ADD newColumn SMALLDATETIME NULL 

Lihat hasil triggernya dengan melihat langsung ke table yang dibuat tadi:

SELECT * FROM Northwind.dbo.evtlog

6. Memasukkan data ke table TempSPWHO2 dengan menggunakan EXEC 

CREATE TABLE TempSPWHO2

(

SPID1 INT,

Status VARCHAR(60),

Login VARCHAR(16),

Hostname VARCHAR(256),

BlkBy VARCHAR(3),

DBName VARCHAR(256),

Command VARCHAR(32),

CPUTime INT,

DiskIO INT,

LastBatch VARCHAR(16),

ProgramName VARCHAR(256),

SPID2 INT

)

GO

INSERT INTO TempSPWHO2

EXEC sp_who2

GO


My Final Question :

Kalau ingin melihat informasi user harus menggunakan yg mana ??? Hayoo yang mana… anyone knows ?

 

Informasi yang ada di sql command EXEC sp_who,

Informasi yang ada di sql command EXEC sp_who2,

EventData(),

Atau

sys.dm_exec_sessions

 

yocki@hotmail.com. Compiled from many sources. Dikompile dari berbagai sumber.

Special thanks to Kasim Wirama (wkww2004@yahoo.com) and 4guys@npond.com. Feel free to contact them. Don’t contact me cos iam not the MVP.


Hello world!

December 15, 2008

Welcome to WordPress.com. This is your first post. Edit or delete it and start blogging!