Skip to content

Commit

Permalink
Include more examples (#41)
Browse files Browse the repository at this point in the history
* Add new Examples sections
* Updated Crunchy Hugo theme
* Flesh out examples, some light edits
* Added Spatial logo and edited screenshots
  • Loading branch information
kbatuigas authored Apr 8, 2020
1 parent 0d0bacc commit ba0399b
Show file tree
Hide file tree
Showing 17 changed files with 349 additions and 33 deletions.
4 changes: 3 additions & 1 deletion hugo/content/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ date:
draft: false
---

![Crunchy Spatial](/crunchy-spatial-logo.png)

# pg_featureserv

This is the documentation for `pg_featureserv` version 1.1.
Expand All @@ -22,4 +24,4 @@ to spatial data enables richer functionality. Use cases include:
* Download spatial data for use in applications

This guide walks you through how to install and use `pg_featureserv` for your spatial applications. See [Quick Start](/quickstart/) to learn how to get the service up and running with a spatial database. The [Usage](/usage/) section goes in-depth on how the service works.
We'll soon be adding more [basic examples](/examples/) of web map applications that source feature data from `pg_featureserv`.
We're continuing to add [basic examples](/examples/) of working with feature data from `pg_featureserv`.
4 changes: 2 additions & 2 deletions hugo/content/examples/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ draft: false
weight: 40
---

We're currently working on adding examples of using `pg_featureserv` in this guide.
The examples in this section help further illustrate how `pg_featureserv` is used.

In the meantime, we'd encourage you to check this [Github repository](https://github.com/pramsey/examples-pgsql-full-text) for a heat map demo and an address autocomplete demo.
We encourage you to check this [Github repository](https://github.com/pramsey/examples-pgsql-full-text) for a heat map demo and an address autocomplete demo, including sample source code so you can run the demos in a browser.
34 changes: 17 additions & 17 deletions hugo/content/examples/ex_query_data.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ created in the [Quick Start](/quickstart/) section.
For more information about querying feature collections,
see the [Usage](/usage/) section.

## Basic Query
## Basic query

The most basic query against a feature collection is to
retrieve an unfiltered list of the features in a collection.
The number of features returned is limited by the service
configuration for the default feature limit.
The number of features returned is limited by the [service
configuration](/installation/configuration/) for the default feature limit.

The following query returns a partial list of
the countries in the `ne.countries` collection, as a GeoJSON FeatureCollection:
Expand All @@ -33,25 +33,26 @@ which should display a page like this:

![Map view of basic query](/ex-query-data-countries-basic.png)

## Query using a Bounding Box Filter and Limit
## Query using a bounding box filter and limit

The extent and number of features returned can be controlled
by the `bbox` and `limit` query parameters.
To query the countries in the Caribbean and ensure that all of
them are returned this query could be used:
You can control the extent as well as number of features returned with the `bbox` and `limit` query parameters.

For example, to query the countries in the Caribbean and ensure that all of
them are returned, you can use the query parameters like so:
```
http://localhost:9000/collections/ne.countries/items.html
?bbox=-93.0688,9.3746,-54.0296,25.9053&limit=100
```
![Map view of query with bbox and limit](/ex-query-data-countries-bbox-limit.png)

## Query with a Property filter and Properties list
## Query with a property filter and properties list

Another way to limit the features returned is via a property filter query parameter.
For instance, the countries in Europe can be returned via using the query parameter `continent=Europe`.
To make it easy to verify the result, the returned properties have been
restricted using the `properties` query parameter.
And as before a higher `limit` value is used to ensure all features are returned.
For instance, the countries in Europe can be returned using the query parameter `continent=Europe`.

To make it easy to verify the result, the `properties` query parameter has been restricted to only three properties (including `continent` itself).
And as before, a higher `limit` value ensures that all features are returned.

```
http://localhost:9000/collections/ne.countries/items.html
?continent=Europe&properties=gid,name,continent&limit=100
Expand All @@ -60,11 +61,10 @@ http://localhost:9000/collections/ne.countries/items.html

## Query a Feature by ID

A single feature can queried by providing the feature ID
You can also query a single feature by providing the feature ID
as part of the resource path.
Fitering parameters do not apply to feature queries,
but the response properties can be specified
using `properties`.

Most query parameters do not apply to single feature queries. With that said, the `properties` parameter can be used to specify what response properties are included.

```
http://localhost:9001/collections/ne.countries/items/55.html
Expand Down
101 changes: 101 additions & 0 deletions hugo/content/examples/ex_query_functions_countries_name.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
---
title: "Example: Query Countries by Name"
date:
draft: false
weight: 210
---

This is the same spatial function example shown in the [Usage](/usage/functions/) section, but we'll include a sample GeoJSON response, as well as the web UI preview.

## Create a spatial function that returns a filtered set of countries

```sql
CREATE OR REPLACE FUNCTION postgisftw.countries_name(
name_prefix text DEFAULT 'A')
RETURNS TABLE(name text, abbrev text, continent text, geom geometry)
AS $$
BEGIN
RETURN QUERY
SELECT t.name::text,
t.abbrev::text,
t.continent::text,
t.geom
FROM ne.admin_0_countries t
WHERE t.name ILIKE name_prefix || '%';
END;
$$
LANGUAGE 'plpgsql' STABLE PARALLEL SAFE;

COMMENT ON FUNCTION postgisftw.countries_name IS 'Filters the countries table by the initial letters of the name using the "name_prefix" parameter.';
```

## Example of API query

The function can be called via the API by providing a value for the `name_prefix` parameter.

`http://localhost:9000/functions/countries_name/items?name_prefix=Mo`

Since a default value is included in the function declaration, you could omit the parameter in the call -- a random sample of features will be returned.

## Sample GeoJSON response

The response is a GeoJSON document containing the 7 countries starting with the letters 'Mo'.

```json
{
"type":"FeatureCollection",
"features":[
{
"type":"Feature",
"geometry":{
"type":"MultiPolygon",
"coordinates":[
[
[
[
-62.1484375,
16.74033203125
],
[
-62.154248046875,
16.681201171875
],
...
[
-62.1484375,
16.74033203125
]
]
]
]
},
"properties":{
"abbrev":"Monts.",
"continent":"North America",
"name":"Montserrat"
}
},
...
],
"numberReturned":7,
"timeStamp":"2020-03-18T03:15:15Z",
"links":[
{
"href":"http://localhost:9000/collections/countries_name/items.json",
"rel":"self",
"type":"application/json",
"title":"This document as JSON"
},
{
"href":"http://localhost:9000/collections/countries_name/items.html",
"rel":"alternate",
"type":"text/html",
"title":"This document as HTML"
}
]
}
```

## Web preview

![Countries starting with 'Mo'](/ex-query-countries.png)
151 changes: 151 additions & 0 deletions hugo/content/examples/ex_query_functions_geo_grid.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
---
title: "Example: Generate a Geographic Grid"
date:
draft: false
weight: 220
---

This example shows how to generate geometry data from a function, controlled by some input parameters.

This particular function does not query an existing table in the database; rather, it uses PostGIS functions to generate spatial data. Grids generated in this way could be used for data visualization, analysis, or clustering.

## Create a spatial function that generates a grid over a desired area

```sql
CREATE OR REPLACE FUNCTION postgisftw.geo_grid(
num_x integer DEFAULT 10,
num_y integer DEFAULT 10,
lon_min numeric DEFAULT -180.0,
lat_min numeric DEFAULT -90.0,
lon_max numeric DEFAULT 180.0,
lat_max numeric DEFAULT 90.0)
RETURNS TABLE(id text, geom geometry)
AS $$
DECLARE
dlon numeric;
dlat numeric;
BEGIN
dlon := (lon_max - lon_min) / num_x;
dlat := (lat_max - lat_min) / num_y;
RETURN QUERY
SELECT
x.x::text || '_' || y.y::text AS id,
ST_MakeEnvelope(
lon_min + (x.x - 1) * dlon, lat_min + (y.y - 1) * dlat,
lon_min + x.x * dlon, lat_min + y.y * dlat, 4326
) AS geom
FROM generate_series(1, num_x) AS x(x)
CROSS JOIN generate_series(1, num_y) AS y(y);
END;
$$
LANGUAGE 'plpgsql'
STABLE
STRICT;
```

Notes:

* The `geo_grid` function accepts a num_x and a num_y value to define the number of grid cells along the longitudinal (X) and latitudinal (Y) axes respectively. It also takes in minimum and maximum longitude and latitude values for the map area we want covered.
* The function first calculates the lengths of the sides of the grid (dlon and dlat).
* A CROSS JOIN on two generate_series() functions produces X and Y indices for each grid cell.
* The PostGIS function [ST_MakeEnvelope()](https://postgis.net/docs/ST_MakeEnvelope.html) contructs a rectangular polygon for each cell. An `id` value is also returned that encodes the grid index.

## Example of API query

`http://localhost:9000/functions/geo_grid/items?num_x=5&num_y=5&lon_min=-128&lat_min=25&lon_max=-65&lat_max=49&limit=50`

This generates a 5x5 grid over the United States.

The server returns a limited number of features by default, so we add a `limit` parameter in the call to ensure that we get all the grid cells. See _Limiting and Paging_ in [Executing Functions](/usage/query_function/) for more details on the `limit` parameter.

## Sample GeoJSON response

The function returns a feature collection of Polygons.

```json
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"id": "1_1",
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
-128,
25
],
[
-128,
29.8
],
[
-115.4,
29.8
],
[
-115.4,
25
],
[
-128,
25
]
]
]
},
"properties": {
"id": "1_1"
}
},
...
{
"type": "Feature",
"id": "5_5",
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
-77.6,
44.2
],
...
[
-77.6,
44.2
]
]
]
},
"properties": {
"id": "5_5"
}
}
],
"numberReturned": 25,
"timeStamp": "2020-04-05T19:54:17Z",
"links": [
{
"href": "http://localhost:9000/collections/geo_grid/items.json",
"rel": "self",
"type": "application/json",
"title": "This document as JSON"
},
{
"href": "http://localhost:9000/collections/geo_grid/items.html",
"rel": "alternate",
"type": "text/html",
"title": "This document as HTML"
}
]
}
```

Each cell has an `id` value that also indicates where it is on the grid. Since longitude and latitude values increase as you move east and north respectively, the cell with `id` 1_1 is the most southwestern corner of the grid, while cell 1_2 is immediately east and cell 2_1 immediately north.

## Web preview

![Geographic grid over the United States](/ex-query-grid.png)
Loading

0 comments on commit ba0399b

Please sign in to comment.