|
1 | | -# Mapcode package for Python |
| 1 | +# Mapcode module for Python |
2 | 2 |
|
3 | 3 | Copyright (C) 2014-2015 Stichting Mapcode Foundation (http://www.mapcode.com) |
4 | 4 |
|
5 | 5 | ---- |
6 | 6 |
|
7 | | -This Python project contains a package to encode latitude/longitude pairs to mapcodes |
| 7 | +This Python project contains a module to encode latitude/longitude pairs to mapcodes |
8 | 8 | and to decode mapcodes back to latitude/longitude pairs. |
9 | 9 |
|
10 | 10 | If you wish to use mapcodes in your own application landscape, consider using running an instance of the |
@@ -34,67 +34,141 @@ At the moment this module is still in development! |
34 | 34 |
|
35 | 35 | # Bug Reports and New Feature Requests |
36 | 36 |
|
37 | | -If you encounter any problems with this package, don't hesitate to use the `Issues` session to file your issues. |
| 37 | +If you encounter any problems with this module, don't hesitate to use the `Issues` session to file your issues. |
38 | 38 | Normally, one of our developers should be able to comment on them and fix. |
39 | 39 |
|
40 | 40 | # Prequisites |
41 | 41 |
|
42 | | -As the Python modules relies upon the Mapcode C library you will |
| 42 | +As this Python module relies upon the Mapcode C library you will |
43 | 43 | need to have a compiler installed to be able to build and install |
44 | 44 | this module. |
45 | 45 |
|
46 | 46 | The assumption is that the mapcode-cpp source is available in |
47 | 47 | ../mapcode-cpp/mapcodelib, if it located in a different place you |
48 | | -will have to update the include directory in setup.py |
| 48 | +will have to update the include directory in setup.py. |
49 | 49 |
|
50 | 50 |
|
51 | 51 | # Installation |
52 | 52 |
|
53 | | -Get both the Python and C repositories using: |
| 53 | +Get both the mapcode Python and C repositories using: |
54 | 54 |
|
55 | 55 | ``` |
56 | 56 | git clone https://github.com/mapcode-foundation/mapcode-python |
57 | 57 | git clone https://github.com/mapcode-foundation/mapcode-cpp |
58 | 58 | cd mapcode-python |
59 | 59 | ``` |
60 | 60 |
|
61 | | -Compile the package in current directory using: |
| 61 | +Compile the package in current directory: `python setup.py --inplace` |
| 62 | +Install in your Python environment using: `python setup.py install` |
62 | 63 |
|
| 64 | + |
| 65 | +## Usage in Python |
| 66 | + |
| 67 | +```python |
| 68 | +import mapcode |
63 | 69 | ``` |
64 | | -python setup.py --inplace |
| 70 | +dir (mapcode) |
| 71 | + |
| 72 | +e.g.: |
| 73 | + |
| 74 | +import mapcode |
| 75 | +dir(mapcode) |
| 76 | +['__doc__', '__file__', '__name__', '__package__', 'decode', 'encode', 'encode_single', 'isvalid', 'version'] |
65 | 77 | ``` |
66 | 78 |
|
67 | | -Install in your Python environment using: |
| 79 | +## Mapcode syntax validation |
| 80 | +
|
| 81 | +To validate the syntax of a mapcode string use the isvalid() method. |
68 | 82 |
|
| 83 | +```python |
| 84 | +print mapcode.isvalid('VHXG9.FQ9Z') |
| 85 | +True |
| 86 | +print mapcode.isvalid('Amsterdam') |
| 87 | +False |
69 | 88 | ``` |
70 | | -python setup.py install |
| 89 | + |
| 90 | +As optional parameter you can pass 1 or 0: if you pass 1, full mapcodes |
| 91 | +(including optional territory context) will be recognized. If you pass 0, |
| 92 | +only “proper” mapcodes will be recognized. |
| 93 | + |
| 94 | +```python |
| 95 | +print mapcode.isvalid('NLD 49.4V', 1) |
| 96 | +True |
| 97 | +print mapcode.isvalid('VHXG9.FQ9Z',0) |
| 98 | +True |
| 99 | +print mapcode.isvalid('NLD 49.4V', 0) |
| 100 | +False |
| 101 | + |
| 102 | +(This one fails as it is not a full mapcode) |
71 | 103 | ``` |
72 | 104 |
|
73 | 105 |
|
74 | | -# Usage |
| 106 | +## Enconding latitude, longitude to a mapcode |
| 107 | + |
| 108 | +For encoding there are two methods avaiable. |
| 109 | + |
| 110 | +Use the encode_single() method to find one possible mapcode. Optionally |
| 111 | +a territory code can be provide to encode for just that territory. |
75 | 112 |
|
76 | 113 |
|
77 | 114 | ```python |
78 | | -import mapcode |
| 115 | +print mapcode.encode_single(52.376514, 4.908542) |
| 116 | +NLD 49.4V |
79 | 117 |
|
80 | | -dir (mapcode) |
| 118 | +For this coordinate there are multiple mapcodes possible, hence providing |
| 119 | +a territory code can give a different one. |
81 | 120 |
|
82 | | -print mapcode.__doc__ |
83 | | -print mapcode.version() |
84 | | -print mapcode.decode('NLD 49.4V','') |
85 | | -print mapcode.decode('IN VY.HV','USA') |
86 | | -print mapcode.decode('IN VY.HV','RUS') |
87 | | -print mapcode.decode('D6.58','RU-IN DK.CN0') |
88 | | -print mapcode.decode('IN VY.HV','') |
89 | | -print mapcode.decode('RU-IN VY.HV','') |
90 | | -print mapcode.decode('IN VY.HV','RUS') |
| 121 | +print mapcode.encode_single(41.851944, 12.433114) |
| 122 | +ITA 0Z.0Z |
| 123 | +print mapcode.encode_single(41.851944, 12.433114, 'AAA') |
| 124 | +TJKM1.D2Z6 |
| 125 | +``` |
91 | 126 |
|
92 | | -print mapcode.isvalid('NLD 49.4V', 1) |
93 | | -print mapcode.isvalid('VHXG9.FQ9Z',0) |
| 127 | +Use the encode() method to find all possible mapcodes. Optionally |
| 128 | +a territory code can be provide to encode for just that territory. |
| 129 | + |
| 130 | + |
| 131 | +```python |
| 132 | +print mapcode.encode(52.376514, 4.908542) |
| 133 | +[['49.4V', 'NLD'], ['G9.VWG', 'NLD'], ['DL6.H9L', 'NLD'], ['P25Z.N3Z', 'NLD'], ['VHXGB.1J9J', 'AAA']] |
| 134 | +``` |
| 135 | + |
| 136 | +A territory code can be provide to encode for just that territory. |
| 137 | + |
| 138 | +```python |
| 139 | +print mapcode.encode(52.376514, 4.908542, 'NLD') |
| 140 | +print mapcode.encode(39.609999,45.949999, 'AZE') |
| 141 | +[['XLT.HWB', 'AZE'], ['2Z.05XL', 'AZE'], ['6N49.HHV', 'AZE']]``` |
| 142 | + |
| 143 | +## Decoding mapcode to latitude, longitude |
94 | 144 |
|
95 | | -print mapcode.encode_single(52.376514, 4.908542, 'NLD', 2) |
96 | | -print mapcode.encode_single(41.938434,12.433114, None, 2) |
| 145 | +Use the decode() method to decode a mapcode back to latitude and longitude. |
97 | 146 |
|
98 | | -print mapcode.encode(52.376514, 4.908542, 'NLD', 2) |
99 | | -print mapcode.encode(39.609999,45.949999, 'AZE', 0) |
| 147 | +```python |
| 148 | +print mapcode.decode('NLD 49.4V') |
| 149 | +(52.376514, 4.908542) |
| 150 | +print mapcode.decode('IN VY.HV') |
| 151 | +(43.193485, 44.826592) |
| 152 | +print mapcode.decode('VHXG9.DNRF') |
| 153 | +(52.371422, 4.872497) |
100 | 154 | ``` |
| 155 | + |
| 156 | +An optional territory code can be provide to disambiguate the mapcode. |
| 157 | + |
| 158 | +A territory is require to decode this mapcode: |
| 159 | + |
| 160 | +```python |
| 161 | +print mapcode.decode('D6.58') |
| 162 | +False |
| 163 | +print mapcode.decode('D6.58','RU-IN DK.CN0') |
| 164 | +(43.259275, 44.77198) |
| 165 | +``` |
| 166 | + |
| 167 | +A territory is require to give context: |
| 168 | + |
| 169 | +```python |
| 170 | +print mapcode.decode('IN VY.HV','USA') |
| 171 | +(39.72795, -86.118444) |
| 172 | +print mapcode.decode('IN VY.HV','RUS') |
| 173 | +(43.193485, 44.826592)``` |
| 174 | +``` |
0 commit comments