dandye: WeoGeo API: Python Examples, Part 1

The Developer Documentation for WeoGeo’s API provides examples using curl:

curl example in WeoGeo Developer Documentation (wiki.weogeo.com)

curl example in WeoGeo Developer Documentation (wiki.weogeo.com)

However, after fielding some questions from Developers, I’m thinking some Python (and C#?) examples might be useful too.  So here is the first installment.

This is a bare-bones example of the List Datasets API, which returns a list of Datasets within the (optional) defined parameters.  Possible parameters include a Minimum Bounding Rectangle (MBR), date range, ratings, and/or Tags.

import urllib

usern = [your WeoGeo username]
passw = [your WeoGeo password]
library = [the * part of your Library's domain name *.weogeo.com]

# List Datasets (GET) curl example:
# curl -H 'Content-Type: application/xml' http://#{hostname}/datasets.weo?page=1&east=0&north=90&south=0&west=-180&scale=5

# Optional parameters:
params =  urllib.urlencode(
    {
        'page': 1,
        'per_page': '15',
        'north': '90.0',
        'south': '-90.0',
        'east': '180.0',
        'west': '-180.0',
        'date_from': '1990-01-01',
        'date_to': '2010-01-01',
        'data_type': 'RASTER',
        'min_provider_rating': '0',
        'min_dataset_rating': '0',
        'tags': 'test'
    }
)

# construct the URI for the API with parameters:
url = "https://%s:%s@%s.weogeo.com/datasets.weo?%s" % (usern,passw,library,params)

# call the URI
f = urllib.urlopen(url)

# print results to the CLI
print f.read()

For extra credit, try changing the extension from “.weo” to [".kml",".json",".rss"].

Next time, I’ll show the same WeoGeo API call in Python but use httplib instead of urllib.  Although, urllib is simpler, httplib handles authentication better and enables the more sophisticated requests that will be required for later API examples.

Tags: ,

Comments are closed.