You are on page 1of 84

Part 1:

Use of Sqlite as Relational and


Spatial Database System in
REDAS (Familiarization)

1
Why are databases important ?

MetroManila, Typhoon Ondoy 26 Sept 2009

• Useful in times of emergencies


• Essential for long term development plans of a community/country

2
Database of Elements-at-risk are useful in describing a population
center, prioritization of actions and projects

Riverbanks settlement, MetroManila, Typhoon Ondoy 26 Sept 2009

3
A database is a structured collection of data. The data are
typically organized to model relevant aspects of reality

A relational database is a collection of data items organized


as a set of formally described tables from which data can
be accessed easily. A relational database is created using
the relational model.

source: wikipedia

4
RISK DATABASES are listings of locations and pertinent information of
buildings and critical structures that may be exposed to hazards
In REDAS, risk elements are classified as:

POINTS
-particular spots

LINES

POINTS
LINES
-for linear features

POLYGONS
- for areas
POLYGONS
5
Previous REDAS User_Database

• Text file (.dat/.csv)


• Uses PFE Application (Programmers File
Editor)
• Limited to 12 columns

6
REDAS New_User_Database (~2012)

• Uses SQLite
• Non Server
Based;
• Portable
(easy to build;
not secured)
• User-Built
Database;
• Spatial
extension
(SPATIALITE)

7
SQLite and Spatialite
SQLite – relational database
management system

Spatialite – is a spatial extension to


Sqlite, providing vector geodatabase
functionality. Will use both REDAS and
Quantum GIS (QGIS) to display

8
Run REDAS
REDAS_2.8k

9
Tools

Run REDAS
SQLITE Module

10
The REDAS SQLite database module allows user to create
database (db), its tables and create and execute
commands

11
Three main parts of the REDAS Sqlite module
1 2 3

Database and Tables Sorting menu Column to plot/annotate

12
Upon opening REDAS SQLite module, a default database
(buildings.db) will automatically open. It has only one table
named “barangays”

DATABASE _NAME TABLE_NAME

13
Column Names – Should be All Capital, no spaces must be used

14
Commands/scripts are displayed in this space

15
Familiarization with Sqlite
buttons and functions

16
1. FILE
LOAD DATABASE
EXIT

17
REDAS and its New_User_database folders
Regional Metro_Manila Province
(National Data)

db db db

Imageries Imageries Imageries


Lines Lines Lines

Mapfiles Mapfiles Mapfiles

Points Points Points

Polygons Polygons Polygons

Reports Reports Reports

MyWork MyWork MyWork


Structure of a Database db:
Database = db
Consists of several tables

Regional
(National Data)

db

Points

19
X

1a. Regional\Points Regional


(National Data)

db

- The Buildings.db Points

- Original db file
- Source is the barangay centroid
data from NSO (2007)

20
1b. Regional Folder

• Barangays.db
- (duplicate of Buildings.db found
in Regional/points)
• Regional_exposure.db
- Consists of tables of regional
(national) point data (schools,
hospitals,churches, bridges, etc)

21
2. Metro_Manila folder

 Metro_Manila.db

*MMEIRS = Metro Manila Earthquake Impact Reduction Study


22
The Province folder
Province

pdf Reports db

MyWork plot files Lines

PHOTOS
Grid files Raster plot files Points
Unit_ID
Important:
plot files Polygons
• PHOTOS should be
inside Points folder
Annotations
• UNIT_ID should be
inside PHOTOS folder mapfiles Mapfiles
• Each PC has its own
UNIT_ID geotiff,jpg,bmp Imageries
23
2. TOOLS
Open New SQLITE Window
Open 3rd Party SQLITE Browser
Open Spatialite Gui
Open Spatialite Scripting Window
CREATE NEW DATABASE
CREATE TABLE
Export Data
Import Data

24
2a. You can also duplicate windows by clicking

TOOLS
Open New SQLITE Window

25
2b. You can open the SQLite Browser by clicking

TOOLS
Open 3rd Party SQLITE Browser

SQLite Browser is where


you can edit your tables
including its column
names and other
features

26
2c. You can open the Spatialite GUI by clicking

TOOLS

Open Spatialite GUI

In the Spatialite GUI, we can


edit spatialite table and even
SQlite tables

27
2d. You can open the Spatialite GUI by clicking

TOOLS
Open Spatialite Scripting Window

28
2e. Create new database by clicking:

TOOLS
CREATE NEW
DATABASE (db)

29
2f. Create new table by clicking:

TOOLS
CREATE TABLE

30
3. HELP

SQLITE MANUAL
SPATIALITE MANUAL
SPATIALITE TUTORIAL

31
3a. Viewing of SQLite Manual by clicking

HELP
SQLITE MANUAL

32
3b. Viewing of Spatialite Manual by clicking

HELP
SPATIALITE
MANUAL

33
3c. Viewing of Spatialite Tutorial by clicking

HELP
SPATIALITE
TUTORIAL

34
4. DATABASE NAME(Sample:buildings.db)

35
5. TABLE NAME (Sample table: barangays)

36
6. Show All (Shows all the contents of the database)

37
7a. Column for Sorting

38
7b. Comparator

39
Comparators, in-depth

COMPARATOR TERM MEANING

= EQUALS Exactly the same value as


