In [71]:
import pywikibot
In [64]:
import pandas as pd

import matplotlib.pyplot as plt
%matplotlib inline
In [3]:
site = pywikibot.Site('en', 'wikipedia')  # any site will work, this is just an example
page = pywikibot.Page(site, 'Douglas Adams')
In [ ]:
# page?
In [7]:
item = pywikibot.ItemPage.fromPage(page)
In [8]:
item?
In [9]:
repo = site.data_repository()
In [10]:
repo
Out[10]:
DataSite("wikidata", "wikidata")
In [12]:
ser=pd.Series(item.sitelinks)
In [85]:
ser.head()
Out[85]:
arwiki               دوغلاس آدمز
arzwiki             دوجلاس ادامز
astwiki            Douglas Adams
azwiki              Duqlas Adams
azwikiquote    Duqlas Noel Adams
dtype: object
In [19]:
if "P31" in item.claims:
    print (item.claims['P31'][0].getTarget())
[[wikidata:Q5]]
In [21]:
item.claims?
In [28]:
item_Berlin = pywikibot.ItemPage.fromPage(pywikibot.Page(site,'Berlin'))
In [48]:
item_Berlin.claims['P625'][0].getTarget()
Out[48]:
Coordinate(lat=52.516666666667, lon=13.383333333333, globe=earth)
In [56]:
for i in item_Berlin.claims['P17']:  #Object with border to Berlin
    print (i.getTarget())
[[wikidata:Q183]]
[[wikidata:Q16957]]
[[wikidata:Q55300]]
[[wikidata:Q7318]]
[[wikidata:Q41304]]
[[wikidata:Q43287]]
[[wikidata:Q150981]]
[[wikidata:Q27306]]
[[wikidata:Q157367]]
[[wikidata:Q148499]]
In [61]:
for i in item_Berlin.claims['P625']:
    print (i.getTarget())
{
    "altitude": null,
    "globe": "http://www.wikidata.org/entity/Q2",
    "latitude": 52.516666666667,
    "longitude": 13.383333333333,
    "precision": 0.016666666666667
}
In [86]:
plt.title('Minimalistic Map of Berlin')
plt.plot(item_Berlin.claims['P625'][0].getTarget().lon,item_Berlin.claims['P625'][0].getTarget().lat,"x");
In [66]:
item_Berlin.claims['P625'][0].getTarget().lat
Out[66]:
52.516666666667
In [72]:
import mplleaflet
In [74]:
fig=plt.figure(figsize=(17,10))

plt.plot(item_Berlin.claims['P625'][0].getTarget().lon,item_Berlin.claims['P625'][0].getTarget().lat,"o");

mplleaflet.display(fig=fig)
Out[74]:
In [1]:
import ipyleaflet as ll
!wget "https://raw.githubusercontent.com/ellisonbg/ipyleaflet/master/examples/europe_110.geo.json"
--2016-12-14 12:36:24--  https://raw.githubusercontent.com/ellisonbg/ipyleaflet/master/examples/europe_110.geo.json
Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 151.101.32.133
Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|151.101.32.133|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 117900 (115K) [text/plain]
Saving to: ‘europe_110.geo.json.1’

europe_110.geo.json 100%[=====================>] 115.14K  --.-KB/s   in 0.007s 

2016-12-14 12:36:25 (16.6 MB/s) - ‘europe_110.geo.json.1’ saved [117900/117900]

In [2]:
import json
In [3]:
import sys, os, time

map = ll.Map(center=[53.88,27.45], zoom=4)

with open('./europe_110.geo.json') as f:
    data = json.load(f)

for feature in data['features']:
    feature['properties']['style'] = {
        'color': 'grey',
        'weight': 1,
        'fillColor': 'grey',
        'fillOpacity': 0.5
    }

layer = ll.GeoJSON(data=data, hover_style={'fillColor': 'red'})

def hover_handler(event=None, id=None, properties=None):
    sys.stdout.write("\r" + properties['geounit'])
    sys.stdout.flush()
    
layer.on_hover(hover_handler)
    
map.add_layer(layer)

map
Germany
In [84]:
!dir
Parse01.ipynb	database01.ipynb     pydata01.ipynb
Untitled.ipynb	europe_110.geo.json  untitled.txt
In [ ]: