Using Python map drawing tool -- folium introduction

Posted by fredouille on Thu, 16 Dec 2021 04:24:38 +0100

1. Preparation

Some friends may not have used folium. It is actually a third-party library of python that specializes in drawing maps, so you need to install it before using it.

pip install folium

After installation, we can demonstrate in jupyterab as follows:

import folium

m = folium.Map()
m

For the above output, it is actually an interactive map, which supports zoom in, abbreviation, drag and so on.

If you want to store the output locally, you can:

m.save('map.html')

You can see that this file is saved locally, and the browser can be opened for interactive operation.

The above is an ordinary process

2. About Folium Map()

In the previous part, we can see that this map is directly a map. Here we will introduce some common parameters.

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,
)

There are so many parameters!!

Folium without parameters Map () will get a map of the world.

  • location: Map center, [40.002694, 116.322373] is the campus of Tsinghua University;
  • zoom_start: scale bar. The default is level 10, which is about the range of a city;

Other common parameters include:

  • Width and height: the length and width of the map. If it is int, it represents the pixel value; if it is str, it represents the percentage;
  • max_zoom: the maximum scale of the map that can be manually adjusted. The default is level 18;
  • control_scale: whether to add a scale bar to the map. The default is False;
  • no_touch: whether manual operation is prohibited. The default value is False;
  • tiles: map style. The default is OpenStreetMap
  • attr: if you set a non built-in map style, you need to pass in this value, which can be understood as the name of the selected map style

The above are some commonly used parameters, and the most commonly used are location and zoom_start and tiles, etc.

There are also several built-in map styles:

- "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)

Let's simply try location and zoo_start parameter:

import folium

m = folium.Map([40.002694, 116.322373],
               zoom_start=15,
               control_scale=True
              )
m

You can see the campus of Tsinghua University

The above briefly introduces the parameters of Map. Next, let's take a look at the selection of Map basemap style~

3. Style of built-in map basemap

We can see that folium actually has several built-in map basemap styles, some of which need to apply for key. Because I didn't apply successfully, I won't make a demonstration.

  • "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)

Topographic base map

m = folium.Map([40.002694, 116.322373],
               tiles='Stamen Terrain',
               zoom_start=15,
               control_scale=True
              )
m

Black and white unmarked basemap

m = folium.Map([40.002694, 116.322373],
               tiles='Stamen Toner',
               zoom_start=15,
               control_scale=True
              )
m

Ink painting base map

m = folium.Map([40.002694, 116.322373],
               tiles='Stamen Watercolor',
               zoom_start=15,
               control_scale=True
              )
m

The above is some display of the style of the built-in map base map. If you need a key, you can apply on this website:

http://openwhatevermap.xyz/ (unfortunately, I can't go up)

In addition, you can also find some map basemaps here

http://leaflet-extras.github....

I will also study these map basemap styles later and try to share more interesting maps with you.

Of course, most of the maps we use in China are Gaode, Baidu and Tencent maps. Next, let's play!

4. Various third-party map basemap styles

Here I will demonstrate Gaode map, Zhitu GeoQ and Tencent map

4.1. Gaude map

Chinese and English maps, satellite images, street maps and conventional maps of Gaode map

Chinese and English map

folium.Map([40.002694, 116.322373],
           tiles='https://webrd02.is.autonavi.com/appmaptile?lang=zh_en&size=1&scale=1&style=8&x={x}&y={y}&z={z}',
           attr='Gaode-Both Chinese and English',
           zoom_start=15,
          )

English only map

folium.Map([40.002694, 116.322373],
           tiles='https://webrd02.is.autonavi.com/appmaptile?lang=en&size=1&scale=1&style=8&x={x}&y={y}&z={z}',
           attr='Gaode-Pure English',
           zoom_start=15,
          )

Satellite image map

tiles = 'https://webst02.is.autonavi.com/appmaptile?style=6&x={x}&y={y}&z={z}'
folium.Map([40.002694, 116.322373],
           tiles= tiles,
           attr='Gaode-Satellite image map',
           zoom_start=15,
          )

Street Map

folium.Map([40.002694, 116.322373],
           tiles= 'https://wprd01.is.autonavi.com/appmaptile?x={x}&y={y}&z={z}&lang=zh_cn&size=1&scl=1&style=8&ltype=11',
           attr='Gaode-Street road network map',
           zoom_start=10,
          )

General diagram

folium.Map([40.002694, 116.322373],
           tiles= 'https://wprd01.is.autonavi.com/appmaptile?x={x}&y={y}&z={z}&lang=zh_cn&size=1&scl=1&style=7',
           attr='Gaode-General diagram',
           zoom_start=15,
          )

4.2. Zhitu GeoQ

Anyway, I think this is very good and easy to use

Multi style maps, ready to use

Color version

m = folium.Map([40.002694, 116.322373],
               tiles='http://map.geoq.cn/ArcGIS/rest/services/ChinaOnlineCommunity/MapServer/tile/{z}/{y}/{x}',
               attr='Color version',
               zoom_start=15,
              )
m

Warm version

m = folium.Map([40.002694, 116.322373],
               tiles='http://map.geoq.cn/ArcGIS/rest/services/ChinaOnlineStreetWarm/MapServer/tile/{z}/{y}/{x}',
               attr='Warm version',
               zoom_start=15,
              )
