SlideShare a Scribd company logo
Intro to PostGis
  For beginners


Alban de Lavenne


17 décembre 2012
How do you organise your data ?


The problem :
    lots of dierent les, formats : csv, txt, xls, shp, asc ...
    in dierent folders more or less well organised and easy to nd
    lack of descriptions (source, stations, projection, last version ...)
    reproducible science ?
    easy sharing ?
    easy backup ?

Do you need a database ?
    not so dicult to built
    enforce you to organize you data
    save you a lot of time in the long run




A. de Lavenne (INRA, Rennes)          University of Trento              17 décembre 2012   2 / 25
How do you organise your data ?


The problem :
    lots of dierent les, formats : csv, txt, xls, shp, asc ...
    in dierent folders more or less well organised and easy to nd
    lack of descriptions (source, stations, projection, last version ...)
    reproducible science ?
    easy sharing ?
    easy backup ?

Do you need a database ?
    not so dicult to built
    enforce you to organize you data
    save you a lot of time in the long run




A. de Lavenne (INRA, Rennes)          University of Trento              17 décembre 2012   2 / 25
How do you organise your data ?


The problem :
    lots of dierent les, formats : csv, txt, xls, shp, asc ...
    in dierent folders more or less well organised and easy to nd
    lack of descriptions (source, stations, projection, last version ...)
    reproducible science ?
    easy sharing ?
    easy backup ?

Do you need a database ?
    not so dicult to built
    enforce you to organize you data
    save you a lot of time in the long run




A. de Lavenne (INRA, Rennes)          University of Trento              17 décembre 2012   2 / 25
How do you organise your data ?


The problem :
    lots of dierent les, formats : csv, txt, xls, shp, asc ...
    in dierent folders more or less well organised and easy to nd
    lack of descriptions (source, stations, projection, last version ...)
    reproducible science ?
    easy sharing ?
    easy backup ?

Do you need a database ?
    not so dicult to built
    enforce you to organize you data
    save you a lot of time in the long run




A. de Lavenne (INRA, Rennes)          University of Trento              17 décembre 2012   2 / 25
How do you organise your data ?


The problem :
    lots of dierent les, formats : csv, txt, xls, shp, asc ...
    in dierent folders more or less well organised and easy to nd
    lack of descriptions (source, stations, projection, last version ...)
    reproducible science ?
    easy sharing ?
    easy backup ?

Do you need a database ?
    not so dicult to built
    enforce you to organize you data
    save you a lot of time in the long run




A. de Lavenne (INRA, Rennes)          University of Trento              17 décembre 2012   2 / 25
How do you organise your data ?


The problem :
    lots of dierent les, formats : csv, txt, xls, shp, asc ...
    in dierent folders more or less well organised and easy to nd
    lack of descriptions (source, stations, projection, last version ...)
    reproducible science ?
    easy sharing ?
    easy backup ?

Do you need a database ?
    not so dicult to built
    enforce you to organize you data
    save you a lot of time in the long run




A. de Lavenne (INRA, Rennes)          University of Trento              17 décembre 2012   2 / 25
How do you organise your data ?


The problem :
    lots of dierent les, formats : csv, txt, xls, shp, asc ...
    in dierent folders more or less well organised and easy to nd
    lack of descriptions (source, stations, projection, last version ...)
    reproducible science ?
    easy sharing ?
    easy backup ?

Do you need a database ?
    not so dicult to built
    enforce you to organize you data
    save you a lot of time in the long run




A. de Lavenne (INRA, Rennes)          University of Trento              17 décembre 2012   2 / 25
What Is PostGIS ?




Open source object-relational database management system (ORDBMS)
Widely considered as the most full-featured open-source database system.




PostGIS = database extender for the PostgreSQL Database Management System
     open source, freely available, fairly OGC compliant
In a nutshell it adds spatial functions such as :
     distance, area, union, intersection
     specialty geometry data types to the database
A. de Lavenne (INRA, Rennes)       University of Trento           17 décembre 2012   3 / 25
Installing PostgreSQL with PostGIS Functionality



         Install PostgreSQL rst, then PostGIS extension (using Stack Builder)
                        http://www.postgresql.org/download/
                      http://postgis.refractions.net/download/

Windows
http://www.postgresql.org/download/windows

Mac
http://www.kyngchaos.com/software:postgres

Ubuntu
sudo apt-get install postgresql postgis
psql -d template1 -c alter user postgres with password 'postgres'




A. de Lavenne (INRA, Rennes)        University of Trento            17 décembre 2012   4 / 25
Creating a database




Using pgAdminIII interface
Right clic on database

or using SQL query

CREATE DATABASE blavet
  WITH ENCODING=' UTF8 '
       OWNER=postgres
       LC_COLLATE=' fr_FR . UTF -8 '
       LC_CTYPE=' fr_FR . UTF -8 '
       CONNECTION LIMIT =−1;




A. de Lavenne (INRA, Rennes)       University of Trento   17 décembre 2012   5 / 25
Adding spatial extension




Using pgAdminIII interface
Right clic on extensions

or using SQL query

