Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

futurize --stage1: first stage of Py2/3 compatibility #122

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions kartograph/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import absolute_import

from kartograph import Kartograph
from kartograph import verbose
from map import projections
from .kartograph import Kartograph
from .kartograph import verbose
from .map import projections

__all__ = ['Kartograph', 'projections', 'verbose']
5 changes: 3 additions & 2 deletions kartograph/cartogram.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"""
computes a circle cartogram for a given svg map + data file
"""
from __future__ import absolute_import
import sys

class Cartogram:
Expand Down Expand Up @@ -61,7 +62,7 @@ def load_csv(self, url, key='id', value='val'):
def compute_radii(self):
import sys, math
minv = 0
maxv = sys.maxint * -1
maxv = sys.maxsize * -1
for c in self.circles:
minv = min(minv, c.value)
maxv = max(maxv, c.value)
Expand Down Expand Up @@ -105,7 +106,7 @@ def layout_step(self, correct=False):
C.move()

def rescale(self):
from geometry import BBox, View
from .geometry import BBox, View
svg = self.svg
svg_view = svg[1][0][0]
vh = float(svg_view['h'])
Expand Down
16 changes: 9 additions & 7 deletions kartograph/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
"""
command line interface for kartograph
"""
from __future__ import print_function
from __future__ import absolute_import

import argparse
import os
import os.path
from options import read_map_config
from .options import read_map_config
import sys


Expand Down Expand Up @@ -37,7 +39,7 @@ def disable(self):
parser.add_argument('--preview', '-p', nargs='?', metavar='', const=True, help='opens the generated svg for preview')
parser.add_argument('--pretty-print', '-P', dest='pretty_print', action='store_true', help='pretty print the svg file')

from kartograph import Kartograph
from .kartograph import Kartograph
import time
import os

Expand Down Expand Up @@ -74,7 +76,7 @@ def render_map(args):
# print str(r)
pass

except Exception, e:
except Exception as e:
print_error(e)
exit(-1)

Expand All @@ -98,17 +100,17 @@ def main():

try:
args = parser.parse_args()
except IOError, e:
except IOError as e:
# parser.print_help()
sys.stderr.write('\n' + str(e) + '\n')
except Exception, e:
except Exception as e:
parser.print_help()
print '\nError:', e
print('\nError:', e)
else:
args.func(args)
elapsed = (time.time() - start)
if args.output != '-':
print 'execution time: %.3f secs' % elapsed
print('execution time: %.3f secs' % elapsed)

sys.exit(0)

Expand Down
9 changes: 5 additions & 4 deletions kartograph/geometry/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
"""
geometry package
"""
from __future__ import absolute_import

__all__ = ['Feature', 'BBox', 'Point', 'View', 'create_feature']

from feature import *
from point import Point
from bbox import BBox
from view import View
from .feature import *
from .point import Point
from .bbox import BBox
from .view import View
11 changes: 6 additions & 5 deletions kartograph/geometry/bbox.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
from __future__ import absolute_import

from point import Point
from .point import Point


class BBox(object):
""" 2D bounding box """
def __init__(self, width=None, height=None, left=0, top=0):
import sys
if width == None:
self.xmin = sys.maxint
self.xmax = sys.maxint * -1
self.xmin = sys.maxsize
self.xmax = sys.maxsize * -1
else:
self.xmin = self.left = left
self.xmax = self.right = left + width
self.width = width
if height == None:
self.ymin = sys.maxint
self.ymax = sys.maxint * -1
self.ymin = sys.maxsize
self.ymax = sys.maxsize * -1
else:
self.ymin = self.top = top
self.ymax = self.bottom = height + top
Expand Down
3 changes: 2 additions & 1 deletion kartograph/geometry/feature/MultiLineFeature.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from Feature import Feature
from __future__ import absolute_import
from .Feature import Feature
from kartograph.simplify.unify import unify_rings


Expand Down
11 changes: 6 additions & 5 deletions kartograph/geometry/feature/MultiPolygonFeature.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from Feature import Feature
from __future__ import absolute_import
from .Feature import Feature
from kartograph.errors import KartographError
from kartograph.simplify.unify import unify_rings

Expand Down Expand Up @@ -113,12 +114,12 @@ def restore_geometry(self, lines, minArea=0):
polygons = []
holes_total = 0
for num_hole in self._topology_num_holes:
ext = ring_iter.next()
island = islands_iter.next()
ext = next(ring_iter)
island = next(islands_iter)
holes = []
while num_hole > 0:
hole = ring_iter.next()
islands_iter.next() # skip island flag for holes
hole = next(ring_iter)
next(islands_iter) # skip island flag for holes
if len(hole) > 3:
holes.append(hole)
holes_total += 1
Expand Down
3 changes: 2 additions & 1 deletion kartograph/geometry/feature/PointFeature.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import absolute_import

from Feature import Feature
from .Feature import Feature


class PointFeature(Feature):
Expand Down
9 changes: 5 additions & 4 deletions kartograph/geometry/feature/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
"""
package geometry.feature
"""
from __future__ import absolute_import

__all__ = ['Feature', 'MultiPolygonFeature', 'create_feature', 'MultiLineFeature', 'PointFeature']

from Feature import Feature
from MultiPolygonFeature import MultiPolygonFeature
from MultiLineFeature import MultiLineFeature
from PointFeature import PointFeature
from .Feature import Feature
from .MultiPolygonFeature import MultiPolygonFeature
from .MultiLineFeature import MultiLineFeature
from .PointFeature import PointFeature


