4 # Various OSM-related utilities useful for mass manipulation
8 from OsmApi
import OsmApi
9 from math
import cos
, radians
12 from copy
import deepcopy
15 class OsmApiExt(OsmApi
):
21 appname
='OsmUtils' + __version__
,
22 comment
=u
'updated with OsmUtils'
25 if username
or password
:
26 raise NotImplementedError(
27 'Username+password authentication not implemented')
30 homedir
= os
.environ
['HOME']
31 self
._pwfile
= homedir
+ '/.config/osm-utils/osm-auth'
33 self
._pwfile
= passwordfile
35 api
= OsmApi(passwordfile
=self
._pwfile
, appid
=appname
,
37 changesetautotags
={u
'comment': comment
})
46 def MapAroundPoint(self
, lon
=None, lat
=None, bbox_km
=10):
48 Given the latitude 'lat' and longitude 'lon', get from the API the map
49 around that point within a bbox_km area
51 Returns a list of dict {type: node|way|relation, data: {}}. """
53 if not lon
and self
._lon
:
58 if not lat
and self
._lat
:
63 # one degree latitude is approximately 111km
64 # and we want to know what's half of bbox_km in lat degrees
65 delta_lat
= bbox_km
/ 222.0
69 # one degree longitude is a cos(lat) * 111
70 # and we want to know what's half of bbox_km in lon degrees
71 delta_lon
= cos(radians(lat
)) * delta_lat
73 lat_b
= lat
- delta_lat
74 lat_t
= lat
+ delta_lat
76 lon_l
= lon
- delta_lon
77 lon_r
= lon
+ delta_lon
79 self
._map
= self
._api
.Map(lon_l
, lat_b
, lon_r
, lat_t
)
82 def mapFocus(self
, lat
, lon
):
86 def getApiMap(self
, lat
=None, lon
=None, bbox
=None, amap
=None):
91 raise NameError('lat not defined')
96 raise NameError('lon not defined')
99 self
.mapFocus(lat
, lon
)
101 amap
= self
.MapAroundPoint()
103 amap
= self
.MapAroundPoint(bbox_km
=bbox
)
105 self
._map
= deepcopy(amap
)
110 def grepElementsInMap(amap
, tag
, pattern
, etype
=u
'way', flags
=re
.I
+ re
.U
):
112 # obtain all elements of type etype
113 elems
= [x
[u
'data'] for x
in amap
if (x
[u
'type'] == etype
)]
115 regex
= re
.compile(pattern
, flags
)
119 if regex
.match(x
[u
'tag'][tag
]):