source: genesis/tools/rdnap.py@ 10376

Last change on this file since 10376 was 10373, checked in by rick, 13 years ago

Latitude and Longitude got shuffled around.

Related-To: beheer#167

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(phi, lam):
36 """ Convert etrs to rd coordinates
37 @Return: (X,Y)
38 """
39
40 item['phi'] = phi
41 item['lam'] = lam
42 item['func'] = 'etrs2rd'
43 url = 'http://vanderzwet.net/rdnap/%(func)s/%(phi)s/%(lam)s/0/' % item
44
45 f = urllib.urlopen(url)
46 raw = f.read()
47
48 rdnap_x,rdnap_y,rdnap_h = raw.split('/')
49 return (rdnap_x, rdnap_y)
50
51
52
53def rd2etrs(xrd, yrd, hnap=0.0):
54 """
55 Convert rd to etrs
56 JavaScript Version: https://rdinfo.kadaster.nl/rd/transformator.html
57 @Return: (latitude,longitude)
58 """
59 # Get cache is exists
60 global coordinates
61 if coordinates == None:
62 try:
63 coordinates = get_yaml(CACHE_FILE)
64 except (IOError,AttributeError):
65 coordinates = dict()
66 pass
67
68 # Check if item in cache
69 xrd = float(str(xrd))
70 yrd = float(str(yrd))
71 if coordinates.has_key((xrd, yrd)):
72 return coordinates[(xrd, yrd)]
73
74 # Get new coordinate
75 item = dict()
76 item['xrd'] = xrd
77 item['yrd'] = yrd
78 item['hnap'] = hnap
79 item['func'] = 'rd2etrs'
80 url = 'http://vanderzwet.net/rdnap/%(func)s/%(xrd)s/%(yrd)s/%(hnap)s/' % item
81 print "### Not in Cache, Fetching coordinate %s, %s from %s" % (xrd, yrd, url)
82 f = urllib.urlopen(url)
83 raw = f.read()
84
85 phi,lam,h = raw.split('/')
86 coordinates[(xrd, yrd)] = (phi, lam)
87 write_yaml(CACHE_FILE, coordinates)
88 return (phi, lam)
Note: See TracBrowser for help on using the repository browser.