def create_feature(geom, props):
Expand Down
3 changes: 2 additions & 1 deletion kartograph/geometry/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
geometry utils
"""
from __future__ import absolute_import


def is_clockwise(pts):
Expand Down Expand Up @@ -61,7 +62,7 @@ def geom_to_bbox(geom, min_area=0):
def join_features(features, props, buf=False):
""" joins polygonal features
"""
from feature import MultiPolygonFeature, MultiLineFeature
from .feature import MultiPolygonFeature, MultiLineFeature
from shapely.ops import linemerge

if len(features) == 0:
Expand Down
16 changes: 9 additions & 7 deletions kartograph/kartograph.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
from __future__ import print_function
from __future__ import absolute_import

from options import parse_options
from .options import parse_options
from shapely.geometry import Polygon, LineString, MultiPolygon
from errors import *
from .errors import *
from copy import deepcopy
from renderer import SvgRenderer
from mapstyle import MapStyle
from map import Map
from .renderer import SvgRenderer
from .mapstyle import MapStyle
from .map import Map
import os


Expand Down Expand Up @@ -64,14 +66,14 @@ def generate(self, opts, outfile=None, format='svg', preview=None, stylesheet=No
command = commands[sys.platform]
else:
sys.stderr.write('don\'t know how to preview SVGs on your system. Try setting the KARTOGRAPH_PREVIEW environment variable.')
print renderer
print(renderer)
return
renderer.preview(command)
# Write the map to a file or return the renderer instance.
if outfile is None:
return renderer
elif outfile == '-':
print renderer
print(renderer)
else:
renderer.write(outfile)
else:
Expand Down
11 changes: 6 additions & 5 deletions kartograph/layersource/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
- KML ? (only polygons and polylines)
- GeoJSON ?
"""
from __future__ import absolute_import

__all__ = ['LayerSource', 'ShapefileLayer', 'CsvLayer', 'GraticuleLayer', 'PostGISLayer']

from shplayer import ShapefileLayer
from csvlayer import CsvLayer
from postgislayer import PostGISLayer
from layersource import LayerSource
from special import GraticuleLayer, SeaLayer
from .shplayer import ShapefileLayer
from .csvlayer import CsvLayer
from .postgislayer import PostGISLayer
from .layersource import LayerSource
from .special import GraticuleLayer, SeaLayer
from kartograph.errors import *


Expand Down
7 changes: 4 additions & 3 deletions kartograph/layersource/csvlayer.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import absolute_import

from layersource import LayerSource
from .layersource import LayerSource
from kartograph.errors import *
from kartograph.geometry import BBox, create_feature
from shapely.geometry import LineString, Point, Polygon
Expand All @@ -24,7 +25,7 @@ def __init__(self, src, mode, xfield, yfield, dialect, crs):
src = src.encode('ascii', 'ignore')
self.cr = UnicodeReader(open(src), dialect=dialect)
# read csv header
self.header = h = self.cr.next()
self.header = h = next(self.cr)
# initialize CRS
self.proj = None
self.mode = mode
Expand Down Expand Up @@ -100,7 +101,7 @@ def __init__(self, f, dialect=csv.excel, encoding="utf-8", **kwds):
self.reader = csv.reader(f, dialect=dialect, **kwds)

def next(self):
row = self.reader.next()
row = next(self.reader)
return [unicode(s, "utf-8") for s in row]

def __iter__(self):
Expand Down
6 changes: 4 additions & 2 deletions kartograph/layersource/postgislayer.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from __future__ import print_function
from __future__ import absolute_import

from layersource import LayerSource
from .layersource import LayerSource
from kartograph.errors import *
from kartograph.geometry import create_feature
import shapely.wkb
Expand Down Expand Up @@ -76,7 +78,7 @@ def get_features(self, filter=None, bbox=None, verbose=False, ignore_holes=False
try:
meta[fields[f]] = rec[f].decode('utf-8')
except:
print 'decoding error', fields[f], rec[f]
print('decoding error', fields[f], rec[f])
meta[fields[f]] = '--decoding error--'
else:
meta[fields[f]] = rec[f]
Expand Down
10 changes: 6 additions & 4 deletions kartograph/layersource/shplayer.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
from __future__ import print_function
from __future__ import absolute_import

from layersource import LayerSource
from .layersource import LayerSource
from kartograph.errors import *
from kartograph.geometry import BBox, create_feature
from os.path import exists
from osgeo.osr import SpatialReference
import pyproj
import shapefile
from . import shapefile


verbose = False
Expand Down Expand Up @@ -107,7 +109,7 @@ def get_features(self, attr=None, filter=None, bbox=None, ignore_holes=False, mi
break
except:
if verbose:
print 'warning: could not decode "%s" to %s' % (val, enc)
print('warning: could not decode "%s" to %s' % (val, enc))
if not decoded:
raise KartographError('having problems to decode the input data "%s"' % val)
if isinstance(val, (str, unicode)):
Expand All @@ -129,7 +131,7 @@ def get_features(self, attr=None, filter=None, bbox=None, ignore_holes=False, mi
feature = create_feature(geom, props)
res.append(feature)
if bbox is not None and ignored > 0 and verbose:
print "-ignoring %d shapes (not in bounds %s )" % (ignored, bbox)
print("-ignoring %d shapes (not in bounds %s )" % (ignored, bbox))
return res

# # shape2geometry
Expand Down
5 changes: 3 additions & 2 deletions kartograph/layersource/special/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import absolute_import

from graticule import GraticuleLayer
from sea import SeaLayer
from .graticule import GraticuleLayer
from .sea import SeaLayer
Loading