Skip to content

Instantly share code, notes, and snippets.

@rsignell-usgs
Created February 17, 2014 16:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rsignell-usgs/9053867 to your computer and use it in GitHub Desktop.
Save rsignell-usgs/9053867 to your computer and use it in GitHub Desktop.
{
"metadata": {
"name": "",
"signature": "sha256:f690f121702a4740aa2e624914c793ad75f740d558c89d491eec164062ed4846"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Build complex query using CSW OWSLib FES"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The search on the next line returns 11 records from NODC geoportal (http://www.nodc.noaa.gov/geoportal/):\n",
"\n",
"`('roms' OR 'selfe' OR 'adcirc' OR 'ncom' OR 'hycom' OR 'fvcom') AND 'ocean' NOT 'regridded' NOT 'espresso'`\n",
"\n",
"How do I do this same request using OWSLib CSW w/ FES?"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"from owslib.csw import CatalogueServiceWeb\n",
"from owslib import fes"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 1
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"search_text = ['roms','selfe','adcirc','ncom','hycom','fvcom']\n",
"filt=[]\n",
"for val in search_text:\n",
" filt.append(fes.PropertyIsLike(propertyname='apiso:AnyText',literal=('*%s*' % val),\n",
" escapeChar='\\\\',wildCard='*',singleChar='?'))\n",
"filter1=fes.Or(filt)\n",
"\n",
"val = 'ocean'\n",
"filter2=fes.PropertyIsLike(propertyname='apiso:AnyText',literal=('*%s*' % val),\n",
" escapeChar='\\\\',wildCard='*',singleChar='?')\n",
"\n",
"val = 'regridded'\n",
"filt=fes.PropertyIsLike(propertyname='apiso:AnyText',literal=('*%s*' % val),\n",
" escapeChar='\\\\',wildCard='*',singleChar='?')\n",
"filter3 = fes.Not([filt])\n",
"\n",
"val = 'espresso'\n",
"filt=fes.PropertyIsLike(propertyname='apiso:AnyText',literal=('*%s*' % val),\n",
" escapeChar='\\\\',wildCard='*',singleChar='?')\n",
"filter4 = fes.Not([filt])\n",
"\n",
"\n",
"filter_list = [fes.And([filter1, filter2, filter3, filter4])]"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 2
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"endpoint = 'http://www.nodc.noaa.gov/geoportal/csw' # NODC/UAF Geoportal: granule level\n",
"csw = CatalogueServiceWeb(endpoint,timeout=60)\n",
"print csw.version"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"2.0.2\n"
]
}
],
"prompt_number": 3
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"csw.getrecords2(constraints=filter_list,maxrecords=1000,esn='full')\n",
"len(csw.records.keys())"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 4,
"text": [
"11"
]
}
],
"prompt_number": 4
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 4
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment