| 1 | #
|
|---|
| 2 | # !!!REMEMBER!!!
|
|---|
| 3 | # latitude : phi
|
|---|
| 4 | # longitude : lam
|
|---|
| 5 | #
|
|---|
| 6 | import os
|
|---|
| 7 | import yaml
|
|---|
| 8 | import urllib
|
|---|
| 9 |
|
|---|
| 10 | try:
|
|---|
| 11 | CACHE_FILE = os.environ['RD2ETRS_CACHE']
|
|---|
| 12 | except KeyError:
|
|---|
| 13 | try:
|
|---|
| 14 | CACHE_FILE = os.path.join(os.environ['HOME'],'.rd2etrs.yaml')
|
|---|
| 15 | except KeyError:
|
|---|
| 16 | CACHE_FILE = '/tmp/rd2etrs.yaml'
|
|---|
| 17 |
|
|---|
| 18 | coordinates = None
|
|---|
| 19 |
|
|---|
| 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'"""
|
|---|
| 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 |
|
|---|
| 35 | def etrs2rd(phi, lam):
|
|---|
| 36 | """ Convert etrs to rd coordinates
|
|---|
| 37 | @Return: (X,Y)
|
|---|
| 38 | """
|
|---|
| 39 |
|
|---|
| 40 | item = { 'phi' : phi, 'lam' : lam, 'func': 'etrs2rd' }
|
|---|
| 41 | url = 'http://vanderzwet.net/rdnap/%(func)s/%(phi)s/%(lam)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 |
|
|---|
| 51 | def rd2etrs(xrd, yrd, hnap=0.0):
|
|---|
| 52 | """
|
|---|
| 53 | Convert rd to etrs
|
|---|
| 54 | JavaScript Version: https://rdinfo.kadaster.nl/rd/transformator.html
|
|---|
| 55 | @Return: (latitude,longitude)
|
|---|
| 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
|
|---|
| 79 | print "### Not in Cache, Fetching coordinate %s, %s from %s" % (xrd, yrd, url)
|
|---|
| 80 | f = urllib.urlopen(url)
|
|---|
| 81 | raw = f.read()
|
|---|
| 82 |
|
|---|
| 83 | phi,lam,h = raw.split('/')
|
|---|
| 84 | coordinates[(xrd, yrd)] = (phi, lam)
|
|---|
| 85 | write_yaml(CACHE_FILE, coordinates)
|
|---|
| 86 | return (phi, lam)
|
|---|