# migurski/Skeletron

Replaced sys.stderr printing with logging

1 parent 4a365a3 commit 2e63f3f6f82181244b84fa9ce427a72a2fe8ea25 committed Nov 28, 2012
Showing with 22 additions and 20 deletions.
1. +9 −12 Skeletron/__init__.py
2. +3 −2 Skeletron/input.py
3. +5 −3 Skeletron/output.py
4. +5 −3 skeletron-osm-route-rels.py
 @@ -28,15 +28,16 @@ """ __version__ = '0.5.1' -from sys import stderr from subprocess import Popen, PIPE from itertools import combinations from tempfile import mkstemp from os import write, close from math import sin, cos, pi from math import ceil, atan2 from time import time + import signal +import logging import numpy, numpy.linalg from shapely.geometry import Point, LineString, Polygon, MultiLineString, MultiPolygon @@ -89,14 +90,12 @@ def multiline_centerline(multiline, buffer=20, density=10, min_length=40, min_ar return False geoms = hasattr(multiline, 'geoms') and multiline.geoms or [multiline] - counts = [len(geom.coords) for geom in geoms] - - print >> stderr, ' ', len(geoms), 'linear parts with', sum(counts), 'points', + pre_counts = [len(geom.coords) for geom in geoms] geoms = [simplify_line_dp(list(geom.coords), buffer) for geom in geoms] counts = [len(geom) for geom in geoms] - print >> stderr, 'reduced to', sum(counts), 'points.' + logging.debug('simplified %d points to %d in %d linestrings' % (sum(pre_counts), sum(counts), len(geoms))) multiline = MultiLineString(geoms) multipoly = multiline_polygon(multiline, buffer) @@ -116,7 +115,7 @@ def multiline_centerline(multiline, buffer=20, density=10, min_length=40, min_ar # QHull failures here are usually signs of tiny geometries, # so they are usually fine to ignore completely and move on. # - print >> stderr, ' -QHull failure:', e + logging.error('QHull failure: %s' % e) handle, fname = mkstemp(dir='.', prefix='qhull-failure-', suffix='.txt') write(handle, 'Error: %s\nDensity: %.6f\nPolygon: %s\n' % (e, density, str(polygon))) @@ -133,7 +132,7 @@ def multiline_centerline(multiline, buffer=20, density=10, min_length=40, min_ar points += sum(map(len, routes)) lines.extend([simplify_line_vw(route, min_area) for route in routes]) - print >> stderr, ' ', points, 'centerline points reduced to', sum(map(len, lines)), 'final points.' + logging.debug('selected %d final points from %d graph route points' % (sum(map(len, lines)), points)) if not lines: return False @@ -302,6 +301,8 @@ def polygon_skeleton_graphs(polygon, buffer=20, density=10): while point_lists: points1, points2, poly1, poly2 = divide_points(point_lists.pop(0)) + logging.debug('split %d points into %d + %d' % (len(points1 + points2), len(points1), len(points2))) + for (_points, _poly) in ((points1, poly1), (points2, poly2)): if len(_points) < max_points: _poly = _poly.buffer(buffer, 3).intersection(polygon) @@ -366,17 +367,13 @@ def divide_points(points): polygon1 = Polygon([(x, y) for (x, y) in bbox1.T]) polygon2 = Polygon([(x, y) for (x, y) in bbox2.T]) - print >> stderr, ' ', len(points), 'points split along', int(180 * theta / pi), 'degree axis' - return points1, points2, polygon1, polygon2 def polygon_dots_skeleton(polygon, points): ''' ''' skeleton = Graph() - print >> stderr, ' ', len(points), 'perimeter points', - rbox = '\n'.join( ['2', str(len(points))] + ['%.2f %.2f' % (x, y) for (x, y) in points] + [''] ) qvoronoi = Popen('qvoronoi o'.split(), stdin=PIPE, stdout=PIPE) @@ -417,7 +414,7 @@ def polygon_dots_skeleton(polygon, points): skeleton.remove_node(index) removing = True - print >> stderr, 'contain', len(skeleton.edge), 'internal edges.' + logging.debug('found %d skeleton edges' % len(skeleton.edge)) return skeleton
 @@ -1,5 +1,6 @@ from copy import deepcopy from xml.parsers.expat import ParserCreate +from logging import debug def name_key(tags): """ Convert way tags to name keys. @@ -156,7 +157,7 @@ def parse_route_relation_waynodes(input, merge_highways): rel_ways[len(rel_ways)] = rel_way - print len(rel_ways), 'rel_ways', len(nodes), 'nodes' + debug('%d rel_ways, %d nodes' % (len(rel_ways), len(nodes))) if merge_highways == 'largest': # @@ -168,7 +169,7 @@ def parse_route_relation_waynodes(input, merge_highways): highway = net_refs[(network, ref, modifier)] rel_ways[key]['key'] = network, ref, modifier, highway - print len(rel_ways), 'rel_ways', len(nodes), 'nodes' + debug('%d rel_ways, %d nodes' % (len(rel_ways), len(nodes))) return rel_ways, nodes
 @@ -1,8 +1,9 @@ -from sys import stderr from pickle import dumps as pickleit from tempfile import mkstemp from os import write, close +import logging + from shapely.geometry import LineString from . import multiline_centerline, mercator, _GraphRoutesOvertime @@ -13,7 +14,8 @@ def multilines_geojson(multilines, key_properties, buffer, density, min_length, geojson = dict(type='FeatureCollection', features=[]) for (key, multiline) in sorted(multilines.items()): - print >> stderr, ', '.join(key).encode('ascii', 'ignore'), '...' + + logging.info('%s...' % ', '.join(key).encode('ascii', 'ignore')) try: centerline = multiline_centerline(multiline, buffer, density, min_length, min_area) @@ -24,7 +26,7 @@ def multilines_geojson(multilines, key_properties, buffer, density, min_length, # and therefore most or all of a complex multiline. We'll keep the # key and a pickled copy of the offending graph. # - print >> stderr, ' -Graph routes went overtime.' + logging.error('Graph routes went overtime') handle, fname = mkstemp(dir='.', prefix='graph-overtime-', suffix='.txt') write(handle, repr(key) + '\n' + pickleit(e.graph))
 @@ -6,14 +6,17 @@ More on route relations: http://wiki.openstreetmap.org/wiki/Relation:route """ -from sys import argv, stdin, stderr, stdout +from sys import argv, stdin, stdout from itertools import combinations from optparse import OptionParser from csv import DictReader from re import compile from json import dump from math import pi +import logging +logging.basicConfig(level=logging.DEBUG, format='%(levelname)08s - %(message)s') + from Skeletron import waynode_multilines from Skeletron.input import parse_route_relation_waynodes from Skeletron.output import multilines_geojson @@ -71,8 +74,7 @@ def key_properties((network, ref, modifier, highway)): return dict(network=network, ref=ref, modifier=modifier, highway=highway, zoomlevel=options.zoom, pixelwidth=options.width) - print >> stderr, 'Buffer: %(buffer).1f, density: %(density).1f, minimum length: %(min_length).1f, minimum area: %(min_area).1f.' % kwargs - print >> stderr, '-' * 20 + logging.info('Buffer: %(buffer).1f, density: %(density).1f, minimum length: %(min_length).1f, minimum area: %(min_area).1f.' % kwargs) geojson = multilines_geojson(multilines, key_properties, **kwargs) output = open_file(output_file, 'w')