# heuristic to find wrong coordinates and / or wrong iso-codes
# Find coordinates by iso-code that are far away from the average center of all coordinates with the same iso-code
# and sort them by descending distance from that center.
# This is a heuristic to find wrong coordinates and / or wrong iso-codes.
# If you get to the first point already inside the iso-code area,
# it is unlikely to find more errors further down below.
# Fix & rerun. You can change iso codes through variables iso1 and iso2 (you have to copy the script).

# Works best for iso areas that are close to cycle shape (Switzerland, as opposed to the USA).
# Original sql-script by Thomas Ledl.

import pywikibot
import re
from pywikibot import pagegenerators
import operator

site     = pywikibot.Site('de')

query = """\
use dewiki_p;
set @iso1:='PT';
#set @iso2:='' or code
set @iso2:='';

select sqrt((gt_lat - cen_gt_lat)*(gt_lat - cen_gt_lat) + (gt_lon - cen_gt_lon)*(gt_lon - cen_gt_lon)) as distanzungefaehr, 
       gt_lat, gt_lon, gt_page_id, gt_name,
       (select page_title from page where page_id = gt_page_id) as title,
       gt_country,gt_region from geo_tags,
       (select avg(gt_lat) cen_gt_lat,avg(gt_lon) cen_gt_lon,count(*) from geo_tags
        where 
        	case 
        		when @iso2=''
        		then gt_country=@iso1
        		else gt_country=@iso1 and gt_region=@iso2
        		end) as zentrumdavon
        where
        	case
            	when @iso2=''
                then gt_country=@iso1 
                else gt_country=@iso1 and gt_region=@iso2
                end
       order by 1 desc limit 100;
"""

for page in pagegenerators.MySQLPageGenerator(query, site):
    print (page.title())
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
/srv/paws/pwb/pywikibot/data/mysql.py in <module>()
     13 try:
---> 14     import oursql as mysqldb
     15 except ImportError:

ModuleNotFoundError: No module named 'oursql'

During handling of the above exception, another exception occurred:

ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-5-43a5a0dc909b> in <module>()
     42 """
     43 
---> 44 for page in pagegenerators.MySQLPageGenerator(query, site):
     45     print (page.title())

/srv/paws/pwb/pywikibot/pagegenerators.py in MySQLPageGenerator(query, site, verbose)
   2613     @return: generator which yield pywikibot.Page
   2614     """
-> 2615     from pywikibot.data import mysql
   2616 
   2617     if site is None:

/srv/paws/pwb/pywikibot/data/mysql.py in <module>()
     14     import oursql as mysqldb
     15 except ImportError:
---> 16     import MySQLdb as mysqldb
     17 
     18 import pywikibot

ModuleNotFoundError: No module named 'MySQLdb'