8

Thanks to this answer , I can successfully use the python ckanclient package to search data.gov for "sea_water_temperature":

import ckanclient
q = 'tags:sea_water_temperature'
ckan = ckanclient.CkanClient('http://catalog.data.gov/api/3')
d = ckan.action('package_search', q=q, rows=10)

What I really want to do is to query for "sea_water_temperature" in a specific geospatial bounding box and temporal extent, and additionally find only datasets that have a specific data service endpoint.

Adrià Mercader provided these examples on the ckan-discuss mailing list:

# WMS + keyword
{"q": "salmon",
 "fq": "res_format:WMS"}

# WMS + temporal
{"q": "metadata_modified:[2013-06-01T00:00:00.000Z TO NOW]",
 "fq": "res_format:WMS"}

# WMS + spatial
{"extras":{"ext_bbox":"-121,45,-120,46"},
 "fq": "res_format:WMS"}

How can I convert these examples into something ckanclient can understand?

Rich Signell
  • 369
  • 1
  • 11
  • What if you query all the database and then filter what you want from the results? It isn't the best solution, but it is one. – Tasos Nov 25 '13 at 22:13
  • I could do this, but it negates the whole point of querying. I know that CKAN can do a filtered search like this, but I'm stuck on the syntax with ckanclient, I believe – Rich Signell Nov 26 '13 at 20:54

1 Answers1

5

ckan.action will pass all the keywords parameters onto whatever API call you are using, so you can pass any parameter supported by package_search to it (I'll use PDF instead of WMS to get some results):

import ckanclient
q = 'tags:sea_water_temperature'
ckan = ckanclient.CkanClient('http://catalog.data.gov/api/3')
d = ckan.action('package_search', q=q, rows=10, fq='res_format:PDF')

You can change slightly your code to pass a whole dict of parameters, which makes easier to see what is going on:

import ckanclient                                                                                                       
search_params = {                                                                                      
    'q': 'tags:"sea_water_temperature"',       
    'fq': 'res_format:PDF',                         
    'rows': 10,                                                                                        
}                                                                                                      

ckan = ckanclient.CkanClient('http://catalog.data.gov/api/3')                                          
d = ckan.action('package_search', **search_params) 

Use these parameters for the other query types that you needed:

# sea_water_temperature + PDF + modified since + bounding box
search_params = {                                                                                      
    'q': 'tags:"sea_water_temperature" AND metadata_modified:[2012-06-01T00:00:00.000Z TO NOW]',       
    'fq': 'res_format:PDF',                                                                            
    'extras': {"ext_bbox":"-121,45,-120,46"},                                                          
    'rows': 10                                                                                        
}

Hope this helps!

Rich Signell
  • 369
  • 1
  • 11
amercader
  • 568
  • 3
  • 7
  • 1
    Awesome. I know I'm not to supposed to thank people in comments, but I can't help it: I never would have figured this out, and now I'm off and running! – Rich Signell Nov 27 '13 at 12:51