[8872] | 1 | #
|
---|
[10373] | 2 | # !!!REMEMBER!!!
|
---|
[8872] | 3 | # latitude : phi
|
---|
| 4 | # longitude : lam
|
---|
| 5 | #
|
---|
[9822] | 6 | import os
|
---|
[8869] | 7 | import yaml
|
---|
| 8 | import urllib
|
---|
| 9 |
|
---|
[9822] | 10 | try:
|
---|
| 11 | CACHE_FILE = os.environ['RD2ETRS_CACHE']
|
---|
| 12 | except 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] | 18 | coordinates = None
|
---|
| 19 |
|
---|
[8869] | 20 | def get_yaml(gfile):
|
---|
| 21 | """ Get configuration yaml for 'item'"""
|
---|
| 22 | f = open(gfile, 'r')
|
---|
| 23 | datadump = yaml.load(f)
|
---|
| 24 | return datadump
|
---|
| 25 |
|
---|
| 26 | def 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] | 35 | def etrs2rd(phi, lam):
|
---|
| 36 | """ Convert etrs to rd coordinates
|
---|
| 37 | @Return: (X,Y)
|
---|
| 38 | """
|
---|
[8622] | 39 |
|
---|
[10400] | 40 | item = { 'phi' : phi, 'lam' : lam, 'func': 'etrs2rd' }
|
---|
[10373] | 41 | url = 'http://vanderzwet.net/rdnap/%(func)s/%(phi)s/%(lam)s/0/' % item
|
---|
[8622] | 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 |
|
---|
| 51 | def rd2etrs(xrd, yrd, hnap=0.0):
|
---|
| 52 | """
|
---|
[10373] | 53 | Convert rd to etrs
|
---|
[8622] | 54 | JavaScript Version: https://rdinfo.kadaster.nl/rd/transformator.html
|
---|
[10373] | 55 | @Return: (latitude,longitude)
|
---|
[8622] | 56 | """
|
---|
| 57 | # Get cache is exists
|
---|
| 58 | global coordinates
|
---|
| 59 | if coordinates == None:
|
---|
| 60 | try:
|
---|
| 61 | coordinates = get_yaml(CACHE_FILE)
|
---|
| 62 | except (IOError,AttributeError):
|
---|
| 63 | coordinates = dict()
|
---|
| 64 | pass
|
---|
| 65 |
|
---|
| 66 | # Check if item in cache
|
---|
| 67 | xrd = float(str(xrd))
|
---|
| 68 | yrd = float(str(yrd))
|
---|
| 69 | if coordinates.has_key((xrd, yrd)):
|
---|
| 70 | return coordinates[(xrd, yrd)]
|
---|
| 71 |
|
---|
| 72 | # Get new coordinate
|
---|
| 73 | item = dict()
|
---|
| 74 | item['xrd'] = xrd
|
---|
| 75 | item['yrd'] = yrd
|
---|
| 76 | item['hnap'] = hnap
|
---|
| 77 | item['func'] = 'rd2etrs'
|
---|
| 78 | url = 'http://vanderzwet.net/rdnap/%(func)s/%(xrd)s/%(yrd)s/%(hnap)s/' % item
|
---|
[10373] | 79 | print "### Not in Cache, Fetching coordinate %s, %s from %s" % (xrd, yrd, url)
|
---|
[8622] | 80 | f = urllib.urlopen(url)
|
---|
| 81 | raw = f.read()
|
---|
[10373] | 82 |
|
---|
[8872] | 83 | phi,lam,h = raw.split('/')
|
---|
[10373] | 84 | coordinates[(xrd, yrd)] = (phi, lam)
|
---|
[8622] | 85 | write_yaml(CACHE_FILE, coordinates)
|
---|
[10373] | 86 | return (phi, lam)
|
---|