staticmap3 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- staticmap3-0.1.0/LICENSE.txt +13 -0
- staticmap3-0.1.0/PKG-INFO +193 -0
- staticmap3-0.1.0/README.md +163 -0
- staticmap3-0.1.0/pyproject.toml +73 -0
- staticmap3-0.1.0/staticmap3/__init__.py +3 -0
- staticmap3-0.1.0/staticmap3/staticmap.py +609 -0
- staticmap3-0.1.0/staticmap3/tests.py +19 -0
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
Copyright 2015 komoot GmbH
|
|
2
|
+
|
|
3
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
you may not use this file except in compliance with the License.
|
|
5
|
+
You may obtain a copy of the License at
|
|
6
|
+
|
|
7
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
|
|
9
|
+
Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
See the License for the specific language governing permissions and
|
|
13
|
+
limitations under the License.
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
Metadata-Version: 2.3
|
|
2
|
+
Name: staticmap3
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Fork of staticmap, a small, python-based library for creating map images with lines and markers.
|
|
5
|
+
License: Apache Software License
|
|
6
|
+
Keywords: static,map,image,osm
|
|
7
|
+
Author: Christoph Lingg
|
|
8
|
+
Author-email: christoph@komoot.de
|
|
9
|
+
Maintainer: SamR1
|
|
10
|
+
Requires-Python: >=3.9
|
|
11
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
12
|
+
Classifier: Operating System :: POSIX :: Linux
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
19
|
+
Provides-Extra: dev
|
|
20
|
+
Provides-Extra: filecache
|
|
21
|
+
Requires-Dist: cachecontrol (>=0.14.3)
|
|
22
|
+
Requires-Dist: filelock (>=3.8.0) ; extra == "filecache"
|
|
23
|
+
Requires-Dist: mypy (>=1.15.0) ; extra == "dev"
|
|
24
|
+
Requires-Dist: pillow (>=11.2.1)
|
|
25
|
+
Requires-Dist: requests (>=2.32.3)
|
|
26
|
+
Requires-Dist: ruff (>=0.11.10) ; extra == "dev"
|
|
27
|
+
Requires-Dist: types-requests ; extra == "dev"
|
|
28
|
+
Description-Content-Type: text/markdown
|
|
29
|
+
|
|
30
|
+
# Static Map 3
|
|
31
|
+
|
|
32
|
+
Static Map 3 is a fork of [Static Map](https://github.com/komoot/staticmap), a small, python-based library for creating map images with lines and markers.\
|
|
33
|
+
The main purpose of this fork is to fix error handling on tile requests, to allow interrupting the execution (see [PR](https://github.com/komoot/staticmap/pull/39) on the original repository).
|
|
34
|
+
|
|
35
|
+
Changes since the fork are detailed in [CHANGELOG.md](CHANGELOG.md).
|
|
36
|
+
|
|
37
|
+
## Example
|
|
38
|
+
```python
|
|
39
|
+
from staticmap3 import StaticMap, Line
|
|
40
|
+
|
|
41
|
+
m = StaticMap(300, 400, 10)
|
|
42
|
+
m.add_line(Line(((13.4, 52.5), (2.3, 48.9)), 'blue', 3))
|
|
43
|
+
image = m.render()
|
|
44
|
+
image.save('map.png')
|
|
45
|
+
```
|
|
46
|
+
This will create a 300px x 400px map with a blue line drawn from Berlin to Paris.
|
|
47
|
+
|
|
48
|
+

|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
## Installation
|
|
52
|
+
Static Map 3 is a small library, all it takes is python and three python packages: [Pillow](https://python-pillow.github.io/), [requests](https://requests.readthedocs.io/) and [cachecontrol](https://cachecontrol.readthedocs.io/en/latest/).
|
|
53
|
+
|
|
54
|
+
To install Static Map 3:
|
|
55
|
+
|
|
56
|
+
```bash
|
|
57
|
+
pip install git+https://github.com/SamR1/staticmap.git
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
To use [`Filecache`](https://cachecontrol.readthedocs.io/en/latest/storage.html#filecache) as storage backend for cache:
|
|
61
|
+
|
|
62
|
+
```bash
|
|
63
|
+
pip install git+https://github.com/SamR1/staticmap.git@main#egg=staticmap3[filecache]
|
|
64
|
+
```
|
|
65
|
+
The default directory for cache is `.staticmap_cache`. It can be changed by exporting the environment variable `STATICMAP_CACHE_DIR`.
|
|
66
|
+
|
|
67
|
+
## Usage
|
|
68
|
+
#### Create a new map instance:
|
|
69
|
+
|
|
70
|
+
```python
|
|
71
|
+
m = StaticMap(width, height, padding_x, padding_y, url_template, tile_size)
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
parameter | description
|
|
75
|
+
------------------- | -------------
|
|
76
|
+
width | width of the image in pixels
|
|
77
|
+
height | height of the image in pixels
|
|
78
|
+
padding_x | (optional) minimum distance in pixel between map features (lines, markers) and map border
|
|
79
|
+
padding_y | (optional) minimum distance in pixel between map features (lines, markers) and map border
|
|
80
|
+
url_template | (optional) the tile server URL for the map base layer, e.g. <code>https://tile.openstreetmap.org/{z}/{x}/{y}.png</code>
|
|
81
|
+
tile_size | (optional) tile size in pixel, usually 256
|
|
82
|
+
|
|
83
|
+
#### Add a line:
|
|
84
|
+
|
|
85
|
+
```python
|
|
86
|
+
line = Line(coordinates, color, width))
|
|
87
|
+
m.add_line(line)
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
parameter | description
|
|
91
|
+
------------- | -------------
|
|
92
|
+
coordinate | a sequence of lon/lat pairs
|
|
93
|
+
color | a color definition Pillow <a href="https://pillow.readthedocs.io/en/stable/reference/ImageColor.html">supports</a>
|
|
94
|
+
width | the stroke width of the line in pixel
|
|
95
|
+
simplify | whether to simplify coordinates, looks less shaky, default is true
|
|
96
|
+
|
|
97
|
+
#### Add a map circle marker:
|
|
98
|
+
|
|
99
|
+
```python
|
|
100
|
+
marker = CircleMarker(coordinate, color, width))
|
|
101
|
+
m.add_marker(marker)
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
parameter | description
|
|
105
|
+
------------- | -------------
|
|
106
|
+
coordinate | a lon/lat pair: e.g. `(120.1, 47.3)`
|
|
107
|
+
color | a color definition Pillow <a href="https://pillow.readthedocs.io/en/stable/reference/ImageColor.html#color-names">supports</a>
|
|
108
|
+
width | diameter of marker in pixel
|
|
109
|
+
|
|
110
|
+
#### Add a polygon:
|
|
111
|
+
|
|
112
|
+
```python
|
|
113
|
+
polygon = Polygon(coordinates, fill_color, outline_color, simplify)
|
|
114
|
+
m.add_polygon(polygon)
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
parameter | description
|
|
118
|
+
------------- | -------------
|
|
119
|
+
coordinate | a lon/lat pair: e.g. `[[9.628, 47.144], [9.531, 47.270], [9.468, 47.057], [9.623, 47.050], [9.628, 47.144]]`
|
|
120
|
+
fill_color | a color definition Pillow <a href="https://pillow.readthedocs.io/en/stable/reference/ImageColor.html#color-names">supports</a>
|
|
121
|
+
outline_color | a color definition Pillow <a href="https://pillow.readthedocs.io/en/stable/reference/ImageColor.html#color-names">supports</a>
|
|
122
|
+
simplify | whether to simplify coordinates, looks less shaky, default is true
|
|
123
|
+
|
|
124
|
+
## Samples
|
|
125
|
+
#### Show Position on Map
|
|
126
|
+
|
|
127
|
+
```python
|
|
128
|
+
from staticmap3 import StaticMap, CircleMarker
|
|
129
|
+
|
|
130
|
+
m = StaticMap(
|
|
131
|
+
200, 200, url_template='https://tile.openstreetmap.org/{z}/{x}/{y}.png'
|
|
132
|
+
)
|
|
133
|
+
|
|
134
|
+
marker_outline = CircleMarker((10, 47), 'white', 18)
|
|
135
|
+
marker = CircleMarker((10, 47), '#0036FF', 12)
|
|
136
|
+
|
|
137
|
+
m.add_marker(marker_outline)
|
|
138
|
+
m.add_marker(marker)
|
|
139
|
+
|
|
140
|
+
image = m.render(zoom=5)
|
|
141
|
+
image.save('marker.png')
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+

|
|
145
|
+
|
|
146
|
+
#### Show Ferry Connection
|
|
147
|
+
|
|
148
|
+
```python
|
|
149
|
+
from staticmap3 import StaticMap, Line
|
|
150
|
+
|
|
151
|
+
m = StaticMap(200, 200, 80)
|
|
152
|
+
|
|
153
|
+
coordinates = [[12.422, 45.427], [13.749, 44.885]]
|
|
154
|
+
line_outline = Line(coordinates, 'white', 6)
|
|
155
|
+
line = Line(coordinates, '#D2322D', 4)
|
|
156
|
+
|
|
157
|
+
m.add_line(line_outline)
|
|
158
|
+
m.add_line(line)
|
|
159
|
+
|
|
160
|
+
image = m.render()
|
|
161
|
+
image.save('ferry.png')
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+

|
|
165
|
+
|
|
166
|
+
#### Show Icon Marker
|
|
167
|
+
|
|
168
|
+
```python
|
|
169
|
+
from staticmap3 import StaticMap, IconMarker
|
|
170
|
+
|
|
171
|
+
m = StaticMap(240, 240, 80)
|
|
172
|
+
icon_flag = IconMarker(
|
|
173
|
+
(6.63204, 45.85378), './samples/icon-flag.png', 12, 32
|
|
174
|
+
)
|
|
175
|
+
icon_factory = IconMarker(
|
|
176
|
+
(6.6015, 45.8485), './samples/icon-factory.png', 18, 18
|
|
177
|
+
)
|
|
178
|
+
m.add_marker(icon_flag)
|
|
179
|
+
m.add_marker(icon_factory)
|
|
180
|
+
image = m.render()
|
|
181
|
+
image.save('icons.png')
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+

|
|
185
|
+
|
|
186
|
+
### Alternatives
|
|
187
|
+
With [papermap](https://github.com/sgraaf/papermap) there is also a spin-off project dedicated to print maps.
|
|
188
|
+
|
|
189
|
+
### Licence
|
|
190
|
+
Static Map 3 is open source and licensed under Apache License, Version 2.0
|
|
191
|
+
|
|
192
|
+
The map samples on this page are made with [OSM](https://www.openstreetmap.org) data, © [OpenStreetMap](https://www.openstreetmap.org/copyright) contributors
|
|
193
|
+
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
# Static Map 3
|
|
2
|
+
|
|
3
|
+
Static Map 3 is a fork of [Static Map](https://github.com/komoot/staticmap), a small, python-based library for creating map images with lines and markers.\
|
|
4
|
+
The main purpose of this fork is to fix error handling on tile requests, to allow interrupting the execution (see [PR](https://github.com/komoot/staticmap/pull/39) on the original repository).
|
|
5
|
+
|
|
6
|
+
Changes since the fork are detailed in [CHANGELOG.md](CHANGELOG.md).
|
|
7
|
+
|
|
8
|
+
## Example
|
|
9
|
+
```python
|
|
10
|
+
from staticmap3 import StaticMap, Line
|
|
11
|
+
|
|
12
|
+
m = StaticMap(300, 400, 10)
|
|
13
|
+
m.add_line(Line(((13.4, 52.5), (2.3, 48.9)), 'blue', 3))
|
|
14
|
+
image = m.render()
|
|
15
|
+
image.save('map.png')
|
|
16
|
+
```
|
|
17
|
+
This will create a 300px x 400px map with a blue line drawn from Berlin to Paris.
|
|
18
|
+
|
|
19
|
+

|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
## Installation
|
|
23
|
+
Static Map 3 is a small library, all it takes is python and three python packages: [Pillow](https://python-pillow.github.io/), [requests](https://requests.readthedocs.io/) and [cachecontrol](https://cachecontrol.readthedocs.io/en/latest/).
|
|
24
|
+
|
|
25
|
+
To install Static Map 3:
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
pip install git+https://github.com/SamR1/staticmap.git
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
To use [`Filecache`](https://cachecontrol.readthedocs.io/en/latest/storage.html#filecache) as storage backend for cache:
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
pip install git+https://github.com/SamR1/staticmap.git@main#egg=staticmap3[filecache]
|
|
35
|
+
```
|
|
36
|
+
The default directory for cache is `.staticmap_cache`. It can be changed by exporting the environment variable `STATICMAP_CACHE_DIR`.
|
|
37
|
+
|
|
38
|
+
## Usage
|
|
39
|
+
#### Create a new map instance:
|
|
40
|
+
|
|
41
|
+
```python
|
|
42
|
+
m = StaticMap(width, height, padding_x, padding_y, url_template, tile_size)
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
parameter | description
|
|
46
|
+
------------------- | -------------
|
|
47
|
+
width | width of the image in pixels
|
|
48
|
+
height | height of the image in pixels
|
|
49
|
+
padding_x | (optional) minimum distance in pixel between map features (lines, markers) and map border
|
|
50
|
+
padding_y | (optional) minimum distance in pixel between map features (lines, markers) and map border
|
|
51
|
+
url_template | (optional) the tile server URL for the map base layer, e.g. <code>https://tile.openstreetmap.org/{z}/{x}/{y}.png</code>
|
|
52
|
+
tile_size | (optional) tile size in pixel, usually 256
|
|
53
|
+
|
|
54
|
+
#### Add a line:
|
|
55
|
+
|
|
56
|
+
```python
|
|
57
|
+
line = Line(coordinates, color, width))
|
|
58
|
+
m.add_line(line)
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
parameter | description
|
|
62
|
+
------------- | -------------
|
|
63
|
+
coordinate | a sequence of lon/lat pairs
|
|
64
|
+
color | a color definition Pillow <a href="https://pillow.readthedocs.io/en/stable/reference/ImageColor.html">supports</a>
|
|
65
|
+
width | the stroke width of the line in pixel
|
|
66
|
+
simplify | whether to simplify coordinates, looks less shaky, default is true
|
|
67
|
+
|
|
68
|
+
#### Add a map circle marker:
|
|
69
|
+
|
|
70
|
+
```python
|
|
71
|
+
marker = CircleMarker(coordinate, color, width))
|
|
72
|
+
m.add_marker(marker)
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
parameter | description
|
|
76
|
+
------------- | -------------
|
|
77
|
+
coordinate | a lon/lat pair: e.g. `(120.1, 47.3)`
|
|
78
|
+
color | a color definition Pillow <a href="https://pillow.readthedocs.io/en/stable/reference/ImageColor.html#color-names">supports</a>
|
|
79
|
+
width | diameter of marker in pixel
|
|
80
|
+
|
|
81
|
+
#### Add a polygon:
|
|
82
|
+
|
|
83
|
+
```python
|
|
84
|
+
polygon = Polygon(coordinates, fill_color, outline_color, simplify)
|
|
85
|
+
m.add_polygon(polygon)
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
parameter | description
|
|
89
|
+
------------- | -------------
|
|
90
|
+
coordinate | a lon/lat pair: e.g. `[[9.628, 47.144], [9.531, 47.270], [9.468, 47.057], [9.623, 47.050], [9.628, 47.144]]`
|
|
91
|
+
fill_color | a color definition Pillow <a href="https://pillow.readthedocs.io/en/stable/reference/ImageColor.html#color-names">supports</a>
|
|
92
|
+
outline_color | a color definition Pillow <a href="https://pillow.readthedocs.io/en/stable/reference/ImageColor.html#color-names">supports</a>
|
|
93
|
+
simplify | whether to simplify coordinates, looks less shaky, default is true
|
|
94
|
+
|
|
95
|
+
## Samples
|
|
96
|
+
#### Show Position on Map
|
|
97
|
+
|
|
98
|
+
```python
|
|
99
|
+
from staticmap3 import StaticMap, CircleMarker
|
|
100
|
+
|
|
101
|
+
m = StaticMap(
|
|
102
|
+
200, 200, url_template='https://tile.openstreetmap.org/{z}/{x}/{y}.png'
|
|
103
|
+
)
|
|
104
|
+
|
|
105
|
+
marker_outline = CircleMarker((10, 47), 'white', 18)
|
|
106
|
+
marker = CircleMarker((10, 47), '#0036FF', 12)
|
|
107
|
+
|
|
108
|
+
m.add_marker(marker_outline)
|
|
109
|
+
m.add_marker(marker)
|
|
110
|
+
|
|
111
|
+
image = m.render(zoom=5)
|
|
112
|
+
image.save('marker.png')
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+

|
|
116
|
+
|
|
117
|
+
#### Show Ferry Connection
|
|
118
|
+
|
|
119
|
+
```python
|
|
120
|
+
from staticmap3 import StaticMap, Line
|
|
121
|
+
|
|
122
|
+
m = StaticMap(200, 200, 80)
|
|
123
|
+
|
|
124
|
+
coordinates = [[12.422, 45.427], [13.749, 44.885]]
|
|
125
|
+
line_outline = Line(coordinates, 'white', 6)
|
|
126
|
+
line = Line(coordinates, '#D2322D', 4)
|
|
127
|
+
|
|
128
|
+
m.add_line(line_outline)
|
|
129
|
+
m.add_line(line)
|
|
130
|
+
|
|
131
|
+
image = m.render()
|
|
132
|
+
image.save('ferry.png')
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+

|
|
136
|
+
|
|
137
|
+
#### Show Icon Marker
|
|
138
|
+
|
|
139
|
+
```python
|
|
140
|
+
from staticmap3 import StaticMap, IconMarker
|
|
141
|
+
|
|
142
|
+
m = StaticMap(240, 240, 80)
|
|
143
|
+
icon_flag = IconMarker(
|
|
144
|
+
(6.63204, 45.85378), './samples/icon-flag.png', 12, 32
|
|
145
|
+
)
|
|
146
|
+
icon_factory = IconMarker(
|
|
147
|
+
(6.6015, 45.8485), './samples/icon-factory.png', 18, 18
|
|
148
|
+
)
|
|
149
|
+
m.add_marker(icon_flag)
|
|
150
|
+
m.add_marker(icon_factory)
|
|
151
|
+
image = m.render()
|
|
152
|
+
image.save('icons.png')
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+

|
|
156
|
+
|
|
157
|
+
### Alternatives
|
|
158
|
+
With [papermap](https://github.com/sgraaf/papermap) there is also a spin-off project dedicated to print maps.
|
|
159
|
+
|
|
160
|
+
### Licence
|
|
161
|
+
Static Map 3 is open source and licensed under Apache License, Version 2.0
|
|
162
|
+
|
|
163
|
+
The map samples on this page are made with [OSM](https://www.openstreetmap.org) data, © [OpenStreetMap](https://www.openstreetmap.org/copyright) contributors
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "staticmap3"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "Fork of staticmap, a small, python-based library for creating map images with lines and markers."
|
|
5
|
+
authors = [
|
|
6
|
+
{name = "Christoph Lingg", email = "christoph@komoot.de"}
|
|
7
|
+
]
|
|
8
|
+
maintainers = [
|
|
9
|
+
{name = "SamR1"}
|
|
10
|
+
]
|
|
11
|
+
license = {text = "Apache Software License"}
|
|
12
|
+
readme = "README.md"
|
|
13
|
+
keywords = ["static", "map", "image", "osm"]
|
|
14
|
+
classifiers = [
|
|
15
|
+
"License :: OSI Approved :: Apache Software License",
|
|
16
|
+
"Operating System :: POSIX :: Linux",
|
|
17
|
+
"Programming Language :: Python :: 3",
|
|
18
|
+
"Programming Language :: Python :: 3.9",
|
|
19
|
+
"Programming Language :: Python :: 3.10",
|
|
20
|
+
"Programming Language :: Python :: 3.11",
|
|
21
|
+
"Programming Language :: Python :: 3.12",
|
|
22
|
+
"Programming Language :: Python :: 3.13",
|
|
23
|
+
]
|
|
24
|
+
|
|
25
|
+
requires-python = ">=3.9"
|
|
26
|
+
|
|
27
|
+
dependencies = [
|
|
28
|
+
"cachecontrol>=0.14.3",
|
|
29
|
+
"pillow>=11.2.1",
|
|
30
|
+
"requests>=2.32.3"
|
|
31
|
+
]
|
|
32
|
+
|
|
33
|
+
[project.optional-dependencies]
|
|
34
|
+
filecache = ["filelock >= 3.8.0"]
|
|
35
|
+
dev = [
|
|
36
|
+
"mypy>=1.15.0",
|
|
37
|
+
"ruff>=0.11.10",
|
|
38
|
+
"types-requests"
|
|
39
|
+
]
|
|
40
|
+
|
|
41
|
+
[tool.ruff]
|
|
42
|
+
line-length = 79
|
|
43
|
+
|
|
44
|
+
[tool.ruff.lint]
|
|
45
|
+
select = [
|
|
46
|
+
# pycodestyle
|
|
47
|
+
"E",
|
|
48
|
+
# Pyflakes
|
|
49
|
+
"F",
|
|
50
|
+
# isort
|
|
51
|
+
"I",
|
|
52
|
+
# flake8-builtins
|
|
53
|
+
"A",
|
|
54
|
+
# flake8-type-checking
|
|
55
|
+
"TC",
|
|
56
|
+
# flake8-datetimez
|
|
57
|
+
"DTZ",
|
|
58
|
+
# flake8-print
|
|
59
|
+
"T20",
|
|
60
|
+
# flake8-bugbear
|
|
61
|
+
"B",
|
|
62
|
+
# flake8-bandit
|
|
63
|
+
"S",
|
|
64
|
+
# Ruff-specific rules
|
|
65
|
+
"RUF",
|
|
66
|
+
]
|
|
67
|
+
|
|
68
|
+
[tool.ruff.format]
|
|
69
|
+
quote-style = "double"
|
|
70
|
+
|
|
71
|
+
[build-system]
|
|
72
|
+
requires = ["poetry-core>=2.0.0,<3.0.0"]
|
|
73
|
+
build-backend = "poetry.core.masonry.api"
|
|
@@ -0,0 +1,609 @@
|
|
|
1
|
+
import itertools
|
|
2
|
+
import os
|
|
3
|
+
import time
|
|
4
|
+
from concurrent.futures import ThreadPoolExecutor
|
|
5
|
+
from io import BytesIO
|
|
6
|
+
from logging import getLogger
|
|
7
|
+
from math import atan, ceil, cos, floor, log, pi, sinh, sqrt, tan
|
|
8
|
+
from typing import Any, Dict, List, Optional, Sequence, Tuple, Union
|
|
9
|
+
|
|
10
|
+
import requests
|
|
11
|
+
from cachecontrol import CacheControl
|
|
12
|
+
from PIL import Image, ImageDraw
|
|
13
|
+
|
|
14
|
+
try:
|
|
15
|
+
from cachecontrol.caches.file_cache import FileCache
|
|
16
|
+
|
|
17
|
+
cache: Optional[FileCache] = FileCache(
|
|
18
|
+
os.getenv("STATICMAP_CACHE_DIR", ".staticmap_cache")
|
|
19
|
+
)
|
|
20
|
+
except ImportError:
|
|
21
|
+
cache = None
|
|
22
|
+
|
|
23
|
+
logger = getLogger(__name__)
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class Line:
|
|
27
|
+
def __init__(
|
|
28
|
+
self,
|
|
29
|
+
coords: Sequence[Tuple[float, float]],
|
|
30
|
+
color: str,
|
|
31
|
+
width: int,
|
|
32
|
+
simplify: bool = True,
|
|
33
|
+
) -> None:
|
|
34
|
+
"""
|
|
35
|
+
Line that can be drawn in a static map
|
|
36
|
+
|
|
37
|
+
:param coords: an iterable of lon-lat pairs,
|
|
38
|
+
e.g. ((0.0, 0.0), (175.0, 0.0), (175.0, -85.1))
|
|
39
|
+
:param color: color suitable for PIL / Pillow
|
|
40
|
+
:param width: width in pixel
|
|
41
|
+
:param simplify: whether to simplify coordinates, looks less shaky,
|
|
42
|
+
default is true
|
|
43
|
+
"""
|
|
44
|
+
self.coords = coords
|
|
45
|
+
self.color = color
|
|
46
|
+
self.width = width
|
|
47
|
+
self.simplify = simplify
|
|
48
|
+
|
|
49
|
+
@property
|
|
50
|
+
def extent(self) -> Tuple[float, float, float, float]:
|
|
51
|
+
"""
|
|
52
|
+
calculate the coordinates of the envelope / bounding box:
|
|
53
|
+
(min_lon, min_lat, max_lon, max_lat)
|
|
54
|
+
"""
|
|
55
|
+
return (
|
|
56
|
+
min((c[0] for c in self.coords)),
|
|
57
|
+
min((c[1] for c in self.coords)),
|
|
58
|
+
max((c[0] for c in self.coords)),
|
|
59
|
+
max((c[1] for c in self.coords)),
|
|
60
|
+
)
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
class CircleMarker:
|
|
64
|
+
def __init__(
|
|
65
|
+
self, coord: Tuple[float, float], color: str, width: int
|
|
66
|
+
) -> None:
|
|
67
|
+
"""
|
|
68
|
+
:param coord: a lon-lat pair, eg (175.0, 0.0)
|
|
69
|
+
:param color: color suitable for PIL / Pillow
|
|
70
|
+
:param width: marker width
|
|
71
|
+
"""
|
|
72
|
+
self.coord = coord
|
|
73
|
+
self.color = color
|
|
74
|
+
self.width = width
|
|
75
|
+
|
|
76
|
+
@property
|
|
77
|
+
def extent_px(self) -> Tuple[int, int, int, int]:
|
|
78
|
+
return (self.width,) * 4
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
class IconMarker:
|
|
82
|
+
def __init__(
|
|
83
|
+
self,
|
|
84
|
+
coord: Tuple[float, float],
|
|
85
|
+
file_path: str,
|
|
86
|
+
offset_x: int,
|
|
87
|
+
offset_y: int,
|
|
88
|
+
) -> None:
|
|
89
|
+
"""
|
|
90
|
+
:param coord: a lon-lat pair, eg (175.0, 0.0)
|
|
91
|
+
:param file_path: path to icon
|
|
92
|
+
:param offset_x: x position of the tip of the icon. relative to left
|
|
93
|
+
bottom, in pixel
|
|
94
|
+
:param offset_y: y position of the tip of the icon. relative to left
|
|
95
|
+
bottom, in pixel
|
|
96
|
+
"""
|
|
97
|
+
self.coord = coord
|
|
98
|
+
self.img = Image.open(file_path, "r")
|
|
99
|
+
self.offset = (offset_x, offset_y)
|
|
100
|
+
|
|
101
|
+
@property
|
|
102
|
+
def extent_px(self) -> Tuple[int, int, int, int]:
|
|
103
|
+
w, h = self.img.size
|
|
104
|
+
return (
|
|
105
|
+
self.offset[0],
|
|
106
|
+
h - self.offset[1],
|
|
107
|
+
w - self.offset[0],
|
|
108
|
+
self.offset[1],
|
|
109
|
+
)
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
class Polygon:
|
|
113
|
+
def __init__(
|
|
114
|
+
self,
|
|
115
|
+
coords: Sequence[Tuple[float, float]],
|
|
116
|
+
fill_color: str,
|
|
117
|
+
outline_color: str,
|
|
118
|
+
simplify: bool = True,
|
|
119
|
+
) -> None:
|
|
120
|
+
"""
|
|
121
|
+
Polygon that can be drawn on map
|
|
122
|
+
|
|
123
|
+
:param coords: an iterable of lon-lat pairs,
|
|
124
|
+
e.g. ((0.0, 0.0), (175.0, 0.0), (175.0, -85.1))
|
|
125
|
+
:param fill_color: color suitable for PIL / Pillow, can be None
|
|
126
|
+
(transparent)
|
|
127
|
+
:param outline_color: color suitable for PIL / Pillow, can be None
|
|
128
|
+
(transparent)
|
|
129
|
+
:param simplify: whether to simplify coordinates, looks less shaky,
|
|
130
|
+
default is true
|
|
131
|
+
"""
|
|
132
|
+
self.coords = coords
|
|
133
|
+
self.fill_color = fill_color
|
|
134
|
+
self.outline_color = outline_color
|
|
135
|
+
self.simplify = simplify
|
|
136
|
+
|
|
137
|
+
@property
|
|
138
|
+
def extent(self) -> Tuple[float, float, float, float]:
|
|
139
|
+
return (
|
|
140
|
+
min((c[0] for c in self.coords)),
|
|
141
|
+
min((c[1] for c in self.coords)),
|
|
142
|
+
max((c[0] for c in self.coords)),
|
|
143
|
+
max((c[1] for c in self.coords)),
|
|
144
|
+
)
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
def _lon_to_x(lon: float, zoom: int) -> float:
|
|
148
|
+
"""
|
|
149
|
+
transform longitude to tile number
|
|
150
|
+
"""
|
|
151
|
+
if not (-180 <= lon <= 180):
|
|
152
|
+
lon = (lon + 180) % 360 - 180
|
|
153
|
+
|
|
154
|
+
return ((lon + 180.0) / 360) * pow(2, zoom)
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
def _lat_to_y(lat: float, zoom: int) -> float:
|
|
158
|
+
"""
|
|
159
|
+
transform latitude to tile number
|
|
160
|
+
"""
|
|
161
|
+
if not (-90 <= lat <= 90):
|
|
162
|
+
lat = (lat + 90) % 180 - 90
|
|
163
|
+
|
|
164
|
+
return (
|
|
165
|
+
(1 - log(tan(lat * pi / 180) + 1 / cos(lat * pi / 180)) / pi)
|
|
166
|
+
/ 2
|
|
167
|
+
* pow(2, zoom)
|
|
168
|
+
)
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
def _y_to_lat(y: float, zoom: int) -> float:
|
|
172
|
+
return atan(sinh(pi * (1 - 2 * y / pow(2, zoom)))) / pi * 180
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
def _x_to_lon(x: float, zoom: int) -> float:
|
|
176
|
+
return x / pow(2, zoom) * 360.0 - 180.0
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
def _simplify(
|
|
180
|
+
points: List[Tuple[int, int]], tolerance: int = 11
|
|
181
|
+
) -> List[Tuple[int, int]]:
|
|
182
|
+
"""
|
|
183
|
+
:param points: list of lon-lat pairs
|
|
184
|
+
:param tolerance: tolerance in pixel
|
|
185
|
+
:return: list of lon-lat pairs
|
|
186
|
+
"""
|
|
187
|
+
if not points:
|
|
188
|
+
return points
|
|
189
|
+
|
|
190
|
+
new_coords = [points[0]]
|
|
191
|
+
|
|
192
|
+
for p in points[1:-1]:
|
|
193
|
+
last = new_coords[-1]
|
|
194
|
+
|
|
195
|
+
dist = sqrt(pow(last[0] - p[0], 2) + pow(last[1] - p[1], 2))
|
|
196
|
+
if dist > tolerance:
|
|
197
|
+
new_coords.append(p)
|
|
198
|
+
|
|
199
|
+
new_coords.append(points[-1])
|
|
200
|
+
|
|
201
|
+
return new_coords
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
class StaticMap:
|
|
205
|
+
def __init__(
|
|
206
|
+
self,
|
|
207
|
+
width: int,
|
|
208
|
+
height: int,
|
|
209
|
+
padding_x: int = 0,
|
|
210
|
+
padding_y: int = 0,
|
|
211
|
+
url_template: str = "https://tile.openstreetmap.org/{z}/{x}/{y}.png",
|
|
212
|
+
tile_size: int = 256,
|
|
213
|
+
tile_request_timeout: Optional[float] = None,
|
|
214
|
+
headers: Optional[Dict] = None,
|
|
215
|
+
reverse_y: bool = False,
|
|
216
|
+
background_color: str = "#fff",
|
|
217
|
+
delay_between_retries: int = 0,
|
|
218
|
+
) -> None:
|
|
219
|
+
"""
|
|
220
|
+
:param width: map width in pixel
|
|
221
|
+
:param height: map height in pixel
|
|
222
|
+
:param padding_x: min distance in pixel from map features to border
|
|
223
|
+
of map
|
|
224
|
+
:param padding_y: min distance in pixel from map features to border
|
|
225
|
+
of map
|
|
226
|
+
:param url_template: tile URL
|
|
227
|
+
:param tile_size: the size of the map tiles in pixel
|
|
228
|
+
:param tile_request_timeout: time in seconds to wait for requesting
|
|
229
|
+
map tiles
|
|
230
|
+
:param headers: additional headers to add to http requests
|
|
231
|
+
:param reverse_y: tile source has TMS y origin
|
|
232
|
+
:param background_color: Image background color, only visible when
|
|
233
|
+
tiles are transparent
|
|
234
|
+
:param delay_between_retries: number of seconds to wait between retries
|
|
235
|
+
of map tile requests
|
|
236
|
+
"""
|
|
237
|
+
self.width = width
|
|
238
|
+
self.height = height
|
|
239
|
+
self.padding = (padding_x, padding_y)
|
|
240
|
+
self.url_template = url_template
|
|
241
|
+
self.headers = headers if headers else {"User-Agent": "StaticMap"}
|
|
242
|
+
self.tile_size = tile_size
|
|
243
|
+
self.request_timeout = tile_request_timeout
|
|
244
|
+
self.reverse_y = reverse_y
|
|
245
|
+
self.background_color = background_color
|
|
246
|
+
|
|
247
|
+
# features
|
|
248
|
+
self.markers: List[Union["IconMarker", "CircleMarker"]] = []
|
|
249
|
+
self.lines: List["Line"] = []
|
|
250
|
+
self.polygons: List["Polygon"] = []
|
|
251
|
+
|
|
252
|
+
# fields that get set when map is rendered
|
|
253
|
+
self.x_center: float = 0.0
|
|
254
|
+
self.y_center: float = 0.0
|
|
255
|
+
self.zoom = 0
|
|
256
|
+
|
|
257
|
+
self.delay_between_retries = delay_between_retries
|
|
258
|
+
|
|
259
|
+
def add_line(self, line: "Line") -> None:
|
|
260
|
+
"""
|
|
261
|
+
:param line: line to draw
|
|
262
|
+
"""
|
|
263
|
+
self.lines.append(line)
|
|
264
|
+
|
|
265
|
+
def add_marker(self, marker: Union["IconMarker", "CircleMarker"]) -> None:
|
|
266
|
+
"""
|
|
267
|
+
:param marker: marker to draw
|
|
268
|
+
"""
|
|
269
|
+
self.markers.append(marker)
|
|
270
|
+
|
|
271
|
+
def add_polygon(self, polygon: "Polygon") -> None:
|
|
272
|
+
"""
|
|
273
|
+
:param polygon: polygon to be drawn
|
|
274
|
+
"""
|
|
275
|
+
self.polygons.append(polygon)
|
|
276
|
+
|
|
277
|
+
def render(
|
|
278
|
+
self,
|
|
279
|
+
zoom: Optional[int] = None,
|
|
280
|
+
center: Optional[Tuple[float, float]] = None,
|
|
281
|
+
) -> "Image.Image":
|
|
282
|
+
"""
|
|
283
|
+
render static map with all map features that were added to map before
|
|
284
|
+
|
|
285
|
+
:param zoom: optional zoom level, will be optimized automatically if
|
|
286
|
+
not given.
|
|
287
|
+
:param center: optional center of map, will be set automatically from
|
|
288
|
+
markers if not given.
|
|
289
|
+
:return: PIL image instance
|
|
290
|
+
"""
|
|
291
|
+
|
|
292
|
+
if (
|
|
293
|
+
not self.lines
|
|
294
|
+
and not self.markers
|
|
295
|
+
and not self.polygons
|
|
296
|
+
and not (center and zoom)
|
|
297
|
+
):
|
|
298
|
+
raise RuntimeError(
|
|
299
|
+
"cannot render empty map, add lines / markers / polygons first"
|
|
300
|
+
)
|
|
301
|
+
|
|
302
|
+
if zoom is None:
|
|
303
|
+
self.zoom = self._calculate_zoom()
|
|
304
|
+
else:
|
|
305
|
+
self.zoom = zoom
|
|
306
|
+
|
|
307
|
+
if center:
|
|
308
|
+
self.x_center = _lon_to_x(center[0], self.zoom)
|
|
309
|
+
self.y_center = _lat_to_y(center[1], self.zoom)
|
|
310
|
+
else:
|
|
311
|
+
# get extent of all lines
|
|
312
|
+
extent = self.determine_extent(zoom=self.zoom)
|
|
313
|
+
|
|
314
|
+
# calculate center point of map
|
|
315
|
+
lon_center, lat_center = (
|
|
316
|
+
(extent[0] + extent[2]) / 2,
|
|
317
|
+
(extent[1] + extent[3]) / 2,
|
|
318
|
+
)
|
|
319
|
+
self.x_center = _lon_to_x(lon_center, self.zoom)
|
|
320
|
+
self.y_center = _lat_to_y(lat_center, self.zoom)
|
|
321
|
+
|
|
322
|
+
image = Image.new(
|
|
323
|
+
"RGB", (self.width, self.height), self.background_color
|
|
324
|
+
)
|
|
325
|
+
|
|
326
|
+
self._draw_base_layer(image)
|
|
327
|
+
self._draw_features(image)
|
|
328
|
+
|
|
329
|
+
return image
|
|
330
|
+
|
|
331
|
+
def determine_extent(
|
|
332
|
+
self, zoom: Optional[int] = None
|
|
333
|
+
) -> Tuple[float, float, float, float]:
|
|
334
|
+
"""
|
|
335
|
+
calculate common extent of all current map features
|
|
336
|
+
|
|
337
|
+
:param zoom: optional parameter, when set extent of markers can be
|
|
338
|
+
considered
|
|
339
|
+
:return: extent (min_lon, min_lat, max_lon, max_lat)
|
|
340
|
+
"""
|
|
341
|
+
extents = [line.extent for line in self.lines]
|
|
342
|
+
|
|
343
|
+
for m in self.markers:
|
|
344
|
+
e = (m.coord[0], m.coord[1])
|
|
345
|
+
|
|
346
|
+
if zoom is None:
|
|
347
|
+
extents.append(e * 2)
|
|
348
|
+
continue
|
|
349
|
+
|
|
350
|
+
# consider dimension of marker
|
|
351
|
+
e_px = m.extent_px
|
|
352
|
+
|
|
353
|
+
x = _lon_to_x(e[0], zoom)
|
|
354
|
+
y = _lat_to_y(e[1], zoom)
|
|
355
|
+
|
|
356
|
+
extents += [
|
|
357
|
+
(
|
|
358
|
+
_x_to_lon(x - float(e_px[0]) / self.tile_size, zoom),
|
|
359
|
+
_y_to_lat(y + float(e_px[1]) / self.tile_size, zoom),
|
|
360
|
+
_x_to_lon(x + float(e_px[2]) / self.tile_size, zoom),
|
|
361
|
+
_y_to_lat(y - float(e_px[3]) / self.tile_size, zoom),
|
|
362
|
+
)
|
|
363
|
+
]
|
|
364
|
+
|
|
365
|
+
extents += [p.extent for p in self.polygons]
|
|
366
|
+
|
|
367
|
+
return (
|
|
368
|
+
min(e[0] for e in extents),
|
|
369
|
+
min(e[1] for e in extents),
|
|
370
|
+
max(e[2] for e in extents),
|
|
371
|
+
max(e[3] for e in extents),
|
|
372
|
+
)
|
|
373
|
+
|
|
374
|
+
def _calculate_zoom(self) -> int:
|
|
375
|
+
"""
|
|
376
|
+
calculate the best zoom level for given extent
|
|
377
|
+
|
|
378
|
+
:return: lowest zoom level for which the entire extent fits in
|
|
379
|
+
"""
|
|
380
|
+
|
|
381
|
+
for z in range(17, -1, -1):
|
|
382
|
+
extent = self.determine_extent(zoom=z)
|
|
383
|
+
|
|
384
|
+
width = (
|
|
385
|
+
_lon_to_x(extent[2], z) - _lon_to_x(extent[0], z)
|
|
386
|
+
) * self.tile_size
|
|
387
|
+
if width > (self.width - self.padding[0] * 2):
|
|
388
|
+
continue
|
|
389
|
+
|
|
390
|
+
height = (
|
|
391
|
+
_lat_to_y(extent[1], z) - _lat_to_y(extent[3], z)
|
|
392
|
+
) * self.tile_size
|
|
393
|
+
if height > (self.height - self.padding[1] * 2):
|
|
394
|
+
continue
|
|
395
|
+
|
|
396
|
+
# we found first zoom that can display entire extent
|
|
397
|
+
return z
|
|
398
|
+
|
|
399
|
+
# map dimension is too small to fit all features
|
|
400
|
+
return 0
|
|
401
|
+
|
|
402
|
+
def _x_to_px(self, x: float) -> int:
|
|
403
|
+
"""
|
|
404
|
+
transform tile number to pixel on image canvas
|
|
405
|
+
"""
|
|
406
|
+
px = (x - self.x_center) * self.tile_size + self.width / 2
|
|
407
|
+
return round(px)
|
|
408
|
+
|
|
409
|
+
def _y_to_px(self, y: float) -> int:
|
|
410
|
+
"""
|
|
411
|
+
transform tile number to pixel on image canvas
|
|
412
|
+
"""
|
|
413
|
+
px = (y - self.y_center) * self.tile_size + self.height / 2
|
|
414
|
+
return round(px)
|
|
415
|
+
|
|
416
|
+
def _draw_base_layer(self, image: "Image.Image") -> None:
|
|
417
|
+
x_min = floor(self.x_center - (0.5 * self.width / self.tile_size))
|
|
418
|
+
y_min = floor(self.y_center - (0.5 * self.height / self.tile_size))
|
|
419
|
+
x_max = ceil(self.x_center + (0.5 * self.width / self.tile_size))
|
|
420
|
+
y_max = ceil(self.y_center + (0.5 * self.height / self.tile_size))
|
|
421
|
+
|
|
422
|
+
# assemble all map tiles needed for the map
|
|
423
|
+
tiles = []
|
|
424
|
+
for x in range(x_min, x_max):
|
|
425
|
+
for y in range(y_min, y_max):
|
|
426
|
+
# x and y may have crossed the date line
|
|
427
|
+
max_tile = 2**self.zoom
|
|
428
|
+
tile_x = (x + max_tile) % max_tile
|
|
429
|
+
tile_y = (y + max_tile) % max_tile
|
|
430
|
+
|
|
431
|
+
if self.reverse_y:
|
|
432
|
+
tile_y = ((1 << self.zoom) - tile_y) - 1
|
|
433
|
+
|
|
434
|
+
url = self.url_template.format(z=self.zoom, x=tile_x, y=tile_y)
|
|
435
|
+
tiles.append((x, y, url))
|
|
436
|
+
|
|
437
|
+
thread_pool = ThreadPoolExecutor(4)
|
|
438
|
+
|
|
439
|
+
for nb_retry in itertools.count():
|
|
440
|
+
if not tiles:
|
|
441
|
+
# no tiles left
|
|
442
|
+
break
|
|
443
|
+
|
|
444
|
+
if nb_retry > 0 and self.delay_between_retries:
|
|
445
|
+
# to avoid stressing the map tile server to much, wait
|
|
446
|
+
# some seconds
|
|
447
|
+
time.sleep(self.delay_between_retries)
|
|
448
|
+
|
|
449
|
+
if nb_retry >= 3:
|
|
450
|
+
# maximum number of retries exceeded
|
|
451
|
+
raise RuntimeError(
|
|
452
|
+
"could not download {} tiles: {}".format(len(tiles), tiles)
|
|
453
|
+
)
|
|
454
|
+
|
|
455
|
+
failed_tiles = []
|
|
456
|
+
futures = [
|
|
457
|
+
thread_pool.submit(
|
|
458
|
+
self.get,
|
|
459
|
+
tile[2],
|
|
460
|
+
timeout=self.request_timeout,
|
|
461
|
+
headers=self.headers,
|
|
462
|
+
)
|
|
463
|
+
for tile in tiles
|
|
464
|
+
]
|
|
465
|
+
|
|
466
|
+
for tile, future in zip(tiles, futures):
|
|
467
|
+
x, y, url = tile
|
|
468
|
+
|
|
469
|
+
try:
|
|
470
|
+
response_status_code, response_content = future.result()
|
|
471
|
+
except Exception:
|
|
472
|
+
response_status_code, response_content = None, None
|
|
473
|
+
|
|
474
|
+
if response_status_code != 200:
|
|
475
|
+
logger.error(
|
|
476
|
+
f"request failed [{response_status_code}]: {url}"
|
|
477
|
+
)
|
|
478
|
+
failed_tiles.append(tile)
|
|
479
|
+
continue
|
|
480
|
+
|
|
481
|
+
if not response_content:
|
|
482
|
+
logger.error("request failed: no content")
|
|
483
|
+
failed_tiles.append(tile)
|
|
484
|
+
continue
|
|
485
|
+
|
|
486
|
+
tile_image = Image.open(BytesIO(response_content)).convert(
|
|
487
|
+
"RGBA"
|
|
488
|
+
)
|
|
489
|
+
box = (
|
|
490
|
+
self._x_to_px(x),
|
|
491
|
+
self._y_to_px(y),
|
|
492
|
+
self._x_to_px(x + 1),
|
|
493
|
+
self._y_to_px(y + 1),
|
|
494
|
+
)
|
|
495
|
+
image.paste(tile_image, box, tile_image)
|
|
496
|
+
|
|
497
|
+
# put failed back into list of tiles to fetch in next try
|
|
498
|
+
tiles = failed_tiles
|
|
499
|
+
|
|
500
|
+
def get(self, url: str, **kwargs: Any) -> Tuple[int, bytes]:
|
|
501
|
+
"""
|
|
502
|
+
returns the status code and content (in bytes) of the requested
|
|
503
|
+
tile url
|
|
504
|
+
"""
|
|
505
|
+
session = requests.session()
|
|
506
|
+
cached_session = CacheControl(session, cache=cache)
|
|
507
|
+
res = cached_session.get(url, **kwargs)
|
|
508
|
+
return res.status_code, res.content
|
|
509
|
+
|
|
510
|
+
def _draw_features(self, image: "Image.Image") -> None:
|
|
511
|
+
# Pillow does not support anti aliasing for lines and circles
|
|
512
|
+
# There is a trick to draw them on an image that is twice the size and
|
|
513
|
+
# resize it at the end before it gets merged with the base layer
|
|
514
|
+
|
|
515
|
+
image_lines = Image.new(
|
|
516
|
+
"RGBA", (self.width * 2, self.height * 2), (255, 0, 0, 0)
|
|
517
|
+
)
|
|
518
|
+
draw = ImageDraw.Draw(image_lines)
|
|
519
|
+
|
|
520
|
+
for line in self.lines:
|
|
521
|
+
points = [
|
|
522
|
+
(
|
|
523
|
+
self._x_to_px(_lon_to_x(coord[0], self.zoom)) * 2,
|
|
524
|
+
self._y_to_px(_lat_to_y(coord[1], self.zoom)) * 2,
|
|
525
|
+
)
|
|
526
|
+
for coord in line.coords
|
|
527
|
+
]
|
|
528
|
+
|
|
529
|
+
if line.simplify:
|
|
530
|
+
points = _simplify(points)
|
|
531
|
+
|
|
532
|
+
for point in points:
|
|
533
|
+
# draw extra points to make the connection between lines
|
|
534
|
+
# look nice
|
|
535
|
+
draw.ellipse(
|
|
536
|
+
(
|
|
537
|
+
point[0] - line.width + 1,
|
|
538
|
+
point[1] - line.width + 1,
|
|
539
|
+
point[0] + line.width - 1,
|
|
540
|
+
point[1] + line.width - 1,
|
|
541
|
+
),
|
|
542
|
+
fill=line.color,
|
|
543
|
+
)
|
|
544
|
+
|
|
545
|
+
draw.line(points, fill=line.color, width=line.width * 2)
|
|
546
|
+
|
|
547
|
+
for circle in filter(
|
|
548
|
+
lambda m: isinstance(m, CircleMarker), self.markers
|
|
549
|
+
):
|
|
550
|
+
point = (
|
|
551
|
+
self._x_to_px(_lon_to_x(circle.coord[0], self.zoom)) * 2,
|
|
552
|
+
self._y_to_px(_lat_to_y(circle.coord[1], self.zoom)) * 2,
|
|
553
|
+
)
|
|
554
|
+
draw.ellipse(
|
|
555
|
+
(
|
|
556
|
+
point[0] - circle.width, # type: ignore [union-attr]
|
|
557
|
+
point[1] - circle.width, # type: ignore [union-attr]
|
|
558
|
+
point[0] + circle.width, # type: ignore [union-attr]
|
|
559
|
+
point[1] + circle.width, # type: ignore [union-attr]
|
|
560
|
+
),
|
|
561
|
+
fill=circle.color, # type: ignore [union-attr]
|
|
562
|
+
)
|
|
563
|
+
|
|
564
|
+
for polygon in self.polygons:
|
|
565
|
+
points = [
|
|
566
|
+
(
|
|
567
|
+
self._x_to_px(_lon_to_x(coord[0], self.zoom)) * 2,
|
|
568
|
+
self._y_to_px(_lat_to_y(coord[1], self.zoom)) * 2,
|
|
569
|
+
)
|
|
570
|
+
for coord in polygon.coords
|
|
571
|
+
]
|
|
572
|
+
if polygon.simplify:
|
|
573
|
+
points = _simplify(points)
|
|
574
|
+
|
|
575
|
+
if polygon.fill_color or polygon.outline_color:
|
|
576
|
+
draw.polygon(
|
|
577
|
+
points,
|
|
578
|
+
fill=polygon.fill_color,
|
|
579
|
+
outline=polygon.outline_color,
|
|
580
|
+
)
|
|
581
|
+
|
|
582
|
+
image_lines = image_lines.resize(
|
|
583
|
+
(self.width, self.height), Image.Resampling.LANCZOS
|
|
584
|
+
)
|
|
585
|
+
|
|
586
|
+
# merge lines with base image
|
|
587
|
+
image.paste(image_lines, (0, 0), image_lines)
|
|
588
|
+
|
|
589
|
+
# add icon marker
|
|
590
|
+
for icon in filter(lambda m: isinstance(m, IconMarker), self.markers):
|
|
591
|
+
position = (
|
|
592
|
+
self._x_to_px(_lon_to_x(icon.coord[0], self.zoom))
|
|
593
|
+
- icon.offset[0], # type: ignore [union-attr]
|
|
594
|
+
self._y_to_px(_lat_to_y(icon.coord[1], self.zoom))
|
|
595
|
+
- icon.offset[1], # type: ignore [union-attr]
|
|
596
|
+
)
|
|
597
|
+
image.paste(
|
|
598
|
+
icon.img, # type: ignore [union-attr]
|
|
599
|
+
position,
|
|
600
|
+
icon.img, # type: ignore [union-attr]
|
|
601
|
+
)
|
|
602
|
+
|
|
603
|
+
|
|
604
|
+
if __name__ == "__main__":
|
|
605
|
+
static_map = StaticMap(300, 400, 10)
|
|
606
|
+
line = Line([(13.4, 52.5), (2.3, 48.9)], "blue", 3)
|
|
607
|
+
static_map.add_line(line)
|
|
608
|
+
image = static_map.render()
|
|
609
|
+
image.save("berlin_paris.png")
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
from unittest import TestCase
|
|
2
|
+
|
|
3
|
+
from staticmap3.staticmap import _lat_to_y, _lon_to_x, _x_to_lon, _y_to_lat
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class LonLatConversionTest(TestCase):
|
|
7
|
+
def testLon(self) -> None:
|
|
8
|
+
for lon in range(-180, 180, 20):
|
|
9
|
+
for zoom in range(0, 10):
|
|
10
|
+
x = _lon_to_x(lon, zoom)
|
|
11
|
+
longitude = _x_to_lon(x, zoom)
|
|
12
|
+
self.assertAlmostEqual(lon, longitude, places=5)
|
|
13
|
+
|
|
14
|
+
def testLat(self) -> None:
|
|
15
|
+
for lat in range(-89, 89, 2):
|
|
16
|
+
for zoom in range(0, 10):
|
|
17
|
+
y = _lat_to_y(lat, zoom)
|
|
18
|
+
longitude = _y_to_lat(y, zoom)
|
|
19
|
+
self.assertAlmostEqual(lat, longitude, places=5)
|