def test_getfeature(): getcapsin = open(resource_file("wfs_HSRS_GetCapabilities_1_1_0.xml"), "rb").read() wfs = WebFeatureService('http://gis.bnhelp.cz/ows/crwfs', xml=getcapsin, version='1.1.0') assert sorted(wfs.contents.keys()) == ['kraje', 'nuts1', 'nuts2', 'nuts3', 'okresy', 'orp', 'sidla', 'states'] assert wfs.contents['okresy'].crsOptions[0].getcodeurn() == 'urn:ogc:def:crs:EPSG::4326' assert sorted_url_query(wfs.getGETGetFeatureRequest(typename=['okresy'], maxfeatures=2)) == [ 'maxfeatures=2', 'request=GetFeature', 'service=WFS', 'typename=okresy', 'version=1.1.0'] assert sorted_url_query(wfs.getGETGetFeatureRequest( typename=['okresy'], maxfeatures=2, bbox=[15, 49, 16, 51, 'urn:ogc:def:crs:EPSG:4326'])) == [ 'bbox=49%2C15%2C51%2C16%2Curn%3Aogc%3Adef%3Acrs%3AEPSG%3A%3A4326', 'maxfeatures=2', 'request=GetFeature', 'service=WFS', 'typename=okresy', 'version=1.1.0'] assert sorted_url_query(wfs.getGETGetFeatureRequest( typename=['okresy'], maxfeatures=2, bbox=[-685336, -993518, -684996, -993285])) == [ 'bbox=-993518%2C-685336%2C-993285%2C-684996%2Curn%3Aogc%3Adef%3Acrs%3AEPSG%3A%3A4326', 'maxfeatures=2', 'request=GetFeature', 'service=WFS', 'typename=okresy', 'version=1.1.0']
def load_full_month(year, mm, place): import calendar assert datetime.date(year, mm, calendar.monthrange(year, mm)[1]) < datetime.date.today(), \ "wrong date {}/{} given, query only past months excluding current".format(year, mm) # create dictionary of empty lists for feature values values = dict([(feature, []) for feature in [ 'Temperature', 'Humidity', 'WindDirection', 'WindSpeedMS', 'TotalCloudCover', 'Precipitation1h' ]]) wfs11 = WebFeatureService(url='https://opendata.fmi.fi/wfs', version='2.0.0') # query one week at a time for day in range(1, calendar.monthrange(year, mm)[1], 7): starttime = datetime.datetime(year, mm, day) endtime = datetime.datetime( year, mm, min(day + 6, calendar.monthrange(year, mm)[1])) endtime = endtime.strftime('%Y-%m-%d 23:00:00') # fetch data for given feature for feature in [ 'Temperature', 'Humidity', 'WindDirection', 'WindSpeedMS', 'TotalCloudCover', 'Precipitation1h' ]: response = wfs11.getGETGetFeatureRequest( storedQueryID='fmi::observations::weather::timevaluepair', storedQueryParams={ 'parameters': feature, 'place': place, 'timestep': 60, 'starttime': starttime, 'endtime': endtime }) # save response to temp XML file download_file(response) # returns TPV pairs try: TPVs = parse_xml_fields("tempXML.xml") except: print("Error occurred in parsing the XML response: ", response) print('Place: {}, feature: {}'.format(place, feature)) sys.exit() for pair in TPVs: #time = pair[0].text value = pair[1].text # append value to the list of the feature values[feature].append(value) return values
class WFS(): def __init__(self, url, version): self.wfs = WebFeatureService(url=url, version=version) def set_collection(self, collection): collection_exist = self._check_collection(collection) if collection_exist: self.collection = collection return collection_exist def _check_collection(self, collection): feature_types = list(self.wfs.contents) collection_exist = True if collection in feature_types else False return collection_exist def get_schema(self): if hasattr(self, 'collection'): return self.wfs.get_schema(self.collection) else: return None def make_request(self, max_dataset=100, sort_by=None, start_index=0, constraint=None): output_format = self._get_outputformat() result = self.wfs.getfeature(typename=self.collection, filter=constraint, maxfeatures=max_dataset, sortby=sort_by, startindex=start_index, outputFormat=output_format) result = result.read() if 'json' in output_format: return json.loads(result) else: return result def get_request(self, max_dataset=None, sort_by=None, start_index=0, constraint=None): output_format = self._get_outputformat() result = self.wfs.getGETGetFeatureRequest(typename=self.collection, filter=constraint, maxfeatures=max_dataset, sortby=sort_by, startindex=start_index, outputFormat=output_format) return result def _get_outputformat(self): getfeature_param = self.wfs.getOperationByName('GetFeature').parameters allowed_output_format = getfeature_param["outputFormat"]["values"] for output_format in OUTPUT_FORMAT: if output_format in allowed_output_format: return output_format return None def set_filter_equal_to(self, propertyname, value): constraint = PropertyIsLike(propertyname=propertyname, literal=value) filterxml = etree.tostring(constraint.toXML()).decode("utf-8") return filterxml
print(wfs.provider.contact.name) print(wfs.provider.contact.organization) print(wfs.provider.contact.city) print(wfs.provider.contact.region) print(wfs.provider.contact.postcode) print(wfs.provider.contact.country) print("\n\tOperations: ", [op.name for op in wfs.operations]) op_describe = wfs.getOperationByName('DescribeFeatureType') help(op_describe) get_cap = wfs.getOperationByName('GetCapabilities') # help(get_cap) print(wfs.getGETGetFeatureRequest()) get_feat = wfs.getOperationByName('GetFeature') # help(get_feat) for layer in list(wfs.contents): try: print(u'Layer: %s, Features: %s, SR: %s...' % (wfs[layer].title, wfs[layer].abstract, wfs[layer].crsOptions)) print(wfs[layer].boundingBox) print(wfs[layer].boundingBoxWGS84) print(wfs[layer].keywords) # response = wfs.describefeaturetype() # print dir(response)
print(wfs.provider.contact.name) print(wfs.provider.contact.organization) print(wfs.provider.contact.city) print(wfs.provider.contact.region) print(wfs.provider.contact.postcode) print(wfs.provider.contact.country) print(("\n\tOperations: ", [op.name for op in wfs.operations])) op_describe = wfs.getOperationByName('DescribeFeatureType') help(op_describe) get_cap = wfs.getOperationByName('GetCapabilities') # help(get_cap) print(wfs.getGETGetFeatureRequest()) get_feat = wfs.getOperationByName('GetFeature') # help(get_feat) for layer in list(wfs.contents): try: print(u'Layer: %s, Features: %s, SR: %s...' % (wfs[layer].title, wfs[layer].abstract, wfs[layer].crsOptions)) print(wfs[layer].boundingBox) print(wfs[layer].boundingBoxWGS84) print(wfs[layer].keywords) # response = wfs.describefeaturetype() # print dir(response)