Playing with GeoJSON polygons

Playing with GeoJSON polygons

This example shows GeoJSON using the polygon type.

Description

The GeoJSON input documents are GeoJSON containing geographic regions of different kind. The Bermuda Triangle, the Flemish Diamond and the Research Triangle in North Carolina.

{
  "type": "Feature",
  "properties": {
    "name": "Bermuda Triangle",
    "area": 1150180
  },
  "geometry": {
    "type": "Polygon",
    "coordinates": [
      [
        [-64.73, 32.31],
        [-80.19, 25.76],
        [-66.09, 18.43],
        [-64.73, 32.31]
      ]
    ]
  }
}


{
  "type": "Feature",
  "properties": {
    "name": "Flemish Diamond",
    "area": 2947
  },
  "geometry": {
    "type": "Polygon",
    "coordinates": [
      [
        [3.55, 51.08],
        [4.36, 50.73],
        [4.84, 50.85],
        [4.45, 51.30],
        [3.55, 51.08]
      ]
    ]
  }
}


{
  "type": "Feature",
  "properties": {
    "name": "Research Triangle",
    "area": 252
  },
  "geometry": {
    "type": "Polygon",
    "coordinates": [
      [
        [-78.93, 36.00],
        [-78.67, 35.78],
        [-79.04, 35.90],
        [-78.93, 36.00]
      ]
    ]
  }
}   

Creating a Spatial View Function

A spatial view function is created to create a multidimensional view file for GeoJSON polygons.

To query based on the location and the area, create a multidimensional view function with three dimensions. Based on the GeoJSON polygon data information, the first two dimensions are the location and the third dimension is the area.

In the following example, a development view is created with the design document name of _design/areas and a spatial view named locAndArea.

function(doc, meta) {
        if (doc.geometry && doc.properties
        && doc.properties.area && doc.properties.name) {
        emit([doc.geometry, doc.properties.area], doc.properties.name);
        }
        }

Querying All Data

This query retrieves all data based on the GeoJSON polygons example.

Description

The results are either viewable in the web console or are returned when the spatial view is queried without any additional parameters. In both cases, all documents are retrieved.

Note: There isn't a specific ordering, expect it to be arbitrary. Once the view file is updated, the order might change.

HTTP Method and URI

GET /_design/[design-doc]/_spatial/[spatial-name]

Syntax

Curl syntax:

curl http://[localhost]:8092/[bucket-name]/_design/[design-doc]/_spatial/[spatial-name]

Example

The following example is based on the GeoJSON polygon data and the associated spatial view function.

Curl example:

curl http://10.5.2.54:8092/default/_design/areas/_spatial/locAndArea

Response

{"total_rows":0,"rows":[
        {"id": "bermuda_triangle",
        "key": [[-80.19,-64.73],[18.43,32.31],[1150180,1150180]],
        "value": "Bermuda Triangle",
        "geometry": {"type":"Polygon","coordinates":[[[-64.73,32.31],[-80.19,25.76],[-66.09,18.43],[-64.73,32.31]]]}},
        {"id": "research_triangle",
        "key": [[-79.04000000000001,-78.67],[35.78,36],[252,252]],
        "value": "Research Triangle",
        "geometry": {"type":"Polygon","coordinates":[[[-78.93000000000001,36],[-78.67,35.78],[-79.04000000000001,35.9],[-78.93000000000001,36]]]}},
        {"id": "flemish_diamond",
        "key":[[3.55,4.84],[50.73,51.3],[2947,2947]],
        "value":"Flemish Diamond",
        "geometry": {"type":"Polygon","coordinates":[[[3.55,51.08],[4.36,50.73],[4.84,50.85],[4.45,51.3],[3.55,51.08]]]}}}]}

The individual rows contain the total_rows (which is always 0), document ID, the key that got stored, the emitted value, and geometry. The key gets automatically calculated and is the enclosing bounding box of the emitted geometry. When refining the query, the key is always used for comparison.

Querying on the Area

This query retrieves data based on the GeoJSON polygon example and associated with a large square footage.

Description

In this example, all documents queried are associated with any really large area. For example, the criteria could be any areas bigger than 10,000 square kilometers without caring about a specific location or query for areas bigger than 10,000 square kilometers without caring where they are.

In this case, the existing view can be queried with wildcards on the location (the first two dimensions) and an open range for the area.

HTTP method and URI

GET [bucket-name]/_design/[design-doc]/_spatial/[spatial-name]