CREATE EXTENSION postgis
   VERSION  2.0.1 


Check the table spatial_ref_sys
Check also that you have lots of new functions




A. de Lavenne (INRA, Rennes)         University of Trento   17 décembre 2012   6 / 25
Organising a database




How would you organise a database ? Having :
   Runo time series of dierent basins
   Rainfall time series of dierent raingauges
   Spatial description of those measurements
   Other spatial informations (like soil type, geology...)




A. de Lavenne (INRA, Rennes)         University of Trento    17 décembre 2012   7 / 25
Adding tables


Table of basins
CREATE TABLE basins
(
     id_basin serial NOT NULL ,
     name text ,
     city text ,
     altitude numeric ,
     area numeric ,
     x_lambert2e numeric ,
     y_lambert2e numeric ,
     CONSTRAINT basins_pkey PRIMARY KEY           ( id_basin   )
)
WITH (
  OIDS=TRUE
);
ALTER TABLE basins
  OWNER TO postgres ;




A. de Lavenne (INRA, Rennes)    University of Trento               17 décembre 2012   8 / 25
Adding tables


Table of measures of runo
CREATE TABLE measures_runoff
(
     id_runoff serial NOT NULL ,
     id_basin integer NOT NULL ,
     datehourmeasure timestamp with time zone NOT NULL ,
     runoff numeric ,
     timestep text ,
     unit text ,
     source text ,
     CONSTRAINT measures_runoff_pkey PRIMARY KEY ( id_runoff    ,
         id_basin , datehourmeasure )
)
WITH (
  OIDS=FALSE
);
ALTER TABLE measures_runoff
  OWNER TO postgres ;




A. de Lavenne (INRA, Rennes)    University of Trento   17 décembre 2012   9 / 25
Adding tables

Table of raingauges

CREATE TABLE raingauges
(
     id_raingauge serial ,
     id_meteofrance integer ,
     city text ,
     name_raingauge text ,
     altitude numeric ,
     type_raingauge text ,
     producer text ,
     latitude_dec numeric ,
     longitude_dec numeric ,
     CONSTRAINT raingauge_pkey PRIMARY KEY             ( id_raingauge   )
)
WITH (
  OIDS=TRUE
);
ALTER TABLE raingauges
  OWNER TO postgres ;




A. de Lavenne (INRA, Rennes)    University of Trento               17 décembre 2012   10 / 25
Adding tables


Table of measures of rainfall
CREATE TABLE measures_rainfall
(
     id_rainfall serial ,
     id_raingauge integer ,
     datehourmeasure timestamp with time zone ,
     rainfall numeric ,
     timestep text ,
     unit text ,
     CONSTRAINT rainfall_pkey PRIMARY KEY ( id_rainfall    ,
         id_raingauge , datehourmeasure )
)
WITH (
  OIDS=FALSE
);
ALTER TABLE measures_rainfall
  OWNER TO postgres ;




A. de Lavenne (INRA, Rennes)     University of Trento     17 décembre 2012   11 / 25
Adding tables



Table of antilope points

CREATE TABLE antilope_points
(
     id_point numeric NOT NULL ,
     x_lambert2e numeric ,
     y_lambert2e numeric ,
     CONSTRAINT antilope_points_pkey PRIMARY KEY       ( id_point   )
)
WITH (
  OIDS=TRUE
);
ALTER TABLE antilope_points
  OWNER TO postgres ;




A. de Lavenne (INRA, Rennes)    University of Trento        17 décembre 2012   12 / 25
Adding tables


Table of measures of antilope points

CREATE TABLE measures_rainfall_antilope
(
     id_measure serial ,
     id_point integer ,
     datehourmeasure timestamp with time zone ,
     rainfall numeric ,
     units text ,
     CONSTRAINT measures_rainfall_antilope_pkey PRIMARY KEY      (
         id_measure )
)
WITH (
  OIDS=FALSE
);
ALTER TABLE measures_rainfall_antilope
  OWNER TO postgres ;




A. de Lavenne (INRA, Rennes)     University of Trento   17 décembre 2012   13 / 25
Filling tables


Import les to database

COPY basins ( id_basin , name , city , altitude , x_lambert2e , y_lambert2e )
    FROM ' .../ Intro_to_PostGIS / basins . csv ' with delimiter '; ' ;
COPY measures_runoff ( id_basin , datehourmeasure , runoff , timestep ,
    unit ) FROM ' .../ Intro_to_PostGIS / runoff . csv ' with delimiter '
    ;' ;
COPY raingauges ( id_raingauge , id_meteofrance , city , name_raingauge ,
    altitude , latitude_dms , longitude_dms , type_raingauge , producer ,
    latitude_dec , longitude_dec ) FROM ' .../ Intro_to_PostGIS /
    raingauges . csv ' with delimiter '; ' ;
COPY measures_rainfall ( id_raingauge , datehourmeasure , rainfall ,
    timestep , unit ) FROM ' .../ Intro_to_PostGIS / rainfall . csv ' with
    delimiter '; ' ;
COPY antilope_points ( id_point , x_lambert2e , y_lambert2e ) FROM ' .../
    Intro_to_PostGIS / antilope_points . csv ' with delimiter '; ' ;
