Skip to content

Commit

Permalink
Use absolute_import, division, print_function, unicode_literals from …
Browse files Browse the repository at this point in the history
…__future__
  • Loading branch information
sfllaw committed Oct 24, 2012
1 parent 8195347 commit 48d8c97
Show file tree
Hide file tree
Showing 22 changed files with 110 additions and 13 deletions.
4 changes: 4 additions & 0 deletions gdal2mbtiles/__init__.py
@@ -0,0 +1,4 @@
# -*- coding: utf-8 -*-

from __future__ import (absolute_import, division, print_function,
unicode_literals)
6 changes: 6 additions & 0 deletions gdal2mbtiles/constants.py
@@ -1,3 +1,9 @@
# -*- coding: utf-8 -*-

from __future__ import (absolute_import, division, print_function,
unicode_literals)


# EPSG constants
EPSG_WEB_MERCATOR = 3785

Expand Down
5 changes: 5 additions & 0 deletions gdal2mbtiles/exceptions.py
@@ -1,3 +1,8 @@
# -*- coding: utf-8 -*-

from __future__ import (absolute_import, division, print_function,
unicode_literals)

from subprocess import CalledProcessError


Expand Down
12 changes: 9 additions & 3 deletions gdal2mbtiles/gdal.py
@@ -1,4 +1,7 @@
from __future__ import absolute_import, division
# -*- coding: utf-8 -*-

from __future__ import (absolute_import, division, print_function,
unicode_literals)

from collections import OrderedDict
import errno
Expand Down Expand Up @@ -332,6 +335,9 @@ def __init__(self, inputfile, mode=GA_ReadOnly):
"""
# Open the input file and read some metadata
open(inputfile, 'r').close() # HACK: GDAL gives a useless exception

if isinstance(inputfile, unicode):
inputfile = inputfile.encode('utf-8')
try:
# Since this is a SWIG object, clone the ``this`` pointer
self.this = gdal.Open(inputfile, mode).this
Expand Down Expand Up @@ -567,9 +573,9 @@ def GetEPSGString(self):
return

if self.IsGeographic() == 1:
cstype = 'GEOGCS'
cstype = b'GEOGCS'
else:
cstype = 'PROJCS'
cstype = b'PROJCS'
return '{0}:{1}'.format(self.GetAuthorityName(cstype),
self.GetAuthorityCode(cstype))

Expand Down
3 changes: 2 additions & 1 deletion gdal2mbtiles/helpers.py
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-

from __future__ import absolute_import
from __future__ import (absolute_import, division, print_function,
unicode_literals)

from tempfile import NamedTemporaryFile

Expand Down
7 changes: 6 additions & 1 deletion gdal2mbtiles/mbtiles.py
@@ -1,3 +1,8 @@
# -*- coding: utf-8 -*-

from __future__ import (absolute_import, division, print_function,
unicode_literals)

from distutils.version import LooseVersion
import errno
import os
Expand Down Expand Up @@ -437,7 +442,7 @@ def insert(self, x, y, z, hashed, data=None):
"""
# tile_id must be a 64-bit signed integer, but hashing functions
# produce unsigned integers.
hashed = unpack('q', pack('Q', hashed & 0xffffffffffffffff))[0]
hashed = unpack(b'q', pack(b'Q', hashed & 0xffffffffffffffff))[0]

with self._conn:
if data is not None:
Expand Down
5 changes: 5 additions & 0 deletions gdal2mbtiles/pool.py
@@ -1,3 +1,8 @@
# -*- coding: utf-8 -*-

from __future__ import (absolute_import, division, print_function,
unicode_literals)

from collections import deque
from functools import wraps
from multiprocessing import cpu_count, Pipe, Process, TimeoutError
Expand Down
7 changes: 4 additions & 3 deletions gdal2mbtiles/renderers.py
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-

from __future__ import absolute_import
from __future__ import (absolute_import, division, print_function,
unicode_literals)

import os
from subprocess import check_call
Expand Down Expand Up @@ -60,7 +61,7 @@ def __init__(self, compression=None, profile=None, **kwargs):
super(JpegRenderer, self).__init__(**kwargs)