Syntax

Curl syntax:

curl http://[localhost]:8092/[bucket-name]/_design/[design-doc]/_spatial/[spatial-name]?start_range=[]&end_range=[]

Example

The following example is based on the GeoJSON polygon data and the associated spatial view function.

Curl example:

curl http://localhost:8092/default/_design/areas/_spatial/locAndArea?start_range=[null,null,10000]&end_range=[null,null,null]

Alternatively, the query could have used start_range=[-180,-90,10000]&end_range=[180,90,null] because the longitudes and latitudes have those bounds.

Response

The results contain only the Bermuda Triangle:

{"total_rows":0,"rows":[
        {"id": "bermuda_triangle",
        "key": [[-80.19,-64.73],[18.43,32.31],[1150180,1150180]],
        "value": "Bermuda Triangle",
        "geometry": {"type":"Polygon","coordinates":[[[-64.73,32.31],[-80.19,25.76],[-66.09,18.43],[-64.73,32.31]]]}}]}

Querying on the East

This query retrieves data based on the GeoJSON polygons example and associated with the eastern hemisphere.

Description

In this example, all documents that are on the eastern hemisphere are queried. In addition, since the area is also emitted as third dimension, queries need to contain that as well. As we don't care what the area is, we'll query it with the wildcard, null. So the final query is:

This means the following coordinates are specified:

  • longitude (first dimension) greater than 0 and smaller than 180
  • latitude (second dimension) between -90 and 90.
  • null (third dimension) as a wildcard since, in this case, the third dimension doesn't matter.

The longitude and latitude are represented by the query parameters are start_range=[0,-90]&end_range=[180,90]. If just those two query parameters are specified, an error message displays indicating that the dimensionality doesn't match. That's why the null wildcard is used to represent the third dimension.

HTTP method and URI

GET [bucket-name]/_design/[design-doc]/_spatial/[spatial-name]

Syntax

Curl syntax:

curl http://[localhost]:8092/[bucket-name]/_design/[design-doc]/_spatial/[spatial-name]?start_range=[]&end_range=[]

Example

The following example is based on the GeoJSON polygon data and the associated spatial view function.

Curl example:

curl http://10.5.2.54:8092/default/_design/areas/_spatial/locAndArea?start_range=[0,-90,null]&end_range=[180,90,null]

Response

The results contain only the Flemish Diamond:

{"total_rows":0,"rows":[
        {"id": "flemish_diamond",
        "key":[[3.55,4.84],[50.73,51.3],[2947,2947]],
        "value":"Flemish Diamond",
        "geometry":{"type":"Polygon","coordinates":[[[3.55,51.08],[4.36,50.73],[4.84,50.85],[4.45,51.3],[3.55,51.08]]]}}]}

Querying on Non-intersect

This query retrieves data based on the GeoJSON polygon example and on bounding box parameters that are within the query range.

Description

Queries only filter on a bounding box level. This means that even if the actual geometry doesn't intersect the query ranges but its bounding box does, it is still included in the result.

HTTP method and URI

GET [bucket-name]/_design/[design-doc]/_spatial/[spatial-name]

Syntax

Curl syntax:

curl http://[localhost]:8092/[bucket-name]/_design/[design-doc]/_spatial/[spatial-name]?start_range=[]&end_range=[]

Example

The following example is based on the GeoJSON polygon data and the associated spatial view function.

Curl example:

curl http://10.5.2.54:8092/default/_design/areas/_spatial/locAndArea?start_range=[-84,29,null]&end_range=[-72,37,null]

Response

The response shows that the ranges don't intersect the polygon of the Bermuda Triangle, but its enclosing bounding box does which can be found in its key.

{"total_rows":0,"rows":[
        {"id": "bermuda_triangle",
        "key": [[-80.19,-64.73],[18.43,32.31],[1150180,1150180]],
        "value": "Bermuda Triangle",
        "geometry": {"type":"Polygon","coordinates":[[[-64.73,32.31],[-80.19,25.76],[-66.09,18.43],[-64.73,32.31]]]}},
        {"id": "research_triangle",
        "key": [[-79.04000000000001,-78.67],[35.78,36],[252,252]],
        "value": "Research Triangle",
        "geometry": {"type":"Polygon","coordinates":[[[-78.93000000000001,36],[-78.67,35.78],[-79.04000000000001,35.9],[-78.93000000000001,36]]]}}]}