Exemple #1
0
class TestTransform(object):
    def setup(self):
        self.src_img = ImageSource(create_debug_img((200, 200), transparent=False))
        self.src_srs = SRS(31467)
        self.dst_size = (100, 150)
        self.dst_srs = SRS(4326)
        self.dst_bbox = (0.2, 45.1, 8.3, 53.2)
        self.src_bbox = self.dst_srs.transform_bbox_to(self.src_srs, self.dst_bbox)
    def test_transform(self, mesh_div=4):
        transformer = ImageTransformer(self.src_srs, self.dst_srs, mesh_div=mesh_div)
        result = transformer.transform(self.src_img, self.src_bbox, self.dst_size, self.dst_bbox,
            image_opts=ImageOptions(resampling='nearest'))
        assert isinstance(result, ImageSource)
        assert result.as_image() != self.src_img
        assert result.size == (100, 150)
    
    def _test_compare_mesh_div(self):
        """
        Create transformations with different div values.
        """
        for div in [1, 2, 4, 6, 8, 12, 16]:
            transformer = ImageTransformer(self.src_srs, self.dst_srs, mesh_div=div)
            result = transformer.transform(self.src_img, self.src_bbox,
                                           self.dst_size, self.dst_bbox)
            result.as_image().save('/tmp/transform-%d.png' % (div,))
Exemple #2
0
class TestTransform(object):
    def setup(self):
        self.src_img = ImageSource(create_debug_img((200, 200), transparent=False))
        self.src_srs = SRS(31467)
        self.dst_size = (100, 150)
        self.dst_srs = SRS(4326)
        self.dst_bbox = (0.2, 45.1, 8.3, 53.2)
        self.src_bbox = self.dst_srs.transform_bbox_to(self.src_srs, self.dst_bbox)
    def test_transform(self, mesh_div=4):
        transformer = ImageTransformer(self.src_srs, self.dst_srs, mesh_div=mesh_div)
        result = transformer.transform(self.src_img, self.src_bbox, self.dst_size, self.dst_bbox,
            image_opts=ImageOptions(resampling='nearest'))
        assert isinstance(result, ImageSource)
        assert result.as_image() != self.src_img
        assert result.size == (100, 150)

    def _test_compare_mesh_div(self):
        """
        Create transformations with different div values.
        """
        for div in [1, 2, 4, 6, 8, 12, 16]:
            transformer = ImageTransformer(self.src_srs, self.dst_srs, mesh_div=div)
            result = transformer.transform(self.src_img, self.src_bbox,
                                           self.dst_size, self.dst_bbox)
            result.as_image().save('/tmp/transform-%d.png' % (div,))
Exemple #3
0
def transform_bbox():
    source = SRS(request.json.get('source'))
    dest = SRS(request.json.get('dest'))
    bbox = source.align_bbox(request.json.get('bbox'))

    if grid.is_valid_transformation(bbox, source, dest):
        transformed_bbox = source.transform_bbox_to(dest, bbox)
        return {'bbox': transformed_bbox}
    else:
        response.status = 400
        return {'error': 'Could not transform bbox'}
Exemple #4
0
class TestTransform(object):
    def setup(self):
        self.src_img = ImageSource(
            create_debug_img((200, 200), transparent=False))
        self.src_srs = SRS(31467)
        self.dst_size = (100, 150)
        self.dst_srs = SRS(4326)
        self.dst_bbox = (0.2, 45.1, 8.3, 53.2)
        self.src_bbox = self.dst_srs.transform_bbox_to(self.src_srs,
                                                       self.dst_bbox)

    def test_transform(self):
        transformer = ImageTransformer(self.src_srs, self.dst_srs)
        result = transformer.transform(
            self.src_img,
            self.src_bbox,
            self.dst_size,
            self.dst_bbox,
            image_opts=ImageOptions(resampling="nearest"),
        )
        assert isinstance(result, ImageSource)
        assert result.as_image() != self.src_img.as_image()
        assert result.size == (100, 150)

    def _test_compare_max_px_err(self):
        """
        Create transformations with different div values.
        """
        for err in [0.2, 0.5, 1, 2, 4, 6, 8, 12, 16]:
            transformer = ImageTransformer(self.src_srs,
                                           self.dst_srs,
                                           max_px_err=err)
            result = transformer.transform(
                self.src_img,
                self.src_bbox,
                self.dst_size,
                self.dst_bbox,
                image_opts=ImageOptions(resampling="nearest"),
            )
            result.as_image().save("/tmp/transform-%03d.png" % (err * 10, ))
