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?

2
  • 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, 2013 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 Nov 26, 2013 at 20:54

1 Answer 1

5
+50

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!

1
  • 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! Nov 27, 2013 at 12:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.