{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 地图绘制入门\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 基础地图 WGS84" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "# import plotly.io as pio\n", "# pio.renderers.default = \"notebook_connected\"\n", "\n", "from plotly import graph_objects as go" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "基础数据:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "data = []\n", "scattermapbox = go.Scattermapbox() \n", "data.append(scattermapbox)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "布局:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "mapbox_kargs = dict(\n", " zoom=10,\n", " center=dict(\n", " lat=30.272934,\n", " lon=120.147376,\n", " ),\n", " style=\"open-street-map\",\n", " accesstoken='!!! replace with your mapbox ak !!!'\n", ")\n", "\n", "layout_kargs = dict(\n", " autosize=False,\n", " width=1000,\n", " height=600,\n", " margin=dict(\n", " r=0, t=38, l=0, b=0, pad=0\n", " ),\n", ")\n", "\n", "layout = go.Layout(\n", " mapbox=mapbox_kargs,\n", " **layout_kargs\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "渲染:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "61ba53ea27f44d7b963ae4eb9ca0b449", "version_major": 2, "version_minor": 0 }, "text/plain": [ "FigureWidget({\n", " 'data': [{'type': 'scattermapbox', 'uid': '83fa933b-be58-4374-8efd-e01e1dab8424'}],\n", " 'la…" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "fig = go.Figure(\n", " data=data,\n", " layout=layout\n", ")\n", "f = go.FigureWidget(fig)\n", "f" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "可选的地图样式:(无需 ak)`\"open-street-map\"`,`\"carto-positron\"`,`\"stamen-terrain\"` 等 ;(需要 ak)`\"basic\"`,`\"streets\"`,`\"outdoors\"` 等。\n", "\n", "## 使用高德地图 GCJ-02" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "8300da19e8e14492a0c3f29085bfadf9", "version_major": 2, "version_minor": 0 }, "text/plain": [ "FigureWidget({\n", " 'data': [{'type': 'scattermapbox', 'uid': '24bd986d-7bcb-4add-bdc5-d29bd2164867'}],\n", " 'la…" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "import plotly.graph_objects as go\n", "\n", "data = []\n", "\n", "scattermapbox = go.Scattermapbox() \n", "data.append(scattermapbox)\n", "\n", "basemap_layer = [\n", " dict(\n", " below=\"traces\",\n", " sourcetype=\"raster\",\n", " sourceattribution=\"高德地图\",\n", " source=[\n", " \"http://wprd01.is.autonavi.com/appmaptile?x={x}&y={y}&z={z}&lang=zh_cn&size=1&scl=1&style=7\"\n", " ]\n", " )\n", "]\n", "\n", "mapbox_kargs = dict(\n", " zoom=10,\n", " center=dict(\n", " lat=30.272934,\n", " lon=120.147376,\n", " ),\n", " style=\"white-bg\",\n", " layers=basemap_layer,\n", ")\n", "\n", "layout_kargs = dict(\n", " autosize=False,\n", " width=1000,\n", " height=500,\n", " margin=dict(\n", " r=0, t=38, l=0, b=0, pad=0\n", " ),\n", ")\n", "\n", "layout = go.Layout(\n", " mapbox=mapbox_kargs,\n", " **layout_kargs\n", ")\n", "\n", "fig = go.Figure(\n", " data=data,\n", " layout=layout\n", ")\n", "\n", "go.FigureWidget(fig)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 使用天地图 CGCS2000" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "d362bde2084440a29859ddf6d2b549e7", "version_major": 2, "version_minor": 0 }, "text/plain": [ "FigureWidget({\n", " 'data': [{'type': 'scattermapbox', 'uid': 'a2c317db-c82d-4a87-a15c-60420e1e4012'}],\n", " 'la…" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "import plotly.graph_objects as go\n", "\n", "data = []\n", "\n", "scattermapbox = go.Scattermapbox() \n", "data.append(scattermapbox)\n", "\n", "tk = \"!!! replace with your tianditu tk !!!\"\n", "layer = \"vec\"\n", "basemap_layer = [\n", " dict(\n", " below=\"traces\",\n", " sourcetype=\"raster\",\n", " sourceattribution=\"天地图\",\n", " source=[\n", " \"https://t0.tianditu.gov.cn/{layer}_w/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER={layer}&STYLE=default&TILEMATRIXSET=w&FORMAT=tiles&TILECOL={{x}}&TILEROW={{y}}&TILEMATRIX={{z}}&tk={tk}\".format(layer=layer, tk=tk)\n", " ]\n", " ),\n", " dict(\n", " below=\"traces\",\n", " sourcetype=\"raster\",\n", " source=[\n", " \"https://t3.tianditu.gov.cn/cva_w/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=cva&STYLE=default&TILEMATRIXSET=w&FORMAT=tiles&TILECOL={{x}}&TILEROW={{y}}&TILEMATRIX={{z}}&tk={tk}\".format(tk=tk)\n", " ]\n", " )\n", "]\n", "\n", "mapbox_kargs = dict(\n", " zoom=10,\n", " center=dict(\n", " lat=30.272934,\n", " lon=120.147376,\n", " ),\n", " style=\"carto-positron\",\n", " layers=basemap_layer,\n", ")\n", "\n", "layout_kargs = dict(\n", " autosize=False,\n", " width=1200,\n", " height=500,\n", " margin=dict(\n", " r=0, t=38, l=0, b=0, pad=0\n", " ),\n", ")\n", "\n", "layout = go.Layout(\n", " mapbox=mapbox_kargs,\n", " **layout_kargs\n", ")\n", "\n", "fig = go.Figure(\n", " data=data,\n", " layout=layout\n", ")\n", "\n", "go.FigureWidget(fig)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3.10.4 ('tvmx': conda)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.4" }, "orig_nbformat": 4, "vscode": { "interpreter": { "hash": "e579259ee6098e2b9319de590d145b4b096774fe457bdf04260e3ba5c171e887" } } }, "nbformat": 4, "nbformat_minor": 2 }