Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Introduction to post_gis

954 views

Published on

This is the presentation given by Alban de Lavenne, here in Trento on Dec, 17, 2012 about Posgress use for Hydrological Purposes.

Published in: Education
  • Be the first to comment

  • Be the first to like this

Introduction to post_gis

  1. 1. Intro to PostGis For beginnersAlban de Lavenne17 décembre 2012
  2. 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 runA. de Lavenne (INRA, Rennes) University of Trento 17 décembre 2012 2 / 25
  3. 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 runA. de Lavenne (INRA, Rennes) University of Trento 17 décembre 2012 2 / 25
  4. 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 runA. de Lavenne (INRA, Rennes) University of Trento 17 décembre 2012 2 / 25
  5. 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 runA. de Lavenne (INRA, Rennes) University of Trento 17 décembre 2012 2 / 25
  6. 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 runA. de Lavenne (INRA, Rennes) University of Trento 17 décembre 2012 2 / 25
  7. 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 runA. de Lavenne (INRA, Rennes) University of Trento 17 décembre 2012 2 / 25
  8. 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 runA. de Lavenne (INRA, Rennes) University of Trento 17 décembre 2012 2 / 25
  9. 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 compliantIn a nutshell it adds spatial functions such as : distance, area, union, intersection specialty geometry data types to the databaseA. de Lavenne (INRA, Rennes) University of Trento 17 décembre 2012 3 / 25
  10. 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/Windowshttp://www.postgresql.org/download/windowsMachttp://www.kyngchaos.com/software:postgresUbuntusudo apt-get install postgresql postgispsql -d template1 -c alter user postgres with password postgresA. de Lavenne (INRA, Rennes) University of Trento 17 décembre 2012 4 / 25
  11. 11. Creating a databaseUsing pgAdminIII interfaceRight clic on databaseor using SQL queryCREATE 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. 12. Adding spatial extensionUsing pgAdminIII interfaceRight clic on extensionsor using SQL queryCREATE EXTENSION postgis VERSION 2.0.1 Check the table spatial_ref_sysCheck also that you have lots of new functionsA. de Lavenne (INRA, Rennes) University of Trento 17 décembre 2012 6 / 25
  13. 13. Organising a databaseHow 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. 14. Adding tablesTable of basinsCREATE 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. 15. Adding tablesTable of measures of runoCREATE 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. 16. Adding tablesTable of raingaugesCREATE 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. 17. Adding tablesTable of measures of rainfallCREATE 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. 18. Adding tablesTable of antilope pointsCREATE 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. 19. Adding tablesTable of measures of antilope pointsCREATE 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. 20. Filling tablesImport les to databaseCOPY 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. 21. Adding internal spatial informationAddGeometryColumnAddGeometryColumn ( 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. 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. 23. Geometry Constructors and Geometry AccessorsST_MakePointST_MakePoint ( x , y , z ) Full list here : Geometry ConstructorsST_X, ST_Y and ST_ZST_X ( geometry )ST_Y ( geometry )ST_Z ( geometry ) Full list here : Geometry AccessorsA. de Lavenne (INRA, Rennes) University of Trento 17 décembre 2012 17 / 25
  24. 24. Geometry EditorsST_SetSRIDST_SetSRID ( geom , srid )ST_TransformST_Transform ( geom , srid ) Full list here : Geometry EditorsA 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. 25. Spatial Relationships and MeasurementsSpatial Relationships and MeasurementsST_Area ( geometry )ST_Centroid ( geometry )ST_Contains ( geometry , geometry )ST_Distance ( geometry , geometry ) Full list here : Spatial Relationships and MeasurementsGeometry ProcessingST_Buffer ( geometry , radius_of_buffer )ST_Union ( geometry set g1field )ST_Union ( geometry , geometry )ST_Intersection ( geometry , geometry ) Full list here : Geometry ProcessingA. de Lavenne (INRA, Rennes) University of Trento 17 décembre 2012 19 / 25
  26. 26. Adding internal spatial informationAdding geometry columns−−A d d i n g g e o m e t r y c o l u m n sSELECT 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. 27. Adding internal spatial informationFilling geometry columns−− F i l l g e o m e t r y c o l u m n sUPDATE 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. 28. Adding external spatial informationUsing graphical interfaceUsing Quantum-GIS extension (like QGIS spit)pgShapeLoader...Using command line with shp2pgsqlshp2pgsql -iIDd -s 2154 -W utf8 basins_lambert93.shp basins_polygon impopsql -U postgres -W -h localhost -p 5432 -d blavet -f import.sql ORshp2pgsql -iIDd -s 2154 -W utf8 basins_lambert93.shp basins_polygon |psql -U postgres -W -h localhost -p 5432 -d blavetFilling geometry columnsUPDATE basins SET limits_lambert93=st_geometryn ( geom , 1 )FROM basins_polygonWHERE 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. 29. Exemple SQL of querySelect a chronique of runoCalculate the area of one basinSelect raingauges inside one basinSelect average antilope rainfall time serie for each basinsImport shapele geology and get a table of statistics of geology for each basinCheck if there is double measures in runo and rainfallA. de Lavenne (INRA, Rennes) University of Trento 17 décembre 2012 23 / 25
  30. 30. Visualizing your dataUsing Qgis to vizualize your dataPlugin in Qgis for query and shapele creationPlugin in Qgis for importing dataA. de Lavenne (INRA, Rennes) University of Trento 17 décembre 2012 24 / 25
  31. 31. Connect to your database with RA. de Lavenne (INRA, Rennes) University of Trento 17 décembre 2012 25 / 25
  32. 32. Connect to your database with RA. de Lavenne (INRA, Rennes) University of Trento 17 décembre 2012 25 / 25

×