source: genesis/tools/rdnap.py@ 9095

Last change on this file since 9095 was 8872, checked in by rick, 14 years ago

Damm I lost my nodes on the maps and found them back in Africa...

File size: 1.7 KB
Line 
1#
2# REMEMBER
3# latitude : phi
4# longitude : lam
5#
6import yaml
7import urllib
8
9CACHE_FILE = '/tmp/rd2etrs.yaml'
10coordinates = None
11
12def get_yaml(gfile):
13 """ Get configuration yaml for 'item'"""
14 f = open(gfile, 'r')
15 datadump = yaml.load(f)
16 return datadump
17
18def write_yaml(gfile, datadump):
19 """ Write configuration yaml for 'item'"""
20 f = open(gfile, 'w')
21 f.write(yaml.dump(datadump, default_flow_style=False))
22 f.close()
23
24def etrs2rd(lam, phi):
25 """ Convert etrs to rd """
26
27 item['lam'] = lam
28 item['phi'] = phi
29 item['func'] = 'etrs2rd'
30 url = 'http://vanderzwet.net/rdnap/%(func)s/%(xrd)s/%(yrd)s/0/' % item
31
32 f = urllib.urlopen(url)
33 raw = f.read()
34
35 rdnap_x,rdnap_y,rdnap_h = raw.split('/')
36 return (rdnap_x, rdnap_y)
37
38
39
40def rd2etrs(xrd, yrd, hnap=0.0):
41 """
42 Convert rd to etrs
43 JavaScript Version: https://rdinfo.kadaster.nl/rd/transformator.html
44 """
45 # Get cache is exists
46 global coordinates
47 if coordinates == None:
48 try:
49 coordinates = get_yaml(CACHE_FILE)
50 except (IOError,AttributeError):
51 coordinates = dict()
52 pass
53
54 # Check if item in cache
55 xrd = float(str(xrd))
56 yrd = float(str(yrd))
57 if coordinates.has_key((xrd, yrd)):
58 return coordinates[(xrd, yrd)]
59
60 # Get new coordinate
61 item = dict()
62 item['xrd'] = xrd
63 item['yrd'] = yrd
64 item['hnap'] = hnap
65 item['func'] = 'rd2etrs'
66 url = 'http://vanderzwet.net/rdnap/%(func)s/%(xrd)s/%(yrd)s/%(hnap)s/' % item
67 print "### Not in Cache, Fetching coordinate %s, %s from %s" % (xrd, yrd, url)
68 f = urllib.urlopen(url)
69 raw = f.read()
70
71 # Coordinates returned in 'odd' order
72 phi,lam,h = raw.split('/')
73 coordinates[(xrd, yrd)] = (lam, phi)
74 write_yaml(CACHE_FILE, coordinates)
75 return (lam, phi)
Note: See TracBrowser for help on using the repository browser.