For strings (text), exactly the same case and spelling.
< LESS THAN Less than the numerical value.
For strings (text), all strings alphabetically before.
> GREATER THAN greater than the numerical value.
For strings (text), all strings alphabetically after.
<> NOT EQUAL TO Not the value. Opposite of EQUALS

LIKE LIKE Strings containing the value as a substring.


LIKE “rain” will return “rain” as well as “rainbow”.

40
7c. Sorting Value

41
7d. SORT (Sorts data you want to view)

42
Data_ID = 2 will appear

43
8a. To plot point data, click the “Column Name for
Plot File” and choose the column name you want to
plot).

44
8b. To plot, click “SHOW POINT PLOT FILE” button. A four-
column database will appear.
8c. followed by the “SAVE POINT PLOT FILE” button

45
8. Then Save the point plot file in
C:\REDAS\NEW_USER_DATABASE\REGIONAL\PO
INTS\barangays_plot.dat

46
The file will scroll down to signify processing.

47
Risk data plotting menu, select point data

48
C:\REDAS\New_User_Database\Regional\Points\
barangays_plot.dat

49
Point Data PlottingInstructions: 50

•Click “query” if you want your point to be queriable


•Click “Layer” if you want your point layer to be visible
•Select your preferred symbol
•Select symbol size
•Select Color from either pull down selection or color palette
•Browse file location of your point data
•Type Legend
•Click “Annotate” if you want your point annotated
•Click “Plot Legend” if you want the legend to show
•Click “Plot Data”
Notes:
•12 point layers may be displayed
•Only one layer may be queried
In some computers,
one needs to zoom in
to your area of
interest in order to be
able to query.

Reason: dataset is
too large

51
Query Points

52
Note: When displaying risk data, REDAS
plots only the risk data in the last selected
geographical coordinates used

This results in not being able to query points

Go back to plot data, point data, unclick and


click desired risk data

Plot map, refresh map


53
Other SQLite features

54
9. Display Photo (1,2,3,4) & DISPLAY PDF

Display Photo – displays up to


four photos you uploaded.
DISPLAY PDF – displays reports
in PDF format

55
10. EXECUTE SQL SCRIPT & View log File

View Log File – is used to


recall the commands or
sql scripts being
executed

56
11. EXPORT AS CSV & IMPORT A CSV FILE

57
Advanced SQLite Queries

58
12a. Always click “Show All” as a preparatory step to
sorting

59
12b. Reminders

• SQL commands are not case sensitive.


Ex: SELECT, select; FROM, from
• Field Names or headers are CASE
SENSITIVE and must NOT have SPACES.
•In TABLE Names, UNDERSCORES are the
only symbols accepted and are commonly
used in place of SPACES.

DATA_ID LONGITUDE LATITUDE BARANGAY_NAME POPULATION_2007


60
13a. Sorting for a particular Barangay

Set these values

Column for sorting : BARANGAY_NAME


Comparator: like
Sorting Value: Santa Maria

61
13b. Check by clicking column for sorting, “like”
comparator and “Santa Maria” as sorting value

62
13c. Click Sort.

Notice that commands are reflected in the script box

63
13d. Result

64
14a. Creating a new table from a sorted data

Set these values

Column for sorting : MUNICIPALITY_CITY


Comparator: like
Sorting Value: Presentacion

65
14b. Click Show All, then set the sorting values

66
14c. Sort

67
14d. Result

14e. We can create a new table for our


results.

68
14f. To create a Table of your selected data, type on
the SQL Script box the command Create Table
Presentacion_Barangays as
and place it before the existing script.

Create Table Presentacion_Barangays as


select * from barangays where
MUNICIPALITY_CITY like ‘%Presentacion%’
69
14g. To execute the script, click “Execute SQL Script”

70
14h. Confirm Command Successful by clicking ok.

71
14i. To update table list, click “Refresh Database”

72
14j. Newly-created tables are shown as the latest in
the list and is found in the lowest part of the list

73
14k. Select the table and check its contents

74
14l. Results

75
15a. Selecting Barangays by their Names from a
given table

Searching for two barangays with specific


names such as Santa Maria and Lagha

To do the above, generate this script in the


SQL Script Box:
Can be
select * from barangays generated
using
where BARANGAY_NAME like ‘%Santa Maria%’ sorting
menu

or BARANGAY_NAME like ‘%Lagha%’


76
15b. It should look like this.

select * from barangays where


BARANGAY_NAME like ‘%Santa Maria%’ or
BARANGAY_NAME like ‘%Lagha%’

77
15c. Execute the SQL Script.

78
15d. Results are as shown.

79
16a. Selecting Barangays in a given Municipality

Can be
select * from barangays generated
using sorting
where MUNICIPALITY_CITY like menu

‘%Presentacion%’
and Can be
(BARANGAY_NAME like ‘%Santa Maria%’ copied
from log
or BARANGAY_NAME like ‘%Lagha%’) file

80
16b. Click EXECUTE SQL SCRIPT

82
16c. Results are as shown.

83
17. Displaying counts

1.Load db: Regional_exposure.db


2.Load Schools table
3.Sort School_name, use “like” as
comparator, type “national high school”
under sorting value
4.Insert count(*) to make the script select
count(*) from Schools where
SCHOOL_NAME like '%national high
school%‘
5. Click “execute sql script”
End of Part 1
Part 2:Building Database in SQLite Module

85

You might also like