Geonames API for RDF/Semantic Web Queries

Geonames has a sprawling set of APIs to search for all manner of geographical information.

Their Search API also has the option of returning the RDF-compatible XML. RDF is often used for Semantic Web-related applications.

The geonames_rdf Python library not only provides you the ability to query the API from your Python code but also provides a tool that can often perform identical queries from the command-line. Used as a library, you can get the raw RDF document, a list of XML nodes, or a simple 2-tuple list of keys and values. Used as a command-line tool, you can print the raw RDF result or a flat list of keys and values.

To install, use PyPI:

$ sudo pip install geonames_rdf

Sourcecode Example

Code fragment

sa = geonames.adapters.search.Search('username')
result = sa.query('detroit').country('us').max_rows(2).execute()
for id_, name in result.get_flat_results():
    # make_unicode() is only used here for Python version-compatibility.
    print(geonames.compat.make_unicode("[{0}]: [{1}]").format(id_, name))

Output

[http://sws.geonames.org/4990729/]: [Detroit]
[http://sws.geonames.org/6955112/]: [Detroit-Warren-Livonia]

Command-line example

Simple list:

Pass the exact same parameter names and values to zero or more “-p” parameters:

$ gn_search dsoprea -p query detroit -p country us -p max_rows 2

Output:

[http://sws.geonames.org/4990729/]: [Detroit]
[http://sws.geonames.org/6955112/]: [Detroit-Warren-Livonia]

Raw RDF response:

Pass the “-x” parameter:

$ gn_search dsoprea -p query detroit -p country us -p max_rows 2 -x

Output:

<rdf:RDF xmlns:cc="http://creativecommons.org/ns#" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:foaf="http://xmlns.com/foaf/0.1/" xmlns:gn="http://www.geonames.org/ontology#" xmlns:owl="http://www.w3.org/2002/07/owl#" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#" xmlns:wgs84_pos="http://www.w3.org/2003/01/geo/wgs84_pos#">
<gn:Feature rdf:about="http://sws.geonames.org/4990729/">
<rdfs:isDefinedBy rdf:resource="http://sws.geonames.org/4990729/about.rdf"/>
<gn:name>Detroit</gn:name>
<gn:alternateName xml:lang="af">Detroit</gn:alternateName>
<gn:alternateName xml:lang="ar">ديترويت</gn:alternateName>
<gn:alternateName xml:lang="az">Detroyt</gn:alternateName>
<gn:alternateName xml:lang="be">Горад Дэтройт</gn:alternateName>
<gn:alternateName xml:lang="bg">Детройт</gn:alternateName>
<gn:alternateName xml:lang="bn">ডেট্রয়েট</gn:alternateName>
<gn:alternateName xml:lang="bs">Detroit</gn:alternateName>
<gn:alternateName xml:lang="ca">Detroit</gn:alternateName>
<gn:alternateName xml:lang="ce">Детройт</gn:alternateName>
<gn:alternateName xml:lang="cs">Detroit</gn:alternateName>
<gn:alternateName xml:lang="da">Detroit</gn:alternateName>
<gn:alternateName xml:lang="de">Detroit</gn:alternateName>
<gn:alternateName xml:lang="el">Ντιτρόιτ</gn:alternateName>
<gn:alternateName xml:lang="en">Detroit</gn:alternateName>
<gn:alternateName xml:lang="eo">Detroit</gn:alternateName>
<gn:alternateName xml:lang="es">Detroit</gn:alternateName>
<gn:alternateName xml:lang="et">Detroit</gn:alternateName>
<gn:alternateName xml:lang="fa">دیترویت</gn:alternateName>
<gn:alternateName xml:lang="fi">Detroit</gn:alternateName>
<gn:alternateName xml:lang="fr">Détroit</gn:alternateName>
<gn:alternateName xml:lang="gl">Detroit</gn:alternateName>
<gn:alternateName xml:lang="he">דטרויט</gn:alternateName>
<gn:alternateName xml:lang="hi">डेट्राइट</gn:alternateName>
<gn:alternateName xml:lang="hu">Detroit</gn:alternateName>
<gn:alternateName xml:lang="hy">Դետրոյթ</gn:alternateName>
<gn:alternateName xml:lang="id">Detroit</gn:alternateName>
<gn:alternateName xml:lang="io">Detroit</gn:alternateName>
<gn:alternateName xml:lang="is">Detroit</gn:alternateName>
<gn:alternateName xml:lang="it">Detroit</gn:alternateName>
<gn:alternateName xml:lang="ja">デトロイト</gn:alternateName>
<gn:alternateName xml:lang="ka">დეტროიტი</gn:alternateName>
<gn:alternateName xml:lang="kk">Детройт</gn:alternateName>
<gn:alternateName xml:lang="ko">디트로이트</gn:alternateName>
<gn:alternateName xml:lang="la">Detroitum</gn:alternateName>
<gn:alternateName xml:lang="lt">Detroitas</gn:alternateName>
<gn:alternateName xml:lang="lv">Detroita</gn:alternateName>
<gn:alternateName xml:lang="mk">Детроит</gn:alternateName>
<gn:alternateName xml:lang="mr">डेट्रॉईट</gn:alternateName>
<gn:alternateName xml:lang="mrj">Детройт</gn:alternateName>
<gn:alternateName xml:lang="mzn">دیترویت</gn:alternateName>
<gn:alternateName xml:lang="nl">Detroit</gn:alternateName>
<gn:alternateName xml:lang="nn">Detroit</gn:alternateName>
<gn:alternateName xml:lang="no">Detroit</gn:alternateName>
<gn:alternateName xml:lang="oc">Detroit</gn:alternateName>
<gn:alternateName xml:lang="pl">Detroit</gn:alternateName>
<gn:alternateName xml:lang="pt">Detroit</gn:alternateName>
<gn:alternateName xml:lang="ro">Detroit</gn:alternateName>
<gn:alternateName xml:lang="ru">Детройт</gn:alternateName>
<gn:alternateName xml:lang="sah">Детройт</gn:alternateName>
<gn:alternateName xml:lang="sk">Detroit</gn:alternateName>
<gn:alternateName xml:lang="sr">Детроит</gn:alternateName>
<gn:alternateName xml:lang="sv">Detroit</gn:alternateName>
<gn:alternateName xml:lang="ta">டிட்ராயிட்</gn:alternateName>
<gn:alternateName xml:lang="te">డెట్రాయిట్</gn:alternateName>
<gn:alternateName xml:lang="tg">Детройт</gn:alternateName>
<gn:alternateName xml:lang="th">ดีทรอยต์</gn:alternateName>
<gn:alternateName xml:lang="tr">Detroit</gn:alternateName>
<gn:alternateName xml:lang="ug">Détroyt</gn:alternateName>
<gn:alternateName xml:lang="uk">Детройт</gn:alternateName>
<gn:alternateName xml:lang="vi">Detroit</gn:alternateName>
<gn:alternateName xml:lang="xmf">დეთროითი</gn:alternateName>
<gn:alternateName xml:lang="yi">דעטרויט</gn:alternateName>
<gn:alternateName xml:lang="zh">底特律</gn:alternateName>
<gn:featureClass rdf:resource="http://www.geonames.org/ontology#P"/>
<gn:featureCode rdf:resource="http://www.geonames.org/ontology#P.PPLA2"/>
<gn:countryCode>US</gn:countryCode>
<gn:population>713777</gn:population>
<gn:postalCode>48258</gn:postalCode>
<wgs84_pos:lat>42.33143</wgs84_pos:lat>
<wgs84_pos:long>-83.04575</wgs84_pos:long>
<wgs84_pos:alt>183</wgs84_pos:alt>
<gn:parentCountry rdf:resource="http://sws.geonames.org/6252001/"/>
<gn:nearbyFeatures rdf:resource="http://sws.geonames.org/4990729/nearby.rdf"/>
<gn:locationMap rdf:resource="http://www.geonames.org/4990729/detroit.html"/>
<gn:wikipediaArticle rdf:resource="http://en.wikipedia.org/wiki/Detroit"/>
<rdfs:seeAlso rdf:resource="http://dbpedia.org/resource/Detroit"/>
</gn:Feature>
<gn:Feature rdf:about="http://sws.geonames.org/6955112/">
<rdfs:isDefinedBy rdf:resource="http://sws.geonames.org/6955112/about.rdf"/>
<gn:name>Detroit-Warren-Livonia</gn:name>
<gn:featureClass rdf:resource="http://www.geonames.org/ontology#L"/>
<gn:featureCode rdf:resource="http://www.geonames.org/ontology#L.RGNE"/>
<gn:countryCode>US</gn:countryCode>
<gn:population>4425110</gn:population>
<wgs84_pos:lat>42.34231</wgs84_pos:lat>
<wgs84_pos:long>-83.07175</wgs84_pos:long>
<gn:parentCountry rdf:resource="http://sws.geonames.org/6252001/"/>
<gn:nearbyFeatures rdf:resource="http://sws.geonames.org/6955112/nearby.rdf"/>
<gn:locationMap rdf:resource="http://www.geonames.org/6955112/detroit-warren-livonia.html"/>
</gn:Feature>
</rdf:RDF>

For more information, go to the project homepage.

Advertisements