def get_extents_from_center_lat_lng(center, zoom, width, height): northeast = Units.add_pixels_to_latlng(center.clone(), zoom, int(width / 2), -1 * int(height / 2)) southwest = Units.add_pixels_to_latlng(center.clone(), zoom, -1 * int(width / 2), int(height / 2)) return Extents(northeast, southwest)
def get_basemap_and_extents(self, map_type, zoom, center, width, height): import os, urllib, StringIO, Image #units.Units.add_pixels_to_latlng(center_lat, center_lng, zoom, 300, 300) map_url = None # http://api.tiles.mapbox.com/v3/{mapid}/{lon},{lat},{z}/{width}x{height}.{format} # http://api.tiles.mapbox.com/v3/examples.map-zr0njcqy/-73.99,40.70,13/500x300.png if map_type.overlay_source.name == 'mapbox': styleid = map_type.provider_id ''' map_url = 'http://staticmaps.cloudmade.com/' + api_key + \ '/staticmap?styleid=' + styleid + '&zoom=' + str(zoom) + \ '¢er=' + str(center.y) + ',' + str(center.x) + \ '&size=' + str(width) + 'x' + str(height) ''' map_url = 'http://api.tiles.mapbox.com/v3/{0}/{1},{2},{3}/{4}x{5}.png' map_url = map_url.format(map_type.provider_id, center.x, center.y, zoom, width, height) #if google is the map provider: else: scale_factor = 1 if not settings.IS_GOOGLE_REGISTERED_NONPROFIT: zoom, width, height = zoom-1, int(width/2), int(height/2) scale_factor = 2 map_url = map_type.wms_url + '&zoom=' + str(zoom) + '¢er=' + \ str(center.y) + ',' + str(center.x) + '&size=' + str(width) + \ 'x' + str(height) + '&scale=' + str(scale_factor) #calculate extents (returns geos Point): #(0,0) in pacific northwest; x = lat, y = lng northeast = Units.add_pixels_to_latlng(center.clone(), zoom, int(width/2), -1*int(height/2)) southwest = Units.add_pixels_to_latlng(center.clone(), zoom, -1*int(width/2), int(height/2)) try: file = urllib.urlopen(map_url) map_image = StringIO.StringIO(file.read()) # constructs a StringIO holding the image map_image = Image.open(map_image).convert('RGB') except IOError: error_image_url = 'https://chart.googleapis.com/chart?chst=d_fnote_title&chld=sticky_y|1|FF0000|l|Map%20Service%20Unavailable|' file = urllib.urlopen(error_image_url) map_image = StringIO.StringIO(file.read()) # constructs a StringIO holding the image map_image = Image.open(map_image).convert('RGB') return { 'map_image': map_image, 'northeast': northeast, 'southwest': southwest }
def generate_image_with_map_margins(self): from localground.apps.lib.helpers.units import Units from django.contrib.gis.geos import Polygon # determine the width of the map margins (given the current scaling): top, left, bottom, right = self.map_rect.get_margins(self.pil_image) # scale image to its original print size: zoom = self.mapimage.source_print.zoom old_width = self.mapimage.source_print.map_width old_height = self.mapimage.source_print.map_height self.logger.log('old width: %s, old height: %s, zoom: %s' % (old_width, old_height, zoom)) # determine scale factor based on current cropped map: how much should # current image size be increased / decreased? w, h = self.map_rect.get_width(), self.map_rect.get_height() sf_w = (1.0*old_width)/w sf_h = (1.0*old_height)/h self.logger.log( 'scale factor width: %s, scale factor height: %s' % (sf_w, sf_h)) # scale margins: top, left, bottom, right = top*sf_h, left*sf_w, bottom*sf_h, right*sf_w center = self.mapimage.source_print.center northeast = Units.add_pixels_to_latlng( center.clone(), zoom, int(1.0*old_width/2 + left), int(-1.0*old_height/2 - top)) southwest = Units.add_pixels_to_latlng( center.clone(), zoom, int(-1.0*old_width/2 - right), int(1.0*old_height/2 + bottom)) bbox = (northeast.coords, southwest.coords) bbox = [element for tupl in bbox for element in tupl] extents = Polygon.from_bbox(bbox) # punch holes in image: hollowed_image = self.pil_image.copy() mask = Image.new('L', hollowed_image.size, color=255) draw = ImageDraw.Draw(mask) draw.polygon(self.map_rect.to_cv_poly(), fill=0) hollowed_image.putalpha(mask) return northeast, southwest, extents, hollowed_image
def get_extents_from_center_lat_lng(center, zoom, width, height): northeast = Units.add_pixels_to_latlng(center.clone(), zoom, int(width/2), -1*int(height/2)) southwest = Units.add_pixels_to_latlng(center.clone(), zoom, -1*int(width/2), int(height/2)) return Extents(northeast, southwest)