Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Mastering PostGIS
Mastering PostGIS

Mastering PostGIS: Modern ways to create, analyze, and implement spatial data

Arrow left icon
Profile Icon George Silva Profile Icon Mikiewicz Profile Icon Michal Mackiewicz Profile Icon Nycz
Arrow right icon
Can$12.99 Can$39.99
Full star icon Empty star icon Empty star icon Empty star icon Empty star icon 1 (1 Ratings)
eBook May 2017 328 pages 1st Edition
eBook
Can$12.99 Can$39.99
Paperback
Can$49.99
Subscription
Free Trial
Arrow left icon
Profile Icon George Silva Profile Icon Mikiewicz Profile Icon Michal Mackiewicz Profile Icon Nycz
Arrow right icon
Can$12.99 Can$39.99
Full star icon Empty star icon Empty star icon Empty star icon Empty star icon 1 (1 Ratings)
eBook May 2017 328 pages 1st Edition
eBook
Can$12.99 Can$39.99
Paperback
Can$49.99
Subscription
Free Trial
eBook
Can$12.99 Can$39.99
Paperback
Can$49.99
Subscription
Free Trial

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Table of content icon View table of contents Preview book icon Preview Book

Mastering PostGIS

Geometry bounding boxes


A bounding box, often abbreviated into BBOX, is a list of the extreme coordinates of a geometry. Bounding boxes play a big role in spatial queries, as they allow for fast coarse computations; If two geometries' BBOXes do not intersect, there's no point in wasting CPU cycles for intersecting them precisely. In PostGIS, bounding boxes are computed and cached internally. There are specialized data types for bounding boxes: box2d, which can be visualized as a rectangle, and box3d, which forms, well, a box.

Accessing bounding boxes

A bounding box of a geometry can be accessed in two ways. The first is to create a Box2D type from a geometry (or a set of them):

SELECT ST_Extent(
ST_GeomFromText('POLYGON((391390 5817855,391490 5817955,391590 5818055, 319590 5817855,391390 5817855))', 32633)
);
             st_extent              
------------------------------------
 BOX(319590 5817855,391590 5818055)

Then to create one for a set of geometries:

SELECT ST_Extent(geom) FROM sometable...

Geometry simplification


In surveying, we strive for the highest accuracy and precision possible. However, this is not always the case in mapmaking. Too-detailed geometries on small scale (zoomed-out) maps are bad for human perception; edges or lines appear jagged, and cause an unnecessary burden on the computer in trying to render them. For that reason, cartographers generalize the geometries used for mapmaking. There are a few algorithms designed for automated generalization. PostGIS provides an implementation of a widely used Douglas-Peucker algorithm in a ST_Simplify function.

The function accepts two arguments, the first being a geometry to be simplified, and the second being a tolerance parameter defining how aggressive the simplification can be. Tolerance is given in the same units as the geometry's coordinate system, and the bigger it is, the more simplified the output geometry becomes.

Here are some examples of simplification:

River meanders simplified with 50 (green) and 100 (red)...

Geometry validation


Invalid geometries are a spatial analyst's nightmare. They can appear in any dataset, and can break a carefully-designed, long-running query in the middle of execution. Or even worse, a failing query might break an application's functionality. Luckily, PostGIS is equipped with the tools to find and repair them.

Simplicity and validity

In PostGIS, there are two concepts: simplicity and validity. For most spatial analyses to succeed, input geometries have to be both simple and valid. Here are some rules:

  • Does not have repeated points (with the exception of closed rings, whose first and last point are identical)
  • Does not self-intersect
  • A point must always be simple and valid
  • A MultiPoint must always be valid, and simple when there are no repeated points with identical coordinates
  • A LineString or MultiLineString must always be valid, and is simple if the line:

Example of a non-simple line: self-intersecting autogenerated contours.

A polygon is always simple, and is valid if:

  • All interior...

Intersecting geometries