COPY measures_rainfall_antilope ( id_point , datehourmeasure , rainfall
    , units ) FROM ' .../ Intro_to_PostGIS / antilope_rainfall . csv '
    with delimiter '; ' ;




A. de Lavenne (INRA, Rennes)    University of Trento        17 décembre 2012   14 / 25
Adding internal spatial information
AddGeometryColumn

AddGeometryColumn ( schema_name           ,   table_name      ,   column_name   ,   srid ,
    type , dimension )




                               (Source : JUMP, Technical Report)
A. de Lavenne (INRA, Rennes)           University of Trento               17 décembre 2012   15 / 25
Adding internal spatial information




                      (Source : OpenGIS Simple Features Specication for SQL)



A. de Lavenne (INRA, Rennes)             University of Trento               17 décembre 2012   16 / 25
Geometry Constructors and Geometry Accessors



ST_MakePoint

ST_MakePoint ( x         ,     y   ,   z   )



                               Full list here : Geometry Constructors
ST_X, ST_Y and ST_Z

ST_X ( geometry )
ST_Y ( geometry )
ST_Z ( geometry )


                                   Full list here : Geometry Accessors




A. de Lavenne (INRA, Rennes)                   University of Trento      17 décembre 2012   17 / 25
Geometry Editors

ST_SetSRID

ST_SetSRID ( geom , srid )



ST_Transform

ST_Transform ( geom , srid )


                               Full list here : Geometry Editors


A Spatial Reference System Identier (SRID) is a unique value used to unambiguously
   identify projected, unprojected, and local spatial coordinate system denitions.

Large database of SRID created by the European Petroleum Survey Group (EPSG) is
                                   widely used.

A. de Lavenne (INRA, Rennes)            University of Trento       17 décembre 2012   18 / 25
Spatial Relationships and Measurements


Spatial Relationships and Measurements

ST_Area ( geometry )
ST_Centroid ( geometry )
ST_Contains ( geometry , geometry )
ST_Distance ( geometry , geometry )


                  Full list here : Spatial Relationships and Measurements
Geometry Processing

ST_Buffer ( geometry , radius_of_buffer )
ST_Union ( geometry set g1field )
ST_Union ( geometry , geometry )
ST_Intersection ( geometry , geometry )


                               Full list here : Geometry Processing


A. de Lavenne (INRA, Rennes)             University of Trento         17 décembre 2012   19 / 25
Adding internal spatial information


Adding geometry columns

