In [24]:
%matplotlib inline
import matplotlib.pyplot as plt
from datetime import datetime, timedelta
import pytz
from urllib import urlencode
from pyoos.collectors.ioos.swe_sos import IoosSweSos
from pyoos.parsers.ioos.describe_sensor import IoosDescribeSensor
from owslib.sos import SensorObservationService
from owslib.swe.sensor.sml import SensorML, Contact
from owslib.util import (testXMLValue, testXMLAttribute, nspath_eval,
xmltag_split, xml_to_dict, dict_union, extract_xml_list)
from owslib.namespaces import Namespaces
from geojson import Point, Feature, FeatureCollection
import numpy as np
import pandas as pd
import geopandas as gpd
def get_namespaces():
n = Namespaces()
# namespaces = n.get_namespaces(["sml", "gml", "xlink"])
namespaces = n.get_namespaces(["sml", "gml", "xlink", "swe"])
namespaces["ism"] = "urn:us:gov:ic:ism:v2"
return namespaces
namespaces = get_namespaces()
def nsp(path):
return nspath_eval(path, namespaces)
In [2]:
# Ran with ioos conda env
%load_ext version_information
%version_information numpy, pandas, owslib, pyoos
Out[2]:
In [3]:
sosurl = 'http://data.nanoos.org/52nsos/sos/kvp'
# NANOOS SOS is failing??
# ConnectionError: HTTPConnectionPool(host='data.nanoos.org', port=80): Max retries exceeded with url:
# /52nsos/sos/kvp?service=SOS&request=GetCapabilities&acceptVersions=1.0.0
# (Caused by NewConnectionError('<requests.packages.urllib3.connection.HTTPConnection object at 0x7f34545dca50>:
# Failed to establish a new connection: [Errno -2] Name or service not known',))
# sosurl = 'http://sos.cencoos.org/sos/sos/kvp'
In [4]:
collector52n = IoosSweSos(sosurl)
offerings52n = collector52n.server.offerings
In [5]:
# contents = collector52n.server.contents
In [6]:
station_urn = 'urn:ioos:station:nanoos:apl_chaba'
# station_urn = 'urn:ioos:station:cencoos:Humboldt'
In [7]:
# procedure = station_urn
collector52n.features = [station_urn]
In [8]:
# sensorml = collector52n.metadata(feature_name_callback=procedure)
sensorml_resp_lst = collector52n.metadata(timeout=200)
sensorml_resp_lst
Out[8]:
In [9]:
sensorml = sensorml_resp_lst[0]
In [10]:
pyoos_ds = IoosDescribeSensor(sensorml._root)
pyoos_ds
Out[10]:
In [11]:
vars(pyoos_ds)
Out[11]:
In [12]:
# vars(pyoos_ds.system)
In [13]:
pos = testXMLValue(pyoos_ds.system.location.find(nsp('gml:Point/gml:pos')))
pos.split()
Out[13]:
In [14]:
len(offerings52n), type(offerings52n), type(offerings52n[1])
Out[14]:
In [15]:
off1 = offerings52n[1]
In [16]:
vars(off1)
Out[16]:
In [17]:
off1.name
Out[17]:
In [18]:
offerings52n[2].name
Out[18]:
In [19]:
station_urn_lst = [urn.name for urn in offerings52n if urn.name.split(':')[-1] != 'all']
len(station_urn_lst)
Out[19]:
In [20]:
collector52n.features = station_urn_lst
sensorml_resp_lst = collector52n.metadata(timeout=200)
In [21]:
station_rec_lst = []
for station_idx, station_urn in enumerate(station_urn_lst):
ds = IoosDescribeSensor(sensorml_resp_lst[station_idx]._root)
pos = testXMLValue(ds.system.location.find(nsp('gml:Point/gml:pos')))
lat, lon = pos.split()
station_rec = dict(
id=ds.id,
longitude=float(lon),
latitude=float(lat),
station_urn_id=ds.id,
shortName=ds.shortName,
longName=ds.longName,
platformType=ds.platformType,
starting=datetime.isoformat(ds.starting),
ending=datetime.isoformat(ds.ending)
)
# variables_lst=pyoos_ds.variables
station_rec_lst.append(station_rec)
In [22]:
# station_rec
# station_rec_lst = [station_rec]
In [25]:
# geojson features and feature collection
features = []
for rec in station_rec_lst:
feature = Feature(geometry=Point((rec['longitude'], rec['latitude'])),
id=rec['id'],
properties=rec)
features.append(feature)
stations_featcoll = FeatureCollection(features)
# stations_featcoll
In [26]:
stations_gdf = gpd.GeoDataFrame.from_features(features=stations_featcoll['features'])
len(stations_gdf)
Out[26]:
In [27]:
stations_gdf.head()
Out[27]:
In [ ]:
# stations_gdf.plot()
In [ ]: