<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          這可能是 Python 里最強(qiáng)的繪制地圖神器

          共 12292字,需瀏覽 25分鐘

           ·

          2021-02-26 12:24

          點(diǎn)擊上方“AirPython”,選擇“置頂公眾號(hào)

          第一時(shí)間關(guān)注Python技術(shù)干貨!


          作者:錢(qián)魏Way
          原文:https://www.biaodianfu.com/folium.html

          大家好,我是安果!


          今天給大家介紹一個(gè)非常 NB 的Python 庫(kù),專門(mén)用來(lái)繪制地圖的,它叫 Folium 

          1. Folium簡(jiǎn)介

          Folium是一個(gè)基于leaflet.js的Python地圖庫(kù),其中,Leaflet是一個(gè)非常輕的前端地圖可視化庫(kù)。即可以使用Python語(yǔ)言調(diào)用Leaflet的地圖可視化能力。它不單單可以在地圖上展示數(shù)據(jù)的分布圖,還可以使用Vincent/Vega在地圖上加以標(biāo)記。Folium可以讓你用Python強(qiáng)大生態(tài)系統(tǒng)來(lái)處理數(shù)據(jù),然后用Leaflet地圖來(lái)展示。

          Folium中有許多來(lái)自O(shè)penStreetMap、MapQuest Open、MapQuestOpen Aerial、Mapbox和Stamen的內(nèi)建地圖元件,而且支持使用Mapbox或Cloudmade的API密鑰來(lái)定制個(gè)性化的地圖元件。Folium支持GeoJSON和TopoJSON兩種文件格式的疊加,也可以將數(shù)據(jù)連接到這兩種文件格式的疊加層,最后可使用color-brewer配色方案創(chuàng)建分布圖。

          2. Folium的使用

          地圖的生成

          img

          folium.folium.Map()詳解

          folium.folium.Map(location=None, width='100%', height='100%', left='0%', top='0%', position='relative', tiles='OpenStreetMap', attr=None, min_zoom=0, max_zoom=18, zoom_start=10, min_lat=-90, max_lat=90, min_lon=-180, max_lon=180, max_bounds=False, crs='EPSG3857', control_scale=False, prefer_canvas=False, no_touch=False, disable_3d=False, png_enabled=False, zoom_control=True, **kwargs)

          參數(shù)說(shuō)明

          • location (tuple or list, default None):緯度和經(jīng)度

          • width (pixel int or percentage string (default: ‘100%’)):地圖寬度

          • height (pixel int or percentage string (default: ‘100%’)):地圖高度

          • tiles (str, default ‘OpenStreetMap’) :瓦片名稱或使用TileLayer classass.

          • min_zoom (int, default 0):地圖可縮放的最小級(jí)別

          • max_zoom (int, default 18):地圖可縮放的最大級(jí)別

          • zoom_start (int, default 10) :地圖的初始縮放級(jí)別

          • attr (string, default None):當(dāng)使用自定義瓦片時(shí),傳入自定義瓦片的名詞

          • crs (str, default ‘EPSG3857’) :投影坐標(biāo)系標(biāo)識(shí)

          • EPSG3857: Web墨卡托投影后的平面地圖,坐標(biāo)單位為米。大部分國(guó)外地圖使用的時(shí)該標(biāo)準(zhǔn)。

          • EPSG4326: Web墨卡托投影后的平面地圖,但仍然使用WGS84的經(jīng)度、緯度表示坐標(biāo)。

          • EPSG3395: 墨卡托投影,主要用于航海圖

          • Simple: 簡(jiǎn)單的x,y匹配,用于自定義瓦片(比如游戲地圖)

          • control_scale (bool, default False) :是否在地圖上顯示縮放標(biāo)尺

          • prefer_canvas (bool, default False):強(qiáng)制使用Canvas渲染

          • no_touch (bool, default False) :是否允許觸摸事件

          • disable_3d (bool, default False) :強(qiáng)制使用CSS 3D效果

          • zoom_control (bool, default True) :是否要限制zoom操作

          • **kwargs:Leaflets地圖類的其他參數(shù): https://leafletjs.com/reference-1.5.1.html#map

          “tiles”參數(shù)可選值:

          • “OpenStreetMap”

          • “Mapbox Bright” (Limited levels of zoom for free tiles)

          • “Mapbox Control Room” (Limited levels of zoom for free tiles)

          • “Stamen” (Terrain, Toner, and Watercolor)

          • “Cloudmade” (Must pass API key)

          • “Mapbox” (Must pass API key)

          • “CartoDB” (positron and dark_matter)

          “tiles”的自定義設(shè)置:

          img

          地球上同一個(gè)地理位置的經(jīng)緯度,在不同的坐標(biāo)系中,會(huì)有少量偏移,國(guó)內(nèi)目前常見(jiàn)的坐標(biāo)系主要分為三種:

          • 地球坐標(biāo)系——WGS84:常見(jiàn)于GPS設(shè)備,Google地圖等國(guó)際標(biāo)準(zhǔn)的坐標(biāo)體系。

          • 火星坐標(biāo)系——GCJ-02:中國(guó)國(guó)內(nèi)使用的被強(qiáng)制加密后的坐標(biāo)體系,高德坐標(biāo)就屬于該種坐標(biāo)體系。

          • 百度坐標(biāo)系——BD-09:百度地圖所使用的坐標(biāo)體系,是在火星坐標(biāo)系的基礎(chǔ)上又進(jìn)行了一次加密處理。

          所以在設(shè)置“tiles”時(shí)需要考慮目前手中得經(jīng)緯度屬于那種坐標(biāo)系。

          由于投影坐標(biāo)系中沒(méi)有GCJ-02和BD-09對(duì)應(yīng)的標(biāo)識(shí),所以在自定義瓦片時(shí)主要經(jīng)緯度能匹配上,crs中的設(shè)置可保持不變。更多詳情介紹請(qǐng)看:瓦片坐標(biāo)系學(xué)習(xí)

          如果需要將地圖保存,只需執(zhí)行:m.save(“map.html”) 即可。

          添加點(diǎn)、線、面要素

          添加點(diǎn)

          import folium
          m = folium.Map(location=[39.917834116.397036], zoom_start=13, width='50%',height='50%', zoom_control='False',
                         tiles='http://webrd02.is.autonavi.com/appmaptile?lang=zh_cn&size=1&scale=1&style=8&x={x}&y={y}&z={z}&ltype=6',attr='AutoNavi')

          tooltip ='請(qǐng)點(diǎn)擊我查看該點(diǎn)信息'
          folium.Marker([39.937282,116.403187], popup='南鑼鼓巷',tooltip=tooltip).add_to(m)
          folium.Marker([39.917834,116.397036], popup='故宮',tooltip=tooltip).add_to(m)
          folium.Marker([39.928614,116.391746], popup='北海公園', tooltip=tooltip, icon=folium.Icon(color='red')).add_to(m)
          folium.Marker([39.942143,116.382590], popup='后海公園', tooltip=tooltip, icon=folium.Icon(color='green', prefix='fa', icon='taxi')).add_to(m)

          m

          img

          Folium.Icon類可以設(shè)置color, icon_color, icon, angle, prefix這5個(gè)參數(shù):

          • color的可選項(xiàng)包括:[‘red’, ‘blue’, ‘green’, ‘purple’, ‘orange’, ‘darkred’, ‘lightred’, ‘beige’, ‘darkblue’, ‘darkgreen’, ‘cadetblue’, ‘darkpurple’, ‘white’, ‘pink’, ‘lightblue’, ‘lightgreen’, ‘gray’, ‘black’, ‘lightgray’] ,或者HTML顏色代碼

          • icon_color同上

          • icon可以在Font-Awesome網(wǎng)站中找到對(duì)應(yīng)的名字,并設(shè)置prefix參數(shù)為’fa’

          • angle以度為單位設(shè)置

          其他:

          m.add_child(folium.LatLngPopup()) #顯示鼠標(biāo)點(diǎn)擊點(diǎn)經(jīng)緯度
          m.add_child(folium.ClickForMarker(popup='Waypoint')) # 將鼠標(biāo)點(diǎn)擊點(diǎn)添加到地圖上

          添加圓

          folium.Circle(
              radius=300,
              location=[39.928614,116.391746],
              popup='北海公園',
              color='crimson',
              fill=False,
          ).add_to(m)
          folium.CircleMarker(
              location=[39.942143,116.382590],
              radius=50,
              popup='后海公園',
              color='#3186cc',
              fill=True,
              fill_color='#3186cc'
          ).add_to(m)

          img

          Circle和CircleMarker的不同:CircleMarker的radius一個(gè)單位是像素,Circle的一個(gè)單位時(shí)米

          添加線段

          folium.PolyLine([
              [39.917834,116.397036],
              [39.928614,116.391746],
              [39.937282,116.403187],
              [39.942143,116.382590]
          ],color='red').add_to(m)

          img

          添加多邊形

          folium.Marker([39.917834,116.397036], popup='故宮').add_to(m)
          folium.Marker([39.928614,116.391746], popup='北海公園').add_to(m)
          folium.Marker([39.937282,116.403187], popup='南鑼鼓巷').add_to(m)
          folium.Marker([39.942143,116.382590], popup='后海公園').add_to(m)

          folium.Polygon([
              [39.917834,116.397036],
              [39.928614,116.391746],
              [39.942143,116.382590],
              [39.937282,116.403187],
          ],color='blue', weight=2, fill=True, fill_color='blue', fill_opacity=0.3).add_to(m)

          img

          Folium的其他高級(jí)應(yīng)用

          在地圖上顯示前200條犯罪數(shù)據(jù)

          import folium
          import pandas as pd

          san_map = folium.Map(location=[37.77-122.42], zoom_start=12,width='50%',height='50%')

          # cdata = pd.read_csv('https://cocl.us/sanfran_crime_dataset')
          cdata = pd.read_csv('Police_Department_Incidents_-_Previous_Year__2016_.csv'#犯罪數(shù)據(jù),包含犯罪所在經(jīng)緯度

          # get the first 200 crimes in the cdata
          limit = 200
          data = cdata.iloc[0:limit, :]
          # Instantiate a feature group for the incidents in the dataframe
          incidents = folium.map.FeatureGroup()
          # Loop through the 200 crimes and add each to the incidents feature group
          for lat, lng, in zip(cdata.Y, data.X):
              incidents.add_child(
                  folium.CircleMarker(
                      [lat, lng],
                      radius=7# define how big you want the circle markers to be
                      color='yellow',
                      fill=True,
                      fill_color='red',
                      fill_opacity=0.4
                  )
              )

          san_map.add_child(incidents)

          img

          統(tǒng)計(jì)區(qū)域犯罪總數(shù)

          from folium import plugins

          # let's start again with a clean copy of the map of San Francisco
          san_map = folium.Map(location=[37.77-122.42], zoom_start=12,width='50%',height='50%')

          # instantiate a mark cluster object for the incidents in the dataframe
          incidents = plugins.MarkerCluster().add_to(san_map)

          # loop through the dataframe and add each data point to the mark cluster
          for lat, lng, label, in zip(data.Y, data.X, cdata.Category):
              folium.Marker(
                  location=[lat, lng],
                  icon=None,
                  popup=label,
              ).add_to(incidents)

          # add incidents to map
          san_map.add_child(incidents)

          img

          以熱力圖的方式呈現(xiàn)

          from folium.plugins import HeatMap

          san_map = folium.Map(location=[37.77-122.42], zoom_start=12,width='50%',height='50%')

          # Convert data format
          heatdata = data[['Y','X']].values.tolist()

          # add incidents to map
          HeatMap(heatdata).add_to(san_map)

          san_map

          img

          在地圖上呈現(xiàn)GeoJSON邊界數(shù)據(jù)

          import json
          import requests

          # url = 'https://cocl.us/sanfran_geojson'
          url = 'san-francisco.geojson'
          san_geo = f'{url}'
          san_map = folium.Map(location=[37.77-122.42], zoom_start=12,width='50%',height='50%')
          folium.GeoJson(
              san_geo,
              style_function=lambda feature: {
                  'fillColor''#ffff00',
                  'color''blue',
                  'weight'2,
                  'dashArray''5, 5'
              }
          ).add_to(san_map)

          san_map

          img

          在GeoJSON上繪制Choropleth分級(jí)著色圖

          # Count crime numbers in each neighborhood
          disdata = pd.DataFrame(cdata['PdDistrict'].value_counts())
          disdata.reset_index(inplace=True)
          disdata.rename(columns={'index':'Neighborhood','PdDistrict':'Count'},inplace=True)

          san_map = folium.Map(location=[37.77-122.42], zoom_start=12,width='50%',height='50%')

          folium.Choropleth(
              geo_data=san_geo,
              data=disdata,
              columns=['Neighborhood','Count'],
              key_on='feature.properties.DISTRICT',
              #fill_color='red',
              fill_color='YlOrRd',
              fill_opacity=0.7,
              line_opacity=0.2,
              highlight=True,
              legend_name='Crime Counts in San Francisco'
          ).add_to(san_map)

          san_map

          img

          3. 各地圖提供商瓦片服務(wù)地圖規(guī)則

          高德地圖

          目前高德的瓦片地址有如下兩種:

          • http://wprd0{1-4}.is.autonavi.com/appmaptile?x={x}&y={y}&z={z}&lang=zh_cn&size=1&scl=1&style=7&ltype=1

          • http://webst0{1-4}.is.autonavi.com/appmaptile?style=7&x={x}&y={y}&z={z}

          前者是高德的新版地址,后者是老版地址。

          高德新版的參數(shù):

          • lang:可以通過(guò)zh_cn設(shè)置中文,en設(shè)置英文

          • size:基本無(wú)作用

          • scl:瓦片尺寸控制,1=256,2=512

          • style:設(shè)置影像和路網(wǎng),style=6為衛(wèi)星圖,style=7為街道圖,style=8為標(biāo)注圖

          • ltype:線性控制,增加后,只對(duì)地圖要素進(jìn)行控制,沒(méi)有文字注記,要素多少,是否透明

          這些規(guī)律并不是絕對(duì)的,有可能有的組合某些參數(shù)不起作用。

          谷歌地圖

          目前谷歌的瓦片地址也存在兩種:

          • 國(guó)內(nèi):http://mt{0-3}.google.cn/vt/lyrs=m&hl=zh-CN&gl=cn&x={x}&y={y}&z={z}

          • 國(guó)外:http://mt{0-3}.google.com/vt/lyrs=m&hl=zh-CN&gl=cn&x={x}&y={y}&z={z}

          參數(shù)詳解:

          • lyrs = 類型

          • h = roads only 僅限道路

          • m = standard roadmap 標(biāo)準(zhǔn)路線圖

          • p = terrain 帶標(biāo)簽的地形圖

          • r = somehow altered roadmap 某種改變的路線圖

          • s = satellite only 僅限衛(wèi)星

          • t = terrain only 僅限地形

          • y = hybrid 帶標(biāo)簽的衛(wèi)星圖

          • gl = 坐標(biāo)系

          • CN = 中國(guó)火星坐標(biāo)系

          • hl = 地圖文字語(yǔ)言

          • zh-CN = 中文

          • en-US = 英文

          • x = 瓦片橫坐標(biāo)

          • y = 瓦片縱坐標(biāo)

          • z = 縮放級(jí)別 衛(wèi)星圖0-14,路線圖0-17

          百度地圖

          百度當(dāng)前的瓦片地址:

          • http://online{0-4}.map.bdimg.com/onlinelabel/?qt=tile&x={x}&y={y}&z={z}&styles=pl&udt=202004151&scaler=2&p=0

          • http://api{0-3}.map.bdimg.com/customimage/tile?&x={x}&y={y}&z={z}&udt=20180601&scale=1

          • http://its.map.baidu.com:8002/traffic/TrafficTileService?level={z}&x={x}&y={y}&time=1373790856265&label=web2D&;v=017

          備注:瓦片地址中的x和y對(duì)應(yīng)的并不是經(jīng)緯度值,而是瓦片編號(hào),中國(guó)主要地圖商的瓦片編號(hào)流派:

          目前百度的瓦片編號(hào)比較特殊,F(xiàn)olium暫不支持。

          其他參考資料:

          • https://github.com/geometalab/pyGeoTile

          • https://github.com/anzhihun/OpenLayers3Primer/blob/master/ch05/05-03.md

          • http://www.winseliu.com/blog/2018/01/30/map-started-guide/

          • https://github.com/CntChen/tile-lnglat-transform

          騰訊地圖

          騰訊地圖的瓦片地圖URL格式:

          • http://rt1.map.gtimg.com/realtimerender?z={z}&x={x}&y={y}&type=vector&style=0

          由于騰訊地圖使用的瓦片編碼時(shí)TMS,所以使用時(shí)需要額外的設(shè)置。具體如下:


          其他底圖


          • {0,1,2,3}代表了url的subDomain,在請(qǐng)求時(shí)會(huì)隨機(jī)的在url中使用mt0、mt1、mt2、mt3。{z}代表zoom,即縮放級(jí)別,{x}代表列號(hào),{y}代表行號(hào)。

          • GeoQ 官網(wǎng)有公開(kāi)的多個(gè)基于 ArcGIS 的地圖服務(wù),均可使用,詳見(jiàn)https://map.geoq.cn/arcgis/rest/services

          4. 參考鏈接:

          • https://leafletjs.com/

          • https://python-visualization.github.io/folium/

          • http://openwhatevermap.xyz/






          覺(jué)得本文對(duì)你有幫助?請(qǐng)分享給更多人
          瀏覽 89
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  妻AV在线 | 免费在线观看亚洲 | 久久成年电影 | 日韩中文字幕视频在线 | 成人电影1区 |