diff --git a/__init__.py b/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/staticmap/staticmap.py b/staticmap/staticmap.py index d2139d9..81386a7 100644 --- a/staticmap/staticmap.py +++ b/staticmap/staticmap.py @@ -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 @@ -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 @@ -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") @@ -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 diff --git a/staticmap/tests.py b/staticmap/tests.py index 70904ee..d94bd6f 100644 --- a/staticmap/tests.py +++ b/staticmap/tests.py @@ -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) \ No newline at end of file