濮阳杆衣贸易有限公司

主頁(yè) > 知識(shí)庫(kù) > Python 里最強(qiáng)的地圖繪制神器

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

熱門標(biāo)簽:螳螂科技外呼系統(tǒng)怎么用 遼寧智能外呼系統(tǒng)需要多少錢 400電話申請(qǐng)資格 阿里電話機(jī)器人對(duì)話 電銷機(jī)器人系統(tǒng)廠家鄭州 qt百度地圖標(biāo)注 舉辦過(guò)冬奧會(huì)的城市地圖標(biāo)注 地圖地圖標(biāo)注有嘆號(hào) 正安縣地圖標(biāo)注app

今天給大家介紹一個(gè)非常 NB 的Python 庫(kù),專門用來(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.917834, 116.397036], zoom_start=13, width='50%',height='50%', zoom_control='False', tiles='http://webrd02.is.autonavi.com/appmaptile?lang=zh_cnsize=1scale=1style=8x={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ù):

  1. color的可選項(xiàng)包括:[‘red', ‘blue', ‘green', ‘purple', ‘orange', ‘darkred', ‘lightred', ‘beige', ‘darkblue', ‘darkgreen', ‘cadetblue', ‘darkpurple', ‘white', ‘pink', ‘lightblue', ‘lightgreen', ‘gray', ‘black', ‘lightgray'] ,或者HTML顏色代碼
  2. icon_color同上
  3. icon可以在Font-Awesome網(wǎng)站中找到對(duì)應(yīng)的名字,并設(shè)置prefix參數(shù)為'fa'
  4. 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) 

添加多邊形

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) 

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) 

統(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) 

以熱力圖的方式呈現(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 

在地圖上呈現(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 

在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 

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

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

  • http://wprd0{1-4}.is.autonavi.com/appmaptile?x={x}y={y}z={z}lang=zh_cnsize=1scl=1style=7ltype=1
  • http://webst0{1-4}.is.autonavi.com/appmaptile?style=7x={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=mhl=zh-CNgl=cnx={x}y={y}z={z}

  • 國(guó)外:

http://mt{0-3}.google.com/vt/lyrs=mhl=zh-CNgl=cnx={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=tilex={x}y={y}z={z}styles=pludt=202004151scaler=2p=0
  • http://api{0-3}.map.bdimg.com/customimage/tile?x={x}y={y}z={z}udt=20180601scale=1
  • http://its.map.baidu.com:8002/traffic/TrafficTileService?level={z}x={x}y={y}time=1373790856265label=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=vectorstyle=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

到此這篇關(guān)于Python 里最強(qiáng)的地圖繪制神器的文章就介紹到這了,更多相關(guān)Python地圖繪制神器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • Python基礎(chǔ)之畫圖神器matplotlib
  • Python繪制K線圖之可視化神器pyecharts的使用
  • Python繪制詞云圖之可視化神器pyecharts的方法
  • python 視頻下載神器(you-get)的具體使用
  • 微軟開(kāi)源最強(qiáng)Python自動(dòng)化神器Playwright(不用寫一行代碼)
  • python實(shí)現(xiàn)跨年表白神器--你值得擁有
  • Python 可視化神器Plotly詳解
  • 地圖可視化神器kepler.gl python接口的使用方法
  • 10款最佳Python開(kāi)發(fā)工具推薦,每一款都是神器
  • 推薦技術(shù)人員一款Python開(kāi)源庫(kù)(造數(shù)據(jù)神器)
  • 詳解Python可視化神器Yellowbrick使用
  • Python實(shí)戰(zhàn)之能監(jiān)控文件變化的神器—看門狗

標(biāo)簽:淘寶好評(píng)回訪 昭通 興安盟 合肥 隨州 信陽(yáng) 阜新 濟(jì)源

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Python 里最強(qiáng)的地圖繪制神器》,本文關(guān)鍵詞  Python,里最,強(qiáng)的,地圖,繪制,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《Python 里最強(qiáng)的地圖繪制神器》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于Python 里最強(qiáng)的地圖繪制神器的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    驻马店市| 于田县| 含山县| 柏乡县| 盘锦市| 大城县| 石景山区| 福州市| 庆阳市| 新龙县| 广德县| 北辰区| 怀化市| 神池县| 万州区| 万安县| 临沧市| 大城县| 泰来县| 柳江县| 佛学| 万荣县| 辉县市| 嘉峪关市| 晋城| 桐乡市| 海宁市| 南皮县| 洮南市| 兖州市| 芜湖市| 呼伦贝尔市| 花莲县| 花垣县| 梅河口市| 鄂托克旗| 土默特右旗| 乐都县| 荣成市| 科尔| 松阳县|