{ "info": { "author": "chenyiming", "author_email": "cymcpak00@gmail.com", "bugtrack_url": null, "classifiers": [], "description": "# Baidu Map Api\n\n\n\n- [Baidu Map Api](#baidu-map-api)\n - [Description](#description)\n - [Get Start](#get-start)\n - [Handle](#handle)\n - [Factory Mode](#factory-mode)\n - [Agent Mode](#agent-mode)\n - [Object](#object)\n - [JsonLike](#jsonlike)\n - [`__init__(json=dict(), **kwargs)`](#__init__jsondict-kwargs)\n - [`__str__()`](#__str__)\n - [`is_list()`](#is_list)\n - [`get_property(p_key, p_default=None)`](#get_propertyp_key-p_defaultnone)\n - [`get_properties(p_keys, p_defaults=None)`](#get_propertiesp_keys-p_defaultsnone)\n - [`set_property(p_key, p_value)`](#set_propertyp_key-p_value)\n - [`keys()`](#keys)\n - [`from_json(json, **kwargs)`](#from_jsonjson-kwargs)\n - [`to_json()`](#to_json)\n - [Location(JsonLike)](#locationjsonlike)\n - [`__str__()`](#__str__-1)\n - [BaiduMapObject(JsonLike)](#baidumapobjectjsonlike)\n - [`from_uid(handle, detail=False)`](#from_uidhandle-detailfalse)\n - [`from_address(handle, detail=False)`](#from_addresshandle-detailfalse)\n - [`from_location(handle, detail=False)`](#from_locationhandle-detailfalse)\n - [Exception](#exception)\n - [BaiduMapApiException](#baidumapapiexception)\n - [HandleNotExistsError](#handlenotexistserror)\n - [NetError](#neterror)\n - [OtherError](#othererror)\n - [How to work](#how-to-work)\n - [core](#core)\n - [collector](#collector)\n - [controller](#controller)\n - [status](#status)\n - [util](#util)\n - [dict_tool & list_tool](#dict_tool--list_tool)\n - [url](#url)\n - [Log](#log)\n - [config](#config)\n\n\n\n## Description\n\nThis module is an python sdk for baidu map. You are only required to write a few code to get an execllent effect. Also, this module may work on another map api after some modification\n\nAuthor: cpak00@github\n\nEmail: cymcpak00@gmail.com\n\n## Get Start\n\n```bash\npip install baidumap\n```\n\ninstall with pip, requestes required only.\n\n test.py\n```python\nfrom baidumap import config\nfrom baidumap.api.handle import get_handle\nfrom baidumap.object import BaiduMapObject\n\nimport logging\n\n# get raw handler\nak_key = 'ZAMW5**********************'\nraw_handler = get_handle(ak_key)\n\n# get logger\nFORMAT = \"%(asctime)s %(thread)d %(message)s\"\nlogging.basicConfig(format=FORMAT, datefmt=\"[%Y-%m-%d %H:%M:%S]\")\nlogger = logging.getLogger()\nlogger.setLevel(logging.DEBUG)\n\nif __name__ == '__main__':\n # log config(no necessary)\n config.mode = config.value.DEBUG\n config.logger = logger\n\n # Agent Mode\n print('---\\nAgent Mode:\\n')\n raw_handler = get_handle(ak_key)\n thu_main = BaiduMapObject(address='\u5317\u4eac\u5e02\u6e05\u534e\u5927\u5b66\u7d2b\u8346\u56ed\u9910\u5385')\n thu_main.from_address(raw_handler)\n print('from address find location: %s' %\n thu_main.get_property('location')['location'])\n print('from address find location: %s' % thu_main.get_properties(\n ['lat', 'lng'], p_defaults={'lat': '-1',\n 'lng': '-1'}))\n thu_main.from_location(raw_handler)\n print('\\nfrom location find uid: %s' % thu_main.get_property('uid'))\n print('and its name: %s' % thu_main.get_property('name'))\n\n find_location = thu_main.get_properties(\n ['uid', 'name'], p_defaults={'uid': '',\n 'name': ''})\n print('--\\nfrom location find uid and name: %s' % find_location)\n\n for index in find_location:\n thu_main.from_json(find_location[index])\n thu_main.from_uid(raw_handler, detail=True)\n print('-\\nfrom uid find info:\\n%s' % thu_main)\n\n # Factory Mode\n print('---\\nFactory Mode:\\n')\n iplocer = get_handle(ak_key, 'location/ip')\n iplocer.set_params()\n print(\n iplocer.run(collect_keys=['address', 'content']).get_property(\n 'address')['address'])\n\n print('---\\n')\n placeser = get_handle(\n ak_key,\n 'place/v2/search',\n is_list=True,\n query='ATM\u673a',\n tag='\u94f6\u884c',\n region='\u5317\u4eac', )\n print(\n placeser.run(page_size=20, max_page_num=1, max_result_num=15)\n .get_property('address'))\n\n # complex sample\n\n # get zijing dormitory location from agent mode\n thu_main = BaiduMapObject(address='\u5317\u4eac\u5e02\u6e05\u534e\u5927\u5b66\u7d2b\u8346\u5bbf\u820d')\n thu_main.from_address(raw_handler)\n thu_location = thu_main.get_property('location')['location']\n print('---\\n\\n\u8d77\u59cb\u5750\u6807: %s' % (thu_location))\n\n # get circle search handle from factory mode\n # sort by distance\n circleser = get_handle(\n ak_key,\n 'place/v2/search',\n is_list=True,\n query='\u706b\u8f66\u7ad9',\n scope=2,\n filter='sort_name:distance|sort_rule:1')\n\n circleser.set_params(radius=10000, location=thu_location)\n nearest_station = circleser.run(max_result_num=5)['results'][0]\n\n station_location = nearest_station.get_property('location')['location']\n print('\u4e07\u7c73\u5185\u6700\u8fd1\u7684\u706b\u8f66\u7ad9: %s' % (nearest_station.get_property('name')['name']))\n\n # get a handle to find road to hospital\n router = get_handle(ak_key, 'direction/v2/transit', is_list=True)\n\n router.set_params(origin=thu_location, destination=station_location)\n\n result = router.run()\n # print(repr(result))\n station = result.get_properties(['on_station', 'off_station'])\n print('\u63d0\u53d6\u51fa\u5168\u90e8\u7684on_station, off_station\u5c5e\u6027(\u5730\u94c1\u7ad9\u540d)')\n print(station)\n```\n\n## Handle\n\n### Factory Mode\n\nGet handle from factory function `get_handle`\n```python\nfrom baidumap.api.handle import get_handle\n```\n\nUse name from [baidu map web api](http://lbsyun.baidu.com/index.php?title=webapi)\n\n---\n\nSample\n\nFrom [baidu map web api](http://lbsyun.baidu.com/index.php?title=webapi)\n\n\u884c\u653f\u533a\u5212\u533a\u57df\u68c0\u7d22\n\nhttp://api.map.baidu.com/place/v2/search?query=ATM\u673a&tag=\u94f6\u884c®ion=\u5317\u4eac&output=json&ak=\u60a8\u7684ak //GET\u8bf7\u6c42\n\nThe api path is `http://api.map.baidu.com/place/v2/search` (the / in the end or not is very important)\n\nSo this handle's name is `place/v2/search`(just remove the head of the api path)\n\n```python\n# ak_key is the authority key of the baidu map\n# you need to apply for it from ('http://lbsyun.baidu.com/index.php?title=%E9%A6%96%E9%A1%B5')\nak_key = '********************'\n\n# you can set params when get it from factory\n# (the first two is not params, they are ak_key and handle's name)\n# set is_list: true to get multi-page result\nplace_search = get_handle(ak_key, 'place/v2/search', is_list=True)\n\n# then use method set_params() to set request parameter\nplace_search.set_params(query='ATM\u673a', region='\u5317\u4eac')\n\n# use method run to get result\n\n# you can limit the max_page_num(=-1 not limited), page_size(=10, limited by baidu mao), max_result_num(=-1 not limited) and interval(=0 second between each request(too frequently request will be block by baidu map and baidumap.api will raise baidumap.api.exceptions.BaiduMapApiException))\n\n# place_search.run([max_page_num=-1[, max_result_num=-1[, page_size=10[, interval=0]]]])\natm_in_beijing = place_search.run(max_page_num=3, page_size=20, max_result_num=55, interval=0.5)\n\n# get result\nprint(atm_in_beijing)\n# get property(find mode)\n# will return a dict\nprint(atm_in_beijing.get_property('address'))\n```\n\n---\n\n### Agent Mode\n\nYou can also use handle by __agent mode__\n\nfirst. you need to import **BaiduMapObject**\n```python\nfrom baidumap.object import BaiduMapObject\n```\n\nsecond. you need to create a BaiduMapObject with some keys and values\n```python\nthu_main = BaiduMapObject(address='\u5317\u4eac\u5e02\u6e05\u534e\u5927\u5b66\u7d2b\u8346\u5bbf\u820d')\n```\n\nthen. you need to create a raw handle with *ak_key*\n```python\nraw_handle = get_handle(ak_key)\n```\n\nfinally. you just call the agent method to fill the data of **BaiduMapObject**\n```python\n# geography decoder call\n# it will fill Object with location\nthu_main.from_address(handle)\n\n# geography encoder call\n# it will fill Object with address and uid found by location\n# it will create a list-like BaiduMapObject\nthu_main.from_location(handle)\n\n# create a object with uid\nthu_main = BaiduMapObject(uid=thu_main.get_property('uid')[0].uid)\n\n# uid info call\n# it will fill Object with detail info found by uid\nthu_main.from_uid(handle, detail=True)\n```\n\n## Object\n\n### JsonLike\n\n#### `__init__(json=dict(), **kwargs)`\n\n>**JsonLike** object can be inited with **list** or **dict**, you can replace some parameters by decalre *kwargs*\n\n#### `__str__()`\n\n>**JsonLike** object will be transfered as **str** just like **dict**\n\n#### `is_list()`\n\n>**JsonLike** object can be *dict-like* or *list-like* determined by which one init it\n\n#### `get_property(p_key, p_default=None)`\n\n>if you want to read value of **JsonLike**, this method is suggested, it will return a *dict*.\n\n>if there is only one result, it will return *dict* as {key: value}\n\n>if there are more results, it will return a *list-like* *dict* which contains location\n\n#### `get_properties(p_keys, p_defaults=None)`\n\n>you can combine two or more properties in one *list-like* *dict*\n\n#### `set_property(p_key, p_value)`\n\n>you can't set key-value using <**JsonLike**>[key]=value\n\n>you are supposed to set property with this method, if there is no *p_key* in the **JsonLike** object, you can not set it with this method\n\n#### `keys()`\n\n>return the keys in the **JsonLike** object\n\n#### `from_json(json, **kwargs)`\n\n>reconstruct the **JsonLike** object by *dict* or *list*, can replace some properties by *kwargs* \n\n#### `to_json()`\n\n>return *dict* in **JsonLike**\n\n### Location(JsonLike)\n\n#### `__str__()`\n\n>location will be formatted as lat,lng\n\n### BaiduMapObject(JsonLike)\n\n#### `from_uid(handle, detail=False)`\n\n>fill the **BaiduMapObject** by uid\n\n>needs a handle with valid ak_key\n\n#### `from_address(handle, detail=False)`\n\n>fill the **BaiduMapObject** by address\n\n>needs a handle with valid ak_key\n\n#### `from_location(handle, detail=False)`\n\n>fill the **BaiduMapObject** by location\n\n>needs a handle with valid ak_key\n\n## Exception\n\n### BaiduMapApiException\n\n>base exception\n\n>in baidumap.api.exception\n\n### HandleNotExistsError\n\n>inherit from **BaiduMapApiException**\n\n>handle name not valid\n\n>in baidumao.api.exception\n\n### NetError\n\n>inherit from **BaiduMapApiException**\n\n>net connection broken\n\n>in baidumao.api.exception\n\n### OtherError\n\n>inherit from **BaiduMapApiException**\n\n>error which can not be recognized\n\n>in baidumao.api.exception\n\n## How to work\n\n### core\n\n>package baidumap.core\n\n#### collector\n\n#### controller\n\n#### status\n\n### util\n\n>package baidumap.util\n\n#### dict_tool & list_tool\n\nsome safe operation functions\n\n#### url\n\nclass **Url**\n\nmanager the params and url path\n\nuse package *requests* to get json by http request[GET]\n\n## Log\n\n### config\n\n*baidumap.config.mode*\n\nvalue | description\n---------------------|------------------------\nconfig.value.DEBUG | log detail information\nconfig.value.INFO | log important statement\nconfig.value.WARNING | log unsafe statement\nconfig.value.ERROR | log error\nconfig.value.NONE | no log\n\n*baidumap.config.filename*\n\nvalue | description\n-----------|-------------\nNone | directly record in shell\n**Logger** | use module logging", "description_content_type": "text/markdown", "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/cpak00/baidumap", "keywords": "baidu,map,api,web", "license": "MIT Licence", "maintainer": "", "maintainer_email": "", "name": "baidumap", "package_url": "https://pypi.org/project/baidumap/", "platform": "any", "project_url": "https://pypi.org/project/baidumap/", "project_urls": { "Homepage": "https://github.com/cpak00/baidumap" }, "release_url": "https://pypi.org/project/baidumap/1.2.4/", "requires_dist": null, "requires_python": "", "summary": "api handle for baidu map", "version": "1.2.4" }, "last_serial": 4426687, "releases": { "1.0.0": [ { "comment_text": "", "digests": { "md5": "4b654356c4eff77c5750511d2a882f59", "sha256": "491b308d6fb5ec87b70fad42b63b85b1e9bb714115c1a8b7de33971bca8838a4" }, "downloads": -1, "filename": "baidumap-1.0.0.tar.gz", "has_sig": false, "md5_digest": "4b654356c4eff77c5750511d2a882f59", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 6310, "upload_time": "2018-10-29T06:52:48", "url": "https://files.pythonhosted.org/packages/7e/64/92776547ad6c1246ea529e51c0126ab04b4d86030e4851dfb3535594e9ce/baidumap-1.0.0.tar.gz" } ], "1.2.4": [ { "comment_text": "", "digests": { "md5": "1392732a9aead20ca21be2a0701a8fa6", "sha256": "5a3965fb2126699af7d89f23228b3269f71924509b667ccb41e41222710a8f57" }, "downloads": -1, "filename": "baidumap-1.2.4.tar.gz", "has_sig": false, "md5_digest": "1392732a9aead20ca21be2a0701a8fa6", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 11329, "upload_time": "2018-10-29T08:31:16", "url": "https://files.pythonhosted.org/packages/07/c0/2d4abcc5480ff191cfd902e75127e151eeb248b57d90c50bd72fa9b5c96c/baidumap-1.2.4.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "1392732a9aead20ca21be2a0701a8fa6", "sha256": "5a3965fb2126699af7d89f23228b3269f71924509b667ccb41e41222710a8f57" }, "downloads": -1, "filename": "baidumap-1.2.4.tar.gz", "has_sig": false, "md5_digest": "1392732a9aead20ca21be2a0701a8fa6", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 11329, "upload_time": "2018-10-29T08:31:16", "url": "https://files.pythonhosted.org/packages/07/c0/2d4abcc5480ff191cfd902e75127e151eeb248b57d90c50bd72fa9b5c96c/baidumap-1.2.4.tar.gz" } ] }