
PostGIS Cookbook
Description
Alles über E-Books | Antworten auf Fragen rund um E-Books, Kopierschutz und Dateiformate finden Sie in unserem Info- & Hilfebereich.
Book DescriptionAn easy-to-use guide, full of hands-on recipes for manipulating spatial data in a PostGIS database. Each topic is explained and placed in context, and for the more inquisitive, there are more details of the concepts used. If you are a web developer or a software architect, especially in location-based companies, and want to expand the range of techniques you are using with PostGIS, then this book is for you. You should have some prior experience with PostgreSQL database and spatial concepts.What you will learn
Import and export geographic data from the PostGIS database using the available tools
Structure spatial data using the functionality provided by the combination of PostgreSQL and PostGIS
Work with a set of PostGIS functions to perform basic and advanced vector analyses
Connect PostGIS with Python
Learn to use programming frameworks around PostGIS
Maintain, optimize, and finetune spatial data for longterm viability
Explore the 3D capabilities of PostGIS, including LiDAR point clouds and point clouds derived from Structure from Motion (SfM) techniques
Distribute 3D models through the Web using the X3D standard
Use PostGIS to develop powerful GIS web applications using Open Geospatial Consortium web standards
Master PostGIS Raster
Who this book is for
All prices
More details
Other editions
New editions

Additional editions