−−A d d i n g g e o m e t r y c o l u m n s
SELECT AddGeometryColumn ( ' basins ' , ' outlet_lambert2e ' , 2 7 5 7 2 , '
    POINT ' , 2 ) ;
SELECT AddGeometryColumn ( ' basins ' , ' outlet_lambert93 ' , 2 1 5 4 , '
    POINT ' , 2 ) ;
SELECT AddGeometryColumn ( ' basins ' , ' limits_lambert2e ' , 2 7 5 7 2 , '
    POLYGON ' , 2 ) ;
SELECT AddGeometryColumn ( ' basins ' , ' limits_lambert93 ' , 2 1 5 4 , '
    POLYGON ' , 2 ) ;
SELECT AddGeometryColumn ( ' raingauges ' , ' raingauges_lambert2e ' ,
    2 7 5 7 2 , ' POINT ' , 2 ) ;
SELECT AddGeometryColumn ( ' raingauges ' , ' raingauges_lambert93 ' ,
    2 1 5 4 , ' POINT ' , 2 ) ;
SELECT AddGeometryColumn ( ' antilope_points ' , '
    antilope_points_lambert2e ' , 2 7 5 7 2 , ' POINT ' , 2 ) ;
SELECT AddGeometryColumn ( ' antilope_points ' , '
    antilope_points_lambert93 ' , 2 1 5 4 , ' POINT ' , 2 ) ;




 A. de Lavenne (INRA, Rennes)                 University of Trento   17 décembre 2012   20 / 25
Adding internal spatial information


Filling geometry columns

−− F i l l g e o m e t r y c o l u m n s
UPDATE basins SET outlet_lambert2e=st_setsrid ( st_makepoint (
    x_lambert2e , y_lambert2e ) , 2 7 5 7 2 ) ;
UPDATE basins SET outlet_lambert93=st_transform ( outlet_lambert2e
       ,2154) ;
UPDATE raingauges SET raingauges_lambert2e=st_transform (
    st_setsrid ( st_makepoint ( longitude_dec , latitude_dec ) ,           4326)
       ,27572) ;
UPDATE raingauges SET raingauges_lambert93=st_transform (
    st_setsrid ( st_makepoint ( longitude_dec , latitude_dec ) ,           4326)
       ,2154) ;
UPDATE antilope_points SET antilope_points_lambert2e=st_setsrid (
    st_makepoint ( x_lambert2e , y_lambert2e ) , 2 7 5 7 2 ) ;
UPDATE antilope_points SET antilope_points_lambert93=st_transform
    ( st_setsrid ( st_makepoint ( x_lambert2e , y_lambert2e ) , 2 7 5 7 2 )
       ,2154) ;




 A. de Lavenne (INRA, Rennes)              University of Trento   17 décembre 2012   21 / 25
Adding external spatial information
Using graphical interface
Using Quantum-GIS extension (like QGIS spit)
pgShapeLoader
...

Using command line with shp2pgsql
shp2pgsql -iIDd -s 2154 -W utf8 basins_lambert93.shp basins_polygon                impo
psql -U postgres -W -h localhost -p 5432 -d blavet -f import.sql

                                           OR
shp2pgsql -iIDd -s 2154 -W utf8 basins_lambert93.shp basins_polygon |
psql -U postgres -W -h localhost -p 5432 -d blavet

Filling geometry columns

UPDATE basins SET limits_lambert93=st_geometryn ( geom , 1 )
FROM basins_polygon
WHERE basins_polygon . id_basin=basins . id_basin ;

and limits_lambert2e ...
A. de Lavenne (INRA, Rennes)        University of Trento      17 décembre 2012   22 / 25
Exemple SQL of query




Select a chronique of runo
Calculate the area of one basin
Select raingauges inside one basin
Select average antilope rainfall time serie for each basins
Import shapele geology and get a table of statistics of geology for each basin
Check if there is double measures in runo and rainfall




A. de Lavenne (INRA, Rennes)        University of Trento            17 décembre 2012   23 / 25
Visualizing your data




Using Qgis to vizualize your data
Plugin in Qgis for query and shapele creation
Plugin in Qgis for importing data




A. de Lavenne (INRA, Rennes)        University of Trento   17 décembre 2012   24 / 25
Connect to your database with R




A. de Lavenne (INRA, Rennes)        University of Trento    17 décembre 2012   25 / 25
Connect to your database with R




A. de Lavenne (INRA, Rennes)        University of Trento    17 décembre 2012   25 / 25

More Related Content

PPT
Water science l2 cwr final full ed
PDF
11 modern-iuh
PDF
Crop Et And Implications For Irrigation
PPTX
Daily evapotranspiration by combining remote sensing with ground observations...
PDF
5 hydrology quantities-measures_instruments_activities
PDF
Water platform 2011-2014
PDF
TIME INTEGRATION OF EVAPOTRANSPIRATION USING A TWO SOURCE SURFACE ENERGY BALA...
PDF
Evapotranspiration Bed Wastewater Treatment and Gardening
Water science l2 cwr final full ed
11 modern-iuh
Crop Et And Implications For Irrigation
Daily evapotranspiration by combining remote sensing with ground observations...
5 hydrology quantities-measures_instruments_activities
Water platform 2011-2014
TIME INTEGRATION OF EVAPOTRANSPIRATION USING A TWO SOURCE SURFACE ENERGY BALA...
Evapotranspiration Bed Wastewater Treatment and Gardening

Viewers also liked (20)

PDF
A sensitivity Analysis of Eddy Covariance Data Processing Methods for Evapotr...
PDF
6 measurement&representation
PDF
14 snow hydrology-part1
PDF
Python IDLE (Integrated Development and Learning Environment) for remote sens...
PDF
Session I: Water Consumption – Evapotranspiration (ET) Case Study Tunisia
PDF
10 water in soil-rev 1
PDF
Introduction tohydrology b
PDF
Introduction tohydrology c
PDF
Using Git Inside Eclipse, Pushing/Cloning from GitHub
PPT
From land use to land cover: evapotraspiration assessment in a metropolitan r...
PDF
4 introduction to uDig
PDF
Time integration of evapotranspiration using a two source surface energy bala...
PPTX
Adding Confidence to Seasonal Drought Forecasts using reference evapotranspir...
PDF
3 introduction gis
PDF
2013 ASPRS Track, Developing an ArcGIS Toolbox for Estimating EvapoTranspirat...
PDF
2 hydro-geomorphology
PPTX
Cu07821 3 precipitation and evapotranspiration
PPT
#4- Comp Plan - Evaporation & Evapotranspiration
PDF
9 precipitations - rainfall
PDF
15 Evapotranspiration
A sensitivity Analysis of Eddy Covariance Data Processing Methods for Evapotr...
6 measurement&representation
14 snow hydrology-part1
Python IDLE (Integrated Development and Learning Environment) for remote sens...
Session I: Water Consumption – Evapotranspiration (ET) Case Study Tunisia
10 water in soil-rev 1
Introduction tohydrology b
Introduction tohydrology c
Using Git Inside Eclipse, Pushing/Cloning from GitHub
From land use to land cover: evapotraspiration assessment in a metropolitan r...
4 introduction to uDig
Time integration of evapotranspiration using a two source surface energy bala...
Adding Confidence to Seasonal Drought Forecasts using reference evapotranspir...
3 introduction gis
2013 ASPRS Track, Developing an ArcGIS Toolbox for Estimating EvapoTranspirat...
2 hydro-geomorphology
Cu07821 3 precipitation and evapotranspiration
#4- Comp Plan - Evaporation & Evapotranspiration
9 precipitations - rainfall
15 Evapotranspiration
Ad

Similar to Introduction to post_gis (20)

PDF
Slides 111017220255-phpapp01
PDF
Python for Financial Data Analysis with pandas
PPTX
Rattle Graphical Interface for R Language
PPT
MapReduce in cgrid and cloud computinge.ppt
PPTX
Democratizing Big Semantic Data management
PDF
Deep learning and applications in non-cognitive domains II
PPT
Vitus Masters Defense
PDF
H2O Distributed Deep Learning by Arno Candel 071614
PPT
A New Partnership for Cross-Scale, Cross-Domain eScience
PDF
PyParis 2017 / Pandas - What's new and whats coming - Joris van den Bossche
PPTX
Stratosphere with big_data_analytics
PDF
AWS SSA Webinar 20 - Getting Started with Data Warehouses on AWS
PPTX
Empowering Transformational Science
PDF
Debunking "Purpose-Built Data Systems:": Enter the Universal Database
PPTX
ACT Talk, Giuseppe Totaro: High Performance Computing for Distributed Indexin...
PPTX
Tg noh jeju_workshop
PPTX
PPT
Hands on data science with r.pptx
PPT
Positional Data Organization and Compression in Web Inverted Indexes
Slides 111017220255-phpapp01
Python for Financial Data Analysis with pandas
Rattle Graphical Interface for R Language
MapReduce in cgrid and cloud computinge.ppt
Democratizing Big Semantic Data management
Deep learning and applications in non-cognitive domains II
Vitus Masters Defense
H2O Distributed Deep Learning by Arno Candel 071614
A New Partnership for Cross-Scale, Cross-Domain eScience
PyParis 2017 / Pandas - What's new and whats coming - Joris van den Bossche
Stratosphere with big_data_analytics
AWS SSA Webinar 20 - Getting Started with Data Warehouses on AWS
Empowering Transformational Science
Debunking "Purpose-Built Data Systems:": Enter the Universal Database
ACT Talk, Giuseppe Totaro: High Performance Computing for Distributed Indexin...
Tg noh jeju_workshop
Hands on data science with r.pptx
Positional Data Organization and Compression in Web Inverted Indexes
Ad

More from AboutHydrology Slides (12)

PDF
RoccoPancieraMesiano sept25 2013
PDF
Luca Brocca seminario trento
PDF
3b jf h-readingdatafromconsole
PDF
3 jf h-linearequations
PDF
2 jfh-yourveryfirstprogram
PDF
1 jf h-getting started
PDF
La piattaforma acqua
PDF
La convenzione delle alpi
PDF
1 introduction to hydrology
PDF
13 solar radiation
PDF
0-RealBookStyleAndNotation
PDF
0-RealBooksOfHydrology
RoccoPancieraMesiano sept25 2013
Luca Brocca seminario trento
3b jf h-readingdatafromconsole
3 jf h-linearequations
2 jfh-yourveryfirstprogram
1 jf h-getting started
La piattaforma acqua
La convenzione delle alpi
1 introduction to hydrology
13 solar radiation
0-RealBookStyleAndNotation
0-RealBooksOfHydrology

Recently uploaded (20)

PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
Pre independence Education in Inndia.pdf
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
GDM (1) (1).pptx small presentation for students
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
Insiders guide to clinical Medicine.pdf
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
Computing-Curriculum for Schools in Ghana
PDF
Classroom Observation Tools for Teachers
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
RMMM.pdf make it easy to upload and study
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
Cell Structure & Organelles in detailed.
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
Renaissance Architecture: A Journey from Faith to Humanism
Pre independence Education in Inndia.pdf
STATICS OF THE RIGID BODIES Hibbelers.pdf
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Microbial disease of the cardiovascular and lymphatic systems
GDM (1) (1).pptx small presentation for students
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Insiders guide to clinical Medicine.pdf
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Computing-Curriculum for Schools in Ghana
Classroom Observation Tools for Teachers
O5-L3 Freight Transport Ops (International) V1.pdf
RMMM.pdf make it easy to upload and study
Module 4: Burden of Disease Tutorial Slides S2 2025
Cell Structure & Organelles in detailed.
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
human mycosis Human fungal infections are called human mycosis..pptx
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
102 student loan defaulters named and shamed – Is someone you know on the list?

Introduction to post_gis

  • 1. Intro to PostGis For beginners Alban de Lavenne 17 décembre 2012
  • 2. How do you organise your data ? The problem : lots of dierent les, formats : csv, txt, xls, shp, asc ... in dierent folders more or less well organised and easy to nd lack of descriptions (source, stations, projection, last version ...) reproducible science ? easy sharing ? easy backup ? Do you need a database ? not so dicult to built enforce you to organize you data save you a lot of time in the long run A. de Lavenne (INRA, Rennes) University of Trento 17 décembre 2012 2 / 25
  • 3. How do you organise your data ? The problem : lots of dierent les, formats : csv, txt, xls, shp, asc ... in dierent folders more or less well organised and easy to nd lack of descriptions (source, stations, projection, last version ...) reproducible science ? easy sharing ? easy backup ? Do you need a database ? not so dicult to built enforce you to organize you data save you a lot of time in the long run A. de Lavenne (INRA, Rennes) University of Trento 17 décembre 2012 2 / 25
  • 4. How do you organise your data ? The problem : lots of dierent les, formats : csv, txt, xls, shp, asc ... in dierent folders more or less well organised and easy to nd lack of descriptions (source, stations, projection, last version ...) reproducible science ? easy sharing ? easy backup ? Do you need a database ? not so dicult to built enforce you to organize you data save you a lot of time in the long run A. de Lavenne (INRA, Rennes) University of Trento 17 décembre 2012 2 / 25
  • 5. How do you organise your data ? The problem : lots of dierent les, formats : csv, txt, xls, shp, asc ... in dierent folders more or less well organised and easy to nd lack of descriptions (source, stations, projection, last version ...) reproducible science ? easy sharing ? easy backup ? Do you need a database ? not so dicult to built enforce you to organize you data save you a lot of time in the long run A. de Lavenne (INRA, Rennes) University of Trento 17 décembre 2012 2 / 25
  • 6. How do you organise your data ? The problem : lots of dierent les, formats : csv, txt, xls, shp, asc ... in dierent folders more or less well organised and easy to nd lack of descriptions (source, stations, projection, last version ...) reproducible science ? easy sharing ? easy backup ? Do you need a database ? not so dicult to built enforce you to organize you data save you a lot of time in the long run A. de Lavenne (INRA, Rennes) University of Trento 17 décembre 2012 2 / 25
  • 7. How do you organise your data ? The problem : lots of dierent les, formats : csv, txt, xls, shp, asc ... in dierent folders more or less well organised and easy to nd lack of descriptions (source, stations, projection, last version ...) reproducible science ? easy sharing ? easy backup ? Do you need a database ? not so dicult to built enforce you to organize you data save you a lot of time in the long run A. de Lavenne (INRA, Rennes) University of Trento 17 décembre 2012 2 / 25
  • 8. How do you organise your data ? The problem : lots of dierent les, formats : csv, txt, xls, shp, asc ... in dierent folders more or less well organised and easy to nd lack of descriptions (source, stations, projection, last version ...) reproducible science ? easy sharing ? easy backup ? Do you need a database ? not so dicult to built enforce you to organize you data save you a lot of time in the long run A. de Lavenne (INRA, Rennes) University of Trento 17 décembre 2012 2 / 25
  • 9. What Is PostGIS ? Open source object-relational database management system (ORDBMS) Widely considered as the most full-featured open-source database system. PostGIS = database extender for the PostgreSQL Database Management System open source, freely available, fairly OGC compliant In a nutshell it adds spatial functions such as : distance, area, union, intersection specialty geometry data types to the database A. de Lavenne (INRA, Rennes) University of Trento 17 décembre 2012 3 / 25
  • 10. Installing PostgreSQL with PostGIS Functionality Install PostgreSQL rst, then PostGIS extension (using Stack Builder) http://www.postgresql.org/download/ http://postgis.refractions.net/download/ Windows http://www.postgresql.org/download/windows Mac http://www.kyngchaos.com/software:postgres Ubuntu sudo apt-get install postgresql postgis psql -d template1 -c alter user postgres with password 'postgres' A. de Lavenne (INRA, Rennes) University of Trento 17 décembre 2012 4 / 25
  • 11. Creating a database Using pgAdminIII interface Right clic on database or using SQL query CREATE DATABASE blavet WITH ENCODING=' UTF8 ' OWNER=postgres LC_COLLATE=' fr_FR . UTF -8 ' LC_CTYPE=' fr_FR . UTF -8 ' CONNECTION LIMIT =−1; A. de Lavenne (INRA, Rennes) University of Trento 17 décembre 2012 5 / 25
  • 12. Adding spatial extension Using pgAdminIII interface Right clic on extensions or using SQL query CREATE EXTENSION postgis VERSION 2.0.1 Check the table spatial_ref_sys Check also that you have lots of new functions A. de Lavenne (INRA, Rennes) University of Trento 17 décembre 2012 6 / 25
  • 13. Organising a database How would you organise a database ? Having : Runo time series of dierent basins Rainfall time series of dierent raingauges Spatial description of those measurements Other spatial informations (like soil type, geology...) A. de Lavenne (INRA, Rennes) University of Trento 17 décembre 2012 7 / 25
  • 14. Adding tables Table of basins CREATE TABLE basins ( id_basin serial NOT NULL , name text , city text , altitude numeric , area numeric , x_lambert2e numeric , y_lambert2e numeric , CONSTRAINT basins_pkey PRIMARY KEY ( id_basin ) ) WITH ( OIDS=TRUE ); ALTER TABLE basins OWNER TO postgres ; A. de Lavenne (INRA, Rennes) University of Trento 17 décembre 2012 8 / 25
  • 15. Adding tables Table of measures of runo CREATE TABLE measures_runoff ( id_runoff serial NOT NULL , id_basin integer NOT NULL , datehourmeasure timestamp with time zone NOT NULL , runoff numeric , timestep text , unit text , source text , CONSTRAINT measures_runoff_pkey PRIMARY KEY ( id_runoff , id_basin , datehourmeasure ) ) WITH ( OIDS=FALSE ); ALTER TABLE measures_runoff OWNER TO postgres ; A. de Lavenne (INRA, Rennes) University of Trento 17 décembre 2012 9 / 25
  • 16. Adding tables Table of raingauges CREATE TABLE raingauges ( id_raingauge serial , id_meteofrance integer , city text , name_raingauge text , altitude numeric , type_raingauge text , producer text , latitude_dec numeric , longitude_dec numeric , CONSTRAINT raingauge_pkey PRIMARY KEY ( id_raingauge ) ) WITH ( OIDS=TRUE ); ALTER TABLE raingauges OWNER TO postgres ; A. de Lavenne (INRA, Rennes) University of Trento 17 décembre 2012 10 / 25
  • 17. Adding tables Table of measures of rainfall CREATE TABLE measures_rainfall ( id_rainfall serial , id_raingauge integer , datehourmeasure timestamp with time zone , rainfall numeric , timestep text , unit text , CONSTRAINT rainfall_pkey PRIMARY KEY ( id_rainfall , id_raingauge , datehourmeasure ) ) WITH ( OIDS=FALSE ); ALTER TABLE measures_rainfall OWNER TO postgres ; A. de Lavenne (INRA, Rennes) University of Trento 17 décembre 2012 11 / 25
  • 18. Adding tables Table of antilope points CREATE TABLE antilope_points ( id_point numeric NOT NULL , x_lambert2e numeric , y_lambert2e numeric , CONSTRAINT antilope_points_pkey PRIMARY KEY ( id_point ) ) WITH ( OIDS=TRUE ); ALTER TABLE antilope_points OWNER TO postgres ; A. de Lavenne (INRA, Rennes) University of Trento 17 décembre 2012 12 / 25
  • 19. Adding tables Table of measures of antilope points CREATE TABLE measures_rainfall_antilope ( id_measure serial , id_point integer , datehourmeasure timestamp with time zone , rainfall numeric , units text , CONSTRAINT measures_rainfall_antilope_pkey PRIMARY KEY ( id_measure ) ) WITH ( OIDS=FALSE ); ALTER TABLE measures_rainfall_antilope OWNER TO postgres ; A. de Lavenne (INRA, Rennes) University of Trento 17 décembre 2012 13 / 25
  • 20. Filling tables Import les to database COPY basins ( id_basin , name , city , altitude , x_lambert2e , y_lambert2e ) FROM ' .../ Intro_to_PostGIS / basins . csv ' with delimiter '; ' ; COPY measures_runoff ( id_basin , datehourmeasure , runoff , timestep , unit ) FROM ' .../ Intro_to_PostGIS / runoff . csv ' with delimiter ' ;' ; COPY raingauges ( id_raingauge , id_meteofrance , city , name_raingauge , altitude , latitude_dms , longitude_dms , type_raingauge , producer , latitude_dec , longitude_dec ) FROM ' .../ Intro_to_PostGIS / raingauges . csv ' with delimiter '; ' ; COPY measures_rainfall ( id_raingauge , datehourmeasure , rainfall , timestep , unit ) FROM ' .../ Intro_to_PostGIS / rainfall . csv ' with delimiter '; ' ; COPY antilope_points ( id_point , x_lambert2e , y_lambert2e ) FROM ' .../ Intro_to_PostGIS / antilope_points . csv ' with delimiter '; ' ; COPY measures_rainfall_antilope ( id_point , datehourmeasure , rainfall , units ) FROM ' .../ Intro_to_PostGIS / antilope_rainfall . csv ' with delimiter '; ' ; A. de Lavenne (INRA, Rennes) University of Trento 17 décembre 2012 14 / 25
  • 21. Adding internal spatial information AddGeometryColumn AddGeometryColumn ( schema_name , table_name , column_name , srid , type , dimension ) (Source : JUMP, Technical Report) A. de Lavenne (INRA, Rennes) University of Trento 17 décembre 2012 15 / 25
  • 22. Adding internal spatial information (Source : OpenGIS Simple Features Specication for SQL) A. de Lavenne (INRA, Rennes) University of Trento 17 décembre 2012 16 / 25
  • 23. Geometry Constructors and Geometry Accessors ST_MakePoint ST_MakePoint ( x , y , z ) Full list here : Geometry Constructors ST_X, ST_Y and ST_Z ST_X ( geometry ) ST_Y ( geometry ) ST_Z ( geometry ) Full list here : Geometry Accessors A. de Lavenne (INRA, Rennes) University of Trento 17 décembre 2012 17 / 25
  • 24. Geometry Editors ST_SetSRID ST_SetSRID ( geom , srid ) ST_Transform ST_Transform ( geom , srid ) Full list here : Geometry Editors A Spatial Reference System Identier (SRID) is a unique value used to unambiguously identify projected, unprojected, and local spatial coordinate system denitions. Large database of SRID created by the European Petroleum Survey Group (EPSG) is widely used. A. de Lavenne (INRA, Rennes) University of Trento 17 décembre 2012 18 / 25
  • 25. Spatial Relationships and Measurements Spatial Relationships and Measurements ST_Area ( geometry ) ST_Centroid ( geometry ) ST_Contains ( geometry , geometry ) ST_Distance ( geometry , geometry ) Full list here : Spatial Relationships and Measurements Geometry Processing ST_Buffer ( geometry , radius_of_buffer ) ST_Union ( geometry set g1field ) ST_Union ( geometry , geometry ) ST_Intersection ( geometry , geometry ) Full list here : Geometry Processing A. de Lavenne (INRA, Rennes) University of Trento 17 décembre 2012 19 / 25
  • 26. Adding internal spatial information Adding geometry columns −−A d d i n g g e o m e t r y c o l u m n s SELECT AddGeometryColumn ( ' basins ' , ' outlet_lambert2e ' , 2 7 5 7 2 , ' POINT ' , 2 ) ; SELECT AddGeometryColumn ( ' basins ' , ' outlet_lambert93 ' , 2 1 5 4 , ' POINT ' , 2 ) ; SELECT AddGeometryColumn ( ' basins ' , ' limits_lambert2e ' , 2 7 5 7 2 , ' POLYGON ' , 2 ) ; SELECT AddGeometryColumn ( ' basins ' , ' limits_lambert93 ' , 2 1 5 4 , ' POLYGON ' , 2 ) ; SELECT AddGeometryColumn ( ' raingauges ' , ' raingauges_lambert2e ' , 2 7 5 7 2 , ' POINT ' , 2 ) ; SELECT AddGeometryColumn ( ' raingauges ' , ' raingauges_lambert93 ' , 2 1 5 4 , ' POINT ' , 2 ) ; SELECT AddGeometryColumn ( ' antilope_points ' , ' antilope_points_lambert2e ' , 2 7 5 7 2 , ' POINT ' , 2 ) ; SELECT AddGeometryColumn ( ' antilope_points ' , ' antilope_points_lambert93 ' , 2 1 5 4 , ' POINT ' , 2 ) ; A. de Lavenne (INRA, Rennes) University of Trento 17 décembre 2012 20 / 25
  • 27. Adding internal spatial information Filling geometry columns −− F i l l g e o m e t r y c o l u m n s UPDATE basins SET outlet_lambert2e=st_setsrid ( st_makepoint ( x_lambert2e , y_lambert2e ) , 2 7 5 7 2 ) ; UPDATE basins SET outlet_lambert93=st_transform ( outlet_lambert2e ,2154) ; UPDATE raingauges SET raingauges_lambert2e=st_transform ( st_setsrid ( st_makepoint ( longitude_dec , latitude_dec ) , 4326) ,27572) ; UPDATE raingauges SET raingauges_lambert93=st_transform ( st_setsrid ( st_makepoint ( longitude_dec , latitude_dec ) , 4326) ,2154) ; UPDATE antilope_points SET antilope_points_lambert2e=st_setsrid ( st_makepoint ( x_lambert2e , y_lambert2e ) , 2 7 5 7 2 ) ; UPDATE antilope_points SET antilope_points_lambert93=st_transform ( st_setsrid ( st_makepoint ( x_lambert2e , y_lambert2e ) , 2 7 5 7 2 ) ,2154) ; A. de Lavenne (INRA, Rennes) University of Trento 17 décembre 2012 21 / 25
  • 28. Adding external spatial information Using graphical interface Using Quantum-GIS extension (like QGIS spit) pgShapeLoader ... Using command line with shp2pgsql shp2pgsql -iIDd -s 2154 -W utf8 basins_lambert93.shp basins_polygon impo psql -U postgres -W -h localhost -p 5432 -d blavet -f import.sql OR shp2pgsql -iIDd -s 2154 -W utf8 basins_lambert93.shp basins_polygon | psql -U postgres -W -h localhost -p 5432 -d blavet Filling geometry columns UPDATE basins SET limits_lambert93=st_geometryn ( geom , 1 ) FROM basins_polygon WHERE basins_polygon . id_basin=basins . id_basin ; and limits_lambert2e ... A. de Lavenne (INRA, Rennes) University of Trento 17 décembre 2012 22 / 25
  • 29. Exemple SQL of query Select a chronique of runo Calculate the area of one basin Select raingauges inside one basin Select average antilope rainfall time serie for each basins Import shapele geology and get a table of statistics of geology for each basin Check if there is double measures in runo and rainfall A. de Lavenne (INRA, Rennes) University of Trento 17 décembre 2012 23 / 25
  • 30. Visualizing your data Using Qgis to vizualize your data Plugin in Qgis for query and shapele creation Plugin in Qgis for importing data A. de Lavenne (INRA, Rennes) University of Trento 17 décembre 2012 24 / 25
  • 31. Connect to your database with R A. de Lavenne (INRA, Rennes) University of Trento 17 décembre 2012 25 / 25
  • 32. Connect to your database with R A. de Lavenne (INRA, Rennes) University of Trento 17 décembre 2012 25 / 25