+def write_opl_file(opl, grid):
+ """ Create a temporary OSM file from OPL and return the file name. It is
+ the responsibility of the caller to delete the file again.
+
+ Node with missing coordinates, can retrieve their coordinates from
+ a supplied grid. Failing that a random coordinate is assigned.
+ """
+ with tempfile.NamedTemporaryFile(suffix='.opl', delete=False) as fd:
+ for line in opl.splitlines():
+ if line.startswith('n') and line.find(' x') < 0:
+ coord = grid.grid_node(int(line[1:].split(' ')[0]))
+ if coord is None:
+ coord = (random.random() * 360 - 180,
+ random.random() * 180 - 90)
+ line += " x%f y%f" % coord
+ fd.write(line.encode('utf-8'))
+ fd.write(b'\n')
+
+ return fd.name
+