Skip to content

Commit

Permalink
Merge pull request #17 from pblottiere/raster_layer_rendering
Browse files Browse the repository at this point in the history
Add basic support for raster style
  • Loading branch information
pblottiere authored May 6, 2024
2 parents 2619a2b + c6fec48 commit 7934c0c
Show file tree
Hide file tree
Showing 13 changed files with 611 additions and 71 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ docs/build
sandbox/projects
*.aux.xml
map.png
map_vector.png
map_raster.png
1 change: 1 addition & 0 deletions docs/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@
- [Vector layers](sandbox/vector/layers.md)
- [Vector styles](sandbox/vector/styles.md)
- [Raster layers](sandbox/raster/layers.md)
- [Raster styles](sandbox/raster/styles.md)
- [Developers](DEVELOPERS.md)
- [Funders](FUNDERS.md)
Binary file added docs/src/images/map_raster_style.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
107 changes: 94 additions & 13 deletions docs/src/qsa-api/endpoints.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,17 @@ in QGIS is very dense but for now, only Marker, Line and Fill simple symbols
are supported. The `/api/symbology` endpoint allows to dynamically retrieve the
corresponding parameters depending on QGIS Server version.

| Method | URL | Description |
|---------|---------------------------------------------------------------|----------------------------------|
| GET | `/api/symbology/vector/point/single_symbol/marker/properties` | Marker simple symbol properties |
| GET | `/api/symbology/vector/line/single_symbol/line/properties` | Line simple symbol properties |
| GET | `/api/symbology/vector/polygon/single_symbol/fill/properties` | Polygon simple symbol properties |
| Method | URL | Description |
|---------|---------------------------------------------------------------|----------------------------------------------|
| GET | `/api/symbology/vector/point/single_symbol/marker/properties` | Marker simple symbol properties |
| GET | `/api/symbology/vector/line/single_symbol/line/properties` | Line simple symbol properties |
| GET | `/api/symbology/vector/polygon/single_symbol/fill/properties` | Polygon simple symbol properties |
| GET | `/api/symbology/vector/rendering/properties` | Vector layer rendering properties |
| GET | `/api/symbology/raster/singlebandgray/properties` | Single band gray properties |
| GET | `/api/symbology/raster/multibandcolor/properties` | Multi band color properties |
| GET | `/api/symbology/raster/rendering/properties` | Raster layer rendering properties |

Example:
Examples:

```` shell
# Return single symbol properties for polygon layers
Expand All @@ -112,24 +116,49 @@ $ curl "http://localhost:5000/api/symbology/vector/polygon/single_symbol/fill/pr
"outline_width_unit": "MM",
"style": "solid"
}

# Return multi band gray properties for raster layers
$ curl "http://localhost:5000/api/symbology/raster/multibandcolor/properties" | jq
{
"blue": {
"band": 3
},
"green": {
"band": 2
},
"red": {
"band": 1
}
"contrast_enhancement": "StretchToMinimumMaximum (StretchToMinimumMaximum, NoEnhancement, StretchAndClipToMinimumMaximum, ClipToMinimumMaximum)"
}
````


## Style

A QSA style may be used through the `STYLE` OGC web services parameter to
specify the rendering for a specific layer. Default styles may be defined and
automatically used when a layer is added to a QSA project.
automatically used when a vector layer is added to a QSA project.

