Skip to content

Commit d7415f0

Browse files
committed
extend the sniffing capability to head out to the endpoint to find out what it is
some comments: - it may return an array of protocols, if multiple are supported - for backwards compatibility, you need to set first=False to allow multiple (multiple is also slow) - you need to set extended=True to activate the extended sniffing
1 parent 519bda6 commit d7415f0

File tree

3 files changed

+114
-7
lines changed

3 files changed

+114
-7
lines changed

Diff for: geolinks/__init__.py

+112-6
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,18 @@
2828
# =================================================================
2929

3030
import logging
31+
from owslib.wms import WebMapService as WMS
32+
from owslib.wfs import WebFeatureService as WFS
33+
from owslib.ogcapi.features import Features as OAPIF
34+
from owslib.ogcapi.coverages import Coverages as OAPIC
35+
from owslib.ogcapi.records import Records as OAPIR
36+
from owslib.wcs import WebCoverageService as WCS
37+
from owslib.csw import CatalogueServiceWeb as CSW
38+
from owslib.wps import WebProcessingService as WPS
39+
from owslib.sos import SensorObservationService as SOS
40+
from owslib.wmts import WebMapTileService as WMTS
3141

32-
LOGGER = logging.getLogger(__name__)
33-
34-
__version__ = '0.2-dev'
35-
42+
__version__ = '1.1'
3643

3744
def inurl(needles, haystack, position='any'):
3845
"""convenience function to make string.find return bool"""
@@ -60,7 +67,7 @@ def inurl(needles, haystack, position='any'):
6067
return False
6168

6269

63-
def sniff_link(url):
70+
def sniff_link(url, extended=False, first=True):
6471
"""performs basic heuristics to detect what the URL is"""
6572

6673
protocol = None
@@ -105,6 +112,105 @@ def sniff_link(url):
105112
elif inurl(['kml', 'kmz'], link, 'end'):
106113
protocol = 'OGC:KML'
107114
else:
108-
LOGGER.info('No link type detected')
115+
if (extended):
116+
protocol = []
117+
#for each servicetype, head out to see if it is valid
118+
try:
119+
wms = WMS(link)
120+
if (wms.identification.type == 'OGC:WMS'):
121+
if (first):
122+
return wms.identification.type
123+
else:
124+
protocol.append(wms.identification.type)
125+
except:
126+
pass # No need to log?
127+
try:
128+
wmts = WMTS(link)
129+
if (wmts.identification.type == 'OGC:WMTS'):
130+
if (first):
131+
return wmts.identification.type
132+
else:
133+
protocol.append(wmts.identification.type)
134+
except:
135+
pass
136+
try:
137+
wps = WPS(link, verbose=False, skip_caps=True)
138+
wps.getcapabilities()
139+
if (wps.identification.type == 'OGC:WPS'):
140+
if (first):
141+
return wps.identification.type
142+
else:
143+
protocol.append(wps.identification.type)
144+
except:
145+
pass
146+
try:
147+
wfs = WFS(link)
148+
if (wfs.identification.type == 'OGC:WFS'):
149+
if (first):
150+
return wfs.identification.type
151+
else:
152+
protocol.append(wfs.identification.type)
153+
except:
154+
pass
155+
try:
156+
csw = CSW('http://geodiscover.cgdi.ca/wes/serviceManagerCSW/csw')
157+
if (csw.identification.type == 'OGC:CSW'):
158+
if (first):
159+
return csw.identification.type
160+
else:
161+
protocol.append(csw.identification.type)
162+
except:
163+
pass
164+
try:
165+
wcs = WCS(link)
166+
if (wcs.identification.type == 'OGC:WCS'):
167+
if (first):
168+
return wcs.identification.type
169+
else:
170+
protocol.append(wcs.identification.type)
171+
except:
172+
pass
173+
try:
174+
sos = SOS(link)
175+
if (sos.identification.type == 'OGC:SOS'):
176+
if (first):
177+
return sos.identification.type
178+
else:
179+
protocol.append(sos.identification.type)
180+
except:
181+
pass
182+
try:
183+
oapir = OAPIR(link)
184+
if (oapir.conformance()):
185+
if (first):
186+
return "OGCAPI:records"
187+
else:
188+
protocol.append("OGCAPI:records")
189+
except:
190+
pass
191+
try:
192+
oapif = OAPIF(link)
193+
if (oapir.conformance()):
194+
if (first):
195+
return "OGCAPI:features"
196+
else:
197+
protocol.append("OGCAPI:features")
198+
except:
199+
pass
200+
try:
201+
oapic = OAPIC(link)
202+
if (oapir.conformance()):
203+
if (first):
204+
return "OGCAPI:coverages"
205+
else:
206+
protocol.append("OGCAPI:coverages")
207+
except:
208+
pass
209+
210+
if len(protocol) == 1:
211+
protocol = protocol[0]
212+
213+
else:
214+
LOGGER.info('No link type detected')
109215

110216
return protocol

Diff for: tests/run_tests.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def test_link_types(self):
4747
"""simple link type tests"""
4848

4949
for test in self.test_data['test_data']:
50-
self.assertEqual(sniff_link(test['link']), test['expected'],
50+
self.assertEqual(sniff_link(test['link'],extended=True), test['expected'],
5151
'Expected %s and %s to be equal' %
5252
(test['link'], test['expected']))
5353

Diff for: tests/test_data.json

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"test_data": [
33
{"link": "http://host/wms?service=WMS", "expected": "OGC:WMS"},
4+
{"link": "https://maps.isric.org/mapserv?map=/map/bdod.map", "expected": "OGC:WMS"},
45
{"link": "http://host/ows?service=WFS", "expected": "OGC:WFS"},
56
{"link": "http://host/ows?service=WCS", "expected": "OGC:WCS"},
67
{"link": "http://host/ows?service=WPS", "expected": "OGC:WPS"},

0 commit comments

Comments
 (0)