Skip to content

Commit ded77cf

Browse files
committed
Make pycairo an optional dependency. Closes #12.
1 parent 7fcb5b8 commit ded77cf

15 files changed

+65
-28
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
.env
33
.mypy_cache
44
.pytest_cache
5+
.vscode
56
*.egg-info
67
*.pyc
78
*.png

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,24 @@ A python module to create static map images (PNG, SVG) with markers, geodesic li
2020

2121
## Installation
2222

23+
### SVG only version
24+
2325
```shell
2426
pip install py-staticmaps
2527
```
2628

29+
### SVG + PNG version (via Cairo)
30+
31+
```shell
32+
pip install py-staticmaps[cairo]
33+
```
34+
2735
`py-staticmaps` uses `pycairo` for creating antialiased raster-graphics, so make sure `libcairo2` is installed on your system (on Ubuntu just install the `libcairo2-dev` package, i.e. `sudo apt install libcairo2-dev`).
2836

2937

3038
## Examples
3139

40+
Note: PNG support (e.g. `context.render_cairo(...)`) is only available if the `pycairo` module is installed.
3241

3342
### Markers and Geodesic Lines
3443

@@ -195,4 +204,4 @@ Please take a look at the command line program which uses the `staticmaps` packa
195204

196205
## License
197206

198-
[MIT](LICENSE) © 2020 Florian Pigorsch
207+
[MIT](LICENSE) © 2020-2012 Florian Pigorsch

examples/custom_objects.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
# py-staticmaps
44
# Copyright (c) 2021 Florian Pigorsch; see /LICENSE for licensing information
55

6-
import cairo # type: ignore
6+
try:
7+
import cairo # type: ignore
8+
except ImportError:
9+
pass
710
import s2sphere # type: ignore
811
import staticmaps
912

@@ -125,8 +128,9 @@ def render_cairo(self, renderer: staticmaps.CairoRenderer) -> None:
125128
context.set_tile_provider(staticmaps.tile_provider_CartoDarkNoLabels)
126129

127130
# render png
128-
image = context.render_cairo(800, 500)
129-
image.write_to_png("custom_objects.png")
131+
if staticmaps.cairo_is_supported():
132+
image = context.render_cairo(800, 500)
133+
image.write_to_png("custom_objects.png")
130134

131135
# render svg
132136
svg_image = context.render_svg(800, 500)

examples/draw_gpx.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@
2727
break
2828

2929
# render png
30-
image = context.render_cairo(800, 500)
31-
image.write_to_png("running.png")
30+
if staticmaps.cairo_is_supported():
31+
image = context.render_cairo(800, 500)
32+
image.write_to_png("running.png")
3233

3334
# render svg
3435
svg_image = context.render_svg(800, 500)

examples/frankfurt_newyork.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@
1616
context.add_object(staticmaps.Marker(newyork, color=staticmaps.RED, size=12))
1717

1818
# render png
19-
image = context.render_cairo(800, 500)
20-
image.write_to_png("frankfurt_newyork.png")
19+
if staticmaps.cairo_is_supported():
20+
image = context.render_cairo(800, 500)
21+
image.write_to_png("frankfurt_newyork.png")
2122

2223
# render svg
2324
svg_image = context.render_svg(800, 500)

examples/freiburg_area.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -445,8 +445,9 @@
445445
)
446446

447447
# render png
448-
image = context.render_cairo(800, 500)
449-
image.write_to_png("freiburg_area.png")
448+
if staticmaps.cairo_is_supported():
449+
image = context.render_cairo(800, 500)
450+
image.write_to_png("freiburg_area.png")
450451

451452
# render svg
452453
svg_image = context.render_svg(800, 500)

examples/geodesic_circles.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@
1515
context.add_object(staticmaps.Circle(center2, 2000, fill_color=staticmaps.TRANSPARENT, color=staticmaps.GREEN, width=2))
1616
context.add_object(staticmaps.Marker(center1, color=staticmaps.RED))
1717
context.add_object(staticmaps.Marker(center2, color=staticmaps.GREEN))
18+
1819
# render png
19-
image = context.render_cairo(800, 600)
20-
image.write_to_png("geodesic_circles.png")
20+
if staticmaps.cairo_is_supported():
21+
image = context.render_cairo(800, 600)
22+
image.write_to_png("geodesic_circles.png")
2123

2224
# render svg
2325
svg_image = context.render_svg(800, 600)

examples/tile_providers.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@
2020
context.set_tile_provider(provider)
2121

2222
# render png
23-
image = context.render_cairo(800, 500)
24-
image.write_to_png(f"provider_{name}.png")
23+
if staticmaps.cairo_is_supported():
24+
image = context.render_cairo(800, 500)
25+
image.write_to_png(f"provider_{name}.png")
2526

2627
# render svg
2728
svg_image = context.render_svg(800, 500)

examples/us_capitals.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@
2020
context.add_object(staticmaps.Marker(capital, size=5))
2121

2222
# render png
23-
image = context.render_cairo(800, 500)
24-
image.write_to_png("us_capitals.png")
23+
if staticmaps.cairo_is_supported():
24+
image = context.render_cairo(800, 500)
25+
image.write_to_png("us_capitals.png")
2526

2627
# render svg
2728
svg_image = context.render_svg(800, 500)

requirements-cairo.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pycairo

0 commit comments

Comments
 (0)