You are on page 1of 11

Bi tp ln mn: C s d liu khng gian

HVTH: Phm Th Hng

1. T VN Vn t chc, xy dng v qun l d liu l mt trong nhng vn c bn, ng vai tr cc k quan trng trong cc ng dng GIS. D liu c m t y , qun l cht ch, trong sng s gip cho cc ng dng c vn hnh trn tru. C s d liu quan h truyn thng khng th m t c y cc thuc tnh ca cc i tng khng gian, ngi nghin cu v GIS phi nghin cu cc m hnh c s d liu quan h m rng. Vic gii quyt cc bi tp trong mn hc C s d liu khng gian l mt trong nhng phng php nghin cu v tip cn vi cc m hnh d liu khng gian. Tn ti: M phng m hnh qun l hot ng ca h thng xe bus khu vc trung tm thnh ph H Ch Minh 2. NI DUNG 2.1. M t cc thc th. Trong m hnh c cc thc th chnh sau y: bus_station (Trm xe bus): l i tng khng gian dng im: gid id: name: the_geom: gid id: name: the_geom: gid name: the_geom: id name: addres: T sinh ra khi chuyn t shape file sang M trm, kiu varchar(10) Tn trm, kiu varchar(50) POINT() T sinh ra khi chuyn t shape file sang M trm, kiu varchar(10) Tn trm, kiu varchar(50) MULTILINESTRING() T sinh ra khi chuyn t shape file sang Tn trm, kiu varchar(50) MULTIPOLYGON() M Hp tc x, kiu varchar(10) Tn hp tc x, cng ty vn ti, kiu varchar(50) a ch, kiu varchar(50)

bus_route (Tuyn xe bus): l i tng khng gian dng ng:

administrative (n v hnh chnh): l i tng khng gian dng vng.

bus_cooperative (Hp tc x xe bus): l mt kiu d liu thng thng.

quantity_of_bus: s lng xe bus, kiu interger Ngoi ra cn c 2 bng geometry_columns v spatial_ref_sys dng m t cc kiu d liu khng gian v tham chiu khng gian. Mt c s d liu thng thng trong PostgeSQL mun to c cc i tng khng gian cn phi np 2 bng ny
Trang 1

Bi tp ln mn: C s d liu khng gian

HVTH: Phm Th Hng

vo c s d liu ca mnh c thng qua hai file: lwpostgis.sql v spatial_ref_sys.sql ng thi mt s mi quan h nhiu nhiu gia cc i tng cng sinh ra cc bng d liu thuc tnh (m t chi tit bn di). 2.2. M t cc quan h gia cc thc th: - bus_station v bus_route: Quan h nhiu nhiu (k hiu M N) (Quan h khng gian) + Mt trm xe bus c th c nhiu tuyn i qua. + Mt tuyn xe bus c th i qua nhiu trm khc nhau. - bus_route v administrative: Quan h nhiu nhiu (k hiu M N) (Quan h khng gian) + Trn mt n v hnh chnh (qun, huyn) s c nhiu tuyn xe bus i qua. + Mt tuyn xe bus c th chy qua nhiu n v hnh chnh (qun, huyn) khc nhau. - Bus_Station v Administrative: Quan h mt nhiu (k hiu 1 M) + Mt trm xe bus ch thuc mt n v hnh chnh (qun, huyn) nht nh. + Trn mt n v hnh chnh (qun, huyn) ngi ta c th t nhiu trm xe bus khc nhau. - Bus_Route v Bus_Cooperative: Quan h nhiu nhiu (k hiu M N) + Mt tuyn xe bus bt k c th c ng k tham gia bi nhiu hp tc x xe bus khc nhau. + Mt hp tc x xe bus c th ng k hot ng trn nhiu tuyn xe bus.

Trang 2

Bi tp ln mn: C s d liu khng gian

HVTH: Phm Th Hng

2.3. ER diagram with pictogram:

2.4. ER schema: bus_station gid (serial) 1 2 3 . id (varchar) HB.001 BTH.001 LDH.001 .. name (varchar) Hong Bang Ba Thang Hai Le Dai Hanh .. the_geom (point) POINT POINT POINT ..