| Method | URL | Description |
|---------|-----------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------|
| GET | `/api/projects/{project}/styles` | List styles in project |
| GET | `/api/projects/{project}/styles/default` | List default styles in project |
| GET | `/api/projects/{project}/styles/{style}` | List style's metadata |
| POST | `/api/projects/{project}/styles/{style}` | Add style to project with `symbology` (only `single_symbol` is supported for now), `symbol`, `name` and symbology `properties` |
| POST | `/api/projects/{project}/styles/default` | Set default style for a specific geometry with `geometry` and `name` |
| POST | `/api/projects/{project}/styles/{style}` | Add style to project. See [Vector style](#vector-style) and [Raster style](#raster-style) for more information. |
| POST | `/api/projects/{project}/styles/default` | Set a default layer's style. See [Vector style](#vector-style) and [Raster style](#raster-style) for more information. |
| DELETE | `/api/projects/{project}/styles/{style}` | Remove style from project |


#### Vector style {#vector-style}

For vector layers, a style can be defined with the parameters listed below:
- `type` : `vector`
- `name` : the name of the style
- `rendering` : rendering parameters (only `opacity` is supported for now)
- `symbology` : dictionary with `type` (only `single_symbol` is supported for
now), `symbol` and `properties`

Example:

```` shell
Expand All @@ -138,15 +167,67 @@ $ curl "http://localhost:5000/api/projects/my_project/styles" \
-X POST \
-H 'Content-Type: application/json' \
-d '{
"symbol": "marker",
"symbology": "single_symbol",
"type": "vector",
"name": "my_marker_style",
"properties": {
"color": "#112233"
"rendering": {
"opacity": 100.
},
"symbology": {
"type": "single_symbol",
"symbol": "marker",
"properties": {
"color": "#112233"
}
}
}'
````

To set a default style for a specific geometry, the parameters listed below are available:
- `name` : the name of the style to use by default
- `geometry` : the geometry for which the style needs to be applied


#### Raster style {#raster-style}

For raster layers, a style can be defined with the parameters listed below:
- `type` : `raster`
- `name` : the name of the style
- `rendering` : rendering parameters
- `symbology` : dictionary with `type` (only `singlebandgray` and
`multibandcolor` are supported for now) and `properties`

Example:

```` shell
# Add a style for point multiband raster
$ curl "http://localhost:5000/api/projects/my_project/styles" \
-X POST \
-H 'Content-Type: application/json' \
-d '{
"type": "raster",
"name": "my_multiband_style",
"rendering": {
"saturation": 3,
"brightness": -148,
"contrast": 42,
"gamma": 4.
},
"symbology": {
"type": "multibandcolor",
"properties": {
"red": {
"band": 1
},
"green": {
"band": 2
},
"blue": {
"band": 3
}
}
}
}'
````

## Instances

Expand Down
59 changes: 59 additions & 0 deletions docs/src/sandbox/raster/styles.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Sandbox : raster styles


### Add style to project

To add a style for a multiband raster layer:

```` shell
$ curl "http://localhost:5000/api/projects/my_project/styles?schema=my_schema" \
-X POST \
-H 'Content-Type: application/json' \
-d '{
"type": "raster",
"name": "my_singlebandgray_style",
"symbology": {
"type": "singlebandgray",
"properties": {
"gray_band": 1
}
},
"rendering": {
"brightness": 255,
"gamma": 0.1,
"contrast": 100
}
}'
true
````

To list styles for a specific project:

```` shell
$ curl "http://localhost:5000/api/projects/my_project/styles?schema=my_schema"
["my_singlebandgray_style"]
````


### Apply style to layer

To apply a specific style to a layer:

```` shell
$ curl "http://localhost:5000/api/projects/my_project/layers/dem/style?schema=my_schema" \
-X POST \
-H 'Content-Type: application/json' \
-d '{
"name":"my_singlebandgray_style",
"current":true
}'
true
````

The layer rendering has changed now:

```` shell
$ curl "http://localhost:5000/api/projects/my_project/layers/dem/map?schema=my_schema" --output map.png
````