m

Grey version

m = folium.Map([40.002694, 116.322373],
               tiles='http://map.geoq.cn/ArcGIS/rest/services/ChinaOnlineStreetGray/MapServer/tile/{z}/{y}/{x}',
               attr='Grey version',
               zoom_start=15,
              )
m

Blue black version

m = folium.Map([40.002694, 116.322373],
               tiles='http://map.geoq.cn/ArcGIS/rest/services/ChinaOnlineStreetPurplishBlue/MapServer/tile/{z}/{y}/{x}',
               attr='Blue black version',
               zoom_start=15,
              )
m

English version

m = folium.Map([40.002694, 116.322373],
               tiles='http://map.geoq.cn/ArcGIS/rest/services/ChinaOnlineCommunityENG/MapServer/tile/{z}/{y}/{x}',
               attr='English version',
               zoom_start=15,
              )
m

China's administrative division boundary

m = folium.Map([40.002694, 116.322373],
               tiles='http://thematic.geoq.cn/arcgis/rest/services/ThematicMaps/administrative_division_boundaryandlabel/MapServer/tile/{z}/{y}/{x}',
               attr='China's administrative division boundary',
              )
m

Special topic of water system

m = folium.Map([40.002694, 116.322373],
               tiles='http://thematic.geoq.cn/arcgis/rest/services/ThematicMaps/WorldHydroMap/MapServer/tile/{z}/{y}/{x}',
               attr='Special topic of water system',
              )
m

Street network

m = folium.Map([40.002694, 116.322373],
               tiles='http://thematic.geoq.cn/arcgis/rest/services/StreetThematicMaps/Gray_OnlySymbol/MapServer/tile/{z}/{y}/{x}',
               attr='Street network',
              )
m

Warm street network

m = folium.Map([40.002694, 116.322373],
               tiles='http://thematic.geoq.cn/arcgis/rest/services/StreetThematicMaps/Warm_OnlySymbol/MapServer/tile/{z}/{y}/{x}',
               attr='Warm color-Street network',
              )
m

4.3. Tencent map

tiles =  'https://rt0.map.gtimg.com/tile?z={z}&x={x}&y={-y}'
folium.Map([39.904989, 116.405285],
           tiles= tiles,
           attr='Tencent map'          
          )

4.4. Sky map

https://www.tianditu.gov.cn/

You need to register a key

Sky map image

m = folium.Map([40.002694, 116.322373],
               tiles='http://t7.tianditu.gov.cn/img_w/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=img&STYLE=default&TILEMATRIXSET=w&FORMAT=tiles&TILEMATRIX={z}&TILEROW={y}&TILECOL={x}&tk=',
               attr='Sky map-image'
              )
m

Sky map image annotation

m = folium.Map([40.002694, 116.322373],
               tiles='http://t7.tianditu.gov.cn/cia_w/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=cia&STYLE=default&TILEMATRIXSET=w&FORMAT=tiles&TILEMATRIX={z}&TILEROW={y}&TILECOL={x}&tk=',
               attr='Sky map-Image annotation'
              )
m

Sky map vector

m = folium.Map([40.002694, 116.322373],
               tiles='http://t7.tianditu.gov.cn/vec_w/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=vec&STYLE=default&TILEMATRIXSET=w&FORMAT=tiles&TILEMATRIX={z}&TILEROW={y}&TILECOL={x}&tk=',
               attr='Sky map-vector',
               zoom_start=10,
              )
m

Vector annotation of sky map

m = folium.Map([40.002694, 116.322373],
               tiles='http://t7.tianditu.gov.cn/cva_w/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=cva&STYLE=default&TILEMATRIXSET=w&FORMAT=tiles&TILEMATRIX={z}&TILEROW={y}&TILECOL={x}&tk=',
               attr='Sky map-Vector annotation'
              )
m

Sky map terrain

m = folium.Map([40.002694, 116.322373],
               tiles='http://t7.tianditu.gov.cn/ter_w/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=ter&STYLE=default&TILEMATRIXSET=w&FORMAT=tiles&TILEMATRIX={z}&TILEROW={y}&TILECOL={x}&tk=',
               attr='Sky map-terrain',
               zoom_start=3,
              )
m

Topographic annotation of sky map

m = folium.Map([40.002694, 116.322373],
               tiles='http://t7.tianditu.gov.cn/cta_w/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=cta&STYLE=default&TILEMATRIXSET=w&FORMAT=tiles&TILEMATRIX={z}&TILEROW={y}&TILECOL={x}&tk=',
               attr='Sky map-Topographic marker',
               zoom_start=3,
              )
m

Baidu map failed the test on my side. I haven't found a suitable replacement scheme for the time being.

5. Supplement

In fact, we can also find more map underlay tile URL s to replace and diversify our map drawing.

In addition, when mapping with longitude and latitude coordinate points, such as marking points, drawing areas, thermal map drawing, etc., you need to consider which map system the longitude and latitude coordinates are under, and then draw with the relevant base map of the corresponding map system!

end of document

Your favorite collection is my greatest encouragement!
Welcome to follow me, share Python dry goods and exchange Python technology.
If you have any opinions on the article or any technical problems, please leave a message in the comment area for discussion!

Topics: Python Back-end