All pastes #1532241 Raw Edit

patch

public diff v1 · immutable
#1532241 ·published 2009-08-17 07:25 UTC
rendered paste body
svn diff lakewalker.py > patch.diffprizren@prizren:~/osm/osm/applications/utils/import/lakewalker$ cat patch.diff Index: lakewalker.py===================================================================--- lakewalker.py	(revision 17084)+++ lakewalker.py	(working copy)@@ -1,16 +1,17 @@ #!/usr/bin/python -# Lakewalker II.+# Forestwalker 0.0001. # 2007-07-09: Initial public release (v0.2) by Dshpak # 2007-07-10: v0.3: Added support for OSM input files. Also reduced start_radius_big, and fixed a division-by-zero error in the degenerate case of point_line_distance(). # 2007-08-04: Added experimental non-recursive Douglas-Peucker algorithm (--dp-nr). Added --startdir option. # 2007-08-06: v0.4: Bounding box support (--left, --right, --top, and --bottom), as well as new --josm mode for JOSM integration. This isn't perfect yet, but it's a good start.+# 2009-08-17 James Michael DuPont h4ck3rm1k3@flossk.org working  """Lakewalker II - A tool to automatically download and trace Landsat imagery, to generate OpenStreetMap data.  Requires the Python Imaging Library, available from http://www.pythonware.com/products/pil/""" -version="Lakewalker II v0.4"+version="Lakewalker II v0.6"  # TODO: # - Accept threshold tags in OSM input files.@@ -84,20 +85,22 @@  def download_landsat(c1, c2, width, height, fname):     layer = "global_mosaic_base"-    style = "IR1"+#    style = "IR1"+    style = "IR2"      (min_lat, min_lon) = c1     (max_lat, max_lon) = c2          message("Downloading Landsat tile for (%.6f,%.6f)-(%.6f,%.6f)." % (min_lat, min_lon, max_lat, max_lon))     url = "http://onearth.jpl.nasa.gov/wms.cgi?request=GetMap&layers=%s&styles=%s&srs=EPSG:4326&format=image/png&bbox=%0.6f,%0.6f,%0.6f,%0.6f&width=%d&height=%d" % (layer, style, min_lon, min_lat, max_lon, max_lat, width, height)-    #print url+    print url     try:         urllib.urlretrieve(url, fname)     except IOError, e:         raise FatalError("Error downloading tile: %s" % e.strerror)     if not os.path.exists(fname):         raise FatalError("Error: Could not retreive url %s" % url)+    print "Got it %s" % fname  def xy_to_geo(xy):     (x, y) = xy@@ -136,7 +139,7 @@                     message("Using imagery in %s for %s." % (fname, xy_to_geo(xy)))                 except IOError:                     error("Download was corrupt...Deleting %s..." % fname)-                    os.unlink(fname)+                    #os.unlink(fname)                     im = None                  if im is None:@@ -156,7 +159,7 @@         (x, y) = xy         (tile, (tx, ty)) = self.get_tile(xy)         tile_xy = (x - tx, (options.tilesize - 1) - (y - ty))-        #print "%s maps to %s" % (xy, tile_xy)+        print "%s maps to %s" % (xy, tile_xy)         return tile.getpixel(tile_xy)  def trace_lake(loc, threshold, start_dir, bbox):@@ -183,7 +186,8 @@      start_xy = xy     start_loc = xy_to_geo(xy)-    message("Found shore at lat %.4f lon %.4f" % start_loc)+    print xy +    message("Found shore at  lat %.4f lon %.4f" % start_loc)      #dirs = ((1, 0), (1, -1), (0, -1), (-1, -1), (-1, 0), (-1, 1), (0, 1), (1, 1))     last_dir = start_dir@@ -203,7 +207,7 @@                 break                          v = wms.get_pixel(test_xy)-            #print "%s: %s: %s" % (new_dir, test_xy, v)+            print "%s: %s: %s" % (new_dir, test_xy, v)             if v > threshold:                 break @@ -211,7 +215,7 @@             error("Got stuck.")             break -        #print "Moving to %s, direction %s (was %s)" % (test_xy, new_dir, dirs[last_dir])+        print "Moving to %s, direction %s (was %s)" % (test_xy, new_dir, dirs[last_dir])         last_dir = (last_dir + d + 4) % 8         xy = test_xy @@ -252,7 +256,7 @@         return abs((x2-x1)*(y1-y0) - (x1-x0)*(y2-y1)) / math.sqrt((x2-x1)**2 + (y2-y1)**2)  def douglas_peucker(nodes, epsilon):-    #print "Running DP on %d nodes" % len(nodes)+    print "Running DP on %d nodes" % len(nodes)     farthest_node = None     farthest_dist = 0     first = nodes[0]@@ -267,7 +271,7 @@     if farthest_dist > epsilon:         seg_a = douglas_peucker(nodes[0:farthest_node+1], epsilon)         seg_b = douglas_peucker(nodes[farthest_node:-1], epsilon)-        #print "Minimized %d nodes to %d + %d nodes" % (len(nodes), len(seg_a), len(seg_b))+        print "Minimized %d nodes to %d + %d nodes" % (len(nodes), len(seg_a), len(seg_b))         nodes = seg_a[:-1] + seg_b     else:         return [nodes[0], nodes[-1]]@@ -277,7 +281,7 @@ def dp_findpoint(nodes, start, end):     farthest_node = None     farthest_dist = 0-    #print "dp_findpoint(nodes, %s, %s)" % (start, end)+    print "dp_findpoint(nodes, %s, %s)" % (start, end)     first = nodes[start]     last = nodes[end] @@ -290,7 +294,7 @@     return (farthest_node, farthest_dist)  def douglas_peucker_nonrecursive(nodes, epsilon):-    #print "Running DP on %d nodes" % len(nodes)+    print "Running DP on %d nodes" % len(nodes)     command_stack = [(0, len(nodes) - 1)]     result_stack = [] @@ -310,7 +314,7 @@             second = result_stack.pop()             if first[-1] == second[0]:                 result_stack.append(first + second[1:])-                #print "Added %s and %s; result is %s" % (first, second, result_stack[-1])+                print "Added %s and %s; result is %s" % (first, second, result_stack[-1])             else:                 error("ERROR: Cannot connect nodestrings!")                 #print first@@ -340,7 +344,7 @@     if farthest_dist > epsilon:         seg_a = douglas_peucker(nodes[0:farthest_node+1], epsilon)         seg_b = douglas_peucker(nodes[farthest_node:-1], epsilon)-        #print "Minimized %d nodes to %d + %d nodes" % (len(nodes), len(seg_a), len(seg_b))+        print "Minimized %d nodes to %d + %d nodes" % (len(nodes), len(seg_a), len(seg_b))         nodes = seg_a[:-1] + seg_b     else:         return [nodes[0], nodes[-1]]@@ -363,7 +367,7 @@     print "x"  def write_osm(f, lakelist, waysize):-    f.write('<osm version="0.4">')+    f.write('<osm version="0.6">')     cur_id = -1     way_count = 0     for nodelist in lakelist:@@ -376,31 +380,40 @@             last_node_id = cur_id             cur_id = cur_id - 1 -        # print "Nodes: %d, %d" % (first_node_id, last_node_id)+        print "Nodes: %d, %d" % (first_node_id, last_node_id) -        first_segment_id = cur_id-        for seg in xrange(first_node_id, last_node_id, -1):-            f.write('<segment id="%d" from="%d" to="%d"/>' % (cur_id, seg, seg-1))-            cur_id = cur_id - 1-        f.write('<segment id="%d" from="%d" to="%d"/>' % (cur_id, last_node_id, first_node_id))-        last_segment_id = cur_id+#        f.write('<way id="%d" >\n' %  cur_id)++#        first_segment_id = cur_id++#        for seg in xrange(first_node_id, last_node_id, -1):+#            f.write('<nd ref="%d" />' % ( seg))+            #<nd ref='-6' />+#            cur_id = cur_id - 1+#        f.write('<segment id="%d" from="%d" to="%d"/>' % (cur_id, last_node_id, first_node_id))++#        f.write('</way>\n')++#        last_segment_id = cur_id         cur_id = cur_id - 1 -        # print "Segments: %d, %d" % (first_segment_id, last_segment_id)+        print "Segments: %d, %d" % (first_node_id, last_node_id)         -        for seg in xrange(first_segment_id, last_segment_id - 1, -1):+        for seg in xrange(first_node_id, last_node_id - 1, -1):             if len(cur_way) >= waysize:                 cur_way = []                 ways.append(cur_way)             cur_way.append(seg)         for way in ways:-            f.write('<way id="%d">' % cur_id)+            f.write('<way id="%d" action="modify" visible="true" >\n' % cur_id)             cur_id = cur_id - 1             for seg in way:-                f.write('<seg id="%d"/>' % seg)+                f.write('<nd ref="%d"/>' % seg)++            f.write('<nd ref="%d"/>' % first_node_id) # close it             f.write('<tag k="natural" v="%s"/>' % options.natural_type)-            f.write('<tag k="source" v="Dshpak_landsat_lakes"/>')-            f.write('</way>')+            f.write('<tag k="source" v="landsat_forests"/>')+            f.write('</way>\n')         way_count = way_count + len(ways)          f.write('</osm>')@@ -428,7 +441,7 @@     parser.add_option("--lon", type="float", metavar="LONGITUDE", help="Starting longitude. Required, unless --infile is used.")     parser.add_option("--startdir", type="string", default="east", metavar="DIR", help="Direction to travel from start position when seeking land. Defaults to \"east\".")     parser.add_option("--infile", "-i", type="string", metavar="FILE", help="OSM file containing nodes representing starting points.")-    parser.add_option("--out", "-o", default="lake.osm", dest="outfile", metavar="FILE", help="Output filename. Defaults to lake.osm.")+    parser.add_option("--out", "-o", default="forest.osm", dest="outfile", metavar="FILE", help="Output filename. Defaults to forest.osm.")     parser.add_option("--threshold", "-t", type="int", default="35", metavar="VALUE", help="Maximum gray value to accept as water (based on Landsat IR-1 data). Can be in the range 0-255. Defaults to 35.")     parser.add_option("--maxnodes", type="int", default="50000", metavar="N", help="Maximum number of nodes to generate before bailing out. Defaults to 50000.")     parser.add_option("--waylength", type="int", default=250, metavar="MAXLEN", help="Maximum nuber of nodes allowed in one way. Defaults to 250.")@@ -439,7 +452,10 @@     parser.add_option("--dp-nr", action="store_true", dest="dp_nr", default=False, help="Use experimental non-recursive DP implementation")     parser.add_option("--vr", action="store_true", dest="use_vr", default=False, help="Use vertex reduction before applying line simplification (off by default).")     parser.add_option("--vr-epsilon", type="float", default=0.0005, metavar="RADIUS", help="Radius used for vertex reduction (measured in degrees). Defaults to 0.0005.")-    parser.add_option("--water", action="store_const", const="water", dest="natural_type", default="coastline", help="Tag ways as natural=water instead of natural=coastline")+#    parser.add_option("--water", action="store_const", const="water", dest="natural_type", default="coastline", help="Tag ways as natural=water instead of natural=coastline")+    parser.add_option("--forest", action="store_const", const="woords", dest="natural_type", default="forest", help="Tag ways as natural=woods instead of natural=forest")+    #options.natural_type+     parser.add_option("--left", type="float", metavar="LONGITUDE", default=-180, help="Left (west) longitude for bounding box")     parser.add_option("--right", type="float", metavar="LONGITUDE", default=180, help="Right (east) longitude for bounding box")     parser.add_option("--top", type="float", metavar="LATITUDE", default=90, help="Top (north) latitude for bounding box")prizre