source: genesis/tools/rdnap.py@ 10094

Last change on this file since 10094 was 10058, checked in by rick, 13 years ago

Makes the configuration production ready.

Related-To: ticket:117

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