def _calc_pixel_dimensions(self, pixel_dimensions):
     """ Calculate the size of the image in pixels, given whatever
     minimum / maximums we've been given.
     """
     if pixel_dimensions is None or len(pixel_dimensions.keys()) == 0:
         pixel_dimensions = {"max": DEFAULT_SIZE}
     if "width" in pixel_dimensions and "height" in pixel_dimensions:
         self.pixel_width = pixel_dimensions["width"]
         self.pixel_height = pixel_dimensions["height"]
         return
     self.aspect_ratio = (self.max_longitude - self.min_longitude) / \
                         (mercator_adjust(self.max_latitude) -
                          mercator_adjust(self.min_latitude))
     if "width" in pixel_dimensions:
         self.pixel_width = pixel_dimensions["width"]
         self.pixel_height = int(float(self.pixel_width) / self.aspect_ratio)
         return
     if "height" in pixel_dimensions:
         self.pixel_height = pixel_dimensions["height"]
         self.pixel_width = int(float(self.pixel_height) * self.aspect_ratio)
         return
     if "max" in pixel_dimensions:
         if self.aspect_ratio > 1:
             self.pixel_width = pixel_dimensions["max"]
             self.pixel_height = int(float(self.pixel_width) /
                                     self.aspect_ratio)
             return
         self.pixel_height = pixel_dimensions["max"]
         self.pixel_width = int(float(self.pixel_height) * self.aspect_ratio)
         return
     if "min" in pixel_dimensions:
         if self.aspect_ratio < 1:
             self.pixel_width = pixel_dimensions["min"]
             self.pixel_height = int(float(self.pixel_width) /
                                     self.aspect_ratio)
             return
         self.pixel_height = pixel_dimensions["min"]
         self.pixel_width = int(float(self.pixel_height) * self.aspect_ratio)
         return
     raise ValueError("Could not calculate the image resolution")
예제 #2
0
 def _convert_to_fraction(self, point):
     """ Convert a lat/long point into an (x,y) fraction of the drawing area
     """
     # TODO: this could be neater. The addition / subtraction should
     # be (a) consistently one or the other, and (b) we should adjust
     # the image width to take it into account.
     # Add half a pixel width so that we're drawing in the centre of
     # a pixel, not on the edge
     x = 0.5 / self.pixel_width + (point.long - self.min_longitude) / \
         (self.max_longitude - self.min_longitude)
     merc_lat = mercator_adjust(point.lat)
     # Subtract half a pixel width so that we're drawing in the
     # centre of a pixel, not on the edge
     y = 1 - (0.5 / self.pixel_height) - \
             (merc_lat - self.min_merc_latitude) / self.merc_latitude_width
     return (x, y)