Script for reducing the precision of nodes

From OpenStreetMap Wiki
Jump to: navigation, search

About

But that's just in theory. In reality the JOSM Validator only gets us as far as snapping the nodes.
Depending on your data source you'll have to adjust to suit
Probably they are using %.9g for both?
Probably they are using %.12g for both?


It's not the most efficient thing in the world, but what the heck, it works for its purpose.

Usage

BASE_URL="http://osmxapi.hypercube.telascience.org/api/0.6"
BBOX="-177,-44.5,-175.5,-43.5"

wget -O chatham_roads_dl.osm \
  "$BASE_URL/way[highway=*][LINZ:source_version=V16][bbox=$BBOX]"

The script

#!/usr/bin/env python
############################################################################
#
# MODULE:       osm_reduce_coord_fidelity.py
# AUTHOR:       Hamish Bowman, Dunedin, New Zealand
# PURPOSE:      Read in LINZ-2-OSM exported bridge_cl.osm and simplify coords
# COPYRIGHT:    (c) 2010 Hamish Bowman, and the OpenStreetMap Foundation
#               Various bits inspired by various GRASS GIS Python scripts (GPL2)
#
#               This program is free software under the GNU General Public
#               License (>=v2) and comes with ABSOLUTELY NO WARRANTY.
#               See http://www.gnu.org/licenses/gpl2.html for details.
#
#############################################################################
 
import sys
import os
 
def main():
    input = 'bridge_cl.osm'
    output = 'bridge_cl_76f.osm'
    #dp=7
    #output = 'bridge_cl_%df.osm' % dp
 
    #### set up input file
    if input == '-':
        infile = None
        inf = sys.stdin
    else:
        infile = input
        if not os.path.exists(infile):
            print("Unable to read input data")
            sys.exit(1)
        inf = file(infile)
        print("input file=[%s]" % infile)
 
    #### set up output file
    if not output:
        outfile = None
        outf = sys.stdout
    else:
        outfile = output
        outf = open(outfile, 'w')
        print("output file=[%s]" % outfile)
 
    lines = inf.readlines()
 
    for i in range(len(lines)):
        lines[i] = lines[i].rstrip('\n')
 
# lines[105]:
#'    <node id="-6698677" lat="-43.9585473707" lon="-176.550362475" />'
 
    for line in lines:
        if 'node id=' not in line:
            outf.write(str(line) + '\n')
        else:
            bits = line.split('"')
            outf.write('%s"%s"%s"%.7f"%s"%.6f"%s\n' % (bits[0],
                         bits[1], bits[2], float(bits[3]),
                         bits[4], float(bits[5]), bits[6] ))
 
    inf.close()
    if outfile is not None:
        outf.close()
 
 
if __name__ == "__main__":
    main()
Personal tools
Namespaces
Variants
Actions
site
Toolbox