Joplin & Python ( Static Map ) : How to create a Maps (JPEG) with REST API and data in Joplin ?

Step 0 : Install Joplin and activate the REST API ( https://joplin.cozic.net/api/ ) .

Step 1: Install staticmap with pip ( for more information see https://github.com/komoot/staticmap )

$ pip install staticmap
Collecting staticmap
  Downloading https://files.pythonhosted.org/packages/f9/9f/5a3843533eab037cba031486175c4db1b214614404a29516208ff228dead/staticmap-0.5.4.tar.gz
Collecting Pillow (from staticmap)
  Downloading https://files.pythonhosted.org/packages/c9/ed/27cc92e99b9ccaa0985a66133baeea7e8a3371d3c04cfa353aaa3b81aac1/Pillow-5.4.1-cp37-cp37m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl (3.7MB)
    100% |████████████████████████████████| 3.7MB 6.3MB/s 
Requirement already satisfied: requests in /usr/local/lib/python3.7/site-packages (from staticmap) (2.21.0)
Requirement already satisfied: chardet<3.1.0,>=3.0.2 in /usr/local/lib/python3.7/site-packages (from requests->staticmap) (3.0.4)
Requirement already satisfied: idna<2.9,>=2.5 in /usr/local/lib/python3.7/site-packages (from requests->staticmap) (2.8)
Requirement already satisfied: urllib3<1.25,>=1.21.1 in /usr/local/lib/python3.7/site-packages (from requests->staticmap) (1.24.1)
Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.7/site-packages (from requests->staticmap) (2018.11.29)
Building wheels for collected packages: staticmap
  Building wheel for staticmap (setup.py) ... done
  Stored in directory: /Users/..../Library/Caches/pip/wheels/fe/a6/a5/2acceb72471d85bd0498973aabd611e6ff1cdd48796790f047
Successfully built staticmap
Installing collected packages: Pillow, staticmap
Successfully installed Pillow-5.4.1 staticmap-0.5.4

The source code :

#
# Version 1
# for Python 3
# 
#   ARIAS Frederic
#   Sorry ... It's difficult for me the python :)
#

from time import gmtime, strftime
import time
import json
import requests
import os
import gmplot
from staticmap import StaticMap, CircleMarker

m = StaticMap(1600, 800, url_template='http://a.tile.osm.org/{z}/{x}/{y}.png')

marker = CircleMarker((10, 47), '#0036FF', 3)
m.add_marker(marker)

#Token
ip = "127.0.0.1"
port = "41184"

token = "Put your token here"
nb_request = 0
nb_plot = 0
headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
url_notes = (
    "http://"+ip+":"+port+"/notes?"
    "token="+token
)
try:
    resp = requests.get(url_notes, headers=headers)
    nb_request += 1
    resp.raise_for_status()
    resp_dict = resp.json()
    #print(resp_dict)
    for my_note in resp_dict:
        #print(my_note.get('id'))
        url_notes2 = (
    "http://"+ip+":"+port+"/notes/"+my_note.get('id')+"?fields=longitude,latitude&"
    "token="+token
)
        try:
           resp2 = requests.get(url_notes2, headers=headers)
           nb_request += 1
           resp2.raise_for_status()
           resp_dict2 = resp2.json()
           #print(resp_dict2)
           long = resp_dict2.get('longitude')
           lat = resp_dict2.get('latitude')
           if (long != '0.00000000'):
              nb_plot += 1
              marker = CircleMarker((float(long), float(lat)), '#0036FF', 12)
              m.add_marker(marker)
        except requests.exceptions.HTTPError as e:
             print("Bad HTTP status code:", e)
        except requests.exceptions.RequestException as e:
             print("Network error:", e)
except requests.exceptions.HTTPError as e:
    print("Bad HTTP status code:", e)
except requests.exceptions.RequestException as e:
    print("Network error:", e)

image = m.render(zoom=1)
image.save('mymap_zoom1.png')
image = m.render(zoom=2)
image.save('mymap_zoom2.png')
image = m.render(zoom=3)
image.save('mymap_zoom3.png')
image = m.render(zoom=4)
image.save('mymap_zoom4.png')
image = m.render(zoom=5)
image.save('mymap_zoom5.png')
image = m.render(zoom=6)
image.save('mymap_zoom6.png')

print ("Number total of request",nb_request)
print ("Number total of pin",nb_plot)

On same folder, you see mymap_zoom*.png .

To change the size of map, it’s here : ( by default 1600 x 800 )

m = StaticMap(1600, 800, url_template='http://a.tile.osm.org/{z}/{x}/{y}.png')

Time to build the pictures :

$ time python3 JoplinToStaticMap.py 
Number total of request 1927
Number total of pin 643

real	0m18.137s
user	0m7.936s
sys	0m0.876s

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *

Time limit is exhausted. Please reload CAPTCHA.