@property
def _options(self):
def _vips_options(self):
return ':{compression:d},{profile}'.format(
compression=self.compression,
profile=self.profile,
Expand All @@ -73,7 +74,7 @@ def render(self, image):
image = image.extract_bands(band=0, nbands=3)
with NamedTemporaryFile(suffix=self.suffix,
dir=self.tempdir) as tempfile:
image.vips2jpeg(tempfile.name + self._options)
image.vips2jpeg(tempfile.name + self._vips_options)
return tempfile.read()


Expand Down
3 changes: 2 additions & 1 deletion gdal2mbtiles/storages.py
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-

from __future__ import absolute_import
from __future__ import (absolute_import, division, print_function,
unicode_literals)

from collections import defaultdict
from functools import partial
Expand Down
5 changes: 5 additions & 0 deletions gdal2mbtiles/types.py
@@ -1,3 +1,8 @@
# -*- coding: utf-8 -*-

from __future__ import (absolute_import, division, print_function,
unicode_literals)

from collections import namedtuple

import webcolors
Expand Down
5 changes: 5 additions & 0 deletions gdal2mbtiles/utils.py
@@ -1,3 +1,8 @@
# -*- coding: utf-8 -*-

from __future__ import (absolute_import, division, print_function,
unicode_literals)

from contextlib import contextmanager
import errno
from hashlib import md5
Expand Down
19 changes: 17 additions & 2 deletions gdal2mbtiles/vips.py
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-

from __future__ import absolute_import, division
from __future__ import (absolute_import, division, print_function,
unicode_literals)

from ctypes import c_int, cdll
from math import ceil
Expand Down Expand Up @@ -63,6 +64,10 @@ def __init__(self, *args, **kwargs):
if VIPS.get_concurrency() == 0:
# Override auto-detection from environ and argv.
VIPS.set_concurrency(processes=cpu_count())
if args and isinstance(args[0], unicode):
# args[0] is a Unicode filename
args = list(args)
args[0] = args[0].encode('utf-8')
super(VImage, self).__init__(*args, **kwargs)

@classmethod
Expand All @@ -75,7 +80,7 @@ def new_rgba(cls, width, height, ink=None):
xres, yres = 2.835, 2.835 # Arbitrary 600 dpi
xo, yo = 0, 0

image = cls("", "p") # Working buffer
image = cls(b"", b"p") # Working buffer
image.initdesc(width, height, bands, bandfmt, coding, _type,
xres, yres, xo, yo)

Expand Down Expand Up @@ -257,6 +262,16 @@ def tms_align(self, tile_width, tile_height, offset):
# Resize
return self.from_vimage(self.embed(_type, x, y, width, height))

def vips2jpeg(self, out):
if isinstance(out, unicode):
out = out.encode('utf-8')
return super(VImage, self).vips2jpeg(out)

def vips2png(self, out):
if isinstance(out, unicode):
out = out.encode('utf-8')
return super(VImage, self).vips2png(out)


class TmsTiles(object):
"""Represents a set of tiles in TMS co-ordinates."""
Expand Down
3 changes: 3 additions & 0 deletions scripts/__init__.py
@@ -1 +1,4 @@
# -*- coding: utf-8 -*-

from __future__ import (absolute_import, division, print_function,
unicode_literals)
5 changes: 4 additions & 1 deletion scripts/gdal2mbtiles
@@ -1,4 +1,5 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Quickstart
# ----------
Expand All @@ -14,7 +15,9 @@
# You can also pipe in any GDAL-readable file:
# $ cat input.tiff | gdal2mbtiles.py > output.mbtiles

from __future__ import absolute_import
from __future__ import (absolute_import, division, print_function,
unicode_literals)


import argparse
from contextlib import contextmanager
Expand Down
5 changes: 4 additions & 1 deletion tests/__init__.py
@@ -1,4 +1,7 @@
from __future__ import absolute_import
# -*- coding: utf-8 -*-

from __future__ import (absolute_import, division, print_function,
unicode_literals)

from .test_gdal import *
from .test_helpers import *
Expand Down
3 changes: 3 additions & 0 deletions tests/test_gdal.py
@@ -1,5 +1,8 @@
# -*- coding: utf-8 -*-

from __future__ import (absolute_import, division, print_function,
unicode_literals)

from math import log
import os
import subprocess
Expand Down
5 changes: 5 additions & 0 deletions tests/test_helpers.py
@@ -1,3 +1,8 @@
# -*- coding: utf-8 -*-

from __future__ import (absolute_import, division, print_function,
unicode_literals)

import os
from tempfile import NamedTemporaryFile
import unittest
Expand Down
5 changes: 5 additions & 0 deletions tests/test_mbtiles.py
@@ -1,3 +1,8 @@
# -*- coding: utf-8 -*-

from __future__ import (absolute_import, division, print_function,
unicode_literals)

import errno
import os
from tempfile import NamedTemporaryFile
Expand Down
3 changes: 3 additions & 0 deletions tests/test_renderers.py
@@ -1,5 +1,8 @@
# -*- coding: utf-8 -*-

from __future__ import (absolute_import, division, print_function,
unicode_literals)

import unittest

from gdal2mbtiles.renderers import JpegRenderer, PngRenderer, TouchRenderer
Expand Down
3 changes: 3 additions & 0 deletions tests/test_storages.py
@@ -1,5 +1,8 @@
# -*- coding: utf-8 -*-

from __future__ import (absolute_import, division, print_function,
unicode_literals)

import errno
import os
from shutil import rmtree
Expand Down
5 changes: 5 additions & 0 deletions tests/test_types.py
@@ -1,3 +1,8 @@
# -*- coding: utf-8 -*-

from __future__ import (absolute_import, division, print_function,
unicode_literals)

import unittest

from gdal2mbtiles.types import rgba
Expand Down
5 changes: 5 additions & 0 deletions tests/test_vips.py
@@ -1,3 +1,8 @@
# -*- coding: utf-8 -*-

from __future__ import (absolute_import, division, print_function,
unicode_literals)

import unittest

from gdal2mbtiles.constants import TILE_SIDE
Expand Down

0 comments on commit 48d8c97

Please sign in to comment.