Persons
Content
Importing nonspatial tabular data (CSV) using PostGIS functions
There are a couple of alternative approaches to import a Comma Separated Values (CSV) file, which stores attributes and geometries in PostGIS. In this recipe, we will use the approach of importing such a file using the PostgreSQL COPY command and a couple of PostGIS functions.
Getting ready
We will import the firenews.csv file that stores a series of web news collected from the various RSS feeds related to forest fires in Europe in the context of the European Forest Fire Information System (EFFIS ), available at http://effis.jrc.ec.europa.eu/.
For each news feed, there are attributes like place name, size of the fire in hectares, URL, and so on. Most importantly, there are the x and y fields that give the position of the geolocalized news in decimal degrees (in the WGS 84 spatial reference system, SRID = 4326).
How to do it...
The steps you need to follow to complete this recipe are as shown:
- Inspect the structure of the CSV file,
firenews.csv, which you can find within the book dataset (if you are on Windows, open the CSV file with an editor such as Notepad).Tip
Downloading the example code
You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.
$ cd ~/postgis_cookbook/data/chp01/ $ head -n 5 firenews.csvThe output of the preceding command is as shown:
x,y,place,size,update,startdate,enddate,title,url-8.2499,42.37657,Avión,52,2011/03/07,2011/03/05,2011/03/06,Dos incendios calcinan 74 hectáreas el fin de semana,http://www.laregion.es/noticia/145578/incendios/calcinan/hectareas/semana/ -8.1013,42.13924,Quintela de Leirado,22,2011/03/07,2011/03/06,2011/03/06,Dos incendios calcinan 74 hectáreas el fin de semana,http://www.laregion.es/noticia/145578/incendios/calcinan/hectareas/semana/ 3.48159,43.99156,Arrigas,4,2011/03/06,2011/03/05,2011/03/05,"À Arrigas, la forêt sous la menace d'un feu",http://www.midilibre.com/articles/2011/03/06/NIMES-A-Arrigas-la-foret-sous-la-menace-d-39-un-feu-1557923.php5 6.1672,44.96038,Vénéon,9,2011/03/06,2011/03/06,2011/03/06,Isère Spectaculaire incendie dans la vallée du Vénéon,http://www.ledauphine.com/isere-sud/2011/03/06/isere-spectaculaire-incendie-dans-la-vallee-du-veneon - Connect to PostgreSQL and create the following table: $ psql -U me -d postgis_cookbook postgis_cookbook=> CREATE TABLE chp01.firenews ( x float8, y float8, place varchar(100), size float8, update date, startdate date, enddate date, title varchar(255), url varchar(255), the_geom geometry(POINT, 4326) );
Note
We are using the
psqlclient for connecting to PostgreSQL, but you can use your favorite one, for example,pgAdmin.Using the
psqlclient, we will not show the host and port options as we will assume that you are using a local PostgreSQL installation on the standard port.If that is not the case, please provide those options!
- Copy the records from the CSV file to the PostgreSQL table using the
COPYcommand (if you are on Windows, use an input directory such asc:\tempinstead of/tmp) as follows: postgis_cookbook=> COPY chp01.firenews (x, y, place, size, update, startdate, enddate, title, url) FROM '/tmp/firenews.csv' WITH CSV HEADER;Tip
Make sure that the
firenews.csvfile is in a location accessible from the PostgreSQL process user. For example, in Linux, copy the file to the/tmpdirectory.If you are on Windows, you most likely will need to set the encoding to
postgis_cookbook=# set client_encoding to 'UTF-8';UTF-8before copying: - Check if all of the records have been imported from the CSV file to the PostgreSQL table: postgis_cookbook=> SELECT COUNT(*) FROM chp01.firenews;
The output of the preceding command is as follows:
count ------- 3006 (1 row) - Check if a record related to this new table is in the PostGIS
geometry_columnsmetadata view: postgis_cookbook=# SELECT f_table_name, f_geometry_column, coord_dimension, srid, type FROM geometry_columns where f_table_name = 'firenews'; f_table_name | f_geometry_column | coord_dimension | srid | type --------------+-------------------+-----------------+-------+------- firenews | the_geom | 2 | 4326 | POINT (1 row)Tip
Before PostGIS 2.0, you had to create a table containing spatial data in two distinct steps; in fact, the
postgis_cookbook=> CREATE TABLE chp01.firenews ( x float8, y float8, place varchar(100), size float8, update date, startdate date, enddate date, title varchar(255), url varchar(255) ) WITHOUT OIDS; postgis_cookbook=> SELECT AddGeometryColumn('chp01', 'firenews', 'the_geom', 4326, 'POINT', 2); chp01.firenews.the_geom SRID:4326 TYPE:POINT DIMS:2geometry_columnsview was a table that needed to be manually updated. For that purpose, you had to use theAddGeometryColumnfunction to create the column. For example, for this recipe:Tip
In PostGIS 2.0, you can still use the
AddGeometryColumnfunction if you wish; however, you need to set itsuse_typmodparameter tofalse. - Now, import the points in the geometric column using the
ST_MakePointorST_PointFromTextfunctions (use one of the following two update commands): postgis_cookbook=> UPDATE chp01.firenews SET the_geom = ST_SetSRID(ST_MakePoint(x,y), 4326); postgis_cookbook=> UPDATE chp01.firenews SET the_geom = ST_PointFromText('POINT(' || x || ' ' || y || ')', 4326); - Check how the geometry field has been updated in some records from the table: postgis_cookbook=# SELECT place, ST_AsText(the_geom) AS wkt_geom FROM chp01.firenews ORDER BY place LIMIT 5;
The output of the preceding comment is as follows:
place | wkt ---------------------------------------------------------- Abbaslık | POINT(29.95... Abeledos, Montederramo | POINT(-7.48... Abreiro | POINT(-7.28... Abrunheira, Montemor-o-Velho | POINT(-8.72... Achaia | POINT(21.89... (5 rows) - Finally, create a spatial index for the geometric column of the table: postgis_cookbook=> CREATE INDEX idx_firenews_geom ON chp01.firenews USING GIST (the_geom);
How it works...
This recipe showed you how to load nonspatial tabular data (in CSV format) in PostGIS using the COPY PostgreSQL command.
After creating the table and copying the CSV file rows to the PostgreSQL table, you updated the geometric column using one of the geometry constructor functions that PostGIS provides (ST_MakePoint and...
System requirements
File format: ePUB
Copy protection: Adobe-DRM (Digital Rights Management)
System requirements:
- Computer (Windows; MacOS X; Linux): Install the free reader Adobe Digital Editions prior to download (see eBook Help).
- Tablet/smartphone (Android; iOS): Install the free app Adobe Digital Editions or the app PocketBook before downloading (see eBook Help).
- E-reader: Bookeen, Kobo, Pocketbook, Sony, Tolino and many more (not Kindle).
The file format ePub works well for novels and non-fiction books – i.e., „flowing” text without complex layout. On an e-reader or smartphone, line and page breaks automatically adjust to fit the small displays.
This eBook uses Adobe-DRM, a „hard” copy protection. If the necessary requirements are not met, unfortunately you will not be able to open the eBook. You will therefore need to prepare your reading hardware before downloading.
Please note: We strongly recommend that you authorise using your personal Adobe ID after installation of any reading software.
For more information, see our ebook Help page.
File format: PDF
Copy-Protection: Adobe-DRM (Digital Rights Management)
System requirements:
- Computer (Windows; MacOS X; Linux): Install the free reader Adobe Digital Editions prior to download (see eBook Help).
- Tablet/smartphone (Android; iOS): Install the free app Adobe Digital Editions or the app PocketBook before downloading (see eBook Help).
- E-reader: Bookeen, Kobo, Pocketbook, Sony, Tolino and many more (only limited: Kindle).
The file format PDF always displays a book page identically on any hardware. This makes PDF suitable for complex layouts such as those used in textbooks and reference books (images, tables, columns, footnotes). Unfortunately, on the small screens of e-readers or smartphones, PDFs are rather annoying, requiring too much scrolling.
This eBook uses Adobe-DRM, a „hard” copy protection. If the necessary requirements are not met, unfortunately you will not be able to open the eBook. You will therefore need to prepare your reading hardware before downloading.
Please note: We strongly recommend that you authorise using your personal Adobe ID after installation of any reading software.
For more information, see our eBook Help page.