bus_route gid (integer) 1 2 3 . id (varchar) R.001 R.002 R.003 . name (varchar) Hong Bang Cong vien Dam Sen Ben xe Cho Lon Thao Cam Vien Ben Thanh Ben xe mien Dong the_geom (mutilinestring) MULTILINESTRING MULTILINESTRING MULTILINESTRING .

Trang 3

Bi tp ln mn: C s d liu khng gian

HVTH: Phm Th Hng

Bus_Cooperative id (varchar) CPML HTXQT XKCL . name (varchar) Cong ty co phan van tai Mai Linh Hop tac xa van tai xe buyt Quyet Thang Cong ty xe khach Cho Lon . quantity_of_bus (interger) 30 15 15 .. address (varchar) 400A, Le Hong Phong, Quan 10 78, An Duong Vuong, Quan 5 205, Phu Huu, Quan 5 ..

administrative gid (serial) 1 2 3 . name (varchar) Quan 1 Quan 10 Quan Tan Binh . Shape (Polygon) MULTIPOLYGON MULTIPOLYGON MULTIPOLYGON ..

route_crosses_station id (serial) 1 2 3 .. route_id (varchar) R.001 R.002 R.003 station_name (varchar) Hong Bang Dai hoc Su Pham Ben xe mien Dong

route_crosses_station id (serial) 1 2 3 .. coop_id (varchar) CPML HTX195 HTXBM route_name (varchar) Hong Bang Cong vien Dam Sen Cho Lon Cho Go Vap Ben Thanh San bay Tan Son Nhat

Trang 4

Bi tp ln mn: C s d liu khng gian

HVTH: Phm Th Hng

2.5. Danh sch mt s cu hi truy vn gi : Lit k tt c cc trm m tuyn Ben xe Cho Lon Thao Cam Vien i qua?
SELECT c.station_name, r.name AS Route_Name FROM route_crosses_station c, bus_route r WHERE (r.name = 'Ben xe Cho Lon - Thao Cam Vien') AND (c.route_id=r.id); station_name | route_name ------------------------+-------------------------------An Dong Plaza | Ben xe Cho Lon - Thao Cam Vien Dai hoc Su Pham | Ben xe Cho Lon - Thao Cam Vien Cong vien Tao Dan | Ben xe Cho Lon - Thao Cam Vien An Duong Vuong | Ben xe Cho Lon - Thao Cam Vien Ben xe Cho Lon | Ben xe Cho Lon - Thao Cam Vien Hung Vuong Plaza | Ben xe Cho Lon - Thao Cam Vien Dinh Thong Nhat | Ben xe Cho Lon - Thao Cam Vien Dai truyen hinh Tp.HCM | Ben xe Cho Lon - Thao Cam Vien Nga Sau Cong Hoa | Ben xe Cho Lon - Thao Cam Vien Thao Cam Vien | Ben xe Cho Lon - Thao Cam Vien Chau Van Liem | Ben xe Cho Lon - Thao Cam Vien Nguyen Trai | Ben xe Cho Lon - Thao Cam Vien (12 rows)

Lit k tt c cc trm xe bus nm Qun 1?


SELECT s.name FROM bus_station s WHERE ST_Contains( (SELECT the_geom FROM administrative WHERE name = 'Quan 1') ,s.the_geom); name -----------------------Cong vien Tao Dan Dinh Thong Nhat Dai truyen hinh Tp.HCM Thao Cam Vien Cho Ben Thanh Nguyen Thi Minh Khai Cong vien Le Van Tam (7 rows)

Lit k tt c cc tuyn xe bus i qua Qun 5?


SELECT r. name FROM bus_route r WHERE ST_Crosses(r.the_geom, (SELECT the_geom FROM administrative WHERE name = 'Quan 5') ); name -------------------------------Ben xe Cho Lon - Thao Cam Vien Cho Lon - Cho Go Vap (2 rows)

Trang 5

Bi tp ln mn: C s d liu khng gian

HVTH: Phm Th Hng

Tnh tng chiu di tt c cc tuyn xe bus?


SELECT sum(ST_Length(the_geom)) AS km_bus FROM bus_route; km_bus -----------------53.7695054045018 (1 row)

Qun no c nhiu trm xe bus nht?


