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 toPixels(self, zoom): self.mode = 'pixel' self.northeast = Units.latlng_to_pixel(self.northeast, zoom) self.southwest = Units.latlng_to_pixel(self.southwest, zoom) self.top = self.northeast[1] self.right = self.northeast[0] self.bottom = self.southwest[1] self.left = self.southwest[0]
def toLatLng(self, zoom): self.mode = 'latlng' self.northeast = Units.pixel_to_latlng(self.northeast[1], self.northeast[0], zoom) self.southwest = Units.pixel_to_latlng(self.southwest[1], self.southwest[0], zoom) self.top = self.northeast.y self.right = self.northeast.x self.bottom = self.southwest.y self.left = self.southwest.x
def add_map(self, map_image, is_data_entry=True, has_mini_form=False): y = self.origin_y map_width, map_height = map_image.size if is_data_entry: y = y + self.qr_size + self.gutter_size if has_mini_form: y = y + Units.pixel_to_point(300) self.canvas.drawInlineImage( map_image, self.origin_x, y, Units.pixel_to_point(map_width), Units.pixel_to_point(map_height))
def add_form(self, num_rows, form, print_object, is_mini_form=False): cols = print_object.get_form_field_layout() field_aliases, field_widths = ['ID'], [5] field_aliases.extend([c.field.col_alias for c in cols]) field_widths.extend([c.width for c in cols]) field_widths = [n/100.0*self.inner_width for n in field_widths] #normalize x, y = self.origin_x, self.origin_y + self.qr_size width = self.inner_width height = self.inner_height - self.qr_size - 35 if is_mini_form: height = Units.pixel_to_point(300) #only render a 300-pixel tall form data, rowheights, header_flowables = [], [39], [] style = ParagraphStyle(name='Helvetica', fontName='Helvetica', fontSize=10) for a in field_aliases: header_flowables.append(Paragraph('<b>%s</b>' % a, style)) data.append(header_flowables) for n in range(0, num_rows): data.append(['' for n in field_widths]) rowheights.append(39) t=Table(data, field_widths, rowheights) GRID_STYLE = TableStyle([ ('GRID', (0,0), (-1,-1), 0.25, colors.black), ('FONT', (0,0), (-1,-1), 'HandSean'), ('BOX',(0,0),(-1,-1),2,colors.black) ]) t.setStyle(GRID_STYLE) frame = Frame(x, y, width, height, showBoundary=0, leftPadding=0, bottomPadding=0, rightPadding=0, topPadding=0) frame.addFromList([t], self.canvas)
def get_pixel_coordinates_from_models(self, models, zoom, minXPixels, minYPixels): pixels = [] for model in models: if model.point is not None: pixel = Units.latlng_to_pixel(model.point, zoom) pixel = (pixel[0] - minXPixels, pixel[1] - minYPixels) pixels.append(pixel) return pixels
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 add_footer(self, qr_image, uuID, instructions): # output QR-code: self.qr_size = Units.pixel_to_point(qr_image.size[0]) x = self.origin_x + self.inner_width - self.qr_size #align right self.canvas.drawInlineImage(qr_image, x, self.origin_y, self.qr_size, self.qr_size) # output Print ID: items = [] style = ParagraphStyle(name='Helvetica', fontName='Helvetica', fontSize=10) items.append(Paragraph('Print ID: ' + uuID, style)) frame = Frame(x, self.origin_y-self.qr_size, self.inner_width-self.qr_size, self.qr_size, showBoundary=0, leftPadding=0, bottomPadding=0, rightPadding=0, topPadding=0) frame.addFromList(items, self.canvas) # output default instructions: link_text = "When you're done drawing on the map, scan or photograph it and \ submit it to our website: http://localground.org/upload, or \ email it to [email protected]." items = [] style = ParagraphStyle( name='Helvetica', fontName='Helvetica', fontSize=8, backColor=colors.HexColor(0xEEEEEE), borderColor=colors.HexColor(0xEEEEEE), leftIndent=0, rightIndent=0, spaceBefore=5, spaceAfter=5, borderWidth=5) items.append(Paragraph(link_text, style)) padding_x, padding_y = -4, 12 width, height = self.inner_width - self.qr_size, self.qr_size frame = Frame(self.origin_x+padding_x, self.origin_y+3, width, height, showBoundary=0) frame.addFromList(items, self.canvas) # output custom instructions: items = [] style = ParagraphStyle(name='Helvetica', fontName='Helvetica', fontSize=10) items.append(Paragraph(instructions, style)) frame = Frame(self.origin_x+padding_x-2, self.origin_y-padding_y, width, height-15, showBoundary=0) frame.addFromList(items, self.canvas)
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)