In PostGIS, there are two ways in which geometries can be intersected. The first way is checking whether two geometries intersect (at least one point of geometry A lies within the interior of geometry B). That's the ST_Intersects function. It accepts two arguments of the geometry type, and returns a Boolean.

For example, these lines intersect:

SELECT ST_Intersects(
    ST_MakeLine(ST_MakePoint(20,50),ST_MakePoint(21,51)),
    ST_MakeLine(ST_MakePoint(20.5,50.5), ST_MakePoint(22,52))
);

But these lines don't:

 st_intersects
---------------
 t

SELECT ST_Intersects(
    ST_MakeLine(ST_MakePoint(20,50),ST_MakePoint(21,51)),
    ST_MakeLine(ST_MakePoint(21.5,51.5), ST_MakePoint(22,52))
);

 st_intersects
---------------
 f

ST_Intersects can also be used for table joining. The following query will return land features that had at least one earthquake:

SELECT ne_110m_land.* FROM data_import.ne_110m_land JOIN data_import.earthquakes_subset_with_geom ON ST_Intersects(ne_110m_land...

Nearest feature queries


This is a key feature for almost any location-based application: given a current location, it will return the nearest feature (or list of nearest features, ordered by distance).

The naive approach to this problem would be to query the table ordering by ST_Distance. Let's find the five earthquakes closest to San Juan:

SELECT * FROM data_import.earthquakes_subset_with_geom
ORDER BY ST_Distance(geom::geography, ST_SetSRID(ST_MakePoint(-66.11,18.46),4326)::geography)
LIMIT 5;

   id     |           time           | depth | mag | magtype |                       place                       |                        geom                       
------------+--------------------------+-------+-----+---------+---------------------------------------------------+----------------------------------------------------
 pr16281009 | 2016-10-08 01:08:46.4+02 |     5 | 2.5 | Md      | 28km SE of El Negro, Puerto Rico                  | 0101000020E610000062A1D634EF6850C0E561A1D634DF3140...

Summary


Spatial analysis functions are a key feature for spatial databases, and PostGIS has a very rich set of them. One can compose geometries from raw coordinates, compose geometries into more complex shapes, break down complex shapes into elementary geometries, calculate spatial metrics, and query features based on their location. Spatial analysis functions require the geometries to conform to a specification, so validation and automated repair functions are also provided.

Most spatial analysis functions are easy to understand, but special attention must be paid to coordinate systems. When using latitude-longitude coordinate systems, it's best to use the geography data type to ensure the calculated measurements are correct.

Intersecting geometries

In PostGIS, there are two ways in which geometries can be intersected. The first way is checking whether two geometries intersect (at least one point of geometry A lies within the interior of geometry B). That's the ST_Intersects function. It accepts two arguments of the geometry type, and returns a Boolean.

For example, these lines intersect:

  SELECT ST_Intersects(
ST_MakeLine(ST_MakePoint(20,50),ST_MakePoint(21,51)),
ST_MakeLine(ST_MakePoint(20.5,50.5), ST_MakePoint(22,52))
);

But these lines don't:

     st_intersects
---------------
t

SELECT ST_Intersects(
ST_MakeLine(ST_MakePoint(20,50),ST_MakePoint(21,51)),
ST_MakeLine(ST_MakePoint(21.5,51.5), ST_MakePoint(22,52))
);

st_intersects
---------------
f

ST_Intersects can also be used for table joining. The following query will return land features that had at least one earthquake:

SELECT ne_110m_land.* FROM data_import...

Nearest feature queries

This is a key feature for almost any location-based application: given a current location, it will return the nearest feature (or list of nearest features, ordered by distance).

The naive approach to this problem would be to query the table ordering by ST_Distance. Let's find the five earthquakes closest to San Juan:

    SELECT * FROM data_import.earthquakes_subset_with_geom
ORDER BY ST_Distance(geom::geography, ST_SetSRID(ST_MakePoint(-66.11,18.46),4326)::geography)
LIMIT 5;

id | time | depth | mag | magtype | place | geom
------------+--------------------------+-------+-----+---------+---------------------------------------------------+----------------------------------------------------
pr16281009 | 2016-10-08 01:08:46.4+02 | 5 | 2.5 | Md | 28km SE of El Negro...

Summary

Spatial analysis functions are a key feature for spatial databases, and PostGIS has a very rich set of them. One can compose geometries from raw coordinates, compose geometries into more complex shapes, break down complex shapes into elementary geometries, calculate spatial metrics, and query features based on their location. Spatial analysis functions require the geometries to conform to a specification, so validation and automated repair functions are also provided.

Most spatial analysis functions are easy to understand, but special attention must be paid to coordinate systems. When using latitude-longitude coordinate systems, it's best to use the geography data type to ensure the calculated measurements are correct.

Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • Learn how you can use PostGIS for spatial data analysis and manipulation
  • Optimize your queries and build custom functionalities for your GIS application
  • A comprehensive guide with hands-on examples to help you master PostGIS with ease

Description

PostGIS is open source extension onf PostgreSQL object-relational database system that allows GIS objects to be stored and allows querying for information and location services. The aim of this book is to help you master the functionalities offered by PostGIS- from data creation, analysis and output, to ETL and live edits. The book begins with an overview of the key concepts related to spatial database systems and how it applies to Spatial RMDS. You will learn to load different formats into your Postgres instance, investigate the spatial nature of your raster data, and finally export it using built-in functionalities or 3th party tools for backup or representational purposes. Through the course of this book, you will be presented with many examples on how to interact with the database using JavaScript and Node.js. Sample web-based applications interacting with backend PostGIS will also be presented throughout the book, so you can get comfortable with the modern ways of consuming and modifying your spatial data.

Who is this book for?

If you are a GIS developer or analyst who wants to master PostGIS to build efficient, scalable GIS applications, this book is for you. If you want to conduct advanced analysis of spatial data, this book will also help you. The book assumes that you have a working installation of PostGIS in place, and have working experience with PostgreSQL.

What you will learn

  • •Refresh your knowledge of the PostGIS concepts and spatial databases
  • •Solve spatial problems with the use of SQL in real-world scenarios
  • •Practical walkthroughs of application development examples using Postgis, GeoServer and OpenLayers.
  • •Extract, transform and load your spatial data
  • •Expose data directly or through web services.
  • •Consume your data in both desktop and web clients

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : May 31, 2017
Length: 328 pages
Edition : 1st
Language : English
ISBN-13 : 9781784395445
Category :
Tools :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Product Details

Publication date : May 31, 2017
Length: 328 pages
Edition : 1st
Language : English
ISBN-13 : 9781784395445
Category :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
$19.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
$199.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just Can$6 each
Feature tick icon Exclusive print discounts
$279.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just Can$6 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total Can$ 189.97
Mastering PostGIS
Can$49.99
Practical GIS
Can$69.99
PostGIS Cookbook
Can$69.99
Total Can$ 189.97 Stars icon
Banner background image

Table of Contents

9 Chapters
Importing Spatial Data Chevron down icon Chevron up icon
Spatial Data Analysis Chevron down icon Chevron up icon
Data Processing - Vector Ops Chevron down icon Chevron up icon
Data Processing - Raster Ops Chevron down icon Chevron up icon
Exporting Spatial Data Chevron down icon Chevron up icon
ETL Using Node.js Chevron down icon Chevron up icon
PostGIS – Creating Simple WebGIS Applications Chevron down icon Chevron up icon
PostGIS Topology Chevron down icon Chevron up icon
pgRouting Chevron down icon Chevron up icon

Customer reviews

Rating distribution
Full star icon Empty star icon Empty star icon Empty star icon Empty star icon 1
(1 Ratings)
5 star 0%
4 star 0%
3 star 0%
2 star 0%
1 star 100%
schwarzwald Oct 19, 2017
Full star icon Empty star icon Empty star icon Empty star icon Empty star icon 1
A disorganized cookbook that assumes you already know most of the material in the book. Possibly useful for people who already know PostGIS well, in which case you could probably just Google everything in here.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.