SELECT a.name AS District_Name FROM administrative a, bus_station s WHERE ST_Contains(a.the_geom,s.the_geom) ORDER BY District_Name DESC LIMIT 1; district_name --------------Quan Tan Binh (1 row)

Qun no c nhiu tuyn xe bus i qua nht?


SELECT a.name AS District_Name FROM administrative a, bus_route r WHERE ST_Crosses(r.the_geom,a.the_geom) ORDER BY District_Name DESC LIMIT 1; district_name --------------Quan Tan Binh (1 row)

Tuyn xe bus no c l trnh di nht?


SELECT name, ST_length(the_geom) AS Length_Km FROM bus_route ORDER BY Length_Km DESC LIMIT 1; name | length_km ----------------------+-----------------Cho Lon - Cho Go Vap | 11.9828896181423 (1 row)

Hp tc x no c nhiu xe nht?
SELECT name, quantity_of_bus FROM bus_cooperative ORDER BY quantity_of_bus DESC LIMIT 1; name | quantity_of_bus ----------------------------------+----------------Cong ty co phan van tai Mai Linh | 30 (1 row)

Trang 6

Bi tp ln mn: C s d liu khng gian

HVTH: Phm Th Hng

Hai tuyn xe bus Cho Lon Cho Go Vap v Ben Thanh San bay Tan Son Nhat c ct nhau khng?
SELECT ST_intersects( (SELECT the_geom FROM bus_route WHERE name ='Cho Lon - Cho Go Vap'), (SELECT the_geom FROM bus_route WHERE name ='Ben Thanh - San bay Tan Son Nhat') ); st_intersects --------------t (1 row)

Lit k tt c nhng tuyn xe bus giao ct vi tuyn Ben Thanh San bay Tan Son Nhat?
SELECT name FROM bus_route r WHERE ST_intersects(the_geom, (SELECT the_geom FROM bus_route WHERE name ='Ben Thanh - San bay Tan Son Nhat')) AND (name !='Ben Thanh - San bay Tan Son Nhat'); name -------------------------------Ben xe Cho Lon - Thao Cam Vien Ben Thanh - Ben xe mien Dong Cho Lon - Cho Go Vap (3 rows)

Tm tuyn xe bus i qua nhiu qun nht?


SELECT r.name, COUNT(distinct a.name) AS district FROM administrative a, bus_route r WHERE st_crosses(r.the_geom,a.the_geom) GROUP BY r.name ORDER BY district DESC LIMIT 1; name | district ----------------------+---------Cho Lon - Cho Go Vap | 6 (1 row)

Tm tuyn xe bus i qua nhiu trm nht?


SELECT r.name AS Route_Name, COUNT(c.station_name) AS Number_Station FROM route_crosses_station c, bus_route r WHERE c.route_id=r.id GROUP BY r.name ORDER BY Number_Station DESC LIMIT 1; route_name | number_station --------------------------------+---------------Ben xe Cho Lon - Thao Cam Vien | 12 (1 row)

Trang 7

Bi tp ln mn: C s d liu khng gian

HVTH: Phm Th Hng

Tnh khong cch gia hai trm Dai hoc Bach Khoa va Dai hoc Su Pham?
SELECT ST_distance( (SELECT the_geom FROM bus_station WHERE name = 'Dai hoc Bach Khoa'), (SELECT the_geom FROM bus_station WHERE name ='Dai hoc Su Pham')) AS Distance_Km; distance_km -----------------3.00883091654776 (1 row)

Tnh t trm Dai hoc Bach Khoa, lit k tt c cc trm trong bn knh 2 km? Tnh khong cch n mi trm v sp xp theo th t t gn n xa?
SELECT id,name,ST_distance( (SELECT the_geom FROM bus_station WHERE name ='Dai hoc Bach Khoa'),the_geom) AS Distance_Km from bus_station WHERE (ST_DWithin( (SELECT the_geom FROM bus_station WHERE NAME ='Dai hoc Bach Khoa'), the_geom,2)) AND (name !='Dai hoc Bach Khoa') ORDER BY Distance_Km; id | name | distance_km ----------+---------------------+------------------LDH.001 | Le Dai Hanh | 0.677032874539835 LTK.001 | Ly Thuong Kiet | 1.03114751093211 LLQ.002 | Lac Long Quan | 1.09247080604834 BTH.002 | Ba Thang Hai | 1.15923686339518 BT.001 | Binh Thoi | 1.42901293042356 OIK.001 | Cong vien Dam Sen | 1.78699808208386 CMT8.003 | Cach Mang Thang Tam | 1.8696803311251 BTH.001 | Ba Thang Hai | 1.90253048453201 HV.001 | Hung Vuong | 1.94893975059578 HV.002 | Hung Vuong Plaza | 1.95264094065191 (10 rows)

