Difference between revisions of "Geograpy"
(16 intermediate revisions by the same user not shown) | |||
Line 3: | Line 3: | ||
{{OsProject | {{OsProject | ||
|id=geograpy3 | |id=geograpy3 | ||
+ | |state=active | ||
|owner=somnathrakshit | |owner=somnathrakshit | ||
|title=geograpy | |title=geograpy | ||
|url=https://github.com/somnathrakshit/geograpy3 | |url=https://github.com/somnathrakshit/geograpy3 | ||
− | |version=0. | + | |version=0.2.6 |
− | |date= | + | |date=2023-04-08 |
+ | |since=2018-09-18 | ||
|storemode=property | |storemode=property | ||
}} | }} | ||
+ | =tickets= | ||
+ | |||
+ | =Freitext= | ||
= What is it? = | = What is it? = | ||
Geograpy3 is a Python library to extract geographic details like: | Geograpy3 is a Python library to extract geographic details like: | ||
Line 19: | Line 24: | ||
== Examples == | == Examples == | ||
=== Example 1 - London 2012 Olympic torch relay route === | === Example 1 - London 2012 Olympic torch relay route === | ||
− | Let's take the [https:// | + | Let's take the [https://en.wikipedia.org/wiki/2012_Summer_Olympics_torch_relay Wikipedia article on the 2012 London Olympics torch relay route]. In this article quite a few countries, regions and cities are mentioned. |
Let's extract that information using geograpy3 | Let's extract that information using geograpy3 | ||
==== Code ==== | ==== Code ==== | ||
Line 26: | Line 31: | ||
<source lang='python'> | <source lang='python'> | ||
import geograpy | import geograpy | ||
− | url='https:// | + | url='https://en.wikipedia.org/wiki/2012_Summer_Olympics_torch_relay' |
− | places = geograpy.get_geoPlace_context(url = url) | + | places = geograpy.get_geoPlace_context(url = url) |
print(places) | print(places) | ||
</source> | </source> | ||
==== Result ==== | ==== Result ==== | ||
+ | <source lang='bash'> | ||
+ | python example1.py | ||
+ | </source> | ||
<source lang='json'> | <source lang='json'> | ||
− | countries=['Jersey', 'Guernsey', 'Greece', 'Belarus', 'South Africa', 'Australia', 'New Zealand', ' | + | countries=['Ireland', 'Jersey', 'Guernsey', 'Turkey', 'Greece', 'United Kingdom', 'Belarus', 'South Africa', 'Australia', 'New Zealand', 'Germany', 'France', 'Jamaica', 'Antigua and Barbuda', 'Montserrat', 'United States', 'Canada', 'Japan'] |
− | regions=[' | + | regions=['Hackney', 'Davy', 'Ireland', 'Burscough', 'Jersey', 'Munich', 'Newton Aycliffe', 'British/Irish', 'Plymouth', 'Greece', 'Thirsk', 'Wales', 'Locog', 'Cumbrian', 'Lincolnshire', 'Guernsey', 'Cardiff', 'Torch', 'Host', 'Cambridge', 'Bristol Harbour', 'Falmouth', 'Athens', 'Turkey', 'Wiltshire', 'British', 'England', 'United Kingdom', 'Sheffield', 'London', 'Aberaeron', 'Abraham', 'Northern Ireland', 'Wanted', 'East', 'Heathrow', 'Gravesend', 'Essex', 'Maidstone', 'Cornwall', 'Hyde Park', 'Hera', 'Swansea', 'Caerphilly', 'Taunton Lancashire', 'Stamford', 'Dublin', 'Derry', 'Portland', 'Ioannina', 'Scotland', 'Bangor', 'Engineering', 'Land'] |
− | cities=['Dublin', ' | + | cities=['Ioannina', 'Athens', 'Dublin', 'Hyde Park', 'Swansea', 'Sheffield', 'Portland', 'Cardiff', 'Cambridge', 'Bangor', 'Thirsk', 'Stamford', 'Plymouth', 'Newton Aycliffe', 'Maidstone', 'London', 'Hackney', 'Gravesend', 'Falmouth', 'Caerphilly', 'Burscough', 'Aberaeron', 'Munich', 'Derry', 'England', 'Scotland', 'Essex', 'Turkey', 'Davy', 'Ireland', 'Lincolnshire', 'Wales', 'Guernsey', 'Cornwall', 'Heathrow', 'Hera'] |
− | other=[] | + | other=['British', 'British', 'East'] |
</source> | </source> | ||
Line 75: | Line 83: | ||
title | title | ||
geograpy Tables | geograpy Tables | ||
− | + | 2021-08-13 | |
− | [[https://github.com/somnathrakshit/geograpy3 © 2020 geograpy3 project]] | + | [[https://github.com/somnathrakshit/geograpy3 © 2020-2021 geograpy3 project]] |
end title | end title | ||
package geograpy3 { | package geograpy3 { | ||
Line 96: | Line 104: | ||
} | } | ||
class countries << Entity >> { | class countries << Entity >> { | ||
− | |||
country : TEXT | country : TEXT | ||
+ | countryCoord : TEXT | ||
countryGDP_perCapita : FLOAT | countryGDP_perCapita : FLOAT | ||
− | countryIsoCode : TEXT | + | countryIsoCode : TEXT |
countryLabel : TEXT | countryLabel : TEXT | ||
countryPopulation : FLOAT | countryPopulation : FLOAT | ||
Line 112: | Line 120: | ||
regionLabel : TEXT | regionLabel : TEXT | ||
regionPopulation : FLOAT | regionPopulation : FLOAT | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
} | } | ||
class cityPops << Entity >> { | class cityPops << Entity >> { | ||
Line 148: | Line 132: | ||
} | } | ||
class citiesWithPopulation << Entity >> { | class citiesWithPopulation << Entity >> { | ||
+ | cityLabel : TEXT | ||
cityPop : FLOAT | cityPop : FLOAT | ||
city_name : TEXT | city_name : TEXT | ||
− | |||
− | |||
country_iso_code : TEXT | country_iso_code : TEXT | ||
country_name : TEXT | country_name : TEXT | ||
geoname_id : TEXT | geoname_id : TEXT | ||
− | |||
− | |||
− | |||
subdivision_1_iso_code : TEXT | subdivision_1_iso_code : TEXT | ||
subdivision_1_name : TEXT | subdivision_1_name : TEXT | ||
− | |||
− | |||
− | |||
wikidataurl : TEXT | wikidataurl : TEXT | ||
+ | } | ||
+ | class Version << Entity >> { | ||
+ | version : TEXT <<PK>> | ||
+ | } | ||
+ | class cities_wikidata << Entity >> { | ||
+ | country_wikidataid : TEXT | ||
+ | lat : TEXT | ||
+ | level : INTEGER | ||
+ | locationKind : TEXT | ||
+ | lon : TEXT | ||
+ | name : TEXT | ||
+ | partOf : TEXT | ||
+ | population : INTEGER | ||
+ | region_wikidataid : TEXT | ||
+ | wikidataid : TEXT <<PK>> | ||
+ | } | ||
+ | class regions_wikidata << Entity >> { | ||
+ | country_wikidataid : TEXT | ||
+ | iso : TEXT | ||
+ | lat : FLOAT | ||
+ | level : INTEGER | ||
+ | locationKind : TEXT | ||
+ | lon : FLOAT | ||
+ | name : TEXT | ||
+ | population : FLOAT | ||
+ | wikidataid : TEXT <<PK>> | ||
+ | } | ||
+ | class countries_wikidata << Entity >> { | ||
+ | iso : TEXT | ||
+ | lat : FLOAT | ||
+ | level : INTEGER | ||
+ | locationKind : TEXT | ||
+ | lon : FLOAT | ||
+ | name : TEXT | ||
+ | population : FLOAT | ||
+ | wikidataid : TEXT <<PK>> | ||
} | } | ||
} | } | ||
' BITPlan Corporate identity skin params | ' BITPlan Corporate identity skin params | ||
− | ' Copyright (c) 2015 BITPlan GmbH | + | ' Copyright (c) 2015-2020 BITPlan GmbH |
' see http://wiki.bitplan.com/PlantUmlSkinParams#BITPlanCI | ' see http://wiki.bitplan.com/PlantUmlSkinParams#BITPlanCI | ||
' skinparams generated by com.bitplan.restmodelmanager | ' skinparams generated by com.bitplan.restmodelmanager | ||
Line 246: | Line 259: | ||
' end of skinparams ' | ' end of skinparams ' | ||
</uml> | </uml> | ||
+ | |||
== Cities == | == Cities == | ||
The cities table is derived from the [http://dev.maxmind.com/geoip/geoip2/geolite2/ GeoLite2 by MaxMind] database | The cities table is derived from the [http://dev.maxmind.com/geoip/geoip2/geolite2/ GeoLite2 by MaxMind] database | ||
Line 261: | Line 275: | ||
PREFIX pq: <http://www.wikidata.org/prop/qualifier/> | PREFIX pq: <http://www.wikidata.org/prop/qualifier/> | ||
# get City details with Country | # get City details with Country | ||
− | SELECT DISTINCT ?country ?countryLabel ?countryIsoCode ?countryPopulation ? | + | SELECT DISTINCT ?country ?countryLabel ?countryIsoCode ?countryCoord ?countryPopulation ?continent ?continentLabel |
− | # instance of | + | WHERE { |
− | ?country wdt:P31/wdt:P279* wd: | + | # instance of Country |
+ | ?country wdt:P31/wdt:P279* wd:Q6256 . | ||
+ | # VALUES ?country { wd:Q55}. | ||
# label for the country | # label for the country | ||
?country rdfs:label ?countryLabel filter (lang(?countryLabel) = "en"). | ?country rdfs:label ?countryLabel filter (lang(?countryLabel) = "en"). | ||
+ | # get the continent (s) | ||
+ | #OPTIONAL { | ||
+ | # ?country wdt:P30 ?continent. | ||
+ | # ?continent rdfs:label ?continentLabel filter (lang(?continentLabel) = "en"). | ||
+ | #} | ||
# get the coordinates | # get the coordinates | ||
− | ?country wdt:P625 ? | + | OPTIONAL { |
+ | ?country wdt:P625 ?countryCoord. | ||
+ | } | ||
# https://www.wikidata.org/wiki/Property:P297 ISO 3166-1 alpha-2 code | # https://www.wikidata.org/wiki/Property:P297 ISO 3166-1 alpha-2 code | ||
?country wdt:P297 ?countryIsoCode. | ?country wdt:P297 ?countryIsoCode. | ||
− | # population of country | + | # population of country |
− | ?country wdt:P1082 ? | + | OPTIONAL |
+ | { | ||
+ | SELECT ?country (max(?countryPopulationValue) as ?countryPopulation) | ||
+ | WHERE { | ||
+ | ?country wdt:P1082 ?countryPopulationValue | ||
+ | } group by ?country | ||
+ | } | ||
# https://www.wikidata.org/wiki/Property:P2132 | # https://www.wikidata.org/wiki/Property:P2132 | ||
− | # | + | # nominal GDP per capita |
− | ?country wdt:P2132 ? | + | # OPTIONAL { ?country wdt:P2132 ?countryGDP_perCapitaValue. } |
} | } | ||
+ | ORDER BY ?countryIsoCode | ||
</source> | </source> | ||
− | [https://query.wikidata.org/#%23%20get%20a%20list%20of%20countries%0A%23%20for%20geograpy3%20library%0A%23%20see%20https%3A%2F%2Fgithub.com%2Fsomnathrakshit%2Fgeograpy3%2Fissues%2F15 | + | [https://query.wikidata.org/#%23%20get%20a%20list%20of%20countries%0A%23%20for%20geograpy3%20library%0A%23%20see%20https%3A%2F%2Fgithub.com%2Fsomnathrakshit%2Fgeograpy3%2Fissues%2F15%0APREFIX%20rdfs%3A%20%3Chttp%3A%2F%2Fwww.w3.org%2F2000%2F01%2Frdf-schema%23%3E%0APREFIX%20wd%3A%20%3Chttp%3A%2F%2Fwww.wikidata.org%2Fentity%2F%3E%0APREFIX%20wdt%3A%20%3Chttp%3A%2F%2Fwww.wikidata.org%2Fprop%2Fdirect%2F%3E%0APREFIX%20p%3A%20%3Chttp%3A%2F%2Fwww.wikidata.org%2Fprop%2F%3E%0APREFIX%20ps%3A%20%3Chttp%3A%2F%2Fwww.wikidata.org%2Fprop%2Fstatement%2F%3E%0APREFIX%20pq%3A%20%3Chttp%3A%2F%2Fwww.wikidata.org%2Fprop%2Fqualifier%2F%3E%0A%23%20get%20City%20details%20with%20Country%0ASELECT%20DISTINCT%20%3Fcountry%20%3FcountryLabel%20%3FcountryIsoCode%20%20%3FcountryCoord%20%20%3FcountryPopulation%20%3Fcontinent%20%3FcontinentLabel%0AWHERE%20%7B%0A%20%20%23%20instance%20of%20Country%0A%20%20%3Fcountry%20wdt%3AP31%2Fwdt%3AP279%2a%20wd%3AQ6256%20.%0A%20%20%23%20VALUES%20%3Fcountry%20%7B%20wd%3AQ55%7D.%0A%20%20%23%20label%20for%20the%20country%0A%20%20%3Fcountry%20rdfs%3Alabel%20%3FcountryLabel%20filter%20%28lang%28%3FcountryLabel%29%20%3D%20%22en%22%29.%0A%20%20%23%20get%20the%20continent%20%28s%29%0A%20%20%23OPTIONAL%20%7B%0A%20%20%23%20%20%3Fcountry%20wdt%3AP30%20%3Fcontinent.%0A%20%20%23%20%20%3Fcontinent%20rdfs%3Alabel%20%3FcontinentLabel%20filter%20%28lang%28%3FcontinentLabel%29%20%3D%20%22en%22%29.%0A%20%20%23%7D%0A%20%20%23%20get%20the%20coordinates%0A%20%20OPTIONAL%20%7B%20%0A%20%20%20%20%20%20%3Fcountry%20wdt%3AP625%20%3FcountryCoord.%0A%20%20%7D%20%0A%20%20%23%20https%3A%2F%2Fwww.wikidata.org%2Fwiki%2FProperty%3AP297%20ISO%203166-1%20alpha-2%20code%0A%20%20%3Fcountry%20wdt%3AP297%20%3FcountryIsoCode.%0A%20%20%23%20population%20of%20country%20%20%20%0A%20%20OPTIONAL%0A%20%20%7B%20%0A%20%20%20%20SELECT%20%3Fcountry%20%28max%28%3FcountryPopulationValue%29%20as%20%3FcountryPopulation%29%0A%20%20%20%20WHERE%20%7B%0A%20%20%20%20%20%20%3Fcountry%20wdt%3AP1082%20%3FcountryPopulationValue%0A%20%20%20%20%7D%20group%20by%20%3Fcountry%0A%20%20%7D%0A%20%20%23%20https%3A%2F%2Fwww.wikidata.org%2Fwiki%2FProperty%3AP2132%0A%20%20%23%20nominal%20GDP%20per%20capita%0A%20%20%23%20OPTIONAL%20%7B%20%3Fcountry%20wdt%3AP2132%20%3FcountryGDP_perCapitaValue.%20%7D%0A%7D%0AORDER%20BY%20%3FcountryIsoCode try it!] - 204 results in some 7.9 s as of 2021-12 |
== Regions == | == Regions == | ||
+ | The regions list is derived from Wikidata | ||
+ | <source lang='sparql'> | ||
+ | # get a list of regions | ||
+ | # for geograpy3 library | ||
+ | # see https://github.com/somnathrakshit/geograpy3/issues/15 | ||
+ | PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> | ||
+ | PREFIX wd: <http://www.wikidata.org/entity/> | ||
+ | PREFIX wdt: <http://www.wikidata.org/prop/direct/> | ||
+ | PREFIX wikibase: <http://wikiba.se/ontology#> | ||
+ | SELECT DISTINCT ?country ?countryLabel ?countryIsoCode ?region ?regionLabel ?regionIsoCode ?regionPopulation ?location | ||
+ | WHERE | ||
+ | { | ||
+ | # administrative unit of first order | ||
+ | ?region wdt:P31/wdt:P279* wd:Q10864048. | ||
+ | OPTIONAL { | ||
+ | ?region rdfs:label ?regionLabel filter (lang(?regionLabel) = "en"). | ||
+ | } | ||
+ | # isocode state/province (mandatory - filters historic regions while at it ...) | ||
+ | # filter historic regions | ||
+ | # FILTER NOT EXISTS {?region wdt:P576 ?end} | ||
+ | { | ||
+ | SELECT ?region (max(?regionAlpha2) as ?regionIsoCode) (max(?regionPopulationValue) as ?regionPopulation) (max(?locationValue) as ?location) | ||
+ | WHERE { | ||
+ | ?region wdt:P300 ?regionAlpha2. | ||
+ | # get the population | ||
+ | # https://www.wikidata.org/wiki/Property:P1082 | ||
+ | OPTIONAL { | ||
+ | ?region wdt:P1082 ?regionPopulationValue | ||
+ | } | ||
+ | # get he location | ||
+ | # https://www.wikidata.org/wiki/Property:P625 | ||
+ | OPTIONAL { | ||
+ | ?region wdt:P625 ?locationValue. | ||
+ | } | ||
+ | } GROUP BY ?region | ||
+ | } | ||
+ | # # https://www.wikidata.org/wiki/Property:P297 | ||
+ | OPTIONAL { | ||
+ | ?region wdt:P17 ?country. | ||
+ | # label for the country | ||
+ | ?country rdfs:label ?countryLabel filter (lang(?countryLabel) = "en"). | ||
+ | ?country wdt:P297 ?countryIsoCode. | ||
+ | } | ||
+ | } ORDER BY ?regionIsoCode | ||
+ | </source> | ||
+ | [https://query.wikidata.org/#%23%20get%20a%20list%20of%20regions%0A%23%20for%20geograpy3%20library%0A%23%20see%20https%3A%2F%2Fgithub.com%2Fsomnathrakshit%2Fgeograpy3%2Fissues%2F15%0APREFIX%20rdfs%3A%20%3Chttp%3A%2F%2Fwww.w3.org%2F2000%2F01%2Frdf-schema%23%3E%0APREFIX%20wd%3A%20%3Chttp%3A%2F%2Fwww.wikidata.org%2Fentity%2F%3E%0APREFIX%20wdt%3A%20%3Chttp%3A%2F%2Fwww.wikidata.org%2Fprop%2Fdirect%2F%3E%0APREFIX%20wikibase%3A%20%3Chttp%3A%2F%2Fwikiba.se%2Fontology%23%3E%0ASELECT%20DISTINCT%20%3Fcountry%20%3FcountryLabel%20%3FcountryIsoCode%20%3Fregion%20%3FregionLabel%20%3FregionIsoCode%20%3FregionPopulation%20%3Flocation%0AWHERE%0A%7B%0A%20%20%23%20administrative%20unit%20of%20first%20order%0A%20%20%3Fregion%20wdt%3AP31%2Fwdt%3AP279%2a%20wd%3AQ10864048.%0A%20%20OPTIONAL%20%7B%0A%20%20%20%20%20%3Fregion%20rdfs%3Alabel%20%3FregionLabel%20filter%20%28lang%28%3FregionLabel%29%20%3D%20%22en%22%29.%0A%20%20%7D%0A%20%20%23%20isocode%20state%2Fprovince%20%28mandatory%20-%20filters%20historic%20regions%20while%20at%20it%20...%29%0A%20%20%23%20filter%20historic%20regions%0A%20%20%23%20FILTER%20NOT%20EXISTS%20%7B%3Fregion%20wdt%3AP576%20%3Fend%7D%0A%20%20%7B%20%0A%20%20%20%20SELECT%20%3Fregion%20%28max%28%3FregionAlpha2%29%20as%20%3FregionIsoCode%29%20%28max%28%3FregionPopulationValue%29%20as%20%3FregionPopulation%29%20%28max%28%3FlocationValue%29%20as%20%3Flocation%29%0A%20%20%20%20WHERE%20%7B%0A%20%20%20%20%20%20%3Fregion%20wdt%3AP300%20%3FregionAlpha2.%0A%20%20%20%20%20%20%23%20get%20the%20population%0A%20%20%20%20%20%20%23%20https%3A%2F%2Fwww.wikidata.org%2Fwiki%2FProperty%3AP1082%0A%20%20%20%20%20%20OPTIONAL%20%7B%0A%20%20%20%20%20%20%20%20%3Fregion%20wdt%3AP1082%20%3FregionPopulationValue%0A%20%20%20%20%20%20%7D%20%0A%20%20%20%20%20%20%23%20get%20he%20location%0A%20%20%20%20%20%20%23%20https%3A%2F%2Fwww.wikidata.org%2Fwiki%2FProperty%3AP625%0A%20%20%20%20%20%20OPTIONAL%20%7B%0A%20%20%20%20%20%20%20%20%3Fregion%20wdt%3AP625%20%3FlocationValue.%20%0A%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%20GROUP%20BY%20%3Fregion%0A%20%20%7D%0A%20%20%23%20%23%20https%3A%2F%2Fwww.wikidata.org%2Fwiki%2FProperty%3AP297%0A%20%20OPTIONAL%20%7B%20%0A%20%20%20%20%3Fregion%20wdt%3AP17%20%3Fcountry.%0A%20%20%20%20%23%20label%20for%20the%20country%0A%20%20%20%20%3Fcountry%20rdfs%3Alabel%20%3FcountryLabel%20filter%20%28lang%28%3FcountryLabel%29%20%3D%20%22en%22%29.%0A%20%20%20%20%3Fcountry%20wdt%3AP297%20%3FcountryIsoCode.%20%0A%20%20%7D%0A%7D%20ORDER%20BY%20%3FregionIsoCode try it!] - 3753 results in 11.4 s as of 2021-08 | ||
= Adding city details from Wikidata = | = Adding city details from Wikidata = |
Latest revision as of 07:51, 8 April 2023
OsProject
OsProject | |
---|---|
edit | |
id | geograpy3 |
state | active |
owner | somnathrakshit |
title | geograpy |
url | https://github.com/somnathrakshit/geograpy3 |
version | 0.2.6 |
description | |
date | 2023-04-08 |
since | 2018-09-18 |
until |
tickets
Freitext
What is it?
Geograpy3 is a Python library to extract geographic details like:
- country
- region
- city
from plaintext and websites.
Examples
Example 1 - London 2012 Olympic torch relay route
Let's take the Wikipedia article on the 2012 London Olympics torch relay route. In this article quite a few countries, regions and cities are mentioned. Let's extract that information using geograpy3
Code
import geograpy
url='https://en.wikipedia.org/wiki/2012_Summer_Olympics_torch_relay'
places = geograpy.get_geoPlace_context(url = url)
print(places)
Result
python example1.py
countries=['Ireland', 'Jersey', 'Guernsey', 'Turkey', 'Greece', 'United Kingdom', 'Belarus', 'South Africa', 'Australia', 'New Zealand', 'Germany', 'France', 'Jamaica', 'Antigua and Barbuda', 'Montserrat', 'United States', 'Canada', 'Japan']
regions=['Hackney', 'Davy', 'Ireland', 'Burscough', 'Jersey', 'Munich', 'Newton Aycliffe', 'British/Irish', 'Plymouth', 'Greece', 'Thirsk', 'Wales', 'Locog', 'Cumbrian', 'Lincolnshire', 'Guernsey', 'Cardiff', 'Torch', 'Host', 'Cambridge', 'Bristol Harbour', 'Falmouth', 'Athens', 'Turkey', 'Wiltshire', 'British', 'England', 'United Kingdom', 'Sheffield', 'London', 'Aberaeron', 'Abraham', 'Northern Ireland', 'Wanted', 'East', 'Heathrow', 'Gravesend', 'Essex', 'Maidstone', 'Cornwall', 'Hyde Park', 'Hera', 'Swansea', 'Caerphilly', 'Taunton Lancashire', 'Stamford', 'Dublin', 'Derry', 'Portland', 'Ioannina', 'Scotland', 'Bangor', 'Engineering', 'Land']
cities=['Ioannina', 'Athens', 'Dublin', 'Hyde Park', 'Swansea', 'Sheffield', 'Portland', 'Cardiff', 'Cambridge', 'Bangor', 'Thirsk', 'Stamford', 'Plymouth', 'Newton Aycliffe', 'Maidstone', 'London', 'Hackney', 'Gravesend', 'Falmouth', 'Caerphilly', 'Burscough', 'Aberaeron', 'Munich', 'Derry', 'England', 'Scotland', 'Essex', 'Turkey', 'Davy', 'Ireland', 'Lincolnshire', 'Wales', 'Guernsey', 'Cornwall', 'Heathrow', 'Hera']
other=['British', 'British', 'East']
Getting the source code
git clone https://github.com/somnathrakshit/geograpy3
cd geograpy3
scripts/install
History
first geograpy (2013)
The name "geograpy" was coined by Chris Albon
Angela Oduor Lungat, Brunobg, Jonathon Morgan, Romina Suarez and other contributors from Ushahidi, Nairobi, Kenya created the first and popular geograpy version. It was forked more than a hundred times and had more than 200 Stars on github.
This version was restricted to python2 and as of 2020-09 there are still some 29 open issues in this project. The project is officially archived and you might want to use geograpy3 instead.
geograpy2 (2014)
The geograpy2 fork was created in 2014. It solves several problems (such as support for utf8, places names with multiple words, confusion over homonyms etc).
Since 2015 the project didn't move forward much so you might want to use geograpy3 instead. https://github.com/Corollarium/geograpy2
geograpy3 (2018)
geograpy3 was forked from geograpy2 in 2018 by Somnath Rakshit. It added python3 compatibility. In 2020 Wolfgang Fahl joined the project since he had a need to use it for the Proceedings Title Parser as part of the ConfIDent project
Data used
Overview
Cities
The cities table is derived from the GeoLite2 by MaxMind database
Countries
The countries table is derived from Wikidata:
# get a list of countries
# for geograpy3 library
# see https://github.com/somnathrakshit/geograpy3/issues/15
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX p: <http://www.wikidata.org/prop/>
PREFIX ps: <http://www.wikidata.org/prop/statement/>
PREFIX pq: <http://www.wikidata.org/prop/qualifier/>
# get City details with Country
SELECT DISTINCT ?country ?countryLabel ?countryIsoCode ?countryCoord ?countryPopulation ?continent ?continentLabel
WHERE {
# instance of Country
?country wdt:P31/wdt:P279* wd:Q6256 .
# VALUES ?country { wd:Q55}.
# label for the country
?country rdfs:label ?countryLabel filter (lang(?countryLabel) = "en").
# get the continent (s)
#OPTIONAL {
# ?country wdt:P30 ?continent.
# ?continent rdfs:label ?continentLabel filter (lang(?continentLabel) = "en").
#}
# get the coordinates
OPTIONAL {
?country wdt:P625 ?countryCoord.
}
# https://www.wikidata.org/wiki/Property:P297 ISO 3166-1 alpha-2 code
?country wdt:P297 ?countryIsoCode.
# population of country
OPTIONAL
{
SELECT ?country (max(?countryPopulationValue) as ?countryPopulation)
WHERE {
?country wdt:P1082 ?countryPopulationValue
} group by ?country
}
# https://www.wikidata.org/wiki/Property:P2132
# nominal GDP per capita
# OPTIONAL { ?country wdt:P2132 ?countryGDP_perCapitaValue. }
}
ORDER BY ?countryIsoCode
try it! - 204 results in some 7.9 s as of 2021-12
Regions
The regions list is derived from Wikidata
# get a list of regions
# for geograpy3 library
# see https://github.com/somnathrakshit/geograpy3/issues/15
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX wikibase: <http://wikiba.se/ontology#>
SELECT DISTINCT ?country ?countryLabel ?countryIsoCode ?region ?regionLabel ?regionIsoCode ?regionPopulation ?location
WHERE
{
# administrative unit of first order
?region wdt:P31/wdt:P279* wd:Q10864048.
OPTIONAL {
?region rdfs:label ?regionLabel filter (lang(?regionLabel) = "en").
}
# isocode state/province (mandatory - filters historic regions while at it ...)
# filter historic regions
# FILTER NOT EXISTS {?region wdt:P576 ?end}
{
SELECT ?region (max(?regionAlpha2) as ?regionIsoCode) (max(?regionPopulationValue) as ?regionPopulation) (max(?locationValue) as ?location)
WHERE {
?region wdt:P300 ?regionAlpha2.
# get the population
# https://www.wikidata.org/wiki/Property:P1082
OPTIONAL {
?region wdt:P1082 ?regionPopulationValue
}
# get he location
# https://www.wikidata.org/wiki/Property:P625
OPTIONAL {
?region wdt:P625 ?locationValue.
}
} GROUP BY ?region
}
# # https://www.wikidata.org/wiki/Property:P297
OPTIONAL {
?region wdt:P17 ?country.
# label for the country
?country rdfs:label ?countryLabel filter (lang(?countryLabel) = "en").
?country wdt:P297 ?countryIsoCode.
}
} ORDER BY ?regionIsoCode
try it! - 3753 results in 11.4 s as of 2021-08
Adding city details from Wikidata
Query
# get a list of human settlements having a geoName identifier
# to add to geograpy3 library
# see https://github.com/somnathrakshit/geograpy3/issues/15
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX wd: <http://www.wikidata.org/entity/>
SELECT ?city ?cityLabel ?cityPop ?geoNameId ?country ?countryLabel ?countryIsoCode ?countryPopulation
WHERE {
# geoName Identifier
?city wdt:P1566 ?geoNameId.
# instance of human settlement https://www.wikidata.org/wiki/Q486972
?city wdt:P31/wdt:P279* wd:Q486972 .
# population of city
OPTIONAL { ?city wdt:P1082 ?cityPop.}
# label of the City
?city rdfs:label ?cityLabel filter (lang(?cityLabel) = "en").
# country this city belongs to
?city wdt:P17 ?country .
# label for the country
?country rdfs:label ?countryLabel filter (lang(?countryLabel) = "en").
# https://www.wikidata.org/wiki/Property:P297 ISO 3166-1 alpha-2 code
?country wdt:P297 ?countryIsoCode.
# population of country
?country wdt:P1082 ?countryPopulation.
OPTIONAL {
?country wdt:P2132 ?countryGdpPerCapita.
}
}
try it! - you may probably experience a timeout on this query. It takes about 1 min on a local wikidata copy based on blazegraph
If your are intested in the result you can download the Sqlite version of query result and e.g. inspect it with the DB Browser for SQLite
CityPops Stats
Here are some statistic queries about the data imported from Wikidata
select count(*) from cityPops where cityPop is not Null
164503
select count(*) from cityPops
453306
select count(distinct geoNameId) from cityPops
414198
select count(*)
from cities c
join cityPops cp on c.geoname_id =cp.geoNameId
90482
Difference in Name/Label
17499 differences:
select c.city_name as name,cp.cityLabel,c.*,city as wikidataurl,cityPop
from cities c
join cityPops cp
on c.geoname_id=cp.geoNameId
where not c.city_name =cp.cityLabel
group by geoNameId