WeoGeo API: Python Examples, Part 4 (PUTting XML)

January 21st, 2010

In this blog series, we’ve looked at WeoGeo API examples using the GET and POST HTTP Verbs.  These were used to GET a List of Datasets and to POST a request for new Tokens in a WeoGeo Library.  Today, we’re going to PUT an XML file in order to update the Name, Description, and Tags associated with a Dataset.

First, let’s take a look at the dataset to be updated.  This Listing on my WeoGeo Library has dummy descriptive data for the name, description, and tags:

Panel 4: Preview with dummy data

Using the GET Dataset as WeoFile API in a web-browser, we can view this descriptive data as XML:

GET Dataset as WeoFile

For the new values, I’ve created an XML file that contains the dataset tag and child nodes for each of the fields that I want updated:

new values in xml

I’ve saved that file locally to my local file system and will read it from the following Python script:

import base64
import string
import httplib, urllib

# Programmer defined variables
usern = "your api key"
passw = "" # Null string when API Key is used
library = "yourLibraryDomain" # the %s part of %s.weogeo.com
token = "the dataset token"
new_xml = "./data/new_xml.xml"  #path in my file-system
# No more programmer changes

# set up the authentication string
auth = 'Basic ' + string.strip(base64.encodestring(usern + ':' + passw))

# create the header with the authentication string
headers = {
 "Content-Type": "application/xml" ,
 "Authorization": auth,
 }

# read the XML data into body and print it
body = open(new_xml).read()
print body

# create the connection
conn = httplib.HTTPSConnection("%s.weogeo.com" % library)

# Construct the request with headers and XML in the body
conn.request("PUT","/datasets/%s.weo" % token, body, headers)

# call the URI
response = conn.getresponse()

#check status of response
print response.status, response.reason

# store the response in data
data = response.read()

# close the connection
conn.close()

# output the results
print data

This is very similar to the last example but note the following changes:

  1. the HTTP verb PUT is used in place of POST
  2. the variable body is used on line 29 where a null string was used before

Calling this Python script from the console outputs the following:

 S:\>python put_example.py
<?xml version="1.0" encoding="UTF-8"?>
<dataset>
 <name>Counties in the Southeast</name>
 <description>
 <![CDATA[
 <h1>Big Header<h1>
 Some <i>HTML</i> elements like <b>bold text</b> and
 <a href="http://blogs.weogeo.com/dandye">links</a> are allowed.
 ]]>
 </description>
 <tags>TIGER shapefile political_boundaries counties</tags>
</dataset>
200 OK

S:\>

The 200 response indicates that all went well with the update and we can verify that by looking at the updated listing in the Preview Panel:

Panel 4: Preview with updated data

WeoGeo API: Python Examples, Part 3

January 19th, 2010

The Developer Documentation for WeoGeo’s API provides examples using curl.  In this blog series, I’m providing similar examples using Python.  In WeoGeo API: Python Examples, Part 1, I showed how to invoke the “GET Dataset as WeoFile” API from Python using urllib.  In Part 2, I showed the same GET Dataset request using httplib.  This time, we’re going to generate Dataset Tokens, which will require use of POST and https.

First, a little background: Dataset Tokens are Universally Unique Identifiers.  There is one Dataset Token for each Dataset Listing in your WeoGeo Library.  If your Dataset is also listed on the WeoGeo Market, the same Token is used there.  Although the WeoApp can create Dataset Tokens for you when it creates a Dataset WeoFile, sometimes developers find it is useful to create a number of them in advance.

The Developer API Documentation shows that the curl request for Dataset Token generation is:

curl -u ${api_key}: -X POST https://${hostname}/datasets/tokens?count=2

There are three things of note here:

  1. For authentication, only the API Key is used
  2. The HTTP method is POST where previous examples have used GET
  3. This API uses https rather than http

Here is a bare-bones Python example demonstrating the same API call:

import base64
import string
import httplib, urllib

# Programmer defined variables
usern = "your API key goes here"
passw = "" # Null string when API Key is used
library = "YourLibraryName" # the %s part of %s.weogeo.com
n_tokens = 2
# No more programmer changes

# set up the authentication string
auth = 'Basic ' + string.strip(base64.encodestring(usern + ':' + passw))

# Generate Tokens Curl Example (POST via HTTPS)
# curl -u #{api_key}: -X POST https://#{hostname}/datasets/tokens?count=2

# create the header with the authentication string
headers = {
 "Content-Type": "application/xml" ,
 "Authorization": auth,
 }

# set parameters for the request
params =  urllib.urlencode( { 'count':n_tokens } )

# create the connection
conn = httplib.HTTPSConnection("%s.weogeo.com" % library)

# Construct the request with headers amd add paramters to the URI
conn.request("POST","/datasets/tokens?%s" % params, "", headers)

# call the URI close the connection
response = conn.getresponse()

#check status of result
print response.status, response.reason

#check status of result
data = response.read()

# close the connection
conn.close()

# output the results
print data

For the three things of interest noted above:

  1. API Key is set on line 5 and used on lines 11, 17, and 28
  2. Line 28 sets the HTTP Request Method to POST
  3. Line 25 uses httplib.HTTPSConnection (previous examples used httplib.HTTPConnection)

That’s it!  Next time, I plan to show the PUT method as used in the Update Dataset API.

WeoGeo API: Python Examples, Part 2

January 12th, 2010

