Skip to content

Commit

Permalink
fixed Result image wrong resolution
Browse files Browse the repository at this point in the history
fix on this:
sethoscope#58

We ignore margin requirement and increase it to fit exact image dimension if it required(i suppose margin can only be increased, but not sure). Also fix not works it we not split common padding variable on padding.x and padding.y
  • Loading branch information
Opostol committed Nov 2, 2018
1 parent 1133acd commit 7ab1247
Showing 1 changed file with 19 additions and 13 deletions.
32 changes: 19 additions & 13 deletions heatmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import logging
import math
from PIL import Image
from PIL import PngImagePlugin

This comment has been minimized.

Copy link
@hugovk

hugovk Nov 3, 2018

This import is unused

from PIL import ImageColor
from itertools import chain
import tempfile
Expand Down Expand Up @@ -178,15 +179,15 @@ def auto_set_scale(self, extent_in, padding, width=None, height=None):
SCALE_FACTOR = 1000000.0
self.pixels_per_degree = SCALE_FACTOR
extent_out = extent_in.map(self.project)
padding *= 2 # padding-per-edge -> padding-in-each-dimension
# padding *= 2 # padding-per-edge -> padding-in-each-dimension
try:
if height:
self.pixels_per_degree = pixels_per_lat = (
float(height - padding) /
float(height - padding[1] * 2) /
extent_out.size().y * SCALE_FACTOR)
if width:
self.pixels_per_degree = (
float(width - padding) /
float(width - padding[0] * 2) /
extent_out.size().x * SCALE_FACTOR)
if height:
self.pixels_per_degree = min(self.pixels_per_degree,
Expand Down Expand Up @@ -274,10 +275,10 @@ def size(self):
self.max.y - self.min.y)

def grow(self, pad):
self.min.x -= pad
self.min.y -= pad
self.max.x += pad
self.max.y += pad
self.min.x -= pad[0]
self.min.y -= pad[1]
self.max.x += pad[0]
self.max.y += pad[1]

def resize(self, width=None, height=None):
if width:
Expand Down Expand Up @@ -729,10 +730,10 @@ def choose_osm_zoom(config, padding):
bbox_crazy_xy = config.extent_in.map(proj.project)
if config.width:
size_ratio = width_ratio = (
float(bbox_crazy_xy.size().x) / (config.width - 2 * padding))
float(bbox_crazy_xy.size().x) / (config.width - 2 * padding[0]))
if config.height:
size_ratio = (
float(bbox_crazy_xy.size().y) / (config.height - 2 * padding))
float(bbox_crazy_xy.size().y) / (config.height - 2 * padding[1]))
if config.width:
size_ratio = max(size_ratio, width_ratio)
# TODO: We use --height and --width as upper bounds, choosing a zoom
Expand All @@ -752,6 +753,10 @@ def get_osm_background(config, padding):
proj.pixels_per_degree = _scale_for_osm_zoom(zoom)
bbox_xy = config.extent_in.map(proj.project)
# We're not checking that the padding fits within the specified size.
if config.width:
padding[0] = (config.width - bbox_xy.size().x) / 2
if config.height:
padding[1] = (config.height - bbox_xy.size().y) / 2
bbox_xy.grow(padding)
bbox_ll = bbox_xy.map(proj.inverse_project)
image, img_bbox_ll = _get_osm_image(bbox_ll, zoom, config.osm_base)
Expand All @@ -764,8 +769,8 @@ def get_osm_background(config, padding):
image = image.crop((
int(offset.x),
int(offset.y),
int(offset.x + bbox_xy.size().x + 1),
int(offset.y + bbox_xy.size().y + 1)))
int(offset.x + bbox_xy.size().x),
int(offset.y + bbox_xy.size().y)))
config.background_image = image
config.extent_in = bbox_ll
config.projection = proj
Expand Down Expand Up @@ -1193,15 +1198,16 @@ def fill_missing(self):
if not self.shapes:
raise ValueError('no input specified')

padding = self.margin + self.kernel.radius
padding = [self.margin + self.kernel.radius] * 2 # [x, y]. Can be changed if osm set

if not self.extent_in:
logging.debug('reading input data')
self.shapes = list(self.shapes)
logging.debug('read %d shapes' % len(self.shapes))
self.extent_in = Extent(shapes=self.shapes)

if self.osm:
get_osm_background(self, padding)
get_osm_background(self, padding) # padding can be changed inside
else:
if not self.projection.is_scaled():
self.projection.auto_set_scale(self.extent_in, padding,
Expand Down

0 comments on commit 7ab1247

Please sign in to comment.