class ConfigGeoJSONGrid(object):

    def __init__(self, request_bbox=[], grid_bbox=[], level=None,
                 grid_srs=None, grid_bbox_srs=None, map_srs=None, res=[],
                 scales=[], origin='ll', units='m', dpi=None):

        self.grid_srs = SRS(grid_srs) if grid_srs else None
        self.grid_bbox_srs = SRS(grid_bbox_srs) if grid_bbox_srs else None
        self.map_srs = SRS(map_srs) if map_srs else None
        self.request_bbox = map(float, request_bbox) if request_bbox else None
        self.origin = origin
        self._res = list(map(float, res)) if res else None
        self._scales = list(map(float, scales)) if scales else None
        self._units = 1 if units == 'm' else constants.UNIT_FACTOR
        self._dpi = float(dpi) if dpi else constants.OGC_DPI

        _grid_bbox = list(map(float, grid_bbox)) if grid_bbox else None
        self.grid_bbox = self.transform_grid_bbox(_grid_bbox)

        if self._res:
            self._num_levels = len(self._res)
        elif self._scales:
            self._num_levels = len(self._scales)
        else:
            self._num_levels = None

        try:
            self.level = int(level)
        except TypeError:
            self.level = None
        if self.grid_srs:
            self.tilegrid = tile_grid(srs=self.grid_srs,
                                      bbox=_grid_bbox,
                                      bbox_srs=self.grid_bbox_srs,
                                      origin=self.origin,
                                      res=self.res,
                                      num_levels=self._num_levels)
            self.validate_tile_bbox_for_level_0()
        else:
            self.tilegrid = None

    def transform_grid_bbox(self, _grid_bbox=None):
        if not _grid_bbox:
            return None
        if self.grid_bbox_srs:
            _grid_bbox = self.grid_bbox_srs.align_bbox(_grid_bbox)
            if self.grid_srs:
                if is_valid_transformation(_grid_bbox, self.grid_bbox_srs, self.grid_srs):
                    return self.grid_bbox_srs.transform_bbox_to(self.grid_srs, _grid_bbox)
                else:
                    raise InvalidGridBBoxTransformationException(
                        'Invalid transformation for grid_bbox')
        return _grid_bbox if _grid_bbox else self._grid_bbox

    def validate_tile_bbox_for_level_0(self):
        tile_bbox = self.tilegrid.tile_bbox((0, 0, 0))
        if not is_valid_transformation(tile_bbox, self.grid_srs, self.map_srs):
            raise InvalidTileBBoxTransformationException(
                'Invalid transformation for tile in level 0')

    @property
    def res(self):
        if not self._res:
            if not self._scales:
                return None
            return [scale_to_res(scale, self._dpi, self._units) for scale in self._scales]
        return self._res

    @property
    def map_bbox(self):
        if not self.map_srs or not self.grid_srs:
            return None

        if self.map_srs == 'EPSG:4326':
            self.request_bbox = self.map_srs.align_bbox(self.request_bbox)
        else:
            (minx, miny, maxx, maxy) = self.request_bbox
            self.request_bbox = minx, miny, maxx, maxy

        if not is_valid_transformation(self.request_bbox, self.map_srs, self.grid_srs):
            return None

        temp = self.map_srs.transform_bbox_to(self.grid_srs, self.request_bbox)
        return temp

    @property
    def view_bbox(self):
        gridbbox = self.grid_bbox if self.grid_bbox else self.tilegrid.bbox()
        mapbbox = self.map_bbox
        if not mapbbox:
            return gridbbox
        return [
            max(gridbbox[0], mapbbox[0]),
            max(gridbbox[1], mapbbox[1]),
            min(gridbbox[2], mapbbox[2]),
            min(gridbbox[3], mapbbox[3])
        ]

    @property
    def scales(self):
        return [res_to_scale(res, self._dpi, self._units) for res in self.tilegrid.resolutions]