The Developer Documentation for WeoGeo’s API provides examples using curl.  In this blog series, I’m providing similar examples but using Python.  In WeoGeo API: Python Examples, Part 1, I showed how to invoke the “GET Dataset as WeoFile” API from Python using urllib.  This time, I’ll show the same request using httplib.

Httplib is a little more complicated but it handles authentication slightly better and, more importantly, specifying the headers will enable more sophisticated requests (that will be required for later API examples).

The curl example for the API call is below:

List Datasets API: curl example

And here is a bare-bones example of that request in Python using httplib:

import base64
import string
import httplib, urllib

usern = "your_username" # or your API key
passw = "your_password" # if API Key is used: = ""
library = "the * part of your Library's domain name in *.weogeo.com"

# Construct the Basic authentication string
auth = 'Basic ' + string.strip(base64.encodestring(usern + ':' + passw))

# 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'
})

# create the header with the authentication
headers = {
 "Content-Type": "application/xml" ,
 "Authorization": auth,
 }

# create teh connection
conn = httplib.HTTPConnection("dandye.weogeo.com")

# Construct the request with headers amd add paramters to the URI
conn.request("GET","/datasets.weo?%s" % params, "", headers)

# call the URI
response = conn.getresponse()

# check status of result
print response.status, response.reason

# read the data retreieved
data = response.read()

# close the connection
conn.close()

# print the data retreieved
print data

Notice on Line 9 that your username and password are base64 encoded.  This is basic access authentication and encoding != encrypting.  So for added security, you may use your Library API Key in place of your username and leave your password blank (password = “”).

Again, for extra credit, try changing the extension (on line 40) from “.weo” to [".kml",".json",".rss"].

WeoGeo API: Python Examples, Part 1

January 7th, 2010

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&amp;east=0&amp;north=90&amp;south=0&amp;west=-180&amp;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.

Embedding a KML File Index Into Your Website

September 23rd, 2009

WeoGeo Library Administrators and Market Providers can share or advertise their WeoGeo datasets outside of the WeoGeo website. For example, this TerraColor wiki entry demonstrates a map of the US with roll-over links to the individual Panel 5:CUSTOMIZE pages, where the datasets can be purchased.

Here, I want to demonstrate how to use WeoGeo APIs and some Python code to aggregate select WeoGeo Preview KML files into a “KML File Index”. This type of KML File Index can be used to share and market the content stored in your Library and Market accounts.

This example was developed for WeoGeo Market Provider ATLIS Geomatics, who requested an index of their WeoGeo Market Listings for their corporate web pages. They wanted one index for each of the cities where they held high resolution air photos.

WeoGeo provides an API for obtaining dataset details in the following formats WeoFile (XML), KML, and json. The wiki article shows how to curl the request but you can also preview the responses (in a web-browser) using the following URI structure:

http://market.weogeo.com/datasets/<token>.weo

For example:

http://market.weogeo.com/datasets/9a764fe6-3903-8151-b824-8a11a6f00552.kml

or

http://market.weogeo.com/datasets/9a764fe6-3903-8151-b824-8a11a6f00552.json

This is the same link found in the lower left corner of Panel 4:PREVIEW.

Preview KML

Preview KML

Those individual dataset KML file descriptions will be aggregated into a single KML File Index viewable within KML-readers like Google Earth, Bing Maps, ArcGIS Explorer, or NASA WorldWind.

Creating the KML File Index

  1. First, we need to obtain a list of tokens that uniquely identify the datasets to be included.  This can be accomplished with an API call.For example: curl -g -u <api key>: \
    'http://<subdomain>.weogeo.com/datasets.weo?&page=1&scale=0' > ./datasets.xml

    curl -g -u <api_key>: \
    'http://<subdomain>.weogeo.com/datasets.weo?&page=2&scale=0' >> ./datasets.xml
    ...
  2. Next, we extract the tokens from the resulting XML file and write them into a plain text file with one token per line.  A Python script for this step can be found in WeoGeo’s Public Code Repository.
  3. Lastly, a Python script is used to:
    1. invoke the WeoGeo API and obtain the KML Preview for each dataset (via it’s token)
    2. merge them into one KML File Index

There are also optional flags in this script to in/exclude the externally hosted preview image inside the wireframe or to create a stand-alone KMZ file that includes the preview images.

The resulting KML/KMZ can be viewed in a variety of applications including Google Maps, Google Earth, Bing Maps, OpenLayers, and many others.

http://kml.community.weogeo.com/ATLIS/atlis_calgary.kml

Using the Static URI embedded in each of the Preview KML files inside the KML File Index, ATLIS customers can now jump from an ATLIS web page directly to the WeoGeo Market page for that dataset.  This allows their customers to browse datasets on their website but customize and purchase on the WeoGeo Market. You can view the results on their Coverage Maps webpage.

To learn more about the APIs that were used, check out the WeoGeo API wiki pages. Also check out the Python code used to interact with the WeoGeo APIs and produce the KML File Index on Google Code.

Checking Out WordPress for iPhone App

March 28th, 2009

Hello World!

LinkedIn Applications

October 30th, 2008

I just installed the WordPress and TripIt Applications to my LinkedIn account.  I’ve been a fan of TripIt for a while; it has proved really useful for single page prints of my itinerary and sharing that info with my signficant other.  Just now, it helped me to see who in my network will be at the ASPRS Pecora17 conference!  I’m not sure about the WordPress plugin yet because I chose the option to only show posts Tagged with LinkedIn.  This will be the first.

Links for 2008-10-15

October 15th, 2008

Links for 2008-10-14

October 14th, 2008

Links for 2008-10-13

October 13th, 2008