Tnh chiu di trung bnh ca cc tuyn xe bus?


SELECT AVG(ST_length(the_geom)) AS Distance_Average_Km FROM bus_route; distance_average_km --------------------8.96158423408364 (1 row)

Trang 8

Bi tp ln mn: C s d liu khng gian

HVTH: Phm Th Hng

Tm trn qun 5 tuyn v xe ca Cong ty co phan van tai Mai Linh c ID l CPML?
SELECT distinct r.name FROM bus_route r, bus_cooperative c, route_of_cooperative rc WHERE ST_Crosses(r.the_geom, (SELECT the_geom FROM administrative WHERE name ='Quan 5')) AND (c.id='CPML') AND (c.id=rc.coop_id); name -------------------------------Ben xe Cho Lon - Thao Cam Vien Cho Lon - Cho Go Vap (2 rows)

2.6. S dng Quantum GIS hin th d liu khng gian 2.6.1. Gii thiu Quantum GIS hay cn gi l Qgis l phn mm GIS m ngun m vi cc tnh nng thao tc trn cc lp bn ch yu di dng vector. D n Qgis c khi u vo thng 5 nm 2002. tng c hnh thnh vo thng 2 nm khi Gary Sherman n lc to ra mt phn mm hin th cc file d liu GIS chy trn h iu hnh m ngun m Linux v phn mm c xy dng trn c s ngn ng lp trnh C++. n thng 6 nm 2002, c ng k ti SourceForge.

Giao din chnh ca Quantum GIS 2.6.2. Tnh nng ca Quantum GIS
Trang 9

Bi tp ln mn: C s d liu khng gian

HVTH: Phm Th Hng

Qgis c th c c nhiu dng d liu: cc lp bn to bi ArcView, MapInfo v GRASS, cc bng thng tin to bi PostgreSQL (thng qua PostGIS). S ha bn v cc cng c kt ni vi GPS. Cc tnh nng bin tp bn , to li kinh v , chn thang t l, mi tn ch hng bc... Phn tch khng gian nh PostGIS hoc kt ni vi GRASS GIS. Thay i cc tnh nng thng qua c ch plug-in. 2.6.3. Cc thao tc kt ni vi c s d liu PostGIS - Chn Layer\ Add PostGIS Layer - Ti ca s Add PossGIS Table, chn New to kt ni mi, in y cc thng s vo v sau thc hin kt ni. V d: Name: Host: Database Port Username: Password: Ket noi voi PostGIS localhost baitap 5432 postgres ********* <tn phin lm vic, t t> <a ch lu tr CSDL kt ni> <tn c s d liu> <cng truy cp> <tn ngi s dng> <mt khu>

Kt ni vi c s d liu postGIS

Trang 10

Bi tp ln mn: C s d liu khng gian

HVTH: Phm Th Hng

- Sau chn i tng khng gian hin th. - Ta c th thc hin cc cu truy vn n gin khi hin th d liu - Quantum GIS h tr mt s chc nng c bn ca cc phn mm GIS: Zoom, Pan, Phn loi i tng da trn thuc tnh,

Hin th d liu khng gian TI LIU THAM KHO [1]. Trung Tun (2004). C s d liu. Nh xut bn i hc quc gia thnh ph H Ch Minh, thnh ph H Ch Minh. [2]. Ralf Hartmut Guting. Tutorial Spatial Database System. Germany. [3]. PostGIS 1.3.5 Manual [4] Quantum GIS Documentation Version 1.01 [5]. Website Bch khoa ton th: http://en.wikipedia.org/ [6]. Website SQL Tutorial: http://www.w3schools.com/SQL/

Trang 11