Exemple #6
0
class ConfigGeoJSONGrid(object):
    def __init__(self,
                 request_bbox=[],
                 grid_bbox=[],
                 level=None,
                 grid_srs=None,
                 grid_bbox_srs=None,
                 map_srs=None,
                 res=[],
                 scales=[],
                 origin='ll',
                 units='m',
                 dpi=None):

        self.grid_srs = SRS(grid_srs) if grid_srs else None
        self.grid_bbox_srs = SRS(grid_bbox_srs) if grid_bbox_srs else None
        self.map_srs = SRS(map_srs) if map_srs else None
        self.request_bbox = map(float, request_bbox) if request_bbox else None
        self.origin = origin
        self._res = list(map(float, res)) if res else None
        self._scales = list(map(float, scales)) if scales else None
        self._units = 1 if units == 'm' else constants.UNIT_FACTOR
        self._dpi = float(dpi) if dpi else constants.OGC_DPI

        _grid_bbox = list(map(float, grid_bbox)) if grid_bbox else None
        self.grid_bbox = self.transform_grid_bbox(_grid_bbox)

        if self._res:
            self._num_levels = len(self._res)
        elif self._scales:
            self._num_levels = len(self._scales)
        else:
            self._num_levels = None

        try:
            self.level = int(level)
        except TypeError:
            self.level = None
        if self.grid_srs:
            self.tilegrid = tile_grid(srs=self.grid_srs,
                                      bbox=_grid_bbox,
                                      bbox_srs=self.grid_bbox_srs,
                                      origin=self.origin,
                                      res=self.res,
                                      num_levels=self._num_levels)
            self.validate_tile_bbox_for_level_0()
        else:
            self.tilegrid = None

    def transform_grid_bbox(self, _grid_bbox=None):
        if not _grid_bbox:
            return None
        if self.grid_bbox_srs:
            _grid_bbox = self.grid_bbox_srs.align_bbox(_grid_bbox)
            if self.grid_srs:
                if is_valid_transformation(_grid_bbox, self.grid_bbox_srs,
                                           self.grid_srs):
                    return self.grid_bbox_srs.transform_bbox_to(
                        self.grid_srs, _grid_bbox)
                else:
                    raise InvalidGridBBoxTransformationException(
                        'Invalid transformation for grid_bbox')
        return _grid_bbox if _grid_bbox else self._grid_bbox

    def validate_tile_bbox_for_level_0(self):
        tile_bbox = self.tilegrid.tile_bbox((0, 0, 0))
        if not is_valid_transformation(tile_bbox, self.grid_srs, self.map_srs):
            raise InvalidTileBBoxTransformationException(
                'Invalid transformation for tile in level 0')

    @property
    def res(self):
        if not self._res:
            if not self._scales:
                return None
            return [
                scale_to_res(scale, self._dpi, self._units)
                for scale in self._scales
            ]
        return self._res

    @property
    def map_bbox(self):
        if not self.map_srs or not self.grid_srs:
            return None

        if self.map_srs == 'EPSG:4326':
            self.request_bbox = self.map_srs.align_bbox(self.request_bbox)
        else:
            (minx, miny, maxx, maxy) = self.request_bbox
            self.request_bbox = minx, miny, maxx, maxy

        if not is_valid_transformation(self.request_bbox, self.map_srs,
                                       self.grid_srs):
            return None

        temp = self.map_srs.transform_bbox_to(self.grid_srs, self.request_bbox)
        return temp

    @property
    def view_bbox(self):
        gridbbox = self.grid_bbox if self.grid_bbox else self.tilegrid.bbox()
        mapbbox = self.map_bbox
        if not mapbbox:
            return gridbbox
        return [
            max(gridbbox[0], mapbbox[0]),
            max(gridbbox[1], mapbbox[1]),
            min(gridbbox[2], mapbbox[2]),
            min(gridbbox[3], mapbbox[3])
        ]

    @property
    def scales(self):
        return [
            res_to_scale(res, self._dpi, self._units)
            for res in self.tilegrid.resolutions
        ]