Package: pyosmkit
pyosmkit: library for building OSM toolsThis package contains helpers for building tools around OSM tiles.Since 0.11package name was changed frompyosmtopyosmkitto avoid pypi conflicts.Installation# from pypipipinstall--userpyosmkit# or from github where ${TAG} is the git version tag, eg v0.6pipinstall--usergit+https://github.com/tierpod/pyosmkit.git@${TAG}#egg=pyosmkitDevelopinggitclonehttps://github.com/tierpod/pyosmkit.git&&cdpyosmkit
makevenvsource./venv/bin/activate
makeinit-devpyosmkit.pointCreate points, convert coordinates.zxy_to_latlong(z, x, y)-> LatLonglatlong_to_zxy(lat, lng, zoom)-> ZXY>>>importpyosmkit.point>>>p=pyosmkit.point.ZXY(z=10,x=697,y=321)>>>pyosmkit.point.zxy_to_latlong(p.z,p.x,p.y)LatLong(lat=55.5783,long=65.0391)pyosmkit.tileCreateosmtile, get filename.Tile.from_url(url)-> TileTile.from_metatile(mt)-> TileTile.filepath(url)-> str>>>frompyosmkit.tileimportTile>>>t=Tile.from_url("/style/1/1/1.png")>>>print(t)Tile(z:1,x:1,y:1,style:style,ext:.png)>>>t.filepath("/cache")'/cache/style/1/1/1.png'pyosmkit.polygonList of tiles can be grouped to theclosedpolygon. You can check if LatLong point inside this
polygon or not (usingray-castingalgorithm):>>>frompyosmkit.pointimportLatLong>>>frompyosmkit.polygonimportPolygon>>>polygon=Polygon([LatLong(0,0),LatLong(10,0),LatLong(10,10),...LatLong(0,10),LatLong(0,0)])>>>LatLong(1,2)inpolygonTrue>>>LatLong(11,12)inpolygonFalseAlso, a list of polygons can be grouped to Region (supportinstatement).pyosmkit.metatileMetatileCreate metatile coordinates, get filename:Metatile.from_url(url)-> MetatileMetatile.from_tile(Tile)-> MetatileMetatile.filepath(basedir)-> str>>>frompyosmkit.tileimportTile>>>frompyosmkit.metatileimportMetatile>>>tile=Tile(z=10,x=697,y=321,style="mapname",ext=".png")>>>mt=Metatile.from_tile(tile)>>>print(mt)Metatile(z:10,x:696-703,y:320-327,style:mapname)>>>mt.filepath("/cache")'/cache/mapname/10/0/0/33/180/128.meta'MetatileFileTry to implement metatile file encoder/decoder in pythonic way (inspired by Raymond Hettinger
videos).pyosmkit.metatile.open(filename, mode)-> MetatileFile: opens file for reading ("rb" mode) or
writing ("wb"). Returns file-like object.Supportwithstatement,instatement,iteratingover points:>>>importpyosmkit.metatile>>>mt=pyosmkit.metatile.open("tests/data/0.meta","rb")>>># check if tile (1, 2) contains in metatile>>>(1,2)inmtTrue>>>(10,10)inmtFalse>>># iterate over Points and print only points with x == 7>>>forpointinmt:...ifpoint.x==7:...print(point)Point(x=7,y=0)Point(x=7,y=1)Point(x=7,y=2)Point(x=7,y=3)Point(x=7,y=4)Point(x=7,y=5)Point(x=7,y=6)Point(x=7,y=7)>>># read all tiles data, iterate over Points and print only none-empty data:>>>tiles_data=mt.readtiles()>>>forpoint,dataintiles_data.items():...ifdata:...# do something with point(z, x, y) or data (bytes)...pass>>>mt.close()MetatileFile.readtile(x, y)-> bytesMetatileFile.readtiles()-> dict {Point(x, y): bytes, ...}MetatileFile.write(x, y, z, data), where z is the metatile zoom level, x, y is the lowest
values, data is the dict {Point(x, y): bytes, ...}metatile format descriptionCan be found inmod_tileproject:pyosmkit.mbtileDecode mbtiles file, read tile from mbtiles file. Supportwith,instatements.>>>importpyosmkit.mbtile>>>frompyosmkit.pointimportZXY>>>point=ZXY(z=1,x=1,y=0)>>>withpyosmkit.mbtile.open("tests/data/0.mbtiles")asmb:...print(pointinmb)...print(len(mb.readtile(point.z,point.x,point.y)))True26298pyosmkit.mbtile.open(file, mode, flip_y)-> MBTileFile: open file for reading. Returns
file-like object.MBTileFile.readtile(z, x, y)-> buffer