source: genesis/tools/rdnap.py@ 10375

Last change on this file since 10375 was 10373, checked in by rick, 14 years ago

Latitude and Longitude got shuffled around.

Related-To: beheer#167

File size: 1.9 KB
RevLine 
[8872]1#
[10373]2# !!!REMEMBER!!!
[8872]3# latitude : phi
4# longitude : lam
5#
[9822]6import os
[8869]7import yaml
8import urllib
9
[9822]10try:
11 CACHE_FILE = os.environ['RD2ETRS_CACHE']
12except KeyError:
[10058]13 try:
14 CACHE_FILE = os.path.join(os.environ['HOME'],'.rd2etrs.yaml')
15 except KeyError:
16 CACHE_FILE = '/tmp/rd2etrs.yaml'
17
[8622]18coordinates = None
19
[8869]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'"""
[9822]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
[8869]34
[10373]35def etrs2rd(phi, lam):
36 """ Convert etrs to rd coordinates
37 @Return: (X,Y)
38 """
[8622]39
[10373]40 item['phi'] = phi
[8622]41 item['lam'] = lam
42 item['func'] = 'etrs2rd'
[10373]43 url = 'http://vanderzwet.net/rdnap/%(func)s/%(phi)s/%(lam)s/0/' % item
[8622]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 """
[10373]55 Convert rd to etrs
[8622]56 JavaScript Version: https://rdinfo.kadaster.nl/rd/transformator.html
[10373]57 @Return: (latitude,longitude)
[8622]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
[10373]81 print "### Not in Cache, Fetching coordinate %s, %s from %s" % (xrd, yrd, url)
[8622]82 f = urllib.urlopen(url)
83 raw = f.read()
[10373]84
[8872]85 phi,lam,h = raw.split('/')
[10373]86 coordinates[(xrd, yrd)] = (phi, lam)
[8622]87 write_yaml(CACHE_FILE, coordinates)
[10373]88 return (phi, lam)
Note: See TracBrowser for help on using the repository browser.