<img src="../../images/map_raster_style.png" width="300">
20 changes: 12 additions & 8 deletions docs/src/sandbox/vector/styles.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,19 @@ $ curl "http://localhost:5000/api/projects/my_project/styles?schema=my_schema" \
-X POST \
-H 'Content-Type: application/json' \
-d '{
"symbol": "fill",
"symbology": "single_symbol",
"type": "vector",
"name": "my_fill_style",
"properties": {
"color": "#00BBBB",
"style": "cross",
"outline_width": 0.16,
"outline_color": "#002222"
}
"symbology": {
"type": "single_symbol",
"symbol": "fill",
"properties": {
"color": "#00BBBB",
"style": "cross",
"outline_width": 0.16,
"outline_color": "#002222"
}
},
"rendering": {}
}'
true
````
Expand Down
28 changes: 14 additions & 14 deletions qsa-api/qsa_api/api/projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,9 @@ def project_del_style(name, style):
project = QSAProject(name, psql_schema)
if project.exists():
if style in project.styles:
rc = project.remove_style(style)
rc, msg = project.remove_style(style)
if not rc:
return {"error": msg}, 415
return jsonify(rc), 201
else:
return {"error": "Style does not exist"}, 415
Expand Down Expand Up @@ -176,12 +178,12 @@ def project_layer_map(name, layer_name):
def project_add_style(name):
schema = {
"type": "object",
"required": ["name", "symbol", "symbology", "properties"],
"required": ["name", "type", "rendering", "symbology"],
"properties": {
"name": {"type": "string"},
"symbol": {"type": "string"},
"symbology": {"type": "string"},
"properties": {"type": "object"},
"type": {"type": "string"},
"symbology": {"type": "object"},
"rendering": {"type": "object"},
},
}

Expand All @@ -194,18 +196,16 @@ def project_add_style(name):
except ValidationError as e:
return {"error": e.message}, 415

# legacy support
symbology = data["symbology"]
if symbology == "single symbol":
symbology = "single_symbol"

rc = project.add_style(
rc, err = project.add_style(
data["name"],
data["symbol"],
data["type"],
data["symbology"],
data["properties"],
data["rendering"],
)
return jsonify(rc), 201
if rc:
return jsonify(rc), 201
else:
return {"error": err}, 415
else:
return {"error": "Project does not exist"}, 415

Expand Down
50 changes: 47 additions & 3 deletions qsa-api/qsa_api/api/symbology.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@

from flask import Blueprint, jsonify

from qgis.core import QgsSimpleFillSymbolLayer
from qgis.core import QgsSimpleLineSymbolLayer
from qgis.core import QgsSimpleMarkerSymbolLayer
from qgis.core import (
QgsSimpleLineSymbolLayer,
QgsSimpleFillSymbolLayer,
QgsSingleBandGrayRenderer,
QgsMultiBandColorRenderer,
QgsSimpleMarkerSymbolLayer,
)


symbology = Blueprint("symbology", __name__)
Expand Down Expand Up @@ -32,3 +36,43 @@ def symbology_symbols_marker():
"outline_style"
] = "solid (no, solid, dash, dot, dash dot, dash dot dot)"
return jsonify(props)


@symbology.get("/vector/rendering/properties")
def symbology_vector_rendering():
props = {}
props["opacity"] = 100.0
return jsonify(props)


@symbology.get(
f"/raster/{QgsSingleBandGrayRenderer(None, 1).type()}/properties"
)
def symbology_raster_singlebandgray():
props = {}
props["gray_band"] = 1
props["contrast_enhancement"] = "NoEnhancement (StretchToMinimumMaximum, NoEnhancement, StretchAndClipToMinimumMaximum, ClipToMinimumMaximum)"
props["color_gradient"] = "BlackToWhite (BlackToWhite, WhiteToBlack)"
return jsonify(props)


@symbology.get(
f"/raster/{QgsMultiBandColorRenderer(None, 1, 1, 1).type()}/properties"
)
def symbology_raster_multibandcolor():
props = {}
props["red"] = {"band": 1}
props["green"] = {"band": 2}
props["blue"] = {"band": 3}
props["contrast_enhancement"] = "StretchToMinimumMaximum (StretchToMinimumMaximum, NoEnhancement, StretchAndClipToMinimumMaximum, ClipToMinimumMaximum)"
return jsonify(props)


@symbology.get("/raster/rendering/properties")
def symbology_raster_rendering():
props = {}
props["gamma"] = 1.0
props["brightness"] = 0
props["contrast"] = 0
props["saturation"] = 0
return jsonify(props)
Binary file added qsa-api/qsa_api/empty.tif
Binary file not shown.
Loading

0 comments on commit 7934c0c

Please sign in to comment.