Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for explicit-boundary blank maps #8

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added __init__.py
Empty file.
38 changes: 37 additions & 1 deletion staticmap/staticmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ def __init__(self, width, height, padding_x=0, padding_y=0, url_template="http:/
self.markers = []
self.lines = []
self.polygons = []
self.extent = []

# fields that get set when map is rendered
self.x_center = 0
Expand Down Expand Up @@ -234,6 +235,30 @@ def add_polygon(self, polygon):
"""
self.polygons.append(polygon)

def set_extent(self, min_lon, min_lat, max_lon, max_lat):
"""
:param min_lon, min_lat, max_lon, max_lat: bounding box of desired map extent
:type integer: min_lon, min_lat, max_lon, max_lat
"""

# define our booleans
relations = max_lon > min_lon and max_lat > min_lat
longitudes = -180 <= max_lon <= 180 and -180 <= min_lon <= 180
latitudes = -90 <= max_lat <= 90 and -90 <= min_lat <= 90

# if they all pass, set the extent and centers
if relations and longitudes and latitudes:
self.extent = [min_lon, min_lat, max_lon, max_lat]

# otherwise print the corresponding error messages
elif not longitudes:
raise RuntimeError("Longitudes must be between -90 and 90. You entered ", min_lon, " - ", max_lon)
elif not latitudes:
raise RuntimeError("Latitutdes must be between -180 and 180. You entered ", min_lat, "-", max_lat)
elif not relations:
raise RuntimeError("max_lon must be more than min_lon, and max_lat must be more than min_lat. \
You entered max_lon: ", max_lon, " min_lon: ", min_lon, " max_lat: ", max_lat, "min_lat: ", min_lat)

def render(self, zoom=None, center=None):
"""
render static map with all map features that were added to map before
Expand All @@ -245,6 +270,13 @@ def render(self, zoom=None, center=None):
:return: PIL image instance
:rtype: Image.Image
"""
if self.extent != []:
ex_poly = [[self.extent[0], self.extent[1]],
[self.extent[0], self.extent[3]],
[self.extent[2], self.extent[1]],
[self.extent[2], self.extent[3]]]
polygon = Polygon(ex_poly, 'white', 'white', True)
self.add_polygon(polygon)

if not self.lines and not self.markers and not self.polygons and not (center and zoom):
raise RuntimeError("cannot render empty map, add lines / markers / polygons first")
Expand All @@ -264,11 +296,15 @@ def render(self, zoom=None, center=None):
# calculate center point of map
lon_center, lat_center = (extent[0] + extent[2]) / 2, (extent[1] + extent[3]) / 2
self.x_center = _lon_to_x(lon_center, self.zoom)
self.y_center = _lat_to_y(lat_center, self.zoom)
self.y_center = _lat_to_y(lat_center, self.zoom)

image = Image.new('RGB', (self.width, self.height), self.background_color)

self._draw_base_layer(image)

if self.extent != []:
self.polygons.remove(polygon)

self._draw_features(image)

return image
Expand Down
2 changes: 1 addition & 1 deletion staticmap/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ def testLat(self):
for zoom in range(0, 10):
y = _lat_to_y(zoom)
l = _y_to_lat(zoom)
self.assertAlmostEqual(lat, l, places=5)
self.assertAlmostEqual(lat, l, places=5)